Compare commits

..

3 Commits

Author SHA1 Message Date
2bbfc79e43 feat(test): added tag
All checks were successful
Gitea Build Check / Build (push) Successful in 1m19s
fully tested tags
2024-08-13 15:08:35 +02:00
92ef2c5059 feat(error): more error handling
added missing checks
2024-08-13 15:08:10 +02:00
5cc5a22d28 feat(error): added additional constant
added missing log content for tag
2024-08-13 15:07:53 +02:00
3 changed files with 275 additions and 0 deletions

View File

@ -32,6 +32,14 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagName == "" {
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
}
if tagType == "" {
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagTypeIsEmpty})
}
tag := models.Tag{
Name: tagName,
Type: tagType,
@ -109,6 +117,10 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagName) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty})
}
result := client.WithContext(ctx).Delete(&tag, tagName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {

262
pkg/database/tag_test.go Normal file
View File

@ -0,0 +1,262 @@
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 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)
}
})
}
}

View File

@ -12,6 +12,7 @@ const (
UserSourceIDToShort = "userSourceID needs to be 25 characters long"
TagNameIsEmpty = "tagName cannot be empty"
TagTypeIsEmpty = "tagType cannot be empty"
TagListIsEmpty = "tagList cannot be empty"
TagAliasNameIsEmpty = "tagAliasName cannot be empty"