fa95d733ca
Add short tests and Tidy internal directories * complete the tasks
95 lines
2.9 KiB
Go
95 lines
2.9 KiB
Go
package queryqueue
|
|
|
|
import (
|
|
"context"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
queryprocessor "queryorchestration/internal/query/processor"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestExecute(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,
|
|
}
|
|
|
|
expectedQueries := []*queryprocessor.Query{
|
|
{ID: uuid.New(), Type: queryprocessor.TypeContextFull, RequiredQueryIDs: []uuid.UUID{}, Version: int32(1)},
|
|
{ID: uuid.New(), Type: queryprocessor.TypeContextFull, RequiredQueryIDs: []uuid.UUID{}, Version: int32(2)},
|
|
}
|
|
|
|
q := &Queue{
|
|
unsyncedQueue: expectedQueries,
|
|
db: db,
|
|
documentId: uuid.New(),
|
|
cleanVersion: int32(1),
|
|
textVersion: int32(2),
|
|
}
|
|
|
|
pool.ExpectQuery("name: ListResultValuesByID :many").WithArgs([]pgtype.UUID{}).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "queryId", "value"}),
|
|
)
|
|
pool.ExpectQuery("name: SetResult :one").WithArgs(database.MustToDBUUID(expectedQueries[0].ID), database.MustToDBUUID(q.documentId), pgxmock.AnyArg(), q.cleanVersion, q.textVersion, expectedQueries[0].Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).AddRow(pgtype.UUID{}),
|
|
)
|
|
|
|
pool.ExpectQuery("name: ListResultValuesByID :many").WithArgs([]pgtype.UUID{}).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "queryId", "value"}),
|
|
)
|
|
pool.ExpectQuery("name: SetResult :one").WithArgs(database.MustToDBUUID(expectedQueries[1].ID), database.MustToDBUUID(q.documentId), pgxmock.AnyArg(), q.cleanVersion, q.textVersion, expectedQueries[1].Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).AddRow(pgtype.UUID{}),
|
|
)
|
|
|
|
err = q.Execute(ctx)
|
|
assert.Nil(t, err)
|
|
}
|
|
|
|
func TestExecuteQuery(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,
|
|
}
|
|
|
|
qu := &queryprocessor.Query{ID: uuid.New(), Type: queryprocessor.TypeContextFull, RequiredQueryIDs: []uuid.UUID{}, Version: int32(1)}
|
|
|
|
q := &Queue{
|
|
db: db,
|
|
documentId: uuid.New(),
|
|
cleanVersion: int32(1),
|
|
textVersion: int32(2),
|
|
}
|
|
|
|
pool.ExpectQuery("name: ListResultValuesByID :many").WithArgs([]pgtype.UUID{}).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "queryId", "value"}),
|
|
)
|
|
pool.ExpectQuery("name: SetResult :one").WithArgs(database.MustToDBUUID(qu.ID), database.MustToDBUUID(q.documentId), pgxmock.AnyArg(), q.cleanVersion, q.textVersion, qu.Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).AddRow(pgtype.UUID{}),
|
|
)
|
|
|
|
err = q.executeQuery(ctx, qu)
|
|
assert.Nil(t, err)
|
|
}
|