package database_test import ( "context" "os" "path" "queryorchestration/internal/database" "queryorchestration/internal/database/repository" "queryorchestration/internal/test" "testing" "github.com/google/uuid" "github.com/pashagolub/pgxmock/v3" "github.com/stretchr/testify/assert" ) func TestDBConn(t *testing.T) { if testing.Short() { t.Skip("Skipping long test in short mode") } ctx := context.Background() _, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{ Migrations: &database.MigrationConfig{ BasePath: path.Join(os.Getenv("PWD"), "../.."), }, }) defer cleanup() pool := database.GetDBPool(ctx) assert.NotNil(t, pool) } func TestExecuteTransaction(t *testing.T) { ctx := context.Background() pool, err := pgxmock.NewPool() if err != nil { t.Fatalf("failed to open pgxmock database: %v", err) } queries := repository.New(pool) db := &database.Connection{ Queries: queries, Pool: pool, } clientID := database.MustToDBUUID(uuid.New()) clientName := "example_client" pool.ExpectBegin() pool.ExpectQuery("name: CreateClient :one").WithArgs(clientName). WillReturnRows( pgxmock.NewRows([]string{"id"}). AddRow(clientID), ) pool.ExpectCommit() err = database.ExecuteTransaction(ctx, db, func(ctx context.Context, q *repository.Queries) error { id, err := q.CreateClient(ctx, "example_client") assert.Nil(t, err) assert.Equal(t, clientID, id) return nil }) assert.Nil(t, err) }