Files
query-orchestration/api/queryRunner/queryrunner_test.go
T
Jay Brown 174644b63c Merged in jb/openapi (pull request #22)
add openapi infra

* add openapi infra

Includes generated stubs and all deps

* generatetolocation

* basesetupoffunctionsandclient

* round1controllertests

* passintegrationtests

* cleanupfromfullsuite

* storedjson

* fixjsonyaml

* fixtests


Approved-by: Michael McGuinness
2025-01-15 19:45:51 +00:00

86 lines
2.4 KiB
Go

package controllers_test
import (
"context"
"encoding/json"
controllers "queryorchestration/api/queryRunner"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document"
"queryorchestration/internal/queue"
"queryorchestration/internal/test"
"testing"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
func TestQueryRunner(t *testing.T) {
ctx := context.Background()
qCfg, cleanup := test.CreateQueue(t, ctx, &test.CreateQueueConfig{})
defer cleanup()
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 := document.New(db)
runner := controllers.NewQueryRunner(svc, validator.New())
assert.NotNil(t, runner)
doc := document.Document{
ID: uuid.New(),
JobID: uuid.New(),
Name: "document_name",
CleanVersion: 1,
TextVersion: 1,
}
bodyBytes, err := json.Marshal(doc)
assert.Nil(t, err)
body := string(bodyBytes)
msg := &types.Message{
Body: &body,
}
collectorID := database.MustToDBUUID(uuid.New())
queryID := database.MustToDBUUID(uuid.New())
minCleanVersion := int32(1)
minTextVersion := int32(1)
cfg := &queue.Config{
URL: qCfg.URL,
Client: qCfg.Client,
}
qV := int32(1)
pool.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(database.MustToDBUUID(doc.JobID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}).
AddRow(collectorID, database.MustToDBUUID(doc.JobID), minCleanVersion, minTextVersion),
)
pool.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
WillReturnRows(
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}).
AddRow(collectorID, queryID, qV),
)
pool.ExpectQuery("name: GetCollectorQueries :many").WithArgs(collectorID).
WillReturnRows(
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "queryVersion", "requiredIds"}).
AddRow(collectorID, queryID, repository.NullQuerytype{Querytype: repository.QuerytypeContextFull, Valid: true}, &qV, []pgtype.UUID{}),
)
err = runner.Process(ctx, cfg, msg)
assert.Nil(t, err)
}