Files
query-orchestration/internal/query/get_test.go
T
Michael McGuinness fa95d733ca Merged in feature/shorttestsanddirtidy (pull request #26)
Add short tests and Tidy internal directories

* complete the tasks
2025-01-17 12:00:32 +00:00

125 lines
3.5 KiB
Go

package query_test
import (
"context"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/query"
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 TestGet(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)
config := "{\"path\":\"example_path\"}"
query := query.Query{
ID: uuid.New(),
Type: queryprocessor.TypeJsonExtractor,
ActiveVersion: int32(1),
LatestVersion: int32(1),
RequiredQueryIDs: []uuid.UUID{
uuid.New(),
},
Config: config,
}
dbReqIDs := database.MustToDBUUIDArray(query.RequiredQueryIDs)
pool.ExpectQuery("name: GetQuery :one").WithArgs(database.MustToDBUUID(query.ID)).WillReturnRows(
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(database.MustToDBUUID(query.ID), repository.QuerytypeJsonExtractor, query.ActiveVersion, query.LatestVersion, []byte(config), dbReqIDs),
)
returnQuery, err := svc.Get(ctx, query.ID)
assert.Nil(t, err)
assert.EqualExportedValues(t, query, *returnQuery)
}
func TestFullActiveQueryEmpty(t *testing.T) {
dbQuery := &repository.Fullactivequery{
ID: database.MustToDBUUID(uuid.New()),
Type: repository.QuerytypeContextFull,
Activeversion: int32(1),
Latestversion: int32(2),
Config: []byte(""),
Requiredids: []pgtype.UUID{},
}
out, err := query.ParseFullActiveQuery(dbQuery)
assert.Nil(t, err)
assert.EqualExportedValues(t, query.Query{
ID: database.MustToUUID(dbQuery.ID),
Type: queryprocessor.TypeContextFull,
ActiveVersion: int32(1),
LatestVersion: int32(2),
Config: "",
RequiredQueryIDs: []uuid.UUID{},
}, *out)
}
func TestFullActiveQueryWithNullUUID(t *testing.T) {
dbQuery := &repository.Fullactivequery{
ID: database.MustToDBUUID(uuid.New()),
Type: repository.QuerytypeContextFull,
Activeversion: int32(1),
Latestversion: int32(2),
Config: []byte(""),
Requiredids: []pgtype.UUID{database.MustToDBUUID(uuid.Nil)},
}
out, err := query.ParseFullActiveQuery(dbQuery)
assert.Nil(t, err)
assert.EqualExportedValues(t, query.Query{
ID: database.MustToUUID(dbQuery.ID),
Type: queryprocessor.TypeContextFull,
ActiveVersion: int32(1),
LatestVersion: int32(2),
Config: "",
RequiredQueryIDs: []uuid.UUID{},
}, *out)
}
func TestFullActiveQueryArray(t *testing.T) {
dbQueries := []*repository.Fullactivequery{
{
ID: database.MustToDBUUID(uuid.New()),
Type: repository.QuerytypeContextFull,
Activeversion: int32(1),
Latestversion: int32(2),
Config: []byte(""),
Requiredids: []pgtype.UUID{},
},
}
out, err := query.ParseFullActiveQueryArray(dbQueries)
assert.Nil(t, err)
assert.EqualExportedValues(t, []*query.Query{
{
ID: database.MustToUUID(dbQueries[0].ID),
Type: queryprocessor.TypeContextFull,
ActiveVersion: int32(1),
LatestVersion: int32(2),
Config: "",
RequiredQueryIDs: []uuid.UUID{},
},
}, out)
}