53ea7d34e6
Start adding Textract + UUID changes * base * startclient * ts * short * tests
155 lines
4.1 KiB
Go
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, ¶ms.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, ¶ms)
|
|
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, ¶ms.DocumentID).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
|
AddRow(&uuid.UUID{}, (*query.RequiredQueryIDs)[0], repository.QuerytypeJsonExtractor, &strVal),
|
|
)
|
|
|
|
pr, err = svc.listRequiredValues(ctx, ¶ms, 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,
|
|
Type: repository.QuerytypeJsonExtractor,
|
|
},
|
|
}
|
|
|
|
out, err := parseQueryRequirementValueArray(in)
|
|
require.NoError(t, err)
|
|
assert.ElementsMatch(t, []resultprocessor.Value{
|
|
jsonextractor.NewResult(exval),
|
|
}, out)
|
|
}
|