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
5 changed files with 214 additions and 4 deletions
Showing only changes of commit 2d4102be6a - Show all commits

View File

@ -62,6 +62,29 @@ func DeleteTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName)
return nil return nil
} }
func GetAllTagByTagsType(ctx context.Context, db *gorm.DB, tagType models.TagType) ([]models.Tag, error) {
var tags []models.Tag
if tagType == "" {
return nil, &otterError.EntityValidationFailed{Reason: "tagType cannot be empty"}
}
result := db.WithContext(ctx).Find(&tags).Where("tag_type = ?", tagType)
fenpaws marked this conversation as resolved Outdated

please use Scan instead of Find if you have a Where clause.
Or if you use find, put the Filter into the find function. Also its best practice if you move the Find, Scan, Create, ... as last method.

please use Scan instead of Find if you have a Where clause. Or if you use find, put the Filter into the find function. Also its best practice if you move the Find, Scan, Create, ... as last method.
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"tags_length": len(tags),
}).Trace("database: got tag")
return tags, nil
}
func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error { func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error {
if anthrovePostID == "" { if anthrovePostID == "" {

View File

@ -1019,3 +1019,97 @@ func TestDeleteTag(t *testing.T) {
}) })
} }
} }
func TestGetAllTagByTagType(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
validTags := []models.Tag{
{
Name: "JayTheFerret",
Type: models.Character,
},
{
Name: "SoXX",
Type: models.Character,
},
{
Name: "Alphyron",
Type: models.Character,
},
{
Name: "Dragon",
Type: models.Species,
},
}
for _, tag := range validTags {
err = CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type)
if err != nil {
t.Fatal(err)
}
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
tagType models.TagType
}
tests := []struct {
name string
args args
want []models.Tag
wantErr bool
}{
{
name: "Test 1: Get Data",
args: args{
ctx: ctx,
db: gormDB,
tagType: models.Character,
},
want: validTags,
wantErr: false,
},
{
name: "Test 2: invalid Tag Type",
args: args{
ctx: ctx,
db: gormDB,
tagType: "aa",
},
want: validTags,
wantErr: false,
},
{
name: "Test 3: No Tag Type",
args: args{
ctx: ctx,
db: gormDB,
tagType: "",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAllTagByTagsType(tt.args.ctx, tt.args.db, tt.args.tagType)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllTagByTagsType() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkTag(got, tt.want) {
t.Errorf("GetAllTagByTagsType() got = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -206,9 +206,8 @@ func (p *postgresqlConnection) CreateTag(ctx context.Context, tagName models.Ant
return postgres.CreateTag(ctx, p.db, tagName, tagType) return postgres.CreateTag(ctx, p.db, tagName, tagType)
} }
func (p *postgresqlConnection) GetAllTagByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) { func (p *postgresqlConnection) GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) {
//TODO implement me return postgres.GetAllTagByTagsType(ctx, p.db, tagType)
panic("implement me")
} }
func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error { func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error {

View File

@ -3143,3 +3143,97 @@ func Test_postgresqlConnection_DeleteTag(t *testing.T) {
}) })
} }
} }
func Test_postgresqlConnection_GetAllTagsByTagType(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
validTags := []models.Tag{
{
Name: "JayTheFerret",
Type: models.Character,
},
{
Name: "SoXX",
Type: models.Character,
},
{
Name: "Alphyron",
Type: models.Character,
},
{
Name: "Dragon",
Type: models.Species,
},
}
for _, tag := range validTags {
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type)
if err != nil {
t.Fatal(err)
}
}
// Test
type args struct {
ctx context.Context
tagType models.TagType
}
tests := []struct {
name string
args args
want []models.Tag
wantErr bool
}{
{
name: "Test 1: Get Data",
args: args{
ctx: ctx,
tagType: models.Character,
},
want: validTags,
wantErr: false,
},
{
name: "Test 2: invalid Tag Type",
args: args{
ctx: ctx,
tagType: "aa",
},
want: validTags,
wantErr: false,
},
{
name: "Test 3: No Tag Type",
args: args{
ctx: ctx,
tagType: "",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &postgresqlConnection{
db: gormDB,
debug: true,
}
got, err := p.GetAllTagsByTagType(tt.args.ctx, tt.args.tagType)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllTagsByTagType() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllTagsByTagType() got = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -12,7 +12,7 @@ type Tag interface {
// GetAllTags retrieves all tags. // GetAllTags retrieves all tags.
GetAllTags(ctx context.Context) ([]models.Tag, error) GetAllTags(ctx context.Context) ([]models.Tag, error)
GetAllTagByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error)
DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error
} }