Files
query-orchestration/internal/query/result/process_test.go
T
Michael McGuinness 7001ca854c Merged in feature/docinitialisation (pull request #41)
Queuing Changes and Cfg Testing

* staarting

* staarting

* startedpush

* note

* save

* mocking

* removederrs

* fixtests

* cleanuperrs

* newenvsetup

* preppingtests

* queue

* mmovetocfgpassunittests

* sortoutconfig

* passinginteg

* deps

* fixtests
2025-02-03 17:30:50 +00:00

141 lines
3.8 KiB
Go

package result
import (
"context"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
resultprocessor "queryorchestration/internal/query/result/processor"
jsonextractor "queryorchestration/internal/query/types/jsonExtractor"
"queryorchestration/internal/serviceconfig"
"testing"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
func TestProcess(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
cfg := &serviceconfig.BaseConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := Service{
cfg: cfg,
}
qcfg := "{\"path\":\"examplekey\"}"
params := Process{
DocumentID: uuid.New(),
MinCleanVersion: 1,
MinTextVersion: 3,
Query: &resultprocessor.Query{
ID: uuid.New(),
Version: 2,
Config: &qcfg,
RequiredQueryIDs: &[]uuid.UUID{
uuid.New(),
},
},
}
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(database.MustToDBUUID(params.Query.ID), params.Query.Version, database.MustToDBUUID(params.DocumentID), params.MinCleanVersion, params.MinTextVersion).
WillReturnRows(
pgxmock.NewRows([]string{"queryId", "value", "type"}).
AddRow(database.MustToDBUUID((*params.Query.RequiredQueryIDs)[0]), "{\"examplekey\":\"example_value\"}", repository.QuerytypeContextFull),
)
pool.ExpectQuery("name: GetQueryConfig :one").WithArgs(database.MustToDBUUID(params.Query.ID), params.Query.Version).WillReturnRows(
pgxmock.NewRows([]string{"id", "config"}).
AddRow(pgtype.UUID{}, []byte(qcfg)),
)
val, err := svc.Process(ctx, &params)
assert.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()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
cfg := &serviceconfig.BaseConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := Service{
cfg: cfg,
}
pr, err := svc.listRequiredValues(ctx, nil)
assert.NoError(t, err)
assert.Nil(t, pr)
pr, err = svc.listRequiredValues(ctx, &Process{})
assert.NoError(t, err)
assert.Nil(t, pr)
pr, err = svc.listRequiredValues(ctx, &Process{
Query: &resultprocessor.Query{},
})
assert.NoError(t, err)
assert.Nil(t, pr)
pr, err = svc.listRequiredValues(ctx, &Process{
Query: &resultprocessor.Query{
RequiredQueryIDs: &[]uuid.UUID{},
},
})
assert.NoError(t, err)
assert.Nil(t, pr)
params := Process{
DocumentID: uuid.New(),
MinCleanVersion: 1,
MinTextVersion: 3,
Query: &resultprocessor.Query{
ID: uuid.New(),
Version: 2,
RequiredQueryIDs: &[]uuid.UUID{
uuid.New(),
},
},
}
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(database.MustToDBUUID(params.Query.ID), params.Query.Version, database.MustToDBUUID(params.DocumentID), params.MinCleanVersion, params.MinTextVersion).
WillReturnRows(
pgxmock.NewRows([]string{"queryId", "value", "type"}).
AddRow(database.MustToDBUUID((*params.Query.RequiredQueryIDs)[0]), "example_value", repository.QuerytypeJsonExtractor),
)
pr, err = svc.listRequiredValues(ctx, &params)
assert.NoError(t, err)
assert.ElementsMatch(t, []resultprocessor.Value{
jsonextractor.NewResult("example_value"),
}, *pr)
}
func TestGetProcessor(t *testing.T) {
svc := Service{}
pr, err := svc.getProcessor(resultprocessor.TypeJsonExtractor)
assert.NoError(t, err)
assert.NotNil(t, pr)
pr, err = svc.getProcessor(resultprocessor.TypeContextFull)
assert.NoError(t, err)
assert.NotNil(t, pr)
_, err = svc.getProcessor(resultprocessor.Type(-1))
assert.Error(t, err)
}