Compare commits

..

6 Commits

Author SHA1 Message Date
f0f216bdaa feat(test): added tagAlias
Some checks failed
Gitea Build Check / Build (push) Failing after 1m1s
fully tested tag groups
2024-08-13 14:44:50 +02:00
5562d75e3a feat(error): more error handling
added missing checks
2024-08-13 14:44:27 +02:00
a9ac2a34fb feat(error): added additional constant 2024-08-13 14:44:16 +02:00
3fcdb0e9ea feat(test): added tagGroups
fully tested tag groups
2024-08-13 14:26:33 +02:00
1cbdd1f4ed feat(error): added additional constant
added missing log content for tag
2024-08-13 14:26:12 +02:00
d6cbea3f58 feat(error): more error handling
added missing checks
2024-08-13 14:25:36 +02:00
5 changed files with 606 additions and 0 deletions

View File

@ -32,6 +32,14 @@ func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagNa
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagAliasName == "" {
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagAliasNameIsEmpty})
}
if tagName == "" {
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
}
tagAlias := models.TagAlias{
Name: tagAliasName,
TagID: tagName,
@ -109,6 +117,10 @@ func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagAliasName) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty})
}
result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {

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)
}
})
}
}

View File

@ -33,6 +33,14 @@ func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagNa
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagGroupName == "" {
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagGroupNameIsEmpty})
}
if tagName == "" {
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
}
tagGroup := models.TagGroup{
Name: tagGroupName,
TagID: tagName,

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)
}
})
}
}

View File

@ -11,8 +11,10 @@ const (
UserSourceIDIsEmpty = "userSourceID cannot be empty"
UserSourceIDToShort = "userSourceID needs to be 25 characters long"
TagNameIsEmpty = "tagName cannot be empty"
TagListIsEmpty = "tagList cannot be empty"
TagAliasNameIsEmpty = "tagAliasName cannot be empty"
TagAliasListIsEmpty = "tagAliasList cannot be empty"
TagGroupListIsEmpty = "tagGroupList cannot be empty"