feat(test): added tagGroups

fully tested tag groups
This commit is contained in:
SoXX 2024-08-13 14:26:33 +02:00
parent 1cbdd1f4ed
commit 3fcdb0e9ea

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 TestCreateTagGroup(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 TagGroup to test with
validTagGroup := models.TagGroup{
Name: "valid_tag_group_name",
TagID: validTag.Name,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagGroupName models.TagGroupName
tagName models.TagName
}
var tests = []struct {
name string
args args
want models.TagGroup
wantErr bool
}{
{
name: "Test 01: Valid TagGroup",
args: args{
ctx: ctx,
tagGroupName: validTagGroup.Name,
tagName: validTag.Name,
},
want: validTagGroup,
wantErr: false,
},
{
name: "Test 02: Duplicate TagGroup",
args: args{
ctx: ctx,
tagGroupName: validTagGroup.Name,
tagName: validTag.Name,
},
want: models.TagGroup{},
wantErr: true,
},
{
name: "Test 03: TagGroup name is empty",
args: args{
ctx: ctx,
tagGroupName: "",
tagName: validTag.Name,
},
want: models.TagGroup{},
wantErr: true,
},
{
name: "Test 04: tagName name is empty",
args: args{
ctx: ctx,
tagGroupName: validTagGroup.Name,
tagName: "",
},
want: models.TagGroup{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateTagGroup(tt.args.ctx, tt.args.tagGroupName, tt.args.tagName)
if (err != nil) != tt.wantErr {
t.Errorf("CreateTagGroup() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateTagGroup() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateTagGroupInBatch(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 TagGroup to test with
validTagGroup := test.GenerateRandomTagGroups(validTags, 5)
// --
// -- -- Tests
type args struct {
ctx context.Context
tagsGroups []models.TagGroup
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid TagGroups",
args: args{
ctx: ctx,
tagsGroups: validTagGroup,
batchSize: len(validTags),
},
wantErr: false,
},
{
name: "Test 02: Duplicate TagGroups",
args: args{
ctx: ctx,
tagsGroups: validTagGroup,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 03: Nil TagGroups",
args: args{
ctx: ctx,
tagsGroups: nil,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 04: Empty TagGroups",
args: args{
ctx: ctx,
tagsGroups: []models.TagGroup{},
batchSize: len(validTags),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateTagGroupInBatch(tt.args.ctx, tt.args.tagsGroups, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreateTagGroupInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDeleteTagGroup(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 TagGroup to test with
validTagGroup := models.TagGroup{
Name: "valid_tag_group_name",
TagID: validTag.Name,
}
validTagGroup, err = CreateTagGroup(ctx, validTagGroup.Name, validTagGroup.TagID)
if err != nil {
t.Fatalf("CreateTagGroup err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagGroupName models.TagGroupName
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid TagGroup",
args: args{
ctx: ctx,
tagGroupName: validTagGroup.Name,
},
wantErr: false,
},
{
name: "Test 02: Not existing TagGroup",
args: args{
ctx: ctx,
tagGroupName: validTagGroup.Name,
},
wantErr: false,
},
{
name: "Test 03: Empty TagGroupName ",
args: args{
ctx: ctx,
tagGroupName: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteTagGroup(tt.args.ctx, tt.args.tagGroupName); (err != nil) != tt.wantErr {
t.Errorf("DeleteTagGroup() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}