package postgres import ( "context" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/test" _ "github.com/lib/pq" "gorm.io/gorm" "testing" ) func TestCreateAnthrovePostNode(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) type args struct { ctx context.Context db *gorm.DB anthrovePostID models.AnthrovePostID anthroveRating models.Rating } tests := []struct { name string args args wantErr bool }{ { name: "Test 1: Valid AnthrovePostID and Rating", args: args{ ctx: context.Background(), db: gormDB, anthrovePostID: "1234", anthroveRating: models.Rating("safe"), }, wantErr: false, }, { name: "Test 2: Invalid AnthrovePostID and Rating", args: args{ ctx: context.Background(), db: gormDB, anthrovePostID: "", anthroveRating: "a4dsa4d", }, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := CreateAnthrovePostNode(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.anthroveRating); (err != nil) != tt.wantErr { t.Errorf("CreateAnthrovePostNode() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestCheckIfAnthrovePostNodeExistsByAnthroveID(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) } // Test type args struct { ctx context.Context db *gorm.DB anthrovePostID models.AnthrovePostID } tests := []struct { name string args args want bool wantErr bool }{ { name: "Test 1: Valid AnthroveID", args: args{ ctx: ctx, db: gormDB, anthrovePostID: "1234", }, want: true, wantErr: false, }, { name: "Test 2: Invalid AnthroveID", args: args{ ctx: ctx, db: gormDB, anthrovePostID: "123456", }, want: false, wantErr: false, }, { name: "Test 3: No AnthroveID", args: args{ ctx: ctx, db: gormDB, anthrovePostID: "", }, want: false, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := CheckIfAnthrovePostNodeExistsByAnthroveID(tt.args.ctx, tt.args.db, tt.args.anthrovePostID) if (err != nil) != tt.wantErr { t.Errorf("CheckIfAnthrovePostNodeExistsByAnthroveID() error = %v, wantErr %v", err, tt.wantErr) return } if got != tt.want { t.Errorf("CheckIfAnthrovePostNodeExistsByAnthroveID() got = %v, want %v", got, tt.want) } }) } }