feat(test): added tagAlias
Some checks failed
Gitea Build Check / Build (push) Failing after 1m1s

fully tested tag groups
This commit is contained in:
SoXX 2024-08-13 14:44:50 +02:00
parent 5562d75e3a
commit f0f216bdaa

View File

@ -0,0 +1,292 @@
package database
import (
"context"
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
func TestCreateTagAlias(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tag to test with
validTag := models.Tag{
Name: "valid_tag",
Type: models.General,
}
validTag, err = CreateTag(ctx, validTag.Name, validTag.Type)
if err != nil {
t.Fatalf("CreateTag err: %v", err)
}
// --
// -- Create TagAlias to test with
validTagAlias := models.TagAlias{
Name: "valid_tag_alias_name",
TagID: validTag.Name,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagAliasName models.TagAliasName
tagName models.TagName
}
tests := []struct {
name string
args args
want models.TagAlias
wantErr bool
}{
{
name: "Test 01: Valid tagAlias",
args: args{
ctx: ctx,
tagAliasName: validTagAlias.Name,
tagName: validTag.Name,
},
want: validTagAlias,
wantErr: false,
},
{
name: "Test 02: Duplicate tagAlias",
args: args{
ctx: ctx,
tagAliasName: validTagAlias.Name,
tagName: validTag.Name,
},
want: models.TagAlias{},
wantErr: true,
},
{
name: "Test 03: tagAlias name is empty",
args: args{
ctx: ctx,
tagAliasName: "",
tagName: validTag.Name,
},
want: models.TagAlias{},
wantErr: true,
},
{
name: "Test 04: tagName name is empty",
args: args{
ctx: ctx,
tagAliasName: validTagAlias.Name,
tagName: "",
},
want: models.TagAlias{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateTagAlias(tt.args.ctx, tt.args.tagAliasName, tt.args.tagName)
if (err != nil) != tt.wantErr {
t.Errorf("CreateTagAlias() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateTagAlias() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateTagAliasInBatch(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tags to test with
validTags := test.GenerateRandomTags(5)
err = CreateTagInBatch(ctx, validTags, len(validTags))
if err != nil {
t.Fatalf("CreateTags err: %v", err)
}
// --
// -- Create TagAlias to test with
validTagGroup := test.GenerateRandomTagAlias(validTags, 5)
// --
// -- -- Tests
type args struct {
ctx context.Context
tagsAliases []models.TagAlias
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid TagAliases",
args: args{
ctx: ctx,
tagsAliases: validTagGroup,
batchSize: len(validTags),
},
wantErr: false,
},
{
name: "Test 02: Duplicate TagAliases",
args: args{
ctx: ctx,
tagsAliases: validTagGroup,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 03: Nil TagAliases",
args: args{
ctx: ctx,
tagsAliases: nil,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 04: Empty TagAliases",
args: args{
ctx: ctx,
tagsAliases: []models.TagAlias{},
batchSize: len(validTags),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateTagAliasInBatch(tt.args.ctx, tt.args.tagsAliases, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreateTagAliasInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDeleteTagAlias(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tag to test with
validTag := models.Tag{
Name: "valid_tag",
Type: models.General,
}
validTag, err = CreateTag(ctx, validTag.Name, validTag.Type)
if err != nil {
t.Fatalf("CreateTag err: %v", err)
}
// --
// -- Create TagAlias to test with
validTagAlias := models.TagAlias{
Name: "valid_tag_group_name",
TagID: validTag.Name,
}
validTagAlias, err = CreateTagAlias(ctx, validTagAlias.Name, validTagAlias.TagID)
if err != nil {
t.Fatalf("CreateTagGroup err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagAliasName models.TagAliasName
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid TagAlias",
args: args{
ctx: ctx,
tagAliasName: validTagAlias.Name,
},
wantErr: false,
},
{
name: "Test 02: Not existing TagAlias",
args: args{
ctx: ctx,
tagAliasName: validTagAlias.Name,
},
wantErr: false,
},
{
name: "Test 03: Empty TagAliasName ",
args: args{
ctx: ctx,
tagAliasName: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteTagAlias(tt.args.ctx, tt.args.tagAliasName); (err != nil) != tt.wantErr {
t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}