Files
query-orchestration/test/unit/internal/result/store_test.go
T
Michael McGuinness 56b447dfcf moveservicesInterfaces
2025-01-07 13:01:14 +00:00

54 lines
1.5 KiB
Go

package document_test
import (
"context"
"fmt"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/result"
"testing"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
func TestStore(t *testing.T) {
ctx := context.Background()
db, err := pgxmock.NewConn()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
defer db.Close(ctx)
queries := repository.New(db)
resultStore := result.ResultStore{
QueryID: uuid.New(),
DocumentID: uuid.New(),
Value: "val",
CleanVersion: int32(1),
TextVersion: int32(1),
QueryVersion: int32(1),
}
db.ExpectQuery("name: SetResult :one").WithArgs(database.MustToDBUUID(resultStore.QueryID), database.MustToDBUUID(resultStore.DocumentID), resultStore.Value, resultStore.CleanVersion, resultStore.TextVersion, resultStore.QueryVersion).
WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(pgtype.UUID{}),
)
id, err := result.Store(ctx, queries, &resultStore)
assert.Nil(t, err)
assert.NotNil(t, id)
errr := "database failing"
db.ExpectQuery("name: SetResult :one").WithArgs(database.MustToDBUUID(resultStore.QueryID), database.MustToDBUUID(resultStore.DocumentID), resultStore.Value, resultStore.CleanVersion, resultStore.TextVersion, resultStore.QueryVersion).
WillReturnError(fmt.Errorf(errr))
id, err = result.Store(ctx, queries, &resultStore)
assert.EqualError(t, err, errr)
assert.Equal(t, uuid.Nil, id)
}