otter-space-sdk/pkg/database/tag_test.go
SoXX 2391bad68e
All checks were successful
Gitea Build Check / Build (push) Successful in 2m33s
feat(version): updated to version 4
2024-08-29 15:02:39 +02:00

263 lines
5.0 KiB
Go

package database
import (
"context"
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
func TestCreateTag(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,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagName models.TagName
tagType models.TagType
}
tests := []struct {
name string
args args
want models.Tag
wantErr bool
}{
{
name: "Test 01: Valid tag",
args: args{
ctx: ctx,
tagType: validTag.Type,
tagName: validTag.Name,
},
want: validTag,
wantErr: false,
},
{
name: "Test 02: Duplicate tag",
args: args{
ctx: ctx,
tagType: validTag.Type,
tagName: validTag.Name,
},
want: models.Tag{},
wantErr: true,
},
{
name: "Test 03: tagName is empty",
args: args{
ctx: ctx,
tagType: "",
tagName: validTag.Name,
},
want: models.Tag{},
wantErr: true,
},
{
name: "Test 04: tagName name is empty",
args: args{
ctx: ctx,
tagType: validTag.Type,
tagName: "",
},
want: models.Tag{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateTag(tt.args.ctx, tt.args.tagName, tt.args.tagType)
if (err != nil) != tt.wantErr {
t.Errorf("CreateTag() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateTag() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateTagInBatch(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)
// --
// -- -- Tests
type args struct {
ctx context.Context
tags []models.Tag
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Tags",
args: args{
ctx: ctx,
tags: validTags,
batchSize: len(validTags),
},
wantErr: false,
},
{
name: "Test 02: Duplicate Tags",
args: args{
ctx: ctx,
tags: validTags,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 03: Nil Tags",
args: args{
ctx: ctx,
tags: nil,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 04: Empty Tags",
args: args{
ctx: ctx,
tags: []models.Tag{},
batchSize: len(validTags),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateTagInBatch(tt.args.ctx, tt.args.tags, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreateTagInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDeleteTag(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)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagName models.TagName
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Tag",
args: args{
ctx: ctx,
tagName: validTag.Name,
},
wantErr: false,
},
{
name: "Test 02: Not existing Tag",
args: args{
ctx: ctx,
tagName: validTag.Name,
},
wantErr: false,
},
{
name: "Test 03: Empty TagName ",
args: args{
ctx: ctx,
tagName: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteTag(tt.args.ctx, tt.args.tagName); (err != nil) != tt.wantErr {
t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}