package postgres import ( "context" "fmt" "reflect" "testing" "time" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" "gorm.io/gorm" ) func TestCreateUser(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") // Test type args struct { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID } tests := []struct { name string args args wantErr bool }{ { name: "Test 1: Valid AnthroveUserID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, }, wantErr: false, }, { name: "Test 2: Invalid AnthroveUserID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: invalidUserID, }, wantErr: true, }, { name: "Test 3: No anthroveUserID given", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", }, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := CreateUser(tt.args.ctx, tt.args.db, tt.args.anthroveUserID); (err != nil) != tt.wantErr { t.Errorf("CreateUser() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestCreateUserNodeWithSourceRelation(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") validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) source := &models.Source{ BaseModel: models.BaseModel[models.AnthroveSourceID]{ ID: validSourceID, }, DisplayName: "e621", Domain: "e621.net", Icon: "icon.e621.net", } err = CreateSource(ctx, gormDB, source) if err != nil { t.Fatal(err) } // Test type args struct { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID sourceID models.AnthroveSourceID userID string username string } tests := []struct { name string args args wantErr bool }{ { name: "Test 1: Valid anthroveUserID, sourceID, userID, username", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: source.ID, userID: "e1", username: "marius", }, wantErr: false, }, { name: "Test 2: Invalid anthroveUserID, valid sourceID, userID, username", args: args{ ctx: ctx, db: gormDB, anthroveUserID: invalidUserID, sourceID: source.ID, userID: "e1", username: "marius", }, wantErr: true, }, { name: "Test 3: Empty anthroveUserID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", sourceID: source.ID, userID: "e1", username: "marius", }, wantErr: true, }, { name: "Test 4: invalid sourceID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: "fa.net", userID: "e1", username: "marius", }, wantErr: true, }, { name: "Test 5: no userID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: source.ID, userID: "", username: "marius", }, wantErr: true, }, { name: "Test 6: no username", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: source.ID, userID: "aa", username: "", }, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := CreateUserWithRelationToSource(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.userID, tt.args.username); (err != nil) != tt.wantErr { t.Errorf("CreateUserWithRelationToSource() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestGetAllUsers(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.User{ { BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID01}, }, { BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID02}, }, { BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID03}, }, } for _, user := range users { err = CreateUser(ctx, gormDB, user.ID) if err != nil { t.Fatal(err) } } // Test type args struct { ctx context.Context db *gorm.DB } tests := []struct { name string args args want []models.User wantErr bool }{ { name: "Test 1: Get Data", args: args{ ctx: ctx, db: gormDB, }, want: users, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := GetAllUsers(tt.args.ctx, tt.args.db) if (err != nil) != tt.wantErr { t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr) return } if !checkUser(got, tt.want) { t.Errorf("GetAllUsers() got = %v, want %v", got, tt.want) } }) } } func TestGetUserSourceBySourceID(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") validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) source := &models.Source{ BaseModel: models.BaseModel[models.AnthroveSourceID]{ ID: validSourceID, }, DisplayName: "e621", Domain: "e621.net", Icon: "https://e621.icon", } expectedResult := &models.UserSource{ UserID: string(validUserID), AccountUsername: "euser", Source: models.Source{ BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: source.ID}, DisplayName: source.DisplayName, Domain: source.Domain, Icon: source.Icon, }, } err = CreateSource(ctx, gormDB, source) if err != nil { t.Fatal(err) } err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult.UserID, expectedResult.AccountUsername) if err != nil { t.Fatal(err) } // Test type args struct { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID sourceID models.AnthroveSourceID } tests := []struct { name string args args want *models.UserSource wantErr bool }{ { name: "Test 1: Valid AnthroveUserID and sourceID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: source.ID, }, want: expectedResult, wantErr: false, }, { name: "Test 2: Invalid AnthroveUserID and valid sourceID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: invalidUserID, sourceID: source.ID, }, want: nil, wantErr: true, }, { name: "Test 3: Valid AnthroveUserID and invalid sourceID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: "fa", }, want: nil, wantErr: true, }, { name: "Test 4: No AnthroveUserID and Valid sourceID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", sourceID: source.ID, }, want: nil, wantErr: true, }, { name: "Test 5: Valid AnthroveUserID and No anthroveUserID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "1", sourceID: "", }, want: nil, wantErr: true, }, { name: "Test 6: No AnthroveUserID and No anthroveUserID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", sourceID: "", }, want: nil, wantErr: true, }, { name: "Test 7: No anthroveUserID given", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: "", }, want: nil, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := GetUserSourceBySourceID(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID) if (err != nil) != tt.wantErr { t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr) return } if !checkUserSource(got, tt.want) { t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want) } }) } } func TestGetUserFavoriteNodeWithPagination(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 = CreateUser(ctx, gormDB, validAnthroveUserID) if err != nil { t.Fatal(err) } for _, expectedResultPost := range expectedResultPosts { err = CreatePost(ctx, gormDB, &expectedResultPost) if err != nil { t.Fatal(err) } err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, expectedResultPost.ID) if err != nil { t.Fatal(err) } } // Test type args struct { ctx context.Context db *gorm.DB 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, db: gormDB, anthroveUserID: validAnthroveUserID, skip: 0, limit: 2000, }, want: expectedResult, wantErr: false, }, { name: "Test 2: Skip first two", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validAnthroveUserID, skip: 2, limit: 2000, }, want: expectedResult2, wantErr: false, }, { name: "Test 3: Limit of 3", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validAnthroveUserID, skip: 0, limit: 3, }, want: expectedResult3, wantErr: false, }, { name: "Test 4: No anthroveUserID given", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", skip: 0, limit: 3, }, want: nil, wantErr: true, }, { name: "Test 5: Short anthroveUserID given", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "aaa", skip: 0, limit: 3, }, want: nil, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := GetUserFavoriteWithPagination(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.skip, tt.args.limit) if (err != nil) != tt.wantErr { t.Errorf("GetAllUserFavoritesWithPagination() error = %v, wantErr %v", err, tt.wantErr) return } if !checkFavoritePosts(got, tt.want) { t.Errorf("GetAllUserFavoritesWithPagination() got = %v, want %v", got, tt.want) } }) } } func checkFavoritePosts(got *models.FavoriteList, want *models.FavoriteList) bool { if got == nil && want == nil { return true } else if got == nil || want == nil { return false } for i, post := range got.Posts { if post.ID == want.Posts[i].ID { } else { return false } } return true } func TestGetUserFavoritesCount(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 = 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, validAnthroveUserID, post.ID) if err != nil { t.Fatal(err) } } // Test type args struct { ctx context.Context db *gorm.DB 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, db: gormDB, anthroveUserID: validAnthroveUserID, }, want: 6, wantErr: false, }, { name: "Test 2: Invalid anthroveUserID and 6 favorite posts", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "2", }, want: 0, wantErr: true, }, { name: "Test 3: no anthroveUserID and 6 favorite posts", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", }, want: 0, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := GetUserFavoritesCount(tt.args.ctx, tt.args.db, 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 TestGetUserSourceLinks(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 = CreateSource(ctx, gormDB, eSource) if err != nil { t.Fatal("Create Source e621:", err) } faSource := &models.Source{ BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID2}, DisplayName: "fa", Domain: "fa.net", } err = CreateSource(ctx, gormDB, faSource) if err != nil { t.Fatal("Create Source fa:", 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 = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) if err != nil { t.Fatal("CreateUserWithRelationToSource e621:", err) } err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) if err != nil { t.Fatal("CreateUserWithRelationToSource fa:", err) } // Test type args struct { ctx context.Context db *gorm.DB 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, db: gormDB, anthroveUserID: validAnthroveUserID, }, want: expectedResult, wantErr: false, }, { name: "Test 3: No AnthroveID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", }, want: nil, wantErr: true, }, { name: "Test 1: AnthroveID to short", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "aaa", }, want: nil, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := GetUserSourceLinks(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) if (err != nil) != tt.wantErr { t.Errorf("GetAllUserSources() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("GetAllUserSources() got = %v, want %v", got, tt.want) } }) } } func TestGetUserTagNodeWitRelationToFavedPosts(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 = 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 = CreatePost(ctx, gormDB, &post) if err != nil { t.Fatal(err) } err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, 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 = 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[2].Name, Type: tags[2].Type, }, }, { Frequency: 1, Tags: models.Tag{ Name: tags[1].Name, Type: tags[1].Type, }, }, } // Test type args struct { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID } tests := []struct { name string args args want []models.TagsWithFrequency wantErr bool }{ { name: "Test 1: Get Data", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validAnthroveUserID, }, want: expectedResult, wantErr: false, }, { name: "Test 2: No anthroveUserID given", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", }, want: nil, wantErr: true, }, { name: "Test 3: short anthroveUserID given", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "aaa", }, want: nil, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := GetUserTagWitRelationToFavedPosts(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) if (err != nil) != tt.wantErr { t.Errorf("GetAllTagsFromUser() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("GetAllTagsFromUser() got = %v, want %v", got, tt.want) } }) } } func checkUser(got []models.User, want []models.User) bool { for i, user := range want { if user.ID != got[i].ID { return false } } return true } func checkUserSource(got *models.UserSource, want *models.UserSource) bool { if got == nil && want == nil { return true } else if got == nil || want == nil { return false } if got.UserID != want.UserID { return false } if got.AccountUsername != want.AccountUsername { return false } if got.Source.DisplayName != want.Source.DisplayName { return false } if got.Source.Domain != want.Source.Domain { return false } if got.Source.Icon != want.Source.Icon { return false } return true } func TestUpdateUserSourceScrapeTimeInterval(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") validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) source := &models.Source{ BaseModel: models.BaseModel[models.AnthroveSourceID]{ ID: validSourceID, }, DisplayName: "e621", Domain: "e621.net", Icon: "https://e621.icon", } err = CreateSource(ctx, gormDB, source) if err != nil { t.Fatal(err) } err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") if err != nil { t.Fatal(err) } // Test type args struct { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID sourceID models.AnthroveSourceID scrapeTime models.AnthroveScrapeTimeInterval } tests := []struct { name string args args wantErr bool }{ { name: "Test 1: Valid Data", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: validSourceID, scrapeTime: 10, }, wantErr: false, }, { name: "Test 2: anthroveUserID to short", args: args{ ctx: ctx, db: gormDB, anthroveUserID: invalidUserID, sourceID: validSourceID, scrapeTime: 10, }, wantErr: true, }, { name: "Test 3: anthroveUserID is empty", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", sourceID: validSourceID, scrapeTime: 10, }, wantErr: true, }, { name: "Test 4: anthroveUserID to short", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: "111", scrapeTime: 10, }, wantErr: true, }, { name: "Test 5: anthroveUserID is empty", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: "", scrapeTime: 10, }, wantErr: true, }, { name: "Test 5: scrapeTime is empty", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: validSourceID, scrapeTime: 0, }, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := UpdateUserSourceScrapeTimeInterval(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.scrapeTime); (err != nil) != tt.wantErr { t.Errorf("UpdateUserSourceScrapeTimeInterval() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestUpdateUserSourceLastScrapeTime(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") validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) validScrapeTime := models.AnthroveUserLastScrapeTime(time.Now()) inValidScrapeTime := models.AnthroveUserLastScrapeTime{} source := &models.Source{ BaseModel: models.BaseModel[models.AnthroveSourceID]{ ID: validSourceID, }, DisplayName: "e621", Domain: "e621.net", Icon: "https://e621.icon", } err = CreateSource(ctx, gormDB, source) if err != nil { t.Fatal(err) } err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") if err != nil { t.Fatal(err) } // Test type args struct { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID sourceID models.AnthroveSourceID lastScrapeTime models.AnthroveUserLastScrapeTime } tests := []struct { name string args args wantErr bool }{ { name: "Test 1: Valid Data", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: validSourceID, lastScrapeTime: validScrapeTime, }, wantErr: false, }, { name: "Test 2: anthroveUserID to short", args: args{ ctx: ctx, db: gormDB, anthroveUserID: invalidUserID, sourceID: validSourceID, lastScrapeTime: validScrapeTime, }, wantErr: true, }, { name: "Test 3: anthroveUserID is empty", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", sourceID: validSourceID, lastScrapeTime: validScrapeTime, }, wantErr: true, }, { name: "Test 4: anthroveUserID to short", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: "111", lastScrapeTime: validScrapeTime, }, wantErr: true, }, { name: "Test 5: anthroveUserID is empty", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: "", lastScrapeTime: validScrapeTime, }, wantErr: true, }, { name: "Test 5: scrapeTime is empty", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: validSourceID, lastScrapeTime: inValidScrapeTime, }, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := UpdateUserSourceLastScrapeTime(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.lastScrapeTime); (err != nil) != tt.wantErr { t.Errorf("UpdateUserSourceLastScrapeTime() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestUpdateUserSourceValidation(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") validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) source := &models.Source{ BaseModel: models.BaseModel[models.AnthroveSourceID]{ ID: validSourceID, }, DisplayName: "e621", Domain: "e621.net", Icon: "https://e621.icon", } err = CreateSource(ctx, gormDB, source) if err != nil { t.Fatal(err) } err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") if err != nil { t.Fatal(err) } // Test type args struct { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID sourceID models.AnthroveSourceID valid bool } tests := []struct { name string args args wantErr bool }{ { name: "Test 1: Valid Data", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: validSourceID, valid: true, }, wantErr: false, }, { name: "Test 2: anthroveUserID to short", args: args{ ctx: ctx, db: gormDB, anthroveUserID: invalidUserID, sourceID: validSourceID, valid: true, }, wantErr: true, }, { name: "Test 3: anthroveUserID is empty", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "", sourceID: validSourceID, valid: true, }, wantErr: true, }, { name: "Test 4: anthroveUserID to short", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: "111", valid: true, }, wantErr: true, }, { name: "Test 5: anthroveUserID is empty", args: args{ ctx: ctx, db: gormDB, anthroveUserID: validUserID, sourceID: "", valid: true, }, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := UpdateUserSourceValidation(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.valid); (err != nil) != tt.wantErr { t.Errorf("UpdateUserSourceValidation() error = %v, wantErr %v", err, tt.wantErr) } }) } }