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 }