Merged in feature/testquery (pull request #39)
Test Query * depstextandclean * startedcleaningresult * resulttidyup * roundone * cleaning * unsyncedquery * startedtestsandsimplification * api * querytests * resultprocessortests * unittests * cleanup
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
package result
|
||||
|
||||
import (
|
||||
"context"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
resultprocessor "queryorchestration/internal/query/result/processor"
|
||||
jsonextractor "queryorchestration/internal/query/types/jsonExtractor"
|
||||
"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)
|
||||
}
|
||||
queries := repository.New(pool)
|
||||
db := &database.Connection{
|
||||
Queries: queries,
|
||||
Pool: pool,
|
||||
}
|
||||
|
||||
svc := Service{
|
||||
db: db,
|
||||
}
|
||||
|
||||
cfg := "{\"path\":\"examplekey\"}"
|
||||
params := Process{
|
||||
DocumentID: uuid.New(),
|
||||
MinCleanVersion: 1,
|
||||
MinTextVersion: 3,
|
||||
Query: &resultprocessor.Query{
|
||||
ID: uuid.New(),
|
||||
Version: 2,
|
||||
Config: &cfg,
|
||||
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(cfg)),
|
||||
)
|
||||
|
||||
val, err := svc.Process(ctx, ¶ms)
|
||||
assert.Nil(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)
|
||||
}
|
||||
queries := repository.New(pool)
|
||||
db := &database.Connection{
|
||||
Queries: queries,
|
||||
Pool: pool,
|
||||
}
|
||||
|
||||
svc := Service{
|
||||
db: db,
|
||||
}
|
||||
|
||||
pr, err := svc.listRequiredValues(ctx, nil)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, pr)
|
||||
|
||||
pr, err = svc.listRequiredValues(ctx, &Process{})
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, pr)
|
||||
|
||||
pr, err = svc.listRequiredValues(ctx, &Process{
|
||||
Query: &resultprocessor.Query{},
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, pr)
|
||||
|
||||
pr, err = svc.listRequiredValues(ctx, &Process{
|
||||
Query: &resultprocessor.Query{
|
||||
RequiredQueryIDs: &[]uuid.UUID{},
|
||||
},
|
||||
})
|
||||
assert.Nil(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, ¶ms)
|
||||
assert.Nil(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.Nil(t, err)
|
||||
assert.NotNil(t, pr)
|
||||
|
||||
pr, err = svc.getProcessor(resultprocessor.TypeContextFull)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, pr)
|
||||
|
||||
_, err = svc.getProcessor(resultprocessor.Type(-1))
|
||||
assert.Error(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user