From 3fcdb0e9ead904be552abc98a7ba2653fb1ef284 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 14:26:33 +0200 Subject: [PATCH] feat(test): added tagGroups fully tested tag groups --- pkg/database/tagGroup_test.go | 292 ++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 pkg/database/tagGroup_test.go diff --git a/pkg/database/tagGroup_test.go b/pkg/database/tagGroup_test.go new file mode 100644 index 0000000..497f8db --- /dev/null +++ b/pkg/database/tagGroup_test.go @@ -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) + } + }) + } +}