From b5437ecc6298c3da5d807f3082ac784d8000ddc0 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 28 Jun 2024 14:16:28 +0200 Subject: [PATCH] tests: fixed tests Signed-off-by: SoXX --- internal/postgres/tag_test.go | 2 +- internal/postgres/user.go | 6 +- internal/postgres/user_test.go | 69 +++++++++++++--- pkg/database/database.go | 3 + pkg/database/post.go | 8 +- pkg/database/{postgres => }/postgres.go | 11 ++- pkg/database/{postgres => }/postgres_test.go | 87 +++++++++++++++----- pkg/database/user.go | 2 +- pkg/models/postReference_test.go | 16 ++-- test/helper.go | 7 +- 10 files changed, 155 insertions(+), 56 deletions(-) rename pkg/database/{postgres => }/postgres.go (93%) rename pkg/database/{postgres => }/postgres_test.go (95%) diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go index 045cd96..3f22c76 100644 --- a/internal/postgres/tag_test.go +++ b/internal/postgres/tag_test.go @@ -94,7 +94,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := CreateTagAndReferenceToPost(tt.args.ctx, tt.args.db, tt.args.PostID, tt.args.tag); (err != nil) != tt.wantErr { - t.Errorf("CreateTagAndReferenceToPost() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("CreatePostWithReferenceToTagAnd() error = %v, wantErr %v", err, tt.wantErr) } }) } diff --git a/internal/postgres/user.go b/internal/postgres/user.go index 8e8b2d7..c0a2543 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -170,7 +170,7 @@ func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models. } func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (*models.UserSource, error) { - var userSource *models.UserSource + var userSource models.UserSource if anthroveUserID == "" { return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} @@ -188,7 +188,7 @@ func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID mo return nil, &otterError.EntityValidationFailed{Reason: "sourceID needs to be 25 characters long"} } - result := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(userSource) + result := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(&userSource) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, &otterError.NoDataFound{} @@ -201,7 +201,7 @@ func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID mo "source_id": sourceID, }).Trace("database: got specified user source link") - return userSource, nil + return &userSource, nil } func GetAllUsers(ctx context.Context, db *gorm.DB) ([]models.User, error) { diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go index c4efa00..8dfa13a 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -198,7 +198,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { } } -func TestGetAllAnthroveUserIDs(t *testing.T) { +func TestGetAllUsers(t *testing.T) { // Setup trow away container ctx := context.Background() container, gormDB, err := test.StartPostgresContainer(ctx) @@ -212,10 +212,20 @@ func TestGetAllAnthroveUserIDs(t *testing.T) { validUserID02 := models.AnthroveUserID(fmt.Sprintf("%025s", "User2")) validUserID03 := models.AnthroveUserID(fmt.Sprintf("%025s", "User3")) - users := []models.AnthroveUserID{validUserID01, validUserID02, validUserID03} + 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) + err = CreateUser(ctx, gormDB, user.ID) if err != nil { t.Fatal(err) } @@ -229,7 +239,7 @@ func TestGetAllAnthroveUserIDs(t *testing.T) { tests := []struct { name string args args - want []models.AnthroveUserID + want []models.User wantErr bool }{ { @@ -249,7 +259,7 @@ func TestGetAllAnthroveUserIDs(t *testing.T) { t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { + if !checkUser(got, tt.want) { t.Errorf("GetAllUsers() got = %v, want %v", got, tt.want) } }) @@ -278,13 +288,14 @@ func TestGetUserSourceBySourceID(t *testing.T) { }, DisplayName: "e621", Domain: "e621.net", + Icon: "https://e621.icon", } - expectedResult := make(map[string]models.UserSource) - expectedResult["e621"] = models.UserSource{ - UserID: "e1", + 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, @@ -296,7 +307,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { t.Fatal(err) } - err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) + err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult.UserID, expectedResult.AccountUsername) if err != nil { t.Fatal(err) } @@ -311,7 +322,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { tests := []struct { name string args args - want map[string]models.UserSource + want *models.UserSource wantErr bool }{ { @@ -400,7 +411,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { + if !checkUserSource(got, tt.want) { t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want) } }) @@ -939,3 +950,39 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { }) } } + +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 +} diff --git a/pkg/database/database.go b/pkg/database/database.go index 7b1aef2..0bce2bf 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -10,6 +10,9 @@ type OtterSpace interface { // Connect establishes a connection to the database. Connect(ctx context.Context, config models.DatabaseConfig) error + // Post contains all function that are needed to manage Posts + Post + // User contains all function that are needed to manage the AnthroveUser User diff --git a/pkg/database/post.go b/pkg/database/post.go index 7edb191..0111170 100644 --- a/pkg/database/post.go +++ b/pkg/database/post.go @@ -20,9 +20,9 @@ type Post interface { // GetPostBySourceID retrieves a post by its source ID. GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) - // CreateTagAndReferenceToPost adds a tag with a relation to a post. - CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error + // CreatePostWithReferenceToTagAnd adds a tag with a relation to a post. + CreatePostWithReferenceToTagAnd(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error - // CreateReferenceBetweenPostAndSource links a post with a source. - CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error + // CreatePostReference links a post with a source. + CreatePostReference(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error } diff --git a/pkg/database/postgres/postgres.go b/pkg/database/postgres.go similarity index 93% rename from pkg/database/postgres/postgres.go rename to pkg/database/postgres.go index da69ba8..b63ca0b 100644 --- a/pkg/database/postgres/postgres.go +++ b/pkg/database/postgres.go @@ -1,4 +1,4 @@ -package postgres +package database import ( "context" @@ -9,7 +9,6 @@ import ( "time" "git.dragse.it/anthrove/otter-space-sdk/internal/postgres" - "git.dragse.it/anthrove/otter-space-sdk/pkg/database" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" _ "github.com/lib/pq" migrate "github.com/rubenv/sql-migrate" @@ -27,7 +26,7 @@ type postgresqlConnection struct { debug bool } -func NewPostgresqlConnection() database.OtterSpace { +func NewPostgresqlConnection() OtterSpace { return &postgresqlConnection{ db: nil, } @@ -85,11 +84,11 @@ func (p *postgresqlConnection) CreatePost(ctx context.Context, anthrovePost *mod return postgres.CreatePost(ctx, p.db, anthrovePost) } -func (p *postgresqlConnection) CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error { +func (p *postgresqlConnection) CreatePostWithReferenceToTagAnd(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error { return postgres.CreateTagAndReferenceToPost(ctx, p.db, anthrovePostID, anthroveTag) } -func (p *postgresqlConnection) CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error { +func (p *postgresqlConnection) CreatePostReference(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error { return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain, postURL, config) } @@ -125,7 +124,7 @@ func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anth return postgres.GetUserSourceBySourceID(ctx, p.db, anthroveUserID, sourceID) } -func (p *postgresqlConnection) GetAllUser(ctx context.Context) ([]models.User, error) { +func (p *postgresqlConnection) GetAllUsers(ctx context.Context) ([]models.User, error) { return postgres.GetAllUsers(ctx, p.db) } diff --git a/pkg/database/postgres/postgres_test.go b/pkg/database/postgres_test.go similarity index 95% rename from pkg/database/postgres/postgres_test.go rename to pkg/database/postgres_test.go index bf7159f..26a46fb 100644 --- a/pkg/database/postgres/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -1,4 +1,4 @@ -package postgres +package database import ( "context" @@ -7,7 +7,6 @@ import ( "testing" "git.dragse.it/anthrove/otter-space-sdk/internal/postgres" - "git.dragse.it/anthrove/otter-space-sdk/pkg/database" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/test" "gorm.io/gorm" @@ -17,7 +16,7 @@ func TestNewPostgresqlConnection(t *testing.T) { // Test tests := []struct { name string - want database.OtterSpace + want OtterSpace }{ { name: "Test 1: Create new postgresql connection", @@ -421,8 +420,8 @@ func Test_postgresqlConnection_CreateTagAndReferenceToPost(t *testing.T) { db: gormDB, debug: true, } - if err := p.CreateTagAndReferenceToPost(tt.args.ctx, tt.args.anthrovePostID, tt.args.anthroveTag); (err != nil) != tt.wantErr { - t.Errorf("CreateTagAndReferenceToPost() error = %v, wantErr %v", err, tt.wantErr) + if err := p.CreatePostWithReferenceToTagAnd(tt.args.ctx, tt.args.anthrovePostID, tt.args.anthroveTag); (err != nil) != tt.wantErr { + t.Errorf("CreatePostWithReferenceToTagAnd() error = %v, wantErr %v", err, tt.wantErr) } }) } @@ -522,8 +521,8 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T) db: gormDB, debug: true, } - if err := p.CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain, tt.args.postURl, models.PostReferenceConfig{}); (err != nil) != tt.wantErr { - t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr) + if err := p.CreatePostReference(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain, tt.args.postURl, models.PostReferenceConfig{}); (err != nil) != tt.wantErr { + t.Errorf("CreatePostReference() error = %v, wantErr %v", err, tt.wantErr) } }) } @@ -714,7 +713,7 @@ func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(t *testing.T) { db: gormDB, debug: true, } - got, err := p.CheckIfUserFavouritedPost(tt.args.ctx, tt.args.anthroveUserID, tt.args.anthrovePostID) + got, err := p.CheckIfUserHasPostAsFavorite(tt.args.ctx, tt.args.anthroveUserID, tt.args.anthrovePostID) if (err != nil) != tt.wantErr { t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr) return @@ -1235,20 +1234,20 @@ func Test_postgresqlConnection_GetUserSourceBySourceID(t *testing.T) { validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - expectedResult := make(map[string]models.UserSource) - source := &models.Source{ BaseModel: models.BaseModel[models.AnthroveSourceID]{ ID: validSourceID, }, DisplayName: "e621", Domain: "e621.net", + Icon: "https://e621.icon", } - expectedResult["e621"] = models.UserSource{ - UserID: "e1", + 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, @@ -1260,7 +1259,7 @@ func Test_postgresqlConnection_GetUserSourceBySourceID(t *testing.T) { t.Fatal(err) } - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) + err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult.UserID, expectedResult.AccountUsername) if err != nil { t.Fatal(err) } @@ -1271,10 +1270,11 @@ func Test_postgresqlConnection_GetUserSourceBySourceID(t *testing.T) { anthroveUserID models.AnthroveUserID sourceID models.AnthroveSourceID } + tests := []struct { name string args args - want map[string]models.UserSource + want *models.UserSource wantErr bool }{ { @@ -1349,14 +1349,14 @@ func Test_postgresqlConnection_GetUserSourceBySourceID(t *testing.T) { t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { + if !checkUserSource(got, tt.want) { t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want) } }) } } -func Test_postgresqlConnection_GetAllAnthroveUserIDs(t *testing.T) { +func Test_postgresqlConnection_GetAllUsers(t *testing.T) { // Setup trow away container ctx := context.Background() container, gormDB, err := test.StartPostgresContainer(ctx) @@ -1370,10 +1370,20 @@ func Test_postgresqlConnection_GetAllAnthroveUserIDs(t *testing.T) { validUserID02 := models.AnthroveUserID(fmt.Sprintf("%025s", "User2")) validUserID03 := models.AnthroveUserID(fmt.Sprintf("%025s", "User3")) - users := []models.AnthroveUserID{validUserID01, validUserID02, validUserID03} + 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 = postgres.CreateUser(ctx, gormDB, user) + err = postgres.CreateUser(ctx, gormDB, user.ID) if err != nil { t.Fatal(err) } @@ -1386,7 +1396,7 @@ func Test_postgresqlConnection_GetAllAnthroveUserIDs(t *testing.T) { tests := []struct { name string args args - want []models.AnthroveUserID + want []models.User wantErr bool }{ { @@ -1403,12 +1413,12 @@ func Test_postgresqlConnection_GetAllAnthroveUserIDs(t *testing.T) { db: gormDB, debug: true, } - got, err := p.GetAllAnthroveUserIDs(tt.args.ctx) + got, err := p.GetAllUsers(tt.args.ctx) if (err != nil) != tt.wantErr { t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { + if !checkUser(got, tt.want) { t.Errorf("GetAllUsers() got = %v, want %v", got, tt.want) } }) @@ -1975,3 +1985,38 @@ func Test_postgresqlConnection_migrateDatabase(t *testing.T) { }) } } + +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 +} diff --git a/pkg/database/user.go b/pkg/database/user.go index e7be12e..b74d672 100644 --- a/pkg/database/user.go +++ b/pkg/database/user.go @@ -20,7 +20,7 @@ type User interface { UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error - GetAllUser(ctx context.Context) ([]models.User, error) + GetAllUsers(ctx context.Context) ([]models.User, error) // GetUserFavoritesCount retrieves the count of a user's favorites. GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) diff --git a/pkg/models/postReference_test.go b/pkg/models/postReference_test.go index 232a695..d203024 100644 --- a/pkg/models/postReference_test.go +++ b/pkg/models/postReference_test.go @@ -26,13 +26,15 @@ func TestPostReference_TableName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { po := PostReference{ - PostID: tt.fields.PostID, - SourceID: tt.fields.SourceID, - URL: tt.fields.URL, - SourcePostID: tt.fields.SourcePostID, - FullFileURL: tt.fields.FullFileURL, - PreviewFileURL: tt.fields.PreviewFileURL, - SampleFileURL: tt.fields.SampleFileURL, + PostID: tt.fields.PostID, + SourceID: tt.fields.SourceID, + URL: tt.fields.URL, + PostReferenceConfig: PostReferenceConfig{ + SourcePostID: tt.fields.SourcePostID, + FullFileURL: tt.fields.FullFileURL, + PreviewFileURL: tt.fields.PreviewFileURL, + SampleFileURL: tt.fields.SampleFileURL, + }, } if got := po.TableName(); got != tt.want { t.Errorf("TableName() = %v, want %v", got, tt.want) diff --git a/test/helper.go b/test/helper.go index ce571d2..b16d9d1 100644 --- a/test/helper.go +++ b/test/helper.go @@ -6,6 +6,7 @@ import ( "net/url" "strconv" "strings" + "time" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" migrate "github.com/rubenv/sql-migrate" @@ -22,7 +23,7 @@ const ( databaseName = "anthrove" databaseUser = "anthrove" databasePassword = "anthrove" - migrationSource = "../../pkg/database/migrations" + migrationSource = "../../pkg/database/migrations/" ) func StartPostgresContainer(ctx context.Context) (*postgrescontainer.PostgresContainer, *gorm.DB, error) { @@ -33,7 +34,9 @@ func StartPostgresContainer(ctx context.Context) (*postgrescontainer.PostgresCon postgrescontainer.WithUsername(databaseUser), postgrescontainer.WithPassword(databasePassword), testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"))) + wait.ForLog("database system is ready to accept connections"). + WithOccurrence(2).WithStartupTimeout(60*time.Second)), + ) if err != nil { return nil, nil, err }