2024-12-24 18:11:25 +00:00
|
|
|
package database_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-01-14 17:28:26 +00:00
|
|
|
"os"
|
|
|
|
|
"path"
|
2024-12-24 18:11:25 +00:00
|
|
|
"queryorchestration/internal/database"
|
2025-01-23 14:56:20 +00:00
|
|
|
"queryorchestration/internal/database/repository"
|
2025-01-10 19:17:20 +00:00
|
|
|
"queryorchestration/internal/test"
|
2024-12-24 18:11:25 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2025-01-23 14:56:20 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
2024-12-24 18:11:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDBConn(t *testing.T) {
|
2025-01-17 12:00:32 +00:00
|
|
|
if testing.Short() {
|
|
|
|
|
t.Skip("Skipping long test in short mode")
|
|
|
|
|
}
|
2024-12-24 18:11:25 +00:00
|
|
|
ctx := context.Background()
|
2025-01-10 19:17:20 +00:00
|
|
|
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
|
|
|
|
Migrations: &database.MigrationConfig{
|
2025-01-14 17:28:26 +00:00
|
|
|
BasePath: path.Join(os.Getenv("PWD"), "../.."),
|
2025-01-10 19:17:20 +00:00
|
|
|
},
|
|
|
|
|
})
|
2025-01-10 11:12:03 +00:00
|
|
|
defer cleanup()
|
2024-12-24 18:11:25 +00:00
|
|
|
|
2025-01-10 19:17:20 +00:00
|
|
|
pool := database.GetDBPool(ctx)
|
|
|
|
|
assert.NotNil(t, pool)
|
2025-01-08 18:14:15 +00:00
|
|
|
}
|
2025-01-23 14:56:20 +00:00
|
|
|
|
2025-01-29 16:26:11 +00:00
|
|
|
func TestExecuteTransaction(t *testing.T) {
|
2025-01-23 14:56:20 +00:00
|
|
|
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)
|
|
|
|
|
}
|