From a842d64f1cb2f39529f64bc5fd6ffde0fabc1b8b Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 1 Jul 2024 22:15:43 +0200 Subject: [PATCH] refactor: added deletion of tags Signed-off-by: SoXX --- internal/postgres/tag.go | 26 ++++++++++++- internal/postgres/tag_test.go | 70 +++++++++++++++++++++++++++++++++++ pkg/database/postgres.go | 3 +- pkg/database/postgres_test.go | 70 +++++++++++++++++++++++++++++++++++ 4 files changed, 165 insertions(+), 4 deletions(-) diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go index bc5ccd0..f810266 100644 --- a/internal/postgres/tag.go +++ b/internal/postgres/tag.go @@ -13,11 +13,11 @@ import ( func CreateTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName, tagType models.TagType) error { if tagName == "" { - return &otterError.EntityValidationFailed{Reason: "tagName is empty"} + return &otterError.EntityValidationFailed{Reason: "tagName cannot be empty"} } if tagType == "" { - return &otterError.EntityValidationFailed{Reason: "tagType is empty"} + return &otterError.EntityValidationFailed{Reason: "tagType cannot be empty"} } result := db.WithContext(ctx).Create(&models.Tag{Name: string(tagName), Type: tagType}) @@ -40,6 +40,28 @@ func CreateTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName, return nil } +func DeleteTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName) error { + + if tagName == "" { + return &otterError.EntityValidationFailed{Reason: "tagName cannot be empty"} + } + + result := db.WithContext(ctx).Delete(&models.Tag{Name: string(tagName)}) + + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.NoDataFound{} + } + return result.Error + } + + log.WithFields(log.Fields{ + "tag_name": tagName, + }).Trace("database: deleted tag") + + return nil +} + func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error { if anthrovePostID == "" { diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go index 5a30658..88eabcd 100644 --- a/internal/postgres/tag_test.go +++ b/internal/postgres/tag_test.go @@ -949,3 +949,73 @@ func TestDeleteTagGroup(t *testing.T) { }) } } + +func TestDeleteTag(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") + + validTag := &models.Tag{ + Name: string(validTagID), + Type: models.Character, + } + + err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) + if err != nil { + t.Fatal(err) + } + + // Test + type args struct { + ctx context.Context + db *gorm.DB + tagName models.AnthroveTagName + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 1: Valid TagName", + args: args{ + ctx: ctx, + db: gormDB, + tagName: models.AnthroveTagName(validTagID), + }, + wantErr: false, + }, + { + name: "Test 2: Invalid TagName", + args: args{ + ctx: ctx, + db: gormDB, + tagName: models.AnthroveTagName("aaa"), + }, + wantErr: false, + }, + { + name: "Test 3: No TagName", + args: args{ + ctx: ctx, + db: gormDB, + tagName: "", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeleteTag(tt.args.ctx, tt.args.db, tt.args.tagName); (err != nil) != tt.wantErr { + t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 4e1b02b..0acb71e 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -212,8 +212,7 @@ func (p *postgresqlConnection) GetAllTagByTagType(ctx context.Context, tagType m } func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error { - //TODO implement me - panic("implement me") + return postgres.DeleteTag(ctx, p.db, tagName) } // HELPER diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index 3a46606..88b10ba 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -3073,3 +3073,73 @@ func Test_postgresqlConnection_UpdateUserSourceValidation(t *testing.T) { }) } } + +func Test_postgresqlConnection_DeleteTag(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") + + validTag := &models.Tag{ + Name: string(validTagID), + Type: models.Character, + } + + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) + if err != nil { + t.Fatal(err) + } + + // Test + type args struct { + ctx context.Context + tagName models.AnthroveTagName + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 1: Valid TagName", + args: args{ + ctx: ctx, + tagName: models.AnthroveTagName(validTagID), + }, + wantErr: false, + }, + { + name: "Test 2: Invalid TagName", + args: args{ + ctx: ctx, + tagName: models.AnthroveTagName("aaa"), + }, + wantErr: false, + }, + { + name: "Test 3: No TagName", + args: args{ + ctx: ctx, + tagName: "", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + if err := p.DeleteTag(tt.args.ctx, tt.args.tagName); (err != nil) != tt.wantErr { + t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}