From 2bbfc79e436c1d8954aa8d555576915ba13f6fc0 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 15:08:35 +0200 Subject: [PATCH] feat(test): added tag fully tested tags --- pkg/database/tag_test.go | 262 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 262 insertions(+) create mode 100644 pkg/database/tag_test.go diff --git a/pkg/database/tag_test.go b/pkg/database/tag_test.go new file mode 100644 index 0000000..5bea94b --- /dev/null +++ b/pkg/database/tag_test.go @@ -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) + } + }) + } +}