2025-02-03 17:30:50 +00:00
|
|
|
package queryrunner_test
|
2025-01-10 19:17:20 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-02-11 15:22:59 +00:00
|
|
|
"fmt"
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2025-02-11 15:22:59 +00:00
|
|
|
queryrunner "queryorchestration/api/queryRunner"
|
2025-01-10 19:17:20 +00:00
|
|
|
"queryorchestration/internal/database/repository"
|
2025-01-23 14:56:20 +00:00
|
|
|
"queryorchestration/internal/query"
|
2025-01-29 11:52:37 +00:00
|
|
|
"queryorchestration/internal/query/result"
|
2025-02-11 15:22:59 +00:00
|
|
|
resultprocessor "queryorchestration/internal/query/result/processor"
|
|
|
|
|
resultset "queryorchestration/internal/query/result/set"
|
2025-02-14 10:56:24 +00:00
|
|
|
resultsync "queryorchestration/internal/query/result/sync"
|
2025-02-11 15:22:59 +00:00
|
|
|
"queryorchestration/internal/server/runner"
|
|
|
|
|
queryc "queryorchestration/internal/serviceconfig/queue/query"
|
|
|
|
|
queuemock "queryorchestration/mocks/queue"
|
2025-01-10 19:17:20 +00:00
|
|
|
|
2025-02-11 15:22:59 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
2025-01-10 19:17:20 +00:00
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2025-02-11 15:22:59 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2025-01-10 19:17:20 +00:00
|
|
|
)
|
|
|
|
|
|
2025-02-11 15:22:59 +00:00
|
|
|
type QueryConfig struct {
|
2025-04-02 18:50:03 +00:00
|
|
|
runner.BaseConfig[queryrunner.Body]
|
2025-02-11 15:22:59 +00:00
|
|
|
queryc.QueryConfig
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 19:17:20 +00:00
|
|
|
func TestQueryRunner(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
2025-03-04 15:51:03 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-10 19:17:20 +00:00
|
|
|
|
2025-02-11 15:22:59 +00:00
|
|
|
cfg := &QueryConfig{}
|
2025-01-31 13:43:55 +00:00
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-02-11 15:22:59 +00:00
|
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
|
|
|
cfg.QueueClient = mockSQS
|
|
|
|
|
cfg.QueryURL = "/i/am/here"
|
2025-01-31 13:43:55 +00:00
|
|
|
|
2025-02-11 15:22:59 +00:00
|
|
|
que := query.New(cfg)
|
|
|
|
|
runner := queryrunner.New(validator.New(), &queryrunner.Services{
|
|
|
|
|
ResultSet: resultset.New(cfg, &resultset.Services{
|
|
|
|
|
Query: que,
|
|
|
|
|
Result: result.New(cfg, &result.Services{
|
|
|
|
|
Query: que,
|
|
|
|
|
}),
|
2025-02-14 10:56:24 +00:00
|
|
|
Sync: resultsync.New(cfg),
|
2025-02-11 15:22:59 +00:00
|
|
|
}),
|
2025-01-24 16:12:25 +00:00
|
|
|
})
|
2025-01-10 19:17:20 +00:00
|
|
|
assert.NotNil(t, runner)
|
|
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
t.Run("valid", func(t *testing.T) {
|
2025-04-02 18:50:03 +00:00
|
|
|
doc := queryrunner.Body{
|
2025-03-05 19:49:03 +00:00
|
|
|
DocumentID: uuid.New(),
|
|
|
|
|
QueryID: uuid.New(),
|
|
|
|
|
}
|
2025-01-10 19:17:20 +00:00
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
qcfg := "{\"path\":\"examplekey\"}"
|
2025-03-20 11:06:41 +00:00
|
|
|
reqQuery := uuid.New()
|
2025-03-05 19:49:03 +00:00
|
|
|
query := &resultprocessor.Query{
|
|
|
|
|
ID: doc.QueryID,
|
|
|
|
|
Version: 2,
|
2025-03-20 11:06:41 +00:00
|
|
|
RequiredQueryIDs: &[]uuid.UUID{reqQuery},
|
2025-03-05 19:49:03 +00:00
|
|
|
Config: &qcfg,
|
|
|
|
|
}
|
|
|
|
|
params := &resultset.Set{
|
|
|
|
|
DocumentID: doc.DocumentID,
|
|
|
|
|
}
|
2025-02-11 15:22:59 +00:00
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
textEntryId := uuid.New()
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetTextEntryByDocId :one").WithArgs(params.DocumentID).
|
2025-03-05 19:49:03 +00:00
|
|
|
WillReturnRows(
|
2025-04-02 18:50:03 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "hash", "cleanId", "triggerId", "textractJobId", "triggerVersion", "extractionVersion"}).
|
|
|
|
|
AddRow(textEntryId, params.DocumentID, "buket", "/i/am/here", "example", uuid.New(), uuid.New(), "jobId", int64(123), int64(543)),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetQuery :one").WithArgs(query.ID).WillReturnRows(
|
2025-03-05 19:49:03 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.Version, query.Version, []byte(*query.Config), *query.RequiredQueryIDs),
|
2025-02-11 15:22:59 +00:00
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetQueryWithVersion :one").WithArgs(&query.ID, &query.Version).WillReturnRows(
|
2025-03-05 19:49:03 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.Version, query.Version, []byte(*query.Config), *query.RequiredQueryIDs),
|
2025-02-11 15:22:59 +00:00
|
|
|
)
|
2025-03-05 19:49:03 +00:00
|
|
|
requiredResultId := uuid.New()
|
|
|
|
|
strVal := "{\"examplekey\":\"example_value\"}"
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(&query.ID, &query.Version, ¶ms.DocumentID).
|
2025-03-05 19:49:03 +00:00
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(&requiredResultId, (*query.RequiredQueryIDs)[0], repository.QuerytypeContextFull, &strVal),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).WillReturnRows(
|
2025-03-05 19:49:03 +00:00
|
|
|
pgxmock.NewRows([]string{"config"}).
|
|
|
|
|
AddRow([]byte(qcfg)),
|
2025-02-11 15:22:59 +00:00
|
|
|
)
|
2025-03-05 19:49:03 +00:00
|
|
|
pool.ExpectBegin()
|
|
|
|
|
resultId := uuid.New()
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: AddResult :one").WithArgs(query.ID, pgxmock.AnyArg(), textEntryId, query.Version).
|
2025-03-05 19:49:03 +00:00
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(resultId),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(&query.ID, &query.Version, ¶ms.DocumentID).
|
2025-03-05 19:49:03 +00:00
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(&requiredResultId, (*query.RequiredQueryIDs)[0], repository.QuerytypeContextFull, &strVal),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectExec("name: AddResultDependency :exec").WithArgs(resultId, requiredResultId).
|
2025-03-05 19:49:03 +00:00
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
|
pool.ExpectCommit()
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: ListQueryDirectDependentsByDocumentID :many").WithArgs(query.ID, doc.DocumentID).
|
2025-03-05 19:49:03 +00:00
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"queryId"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(&reqQuery),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
2025-02-11 15:22:59 +00:00
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
mockSQS.EXPECT().
|
|
|
|
|
SendMessage(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
|
|
|
|
return *in.QueueUrl == cfg.QueryURL && *in.MessageBody == fmt.Sprintf("{\"document_id\":\"%s\",\"query_id\":\"%s\"}", params.DocumentID.String(), (*query.RequiredQueryIDs)[0].String())
|
|
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
2025-01-10 19:17:20 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
assert.True(t, runner.Process(ctx, doc))
|
2025-03-05 19:49:03 +00:00
|
|
|
})
|
2025-01-10 19:17:20 +00:00
|
|
|
}
|