From 26178eb64d6c0949005e6748d9a734ef2a49cb0d Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 28 Jun 2024 22:14:27 +0200 Subject: [PATCH] feat(TagAlias): added function and tests Signed-off-by: SoXX --- internal/postgres/tag_test.go | 78 +++++++++++++++++++++++++++++++++++ pkg/database/postgres.go | 3 +- pkg/database/postgres_test.go | 77 ++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 2 deletions(-) diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go index c18712a..8e51eb9 100644 --- a/internal/postgres/tag_test.go +++ b/internal/postgres/tag_test.go @@ -3,6 +3,7 @@ package postgres import ( "context" "fmt" + "reflect" "testing" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" @@ -344,3 +345,80 @@ func TestCreateTagAlias(t *testing.T) { }) } } + +func TestGetAllTagAlias(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 + } + tests := []struct { + name string + args args + want []models.TagAlias + wantErr bool + }{ + { + name: "Test 1: Get Data", + args: args{ + ctx: ctx, + db: gormDB, + }, + want: expectedResult, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetAllTagAlias(tt.args.ctx, tt.args.db) + if (err != nil) != tt.wantErr { + t.Errorf("GetAllTagAlias() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetAllTagAlias() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index d511a84..bb1e8c0 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -170,8 +170,7 @@ func (p *postgresqlConnection) CreateTagAlias(ctx context.Context, tagAliasName } func (p *postgresqlConnection) GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error) { - //TODO implement me - panic("implement me") + return postgres.GetAllTagAlias(ctx, p.db) } func (p *postgresqlConnection) GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) { diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index 6950118..dbcd844 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -2116,3 +2116,80 @@ func Test_postgresqlConnection_CreateTagAlias(t *testing.T) { }) } } + +func Test_postgresqlConnection_GetAllTagAlias(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 + } + tests := []struct { + name string + args args + want []models.TagAlias + wantErr bool + }{ + { + name: "Test 1: Get Data", + args: args{ctx: ctx}, + 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.GetAllTagAlias(tt.args.ctx) + if (err != nil) != tt.wantErr { + t.Errorf("GetAllTagAlias() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetAllTagAlias() got = %v, want %v", got, tt.want) + } + }) + } +}