67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package query_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/query"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDeprecate(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,
|
|
}
|
|
svc := query.New(db)
|
|
|
|
id := uuid.New()
|
|
|
|
pool.ExpectQuery("name: IsQueryDeprecated :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
|
|
pgxmock.NewRows([]string{"exists"}).
|
|
AddRow(false),
|
|
)
|
|
pool.ExpectExec("name: DeprecateQuery :exec").WithArgs(database.MustToDBUUID(id)).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
err = svc.Deprecate(ctx, id)
|
|
assert.Nil(t, err)
|
|
|
|
pool.ExpectQuery("name: IsQueryDeprecated :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
|
|
pgxmock.NewRows([]string{"exists"}).
|
|
AddRow(true),
|
|
)
|
|
|
|
err = svc.Deprecate(ctx, id)
|
|
assert.Nil(t, err)
|
|
|
|
pool.ExpectQuery("name: IsQueryDeprecated :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
|
|
pgxmock.NewRows([]string{"exists"}),
|
|
)
|
|
|
|
err = svc.Deprecate(ctx, id)
|
|
assert.NotNil(t, err)
|
|
|
|
pool.ExpectQuery("name: IsQueryDeprecated :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
|
|
pgxmock.NewRows([]string{"exists"}).
|
|
AddRow(false),
|
|
)
|
|
pool.ExpectExec("name: DeprecateQuery :exec").WithArgs(database.MustToDBUUID(id)).
|
|
WillReturnError(errors.New("unable to deprecate query"))
|
|
|
|
err = svc.Deprecate(ctx, id)
|
|
assert.NotNil(t, err)
|
|
}
|