From 83a5c894384d718c1b10905c4b8eae41b9ae976d Mon Sep 17 00:00:00 2001 From: SoXX Date: Thu, 20 Jun 2024 14:54:06 +0200 Subject: [PATCH] test(postgres): added tag and fixed bugs Signed-off-by: soxx --- internal/postgres/tag.go | 21 ++++ internal/postgres/tag_test.go | 175 ++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 internal/postgres/tag_test.go diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go index 02072c2..4249069 100644 --- a/internal/postgres/tag.go +++ b/internal/postgres/tag.go @@ -2,14 +2,35 @@ package postgres import ( "context" + "fmt" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" log "github.com/sirupsen/logrus" "gorm.io/gorm" ) +func CreateTag(ctx context.Context, db *gorm.DB, tag *pgModels.Tag) error { + + resultTag := db.WithContext(ctx).Where(tag).Create(tag) + + if resultTag.Error != nil { + return resultTag.Error + } + + log.WithFields(log.Fields{ + "tag_name": tag.Name, + "tag_type": tag.Type, + }).Trace("database: created tag node") + + return nil +} + func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.AnthrovePostID, tag *pgModels.Tag) error { + if PostID == "" { + return fmt.Errorf("PostID is empty") + } + resultTag := db.WithContext(ctx).Where(tag).FirstOrCreate(tag) if resultTag.Error != nil { diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go new file mode 100644 index 0000000..347754e --- /dev/null +++ b/internal/postgres/tag_test.go @@ -0,0 +1,175 @@ +package postgres + +import ( + "context" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" + "git.dragse.it/anthrove/otter-space-sdk/test" + "gorm.io/gorm" + "testing" +) + +func TestCreateTagNodeWitRelation(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + + err = CreateAnthrovePostNode(ctx, gormDB, "1234", "safe") + if err != nil { + t.Fatal(err) + } + + tag := &pgModels.Tag{ + Name: "JayTheFerret", + Type: "artist", + } + + // Test + type args struct { + ctx context.Context + db *gorm.DB + PostID models.AnthrovePostID + tag *pgModels.Tag + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 1: Valid PostID and Tag", + args: args{ + ctx: ctx, + db: gormDB, + PostID: "1234", + tag: tag, + }, + wantErr: false, + }, + { + name: "Test 2: Valid PostID and invalid Tag", + args: args{ + ctx: ctx, + db: gormDB, + PostID: "1234", + tag: nil, + }, + wantErr: true, + }, + { + name: "Test 3: Invalid PostID and valid Tag", + args: args{ + ctx: ctx, + db: gormDB, + PostID: "123456", + tag: tag, + }, + wantErr: true, + }, + { + name: "Test 4: No PostID and valid Tag", + args: args{ + ctx: ctx, + db: gormDB, + PostID: "", + tag: tag, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreateTagNodeWitRelation(tt.args.ctx, tt.args.db, tt.args.PostID, tt.args.tag); (err != nil) != tt.wantErr { + t.Errorf("CreateTagNodeWitRelation() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestGetTags(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + + tags := []pgModels.Tag{ + { + Name: "JayTheFerret", + Type: "artist", + }, + { + Name: "anthro", + Type: "general", + }, + { + Name: "soxx", + Type: "character", + }, + } + + for _, tag := range tags { + err = CreateTag(ctx, gormDB, &tag) + if err != nil { + t.Fatal(err) + } + } + + // Test + type args struct { + ctx context.Context + db *gorm.DB + } + tests := []struct { + name string + args args + want []pgModels.Tag + wantErr bool + }{ + { + name: "Test 1: Get Tags", + args: args{ + ctx: ctx, + db: gormDB, + }, + want: tags, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetTags(tt.args.ctx, tt.args.db) + if (err != nil) != tt.wantErr { + t.Errorf("GetTags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkTag(got, tt.want) { + t.Errorf("GetTags() got = %v, want %v", got, tt.want) + } + }) + } +} + +func checkTag(got []pgModels.Tag, want []pgModels.Tag) bool { + for i, tag := range want { + if tag.Type != got[i].Type { + return false + } + if tag.Name != got[i].Name { + return false + } + } + + return true + +}