From e7cf655362fb56491ee60fb049f4b5432af142f8 Mon Sep 17 00:00:00 2001 From: soxx Date: Thu, 20 Jun 2024 00:13:39 +0200 Subject: [PATCH] test(postgres): added sources and fixed bugs --- internal/postgres/source.go | 12 +- internal/postgres/source_test.go | 259 +++++++++++++++++++++++++++++++ 2 files changed, 269 insertions(+), 2 deletions(-) create mode 100644 internal/postgres/source_test.go diff --git a/internal/postgres/source.go b/internal/postgres/source.go index 3d2cdca..1b013da 100644 --- a/internal/postgres/source.go +++ b/internal/postgres/source.go @@ -2,6 +2,7 @@ package postgres import ( "context" + "fmt" "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" log "github.com/sirupsen/logrus" @@ -11,6 +12,10 @@ import ( // CreateSourceNode creates a pgModels.Source func CreateSourceNode(ctx context.Context, db *gorm.DB, anthroveSource *pgModels.Source) error { + if anthroveSource.Domain == "" { + return fmt.Errorf("anthroveSource domain is required") + } + result := db.WithContext(ctx).Create(anthroveSource) if result.Error != nil { @@ -45,8 +50,11 @@ func GetAllSourceNodes(ctx context.Context, db *gorm.DB) ([]pgModels.Source, err // GetSourceNodesByURL returns the first source it finds based on the domain func GetSourceNodesByURL(ctx context.Context, db *gorm.DB, domain string) (*pgModels.Source, error) { + var sources pgModels.Source - var sources *pgModels.Source + if domain == "" { + return nil, fmt.Errorf("domain is required") + } result := db.WithContext(ctx).Where("domain = ?", domain).First(&sources) @@ -58,5 +66,5 @@ func GetSourceNodesByURL(ctx context.Context, db *gorm.DB, domain string) (*pgMo "tag_amount": result.RowsAffected, }).Trace("database: get all source nodes") - return sources, nil + return &sources, nil } diff --git a/internal/postgres/source_test.go b/internal/postgres/source_test.go new file mode 100644 index 0000000..2bde71b --- /dev/null +++ b/internal/postgres/source_test.go @@ -0,0 +1,259 @@ +package postgres + +import ( + "context" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" + "git.dragse.it/anthrove/otter-space-sdk/test" + "gorm.io/gorm" + "testing" +) + +func TestCreateSourceNode(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 + + validAnthroveSource := &pgModels.Source{ + DisplayName: "e621", + Domain: "e621.net", + Icon: "icon.e621.net", + } + + invalidAnthroveSource := &pgModels.Source{ + Domain: "", + } + + // Test + type args struct { + ctx context.Context + db *gorm.DB + anthroveSource *pgModels.Source + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test 1: Valid anthroveSource", + args: args{ + ctx: ctx, + db: gormDB, + anthroveSource: validAnthroveSource, + }, + wantErr: false, + }, + { + name: "Test 2: inValid anthroveSource", + args: args{ + ctx: ctx, + db: gormDB, + anthroveSource: invalidAnthroveSource, + }, + wantErr: true, + }, + { + name: "Test 3: unique anthroveSource", + args: args{ + ctx: ctx, + db: gormDB, + anthroveSource: validAnthroveSource, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreateSourceNode(tt.args.ctx, tt.args.db, tt.args.anthroveSource); (err != nil) != tt.wantErr { + t.Errorf("CreateSourceNode() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestGetAllSourceNodes(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 + + sources := []pgModels.Source{ + { + DisplayName: "e621", + Domain: "e621.net", + Icon: "icon.e621.net", + }, + { + DisplayName: "furaffinity", + Domain: "furaffinity.net", + Icon: "icon.furaffinity.net", + }, + { + DisplayName: "fenpaws", + Domain: "fenpa.ws", + Icon: "icon.fenpa.ws", + }, + } + + for _, source := range sources { + err = CreateSourceNode(ctx, gormDB, &source) + if err != nil { + t.Fatal(err) + } + } + + // Test + type args struct { + ctx context.Context + db *gorm.DB + } + tests := []struct { + name string + args args + want []pgModels.Source + wantErr bool + }{ + { + name: "Test 1: Get all entries", + args: args{ + ctx: ctx, + db: gormDB, + }, + want: sources, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetAllSourceNodes(tt.args.ctx, tt.args.db) + if (err != nil) != tt.wantErr { + t.Errorf("GetAllSourceNodes() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkSourcesNode(got, tt.want) { + t.Errorf("GetAllSourceNodes() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGetSourceNodesByURL(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 + + source := &pgModels.Source{ + DisplayName: "e621", + Domain: "e621.net", + Icon: "icon.e621.net", + } + + err = CreateSourceNode(ctx, gormDB, source) + if err != nil { + t.Fatal(err) + } + + // Test + type args struct { + ctx context.Context + db *gorm.DB + domain string + } + tests := []struct { + name string + args args + want *pgModels.Source + wantErr bool + }{ + { + name: "Test 1: Valid URL", + args: args{ + ctx: ctx, + db: gormDB, + domain: "e621.net", + }, + want: source, + wantErr: false, + }, + { + name: "Test 2: Invalid URL", + args: args{ + ctx: ctx, + db: gormDB, + domain: "eeeee.net", + }, + want: nil, + wantErr: true, + }, + { + name: "Test 2: No URL", + args: args{ + ctx: ctx, + db: gormDB, + domain: "", + }, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetSourceNodesByURL(tt.args.ctx, tt.args.db, tt.args.domain) + if (err != nil) != tt.wantErr { + t.Errorf("GetSourceNodesByURL() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkSourceNode(got, tt.want) { + t.Errorf("GetSourceNodesByURL() got = %v, want %v", got, tt.want) + } + }) + } +} + +func checkSourcesNode(got []pgModels.Source, want []pgModels.Source) bool { + for i, source := range want { + if source.DisplayName != got[i].DisplayName { + return false + } + if source.Domain != got[i].Domain { + return false + } + if source.Icon != got[i].Icon { + return false + } + } + + return true + +} + +func checkSourceNode(got *pgModels.Source, want *pgModels.Source) bool { + + if want == nil && got == nil { + return true + } + + if got.Domain != want.Domain { + return false + } + + return true + +}