From c550531a35b5c2449b820a507be8b0980cc67abb Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 26 Jun 2024 09:10:04 +0200 Subject: [PATCH] tests(errors): refactored old tests to reflect new error handling Signed-off-by: SoXX --- internal/postgres/post.go | 5 +- internal/postgres/post_test.go | 5 +- internal/postgres/source.go | 3 +- internal/postgres/tag.go | 3 +- internal/postgres/user.go | 5 +- internal/postgres/user_test.go | 106 ++- pkg/database/postgres_test.go | 1229 +++++++++++++++++++++++++++++++- 7 files changed, 1281 insertions(+), 75 deletions(-) diff --git a/internal/postgres/post.go b/internal/postgres/post.go index 2206e00..edf7ac3 100644 --- a/internal/postgres/post.go +++ b/internal/postgres/post.go @@ -3,6 +3,7 @@ package postgres import ( "context" "errors" + errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" @@ -42,7 +43,9 @@ func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models return nil, &errors2.EntityValidationFailed{Reason: "anthrovePostID is not set"} } - //TODO maybe check ofor id length too...? + if len(anthrovePostID) != 25 { + return nil, &errors2.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"} + } var post models.Post result := db.WithContext(ctx).First(&post, "id = ?", anthrovePostID) diff --git a/internal/postgres/post_test.go b/internal/postgres/post_test.go index 1b1bc83..99e5b32 100644 --- a/internal/postgres/post_test.go +++ b/internal/postgres/post_test.go @@ -3,11 +3,12 @@ package postgres import ( "context" "fmt" + "testing" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/test" _ "github.com/lib/pq" "gorm.io/gorm" - "testing" ) func TestCreateAnthrovePostNode(t *testing.T) { @@ -188,7 +189,7 @@ func TestGetPostBySourceURL(t *testing.T) { t.Fatal("Could not create source", err) } - err = CreateReferenceBetweenPostAndSource(ctx, gormDB, models.AnthrovePostID(post.ID), models.AnthroveSourceDomain(source.Domain)) + err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain)) if err != nil { t.Fatal("Could not create source reference", err) } diff --git a/internal/postgres/source.go b/internal/postgres/source.go index 1238a87..9321e42 100644 --- a/internal/postgres/source.go +++ b/internal/postgres/source.go @@ -3,6 +3,7 @@ package postgres import ( "context" "errors" + errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" @@ -48,7 +49,7 @@ func GetAllSource(ctx context.Context, db *gorm.DB) ([]models.Source, error) { if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, &errors2.NoDataFound{} - } // TODO really...? i don't think this error handling should be here..? / not possible + } return nil, result.Error } diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go index 248a6a7..86f60d8 100644 --- a/internal/postgres/tag.go +++ b/internal/postgres/tag.go @@ -3,6 +3,7 @@ package postgres import ( "context" "errors" + errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" log "github.com/sirupsen/logrus" @@ -78,7 +79,7 @@ func GetTags(ctx context.Context, db *gorm.DB) ([]models.Tag, error) { result := db.WithContext(ctx).Find(&tags) if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { // TODO really...? i don't think this error handling should be here..? / not possible + if errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, &errors2.NoDataFound{} } return nil, result.Error diff --git a/internal/postgres/user.go b/internal/postgres/user.go index 0e4070b..a21b266 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -3,6 +3,7 @@ package postgres import ( "context" "errors" + errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" log "github.com/sirupsen/logrus" @@ -33,10 +34,6 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove return result.Error } - if result.RowsAffected == 0 { - return &errors2.NoDataWritten{} - } - return nil } diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go index 04c270a..3c63d8c 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -3,11 +3,12 @@ package postgres import ( "context" "fmt" + "reflect" + "testing" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/test" "gorm.io/gorm" - "reflect" - "testing" ) func TestCreateUser(t *testing.T) { @@ -382,8 +383,6 @@ func TestGetUserSourceBySourceID(t *testing.T) { } } -// TODO: FIX THE FOUR REMAINING TESTS - func TestGetUserFavoriteNodeWithPagination(t *testing.T) { // Setup trow away containert ctx := context.Background() @@ -394,31 +393,39 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { defer container.Terminate(ctx) // Setup Test + validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) + validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) + validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) + validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) + validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) expectedResultPosts := []models.Post{ { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post1"))}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, Rating: "safe", }, { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post2"))}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, Rating: "safe", }, { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post3"))}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, Rating: "explicit", }, { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post4"))}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, Rating: "explicit", }, { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post5"))}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, Rating: "questionable", }, { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post6"))}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, Rating: "safe", }, } @@ -432,7 +439,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { Posts: expectedResultPosts[:3], } - err = CreateUser(ctx, gormDB, "1") + err = CreateUser(ctx, gormDB, validAnthroveUserID) if err != nil { t.Fatal(err) } @@ -442,7 +449,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { if err != nil { t.Fatal(err) } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(expectedResultPost.ID)) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, expectedResultPost.ID) if err != nil { t.Fatal(err) } @@ -467,7 +474,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validAnthroveUserID, skip: 0, limit: 2000, }, @@ -479,7 +486,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validAnthroveUserID, skip: 2, limit: 2000, }, @@ -491,7 +498,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validAnthroveUserID, skip: 0, limit: 3, }, @@ -524,44 +531,54 @@ func TestGetUserFavoritesCount(t *testing.T) { // Setup Test - err = CreateUser(ctx, gormDB, "1") - if err != nil { - t.Fatal(err) - } + validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) + validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) + validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) + validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) + validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) expectedResultPosts := []models.Post{ { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post1"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, Rating: "safe", }, { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post2"}, + + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, Rating: "safe", }, { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post3"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, Rating: "explicit", }, { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post4"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, Rating: "explicit", }, { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post5"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, Rating: "questionable", }, { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post6"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, Rating: "safe", }, } + err = CreateUser(ctx, gormDB, validAnthroveUserID) + if err != nil { + t.Fatal(err) + } + for _, post := range expectedResultPosts { err = CreatePost(ctx, gormDB, &post) if err != nil { t.Fatal(err) } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID)) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID) if err != nil { t.Fatal(err) } @@ -584,7 +601,7 @@ func TestGetUserFavoritesCount(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validAnthroveUserID, }, want: 6, wantErr: false, @@ -597,7 +614,7 @@ func TestGetUserFavoritesCount(t *testing.T) { anthroveUserID: "2", }, want: 0, - wantErr: false, + wantErr: true, }, { name: "Test 3: no anthroveUserID and 6 favorite posts", @@ -634,8 +651,13 @@ func TestGetUserSourceLinks(t *testing.T) { defer container.Terminate(ctx) // Setup Test + validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + + validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) + validSourceID2 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source2")) + eSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1"))}, + BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, DisplayName: "e621", Domain: "e621.net", } @@ -645,7 +667,7 @@ func TestGetUserSourceLinks(t *testing.T) { } faSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "2"))}, + BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID2}, DisplayName: "fa", Domain: "fa.net", } @@ -672,11 +694,11 @@ func TestGetUserSourceLinks(t *testing.T) { }, } - err = CreateUserWithRelationToSource(ctx, gormDB, "1", eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) + err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) if err != nil { t.Fatal(err) } - err = CreateUserWithRelationToSource(ctx, gormDB, "1", faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) + err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) if err != nil { t.Fatal(err) } @@ -698,7 +720,7 @@ func TestGetUserSourceLinks(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validAnthroveUserID, }, want: expectedResult, wantErr: false, @@ -728,15 +750,21 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { defer container.Terminate(ctx) // Setup Test - err = CreateUser(ctx, gormDB, "1") + validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) + validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) + + err = CreateUser(ctx, gormDB, validAnthroveUserID) if err != nil { t.Fatal(err) } posts := []models.Post{ - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post1"))}, Rating: "safe"}, - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post2"))}, Rating: "safe"}, - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post3"))}, Rating: "explicit"}, + {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, Rating: "safe"}, + {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, Rating: "safe"}, + {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, Rating: "explicit"}, } for _, post := range posts { @@ -744,7 +772,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { if err != nil { t.Fatal(err) } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID)) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, models.AnthrovePostID(post.ID)) if err != nil { t.Fatal(err) } @@ -804,7 +832,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthroveUserID: "1", + anthroveUserID: validAnthroveUserID, }, want: expectedResult, wantErr: false, diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index f4cd28f..4103607 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -3,11 +3,13 @@ package database import ( "context" "fmt" + "reflect" "testing" "git.dragse.it/anthrove/otter-space-sdk/internal/postgres" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/test" + "gorm.io/gorm" ) func TestNewPostgresqlConnection(t *testing.T) { @@ -95,9 +97,11 @@ func Test_postgresqlConnection_CreateUserWithRelationToSource(t *testing.T) { defer container.Terminate(ctx) // Setup Test + validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1"))}, + BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, DisplayName: "e621", Domain: "e621.net", Icon: "icon.e621.net", @@ -124,7 +128,7 @@ func Test_postgresqlConnection_CreateUserWithRelationToSource(t *testing.T) { name: "Test 1: Valid anthroveUserID, sourceID, accountId, accountUsername", args: args{ ctx: ctx, - anthroveUserID: "1", + anthroveUserID: validUserID, sourceID: source.ID, accountId: "e1", accountUsername: "marius", @@ -157,7 +161,7 @@ func Test_postgresqlConnection_CreateUserWithRelationToSource(t *testing.T) { name: "Test 4: invalid sourceID", args: args{ ctx: ctx, - anthroveUserID: "1", + anthroveUserID: validUserID, sourceID: "fa.net", accountId: "e1", accountUsername: "marius", @@ -168,7 +172,7 @@ func Test_postgresqlConnection_CreateUserWithRelationToSource(t *testing.T) { name: "Test 5: no accountId", args: args{ ctx: ctx, - anthroveUserID: "1", + anthroveUserID: validUserID, sourceID: source.ID, accountId: "", accountUsername: "marius", @@ -179,7 +183,7 @@ func Test_postgresqlConnection_CreateUserWithRelationToSource(t *testing.T) { name: "Test 6: no accountUsername", args: args{ ctx: ctx, - anthroveUserID: "1", + anthroveUserID: validUserID, sourceID: source.ID, accountId: "aa", accountUsername: "", @@ -253,7 +257,7 @@ func Test_postgresqlConnection_CreateSource(t *testing.T) { ctx: ctx, anthroveSource: validSource, }, - wantErr: false, + wantErr: true, }, } for _, tt := range tests { @@ -280,9 +284,11 @@ func Test_postgresqlConnection_CreatePost(t *testing.T) { // Setup Tests + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + validPost := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), + ID: validPostID1, }, Rating: "safe", } @@ -341,10 +347,11 @@ func Test_postgresqlConnection_CreateTagAndReferenceToPost(t *testing.T) { defer container.Terminate(ctx) // Setup Test + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), + ID: validPostID1, }, Rating: "safe", } @@ -430,23 +437,28 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T) defer container.Terminate(ctx) // Setup Test + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) + post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), + ID: validPostID1, }, Rating: "safe", } + source := &models.Source{ + BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, + DisplayName: "e621", + Domain: "e621.net", + Icon: "icon.e621.net", + } + err = postgres.CreatePost(ctx, gormDB, post) if err != nil { t.Fatal(err) } - source := &models.Source{ - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } err = postgres.CreateSource(ctx, gormDB, source) if err != nil { t.Fatal(err) @@ -522,14 +534,17 @@ func Test_postgresqlConnection_CreateReferenceBetweenUserAndPost(t *testing.T) { defer container.Terminate(ctx) // Setup Test - err = postgres.CreateUser(ctx, gormDB, "1") + validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + + err = postgres.CreateUser(ctx, gormDB, validUserID) if err != nil { t.Fatal(err) } post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), + ID: validPostID1, }, Rating: "safe", } @@ -554,7 +569,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenUserAndPost(t *testing.T) { name: "Test 1: Valid AnthroveUserID and AnthrovePostID", args: args{ ctx: ctx, - anthroveUserID: "1", + anthroveUserID: validUserID, anthrovePostID: post.ID, }, wantErr: false, @@ -563,7 +578,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenUserAndPost(t *testing.T) { name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", args: args{ ctx: ctx, - anthroveUserID: "1", + anthroveUserID: validUserID, anthrovePostID: "123456", }, wantErr: true, @@ -610,14 +625,17 @@ func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(t *testing.T) { defer container.Terminate(ctx) // Setup Test - err = postgres.CreateUser(ctx, gormDB, "1") + validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + + err = postgres.CreateUser(ctx, gormDB, validUserID) if err != nil { t.Fatal(err) } post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), + ID: validPostID1, }, Rating: "safe", } @@ -627,7 +645,7 @@ func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(t *testing.T) { t.Fatal(err) } - err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", post.ID) + err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID) if err != nil { t.Fatal(err) } @@ -648,7 +666,7 @@ func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(t *testing.T) { name: "Test 1: Valid AnthroveUserID and AnthrovePostID", args: args{ ctx: ctx, - anthroveUserID: "1", + anthroveUserID: validUserID, anthrovePostID: post.ID, }, want: true, @@ -658,11 +676,11 @@ func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(t *testing.T) { name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", args: args{ ctx: ctx, - anthroveUserID: "1", + anthroveUserID: validUserID, anthrovePostID: "qadw", }, want: false, - wantErr: false, + wantErr: true, }, { name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID", @@ -672,7 +690,7 @@ func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(t *testing.T) { anthrovePostID: post.ID, }, want: false, - wantErr: false, + wantErr: true, }, { name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", @@ -682,7 +700,7 @@ func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(t *testing.T) { anthrovePostID: "123456", }, want: false, - wantErr: false, + wantErr: true, }, } for _, tt := range tests { @@ -713,10 +731,11 @@ func Test_postgresqlConnection_GetPostByAnthroveID(t *testing.T) { defer container.Terminate(ctx) // Setup Tests + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) post := &models.Post{ BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), + ID: validPostID1, }, Rating: "safe", } @@ -773,6 +792,1107 @@ func Test_postgresqlConnection_GetPostByAnthroveID(t *testing.T) { } } +func Test_postgresqlConnection_GetPostByURL(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Tests + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + + post := &models.Post{ + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: validPostID1, + }, + Rating: "safe", + } + + err = postgres.CreatePost(ctx, gormDB, post) + if err != nil { + t.Fatal("Could not create post", err) + } + + source := models.Source{ + BaseModel: models.BaseModel[models.AnthroveSourceID]{ + ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")), + }, + DisplayName: "e621", + Domain: "e621.net", + Icon: "https://e621.net/icon.ico", + } + + err = postgres.CreateSource(ctx, gormDB, &source) + if err != nil { + t.Fatal("Could not create source", err) + } + + err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain)) + if err != nil { + t.Fatal("Could not create source reference", err) + } + + // Test + type args struct { + ctx context.Context + sourceUrl string + } + tests := []struct { + name string + args args + want *models.Post + wantErr bool + }{ + { + name: "Test 1: Valid sourceUrl", + args: args{ + ctx: ctx, + sourceUrl: source.Domain, + }, + want: post, + wantErr: false, + }, + { + name: "Test 2: Invalid sourceUrl", + args: args{ + ctx: ctx, + sourceUrl: "1234", + }, + want: nil, + wantErr: true, + }, + { + name: "Test 3: No sourceUrl", + args: args{ + ctx: ctx, + sourceUrl: "", + }, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetPostByURL(tt.args.ctx, tt.args.sourceUrl) + if (err != nil) != tt.wantErr { + t.Errorf("GetPostByURL() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkPost(got, tt.want) { + t.Errorf("GetPostByURL() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_GetPostBySourceID(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Tests + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) + + post := &models.Post{ + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: validPostID1, + }, + Rating: "safe", + } + + source := models.Source{ + BaseModel: models.BaseModel[models.AnthroveSourceID]{ + ID: validSourceID1, + }, + DisplayName: "e621", + Domain: "e621.net", + Icon: "https://e621.net/icon.ico", + } + + err = postgres.CreatePost(ctx, gormDB, post) + if err != nil { + t.Fatal("Could not create post", err) + } + + err = postgres.CreateSource(ctx, gormDB, &source) + if err != nil { + t.Fatal("Could not create source", err) + } + + err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain)) + if err != nil { + t.Fatal("Could not create source reference", err) + } + + // Test + type args struct { + ctx context.Context + sourceID models.AnthroveSourceID + } + tests := []struct { + name string + args args + want *models.Post + wantErr bool + }{ + { + name: "Test 1: Valid sourceID", + args: args{ + ctx: ctx, + sourceID: source.ID, + }, + want: post, + wantErr: false, + }, + { + name: "Test 2: Invalid sourceID", + args: args{ + ctx: ctx, + sourceID: "1234", + }, + want: nil, + wantErr: true, + }, + { + name: "Test 3: No sourceID", + args: args{ + ctx: ctx, + sourceID: "", + }, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetPostBySourceID(tt.args.ctx, tt.args.sourceID) + if (err != nil) != tt.wantErr { + t.Errorf("GetPostBySourceID() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkPost(got, tt.want) { + t.Errorf("GetPostBySourceID() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_GetUserFavoritesCount(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + + validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) + validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) + validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) + validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) + validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) + + expectedResultPosts := []models.Post{ + { + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, + Rating: "safe", + }, + { + + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, + Rating: "safe", + }, + { + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, + Rating: "explicit", + }, + { + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, + Rating: "explicit", + }, + { + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, + Rating: "questionable", + }, + { + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, + Rating: "safe", + }, + } + + err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID) + if err != nil { + t.Fatal(err) + } + + for _, post := range expectedResultPosts { + err = postgres.CreatePost(ctx, gormDB, &post) + if err != nil { + t.Fatal(err) + } + err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID) + if err != nil { + t.Fatal(err) + } + } + + // Test + type args struct { + ctx context.Context + anthroveUserID models.AnthroveUserID + } + tests := []struct { + name string + args args + want int64 + wantErr bool + }{ + { + name: "Test 1: Valid anthroveUserID and 6 favorite posts", + args: args{ + ctx: ctx, + anthroveUserID: validAnthroveUserID, + }, + want: 6, + wantErr: false, + }, + { + name: "Test 2: Invalid anthroveUserID and 6 favorite posts", + args: args{ + ctx: ctx, + anthroveUserID: "2", + }, + want: 0, + wantErr: true, + }, + { + name: "Test 3: no anthroveUserID and 6 favorite posts", + args: args{ + ctx: ctx, + anthroveUserID: "", + }, + want: 0, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetUserFavoritesCount(tt.args.ctx, tt.args.anthroveUserID) + if (err != nil) != tt.wantErr { + t.Errorf("GetUserFavoritesCount() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("GetUserFavoritesCount() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_GetUserSourceLinks(t *testing.T) { + // Setup trow away containert + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + + validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) + validSourceID2 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source2")) + + eSource := &models.Source{ + BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, + DisplayName: "e621", + Domain: "e621.net", + } + err = postgres.CreateSource(ctx, gormDB, eSource) + if err != nil { + t.Fatal(err) + } + + faSource := &models.Source{ + BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID2}, + DisplayName: "fa", + Domain: "fa.net", + } + err = postgres.CreateSource(ctx, gormDB, faSource) + if err != nil { + t.Fatal(err) + } + + expectedResult := make(map[string]models.UserSource) + expectedResult["e621"] = models.UserSource{ + UserID: "e1", + AccountUsername: "e621-user", + Source: models.Source{ + DisplayName: eSource.DisplayName, + Domain: eSource.Domain, + }, + } + expectedResult["fa"] = models.UserSource{ + UserID: "fa1", + AccountUsername: "fa-user", + Source: models.Source{ + DisplayName: faSource.DisplayName, + Domain: faSource.Domain, + }, + } + + err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) + if err != nil { + t.Fatal(err) + } + err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) + if err != nil { + t.Fatal(err) + } + + // Test + type args struct { + ctx context.Context + anthroveUserID models.AnthroveUserID + } + tests := []struct { + name string + args args + want map[string]models.UserSource + wantErr bool + }{ + { + name: "Test 1: Get Data", + args: args{ + ctx: ctx, + anthroveUserID: validAnthroveUserID, + }, + want: expectedResult, + wantErr: false, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetUserSourceLinks(tt.args.ctx, tt.args.anthroveUserID) + if (err != nil) != tt.wantErr { + t.Errorf("GetUserSourceLinks() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetUserSourceLinks() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_GetUserSourceBySourceID(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + + validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + invalidUserID := models.AnthroveUserID("XXX") + + expectedResult := make(map[string]models.UserSource) + expectedResult["e621"] = models.UserSource{ + UserID: "e1", + AccountUsername: "euser", + Source: models.Source{ + DisplayName: "e621", + Domain: "e621.net", + }, + } + + source := &models.Source{ + BaseModel: models.BaseModel[models.AnthroveSourceID]{ + ID: expectedResult["e621"].Source.ID, + }, + DisplayName: expectedResult["e621"].Source.DisplayName, + Domain: expectedResult["e621"].Source.Domain, + } + + err = postgres.CreateSource(ctx, gormDB, source) + if err != nil { + t.Fatal(err) + } + + err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, models.AnthroveSourceID(expectedResult["e621"].SourceID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) + if err != nil { + t.Fatal(err) + } + + // Test + type args struct { + ctx context.Context + anthroveUserID models.AnthroveUserID + sourceID models.AnthroveSourceID + } + tests := []struct { + name string + args args + want map[string]models.UserSource + wantErr bool + }{ + { + name: "Test 1: Valid AnthroveUserID and sourceID", + args: args{ + ctx: ctx, + anthroveUserID: validUserID, + sourceID: source.ID, + }, + want: expectedResult, + wantErr: false, + }, + { + name: "Test 2: Invalid AnthroveUserID and valid sourceID", + args: args{ + ctx: ctx, + anthroveUserID: invalidUserID, + sourceID: source.ID, + }, + want: nil, + wantErr: true, + }, + { + name: "Test 3: Valid AnthroveUserID and invalid sourceID", + args: args{ + ctx: ctx, + anthroveUserID: validUserID, + sourceID: "fa", + }, + want: nil, + wantErr: true, + }, + { + name: "Test 4: No AnthroveUserID and Valid sourceID", + args: args{ + ctx: ctx, + anthroveUserID: "", + sourceID: source.ID, + }, + want: nil, + wantErr: true, + }, + { + name: "Test 5: Valid AnthroveUserID and No SourceDisplayName", + args: args{ + ctx: ctx, + anthroveUserID: "1", + sourceID: "", + }, + want: nil, + wantErr: true, + }, + { + name: "Test 6: No AnthroveUserID and No SourceDisplayName", + args: args{ + ctx: ctx, + anthroveUserID: "", + sourceID: "", + }, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetUserSourceBySourceID(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID) + if (err != nil) != tt.wantErr { + t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_GetAllAnthroveUserIDs(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + validUserID01 := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + validUserID02 := models.AnthroveUserID(fmt.Sprintf("%025s", "User2")) + validUserID03 := models.AnthroveUserID(fmt.Sprintf("%025s", "User3")) + + users := []models.AnthroveUserID{validUserID01, validUserID02, validUserID03} + + for _, user := range users { + err = postgres.CreateUser(ctx, gormDB, user) + if err != nil { + t.Fatal(err) + } + } + + // Test + type args struct { + ctx context.Context + } + tests := []struct { + name string + args args + want []models.AnthroveUserID + wantErr bool + }{ + { + name: "Test 1: Get Data", + args: args{ + ctx: ctx, + }, + want: users, + wantErr: false, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetAllAnthroveUserIDs(tt.args.ctx) + if (err != nil) != tt.wantErr { + t.Errorf("GetAllAnthroveUserIDs() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetAllAnthroveUserIDs() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_GetUserFavoriteWithPagination(t *testing.T) { + // Setup trow away containert + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) + validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) + validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) + validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) + validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) + + expectedResultPosts := []models.Post{ + { + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, + Rating: "safe", + }, + { + + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, + Rating: "safe", + }, + { + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, + Rating: "explicit", + }, + { + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, + Rating: "explicit", + }, + { + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, + Rating: "questionable", + }, + { + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, + Rating: "safe", + }, + } + expectedResult := &models.FavoriteList{ + Posts: expectedResultPosts, + } + expectedResult2 := &models.FavoriteList{ + Posts: expectedResultPosts[2:], + } + expectedResult3 := &models.FavoriteList{ + Posts: expectedResultPosts[:3], + } + + err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID) + if err != nil { + t.Fatal(err) + } + + for _, expectedResultPost := range expectedResultPosts { + err = postgres.CreatePost(ctx, gormDB, &expectedResultPost) + if err != nil { + t.Fatal(err) + } + err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, expectedResultPost.ID) + if err != nil { + t.Fatal(err) + } + } + + // Test + type args struct { + ctx context.Context + anthroveUserID models.AnthroveUserID + skip int + limit int + } + tests := []struct { + name string + args args + want *models.FavoriteList + wantErr bool + }{ + { + name: "Test 1: Valid AnthroveUserID", + args: args{ + ctx: ctx, + anthroveUserID: validAnthroveUserID, + skip: 0, + limit: 2000, + }, + want: expectedResult, + wantErr: false, + }, + { + name: "Test 2: Skip first two", + args: args{ + ctx: ctx, + anthroveUserID: validAnthroveUserID, + skip: 2, + limit: 2000, + }, + want: expectedResult2, + wantErr: false, + }, + { + name: "Test 3: Limit of 3", + args: args{ + ctx: ctx, + anthroveUserID: validAnthroveUserID, + skip: 0, + limit: 3, + }, + want: expectedResult3, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetUserFavoriteWithPagination(tt.args.ctx, tt.args.anthroveUserID, tt.args.skip, tt.args.limit) + if (err != nil) != tt.wantErr { + t.Errorf("GetUserFavoriteWithPagination() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetUserFavoriteWithPagination() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_GetUserTagWitRelationToFavedPosts(t *testing.T) { + // Setup trow away containert + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) + + validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) + validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) + validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) + + err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID) + if err != nil { + t.Fatal(err) + } + + posts := []models.Post{ + {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, Rating: "safe"}, + {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, Rating: "safe"}, + {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, Rating: "explicit"}, + } + + for _, post := range posts { + err = postgres.CreatePost(ctx, gormDB, &post) + if err != nil { + t.Fatal(err) + } + err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, models.AnthrovePostID(post.ID)) + if err != nil { + t.Fatal(err) + } + } + + tags := []models.Tag{ + {Name: "JayTheFerret", Type: "artist"}, + {Name: "Ferret", Type: "species"}, + {Name: "Jay", Type: "character"}, + } + + for i, tag := range tags { + err = postgres.CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag) + if err != nil { + t.Fatal(err) + } + } + + expectedResult := []models.TagsWithFrequency{ + { + Frequency: 1, + Tags: models.Tag{ + Name: tags[0].Name, + Type: tags[0].Type, + }, + }, + { + Frequency: 1, + Tags: models.Tag{ + Name: tags[1].Name, + Type: tags[1].Type, + }, + }, + { + Frequency: 1, + Tags: models.Tag{ + Name: tags[2].Name, + Type: tags[2].Type, + }, + }, + } + + // Test + type args struct { + ctx context.Context + anthroveUserID models.AnthroveUserID + } + tests := []struct { + name string + args args + want []models.TagsWithFrequency + wantErr bool + }{ + { + name: "Test 1: Get Data", + args: args{ + ctx: ctx, + anthroveUserID: validAnthroveUserID, + }, + want: expectedResult, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetUserTagWitRelationToFavedPosts(tt.args.ctx, tt.args.anthroveUserID) + if (err != nil) != tt.wantErr { + t.Errorf("GetUserTagWitRelationToFavedPosts() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetUserTagWitRelationToFavedPosts() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_GetAllTags(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + + tags := []models.Tag{ + { + Name: "JayTheFerret", + Type: "artist", + }, + { + Name: "anthro", + Type: "general", + }, + { + Name: "soxx", + Type: "character", + }, + } + + for _, tag := range tags { + err = postgres.CreateTag(ctx, gormDB, &tag) + if err != nil { + t.Fatal(err) + } + } + + // Test + type args struct { + ctx context.Context + } + tests := []struct { + name string + args args + want []models.Tag + wantErr bool + }{ + { + name: "Test 1: Get Tags", + args: args{ + ctx: ctx, + }, + want: tags, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetAllTags(tt.args.ctx) + if (err != nil) != tt.wantErr { + t.Errorf("GetAllTags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetAllTags() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_GetAllSources(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + + sources := []models.Source{ + { + DisplayName: "e621", + Domain: "e621.net", + Icon: "icon.e621.net", + }, + { + DisplayName: "furaffinity", + Domain: "furaffinity.net", + Icon: "icon.furaffinity.net", + }, + { + DisplayName: "fenpaws", + Domain: "fenpa.ws", + Icon: "icon.fenpa.ws", + }, + } + + for _, source := range sources { + err = postgres.CreateSource(ctx, gormDB, &source) + if err != nil { + t.Fatal(err) + } + } + + // Test + type args struct { + ctx context.Context + } + tests := []struct { + name string + args args + want []models.Source + wantErr bool + }{ + { + name: "Test 1: Get all entries", + args: args{ + ctx: ctx, + }, + want: sources, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetAllSources(tt.args.ctx) + if (err != nil) != tt.wantErr { + t.Errorf("GetAllSources() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkSources(got, tt.want) { + t.Errorf("GetAllSources() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_GetSourceByDomain(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + + source := &models.Source{ + DisplayName: "e621", + Domain: "e621.net", + Icon: "icon.e621.net", + } + + err = postgres.CreateSource(ctx, gormDB, source) + if err != nil { + t.Fatal(err) + } + + // Test + type args struct { + ctx context.Context + sourceDomain models.AnthroveSourceDomain + } + tests := []struct { + name string + args args + want *models.Source + wantErr bool + }{ + { + name: "Test 1: Valid URL", + args: args{ + ctx: ctx, + sourceDomain: "e621.net", + }, + want: source, + wantErr: false, + }, + { + name: "Test 2: Invalid URL", + args: args{ + ctx: ctx, + sourceDomain: "eeeee.net", + }, + want: nil, + wantErr: true, + }, + { + name: "Test 2: No URL", + args: args{ + ctx: ctx, + sourceDomain: "", + }, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetSourceByDomain(tt.args.ctx, tt.args.sourceDomain) + if (err != nil) != tt.wantErr { + t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkSource(got, tt.want) { + t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want) + } + }) + } +} + +func checkSource(got *models.Source, want *models.Source) bool { + + if want == nil && got == nil { + return true + } + + if got.Domain != want.Domain { + return false + } + + return true + +} + func checkPost(got *models.Post, want *models.Post) bool { if got == nil && want == nil { @@ -791,3 +1911,58 @@ func checkPost(got *models.Post, want *models.Post) bool { return true } + +func checkSources(got []models.Source, want []models.Source) bool { + for i, source := range want { + if source.DisplayName != got[i].DisplayName { + return false + } + if source.Domain != got[i].Domain { + return false + } + if source.Icon != got[i].Icon { + return false + } + } + + return true +} + +func Test_postgresqlConnection_migrateDatabase(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + + // Test + type args struct { + dbPool *gorm.DB + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 1: Migrate Databases", + args: args{dbPool: gormDB}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + if err := p.migrateDatabase(tt.args.dbPool); (err != nil) != tt.wantErr { + t.Errorf("migrateDatabase() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}