2024-07-27 17:41:41 +00:00
|
|
|
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) {
|
2024-07-27 18:01:06 +00:00
|
|
|
err := ExecuteRawStatement(tt.args.ctx, tt.args.db, tt.args.query, tt.args.args...)
|
2024-07-27 17:41:41 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("ExecuteRawStatement() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
2024-07-27 18:01:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-07-27 17:41:41 +00:00
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2024-07-27 18:01:06 +00:00
|
|
|
t.Errorf("QueryRawStatement() got = %v, want %v", got, tt.want)
|
2024-07-27 17:41:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|