From 702870db1e87036a8965633409437a77bd19269b Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 28 Jun 2024 22:27:31 +0200 Subject: [PATCH] feat(TagAlias): added function and tests Signed-off-by: SoXX --- internal/postgres/tag_test.go | 174 ++++++++++++++++++++++++++++++++++ pkg/database/postgres.go | 6 +- pkg/database/postgres_test.go | 174 ++++++++++++++++++++++++++++++++++ 3 files changed, 350 insertions(+), 4 deletions(-) diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go index 8e51eb9..54f8c03 100644 --- a/internal/postgres/tag_test.go +++ b/internal/postgres/tag_test.go @@ -422,3 +422,177 @@ func TestGetAllTagAlias(t *testing.T) { }) } } + +func TestGetAllTagAliasByTag(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 + validTagID := models.AnthroveTagID("toothless") + validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} + + validTag := &models.Tag{ + Name: string(validTagID), + Type: models.Character, + } + + expectedResult := []models.TagAlias{ + { + Name: string(validTagAliases[0]), + TagID: string(validTagID), + }, + { + Name: string(validTagAliases[1]), + TagID: string(validTagID), + }, + { + Name: string(validTagAliases[2]), + TagID: string(validTagID), + }, + } + + err = CreateTag(ctx, gormDB, validTag) + if err != nil { + t.Fatal(err) + } + + for _, tagAliasName := range validTagAliases { + err = CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) + } + + // Test + type args struct { + ctx context.Context + db *gorm.DB + tagID models.AnthroveTagID + } + tests := []struct { + name string + args args + want []models.TagAlias + wantErr bool + }{ + { + name: "Test 1: Valid TagID", + args: args{ + ctx: ctx, + db: gormDB, + tagID: validTagID, + }, + want: expectedResult, + wantErr: false, + }, + { + name: "Test 2: No TagID", + args: args{ + ctx: ctx, + db: gormDB, + tagID: "", + }, + want: nil, + wantErr: true, + }, + { + name: "Test 3: Invalid TagID", + args: args{ + ctx: ctx, + db: gormDB, + tagID: "adads", + }, + want: []models.TagAlias{}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetAllTagAliasByTag(tt.args.ctx, tt.args.db, tt.args.tagID) + if (err != nil) != tt.wantErr { + t.Errorf("GetAllTagAliasByTag() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetAllTagAliasByTag() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestDeleteTagAlias(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 + validTagID := models.AnthroveTagID("toothless") + validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} + + validTag := &models.Tag{ + Name: string(validTagID), + Type: models.Character, + } + + err = CreateTag(ctx, gormDB, validTag) + if err != nil { + t.Fatal(err) + } + + for _, tagAliasName := range validTagAliases { + err = CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) + } + + // Test + type args struct { + ctx context.Context + db *gorm.DB + tagAliasName models.AnthroveTagAliasName + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 1: Valid AnthroveTagAliasName", + args: args{ + ctx: ctx, + db: gormDB, + tagAliasName: validTagAliases[0], + }, + wantErr: false, + }, + { + name: "Test 2: Invalid AnthroveTagAliasName", + args: args{ + ctx: ctx, + db: gormDB, + tagAliasName: "asdad", + }, + wantErr: false, + }, + { + name: "Test 3: No AnthroveTagAliasName", + args: args{ + ctx: ctx, + db: gormDB, + tagAliasName: "", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeleteTagAlias(tt.args.ctx, tt.args.db, tt.args.tagAliasName); (err != nil) != tt.wantErr { + t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index bb1e8c0..1eb22de 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -174,13 +174,11 @@ func (p *postgresqlConnection) GetAllTagAlias(ctx context.Context) ([]models.Tag } func (p *postgresqlConnection) GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) { - //TODO implement me - panic("implement me") + return postgres.GetAllTagAliasByTag(ctx, p.db, tagID) } func (p *postgresqlConnection) DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error { - //TODO implement me - panic("implement me") + return postgres.DeleteTagAlias(ctx, p.db, tagAliasName) } func (p *postgresqlConnection) CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) { diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index dbcd844..acd0b0e 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -2193,3 +2193,177 @@ func Test_postgresqlConnection_GetAllTagAlias(t *testing.T) { }) } } + +func Test_postgresqlConnection_GetAllTagAliasByTag(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 + validTagID := models.AnthroveTagID("toothless") + validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} + + validTag := &models.Tag{ + Name: string(validTagID), + Type: models.Character, + } + + expectedResult := []models.TagAlias{ + { + Name: string(validTagAliases[0]), + TagID: string(validTagID), + }, + { + Name: string(validTagAliases[1]), + TagID: string(validTagID), + }, + { + Name: string(validTagAliases[2]), + TagID: string(validTagID), + }, + } + + err = postgres.CreateTag(ctx, gormDB, validTag) + if err != nil { + t.Fatal(err) + } + + for _, tagAliasName := range validTagAliases { + err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) + } + + // Test + type args struct { + ctx context.Context + tagID models.AnthroveTagID + } + tests := []struct { + name string + args args + want []models.TagAlias + wantErr bool + }{ + { + name: "Test 1: Valid TagID", + args: args{ + ctx: ctx, + tagID: validTagID, + }, + want: expectedResult, + wantErr: false, + }, + { + name: "Test 2: No TagID", + args: args{ + ctx: ctx, + tagID: "", + }, + want: nil, + wantErr: true, + }, + { + name: "Test 3: Invalid TagID", + args: args{ + ctx: ctx, + tagID: "adads", + }, + want: []models.TagAlias{}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetAllTagAliasByTag(tt.args.ctx, tt.args.tagID) + if (err != nil) != tt.wantErr { + t.Errorf("GetAllTagAliasByTag() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetAllTagAliasByTag() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_postgresqlConnection_DeleteTagAlias(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 + validTagID := models.AnthroveTagID("toothless") + validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} + + validTag := &models.Tag{ + Name: string(validTagID), + Type: models.Character, + } + + err = postgres.CreateTag(ctx, gormDB, validTag) + if err != nil { + t.Fatal(err) + } + + for _, tagAliasName := range validTagAliases { + err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) + } + + // Test + type args struct { + ctx context.Context + tagAliasName models.AnthroveTagAliasName + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 1: Valid AnthroveTagAliasName", + args: args{ + ctx: ctx, + tagAliasName: validTagAliases[0], + }, + wantErr: false, + }, + { + name: "Test 2: Invalid AnthroveTagAliasName", + args: args{ + ctx: ctx, + tagAliasName: "asdad", + }, + wantErr: false, + }, + { + name: "Test 3: No AnthroveTagAliasName", + args: args{ + ctx: ctx, + tagAliasName: "", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + if err := p.DeleteTagAlias(tt.args.ctx, tt.args.tagAliasName); (err != nil) != tt.wantErr { + t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}