BREAKING CHANGE: V2 of thr SDK #12

Merged
fenpaws merged 124 commits from develop/postgresql into main 2024-07-01 20:46:28 +00:00
4 changed files with 165 additions and 4 deletions
Showing only changes of commit a842d64f1c - Show all commits

View File

@ -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 == "" {

View File

@ -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)
}
})
}
}

View File

@ -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

View File

@ -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)
}
})
}
}