Files
query-orchestration/internal/query/result/process_test.go
T
Michael McGuinness efe87321b2 Merged in feature/parallellogqueries (pull request #132)
Testing Tweaks

* parallel

* slowestquery

* split

* cleanup

* health

* dynamiccores

* go

* profile

* timeout

* commitmychanges

* taskfile

* sqlcheckstart

* client

* cost

* ctxtimeout
2025-05-05 09:31:21 +00:00

155 lines
4.1 KiB
Go

package result
import (
"context"
"testing"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/query"
resultprocessor "queryorchestration/internal/query/result/processor"
jsonextractor "queryorchestration/internal/query/types/jsonExtractor"
"queryorchestration/internal/serviceconfig"
"github.com/google/uuid"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestProcess(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &serviceconfig.BaseConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := Service{
cfg: cfg,
svc: &Services{
Query: query.New(cfg),
},
}
qcfg := "{\"path\":\"examplekey\"}"
query := &resultprocessor.Query{
ID: uuid.New(),
Version: 2,
RequiredQueryIDs: &[]uuid.UUID{uuid.New()},
Config: &qcfg,
}
params := Process{
DocumentID: uuid.New(),
QueryID: query.ID,
QueryVersion: query.Version,
}
pool.ExpectQuery("name: GetQueryWithVersion :one").WithArgs(&query.ID, &query.Version).WillReturnRows(
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.Version, query.Version, []byte(*query.Config), *query.RequiredQueryIDs),
)
strVal := `{"examplekey":"example_value"}`
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(&query.ID, &query.Version, &params.DocumentID).
WillReturnRows(
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
AddRow(&uuid.UUID{}, query.ID, repository.QuerytypeContextFull, &strVal),
)
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).WillReturnRows(
pgxmock.NewRows([]string{"config"}).
AddRow([]byte(qcfg)),
)
val, err := svc.Process(ctx, &params)
require.NoError(t, err)
assert.NotNil(t, val)
assert.Equal(t, "example_value", val.GetStoreValue())
}
func TestListRequiredValue(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &serviceconfig.BaseConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := Service{
cfg: cfg,
}
pr, err := svc.listRequiredValues(ctx, nil, nil)
require.NoError(t, err)
assert.Nil(t, pr)
pr, err = svc.listRequiredValues(ctx, &Process{}, &resultprocessor.Query{})
require.NoError(t, err)
assert.Nil(t, pr)
pr, err = svc.listRequiredValues(ctx, &Process{},
&resultprocessor.Query{
RequiredQueryIDs: &[]uuid.UUID{},
},
)
require.NoError(t, err)
assert.Nil(t, pr)
query := &resultprocessor.Query{
ID: uuid.New(),
Version: 2,
RequiredQueryIDs: &[]uuid.UUID{uuid.New()},
}
params := Process{
DocumentID: uuid.New(),
QueryID: query.ID,
QueryVersion: query.Version,
}
strVal := "axe_value"
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(&query.ID, &query.Version, &params.DocumentID).
WillReturnRows(
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
AddRow(&uuid.UUID{}, (*query.RequiredQueryIDs)[0], repository.QuerytypeJsonExtractor, &strVal),
)
pr, err = svc.listRequiredValues(ctx, &params, query)
require.NoError(t, err)
assert.ElementsMatch(t, []resultprocessor.Value{
jsonextractor.NewResult(strVal),
}, pr)
}
func TestGetProcessor(t *testing.T) {
svc := Service{}
pr, err := svc.getProcessor(resultprocessor.TypeJsonExtractor)
require.NoError(t, err)
assert.NotNil(t, pr)
pr, err = svc.getProcessor(resultprocessor.TypeContextFull)
require.NoError(t, err)
assert.NotNil(t, pr)
_, err = svc.getProcessor(resultprocessor.Type(-1))
assert.Error(t, err)
}
func TestParseQueryRequirementValueArray(t *testing.T) {
exval := "exampleval"
in := []*repository.ListQueryRequirementValuesRow{
{
Queryid: uuid.New(),
Value: &exval,
Querytype: repository.QuerytypeJsonExtractor,
},
}
out, err := parseQueryRequirementValueArray(in)
require.NoError(t, err)
assert.ElementsMatch(t, []resultprocessor.Value{
jsonextractor.NewResult(exval),
}, out)
}