otter-space-sdk/internal/postgres/custom_test.go

126 lines
2.4 KiB
Go
Raw Normal View History

package postgres
import (
"context"
"database/sql"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
"gorm.io/gorm"
"reflect"
"testing"
)
func TestExecuteRawStatement(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)
// Test
type args struct {
ctx context.Context
db *gorm.DB
query string
args []any
}
tests := []struct {
name string
args args
want *sql.Rows
wantErr bool
}{
{
name: "Test 01: Empty Query",
args: args{
ctx: ctx,
db: gormDB,
query: "",
args: nil,
},
want: nil,
wantErr: true,
},
{
name: "Test 02: Nil Query",
args: args{
ctx: ctx,
db: gormDB,
query: "aasd",
args: nil,
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ExecuteRawStatement(tt.args.ctx, tt.args.db, tt.args.query, tt.args.args...)
if (err != nil) != tt.wantErr {
t.Errorf("ExecuteRawStatement() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
func TestQueryRawStatement(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)
// Test
type args struct {
ctx context.Context
db *gorm.DB
query string
args []any
}
tests := []struct {
name string
args args
want *sql.Rows
wantErr bool
}{
{
name: "Test 01: Empty Query",
args: args{
ctx: ctx,
db: gormDB,
query: "",
args: nil,
},
want: nil,
wantErr: true,
},
{
name: "Test 02: Nil Query",
args: args{
ctx: ctx,
db: gormDB,
query: "aasd",
args: nil,
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := QueryRawStatement(tt.args.ctx, tt.args.db, tt.args.query, tt.args.args...)
if (err != nil) != tt.wantErr {
t.Errorf("QueryRawStatement() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("QueryRawStatement() got = %v, want %v", got, tt.want)
}
})
}
}