feat(TagAlias): added function and tests

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-28 22:14:27 +02:00
parent bb45658310
commit 26178eb64d
3 changed files with 156 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package postgres
import ( import (
"context" "context"
"fmt" "fmt"
"reflect"
"testing" "testing"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "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)
}
})
}
}

View File

@ -170,8 +170,7 @@ func (p *postgresqlConnection) CreateTagAlias(ctx context.Context, tagAliasName
} }
func (p *postgresqlConnection) GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error) { func (p *postgresqlConnection) GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error) {
//TODO implement me return postgres.GetAllTagAlias(ctx, p.db)
panic("implement me")
} }
func (p *postgresqlConnection) GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) { func (p *postgresqlConnection) GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) {

View File

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