Files
query-orchestration/internal/database/connection_test.go
T
Michael McGuinness 058f8dba7f Merged in feature/logVersioning (pull request #40)
Can Sync Versioning

* started

* client

* jobcansync

* tests
2025-01-29 16:26:11 +00:00

66 lines
1.4 KiB
Go

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)
}