refactor: added getting of tag by specific type
Some checks failed
Gitea Build Check / Build (push) Has been cancelled
Gitea Build Check / Build (pull_request) Successful in 11m44s

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-07-01 22:29:37 +02:00
parent a842d64f1c
commit 2d4102be6a
5 changed files with 214 additions and 4 deletions

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