feat(TagAlias): added function and tests
All checks were successful
Gitea Build Check / Build (push) Successful in 11m35s

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

View File

@ -422,3 +422,177 @@ func TestGetAllTagAlias(t *testing.T) {
})
}
}
func TestGetAllTagAliasByTag(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
tagID models.AnthroveTagID
}
tests := []struct {
name string
args args
want []models.TagAlias
wantErr bool
}{
{
name: "Test 1: Valid TagID",
args: args{
ctx: ctx,
db: gormDB,
tagID: validTagID,
},
want: expectedResult,
wantErr: false,
},
{
name: "Test 2: No TagID",
args: args{
ctx: ctx,
db: gormDB,
tagID: "",
},
want: nil,
wantErr: true,
},
{
name: "Test 3: Invalid TagID",
args: args{
ctx: ctx,
db: gormDB,
tagID: "adads",
},
want: []models.TagAlias{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAllTagAliasByTag(tt.args.ctx, tt.args.db, tt.args.tagID)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllTagAliasByTag() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllTagAliasByTag() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeleteTagAlias(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,
}
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
tagAliasName models.AnthroveTagAliasName
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid AnthroveTagAliasName",
args: args{
ctx: ctx,
db: gormDB,
tagAliasName: validTagAliases[0],
},
wantErr: false,
},
{
name: "Test 2: Invalid AnthroveTagAliasName",
args: args{
ctx: ctx,
db: gormDB,
tagAliasName: "asdad",
},
wantErr: false,
},
{
name: "Test 3: No AnthroveTagAliasName",
args: args{
ctx: ctx,
db: gormDB,
tagAliasName: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteTagAlias(tt.args.ctx, tt.args.db, tt.args.tagAliasName); (err != nil) != tt.wantErr {
t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -174,13 +174,11 @@ func (p *postgresqlConnection) GetAllTagAlias(ctx context.Context) ([]models.Tag
}
func (p *postgresqlConnection) GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) {
//TODO implement me
panic("implement me")
return postgres.GetAllTagAliasByTag(ctx, p.db, tagID)
}
func (p *postgresqlConnection) DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error {
//TODO implement me
panic("implement me")
return postgres.DeleteTagAlias(ctx, p.db, tagAliasName)
}
func (p *postgresqlConnection) CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) {

View File

@ -2193,3 +2193,177 @@ func Test_postgresqlConnection_GetAllTagAlias(t *testing.T) {
})
}
}
func Test_postgresqlConnection_GetAllTagAliasByTag(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
tagID models.AnthroveTagID
}
tests := []struct {
name string
args args
want []models.TagAlias
wantErr bool
}{
{
name: "Test 1: Valid TagID",
args: args{
ctx: ctx,
tagID: validTagID,
},
want: expectedResult,
wantErr: false,
},
{
name: "Test 2: No TagID",
args: args{
ctx: ctx,
tagID: "",
},
want: nil,
wantErr: true,
},
{
name: "Test 3: Invalid TagID",
args: args{
ctx: ctx,
tagID: "adads",
},
want: []models.TagAlias{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &postgresqlConnection{
db: gormDB,
debug: true,
}
got, err := p.GetAllTagAliasByTag(tt.args.ctx, tt.args.tagID)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllTagAliasByTag() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllTagAliasByTag() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_postgresqlConnection_DeleteTagAlias(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,
}
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
tagAliasName models.AnthroveTagAliasName
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid AnthroveTagAliasName",
args: args{
ctx: ctx,
tagAliasName: validTagAliases[0],
},
wantErr: false,
},
{
name: "Test 2: Invalid AnthroveTagAliasName",
args: args{
ctx: ctx,
tagAliasName: "asdad",
},
wantErr: false,
},
{
name: "Test 3: No AnthroveTagAliasName",
args: args{
ctx: ctx,
tagAliasName: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &postgresqlConnection{
db: gormDB,
debug: true,
}
if err := p.DeleteTagAlias(tt.args.ctx, tt.args.tagAliasName); (err != nil) != tt.wantErr {
t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}