Files
query-orchestration/api/queryRunner/queryrunner_test.go
T

84 lines
2.5 KiB
Go
Raw Normal View History

package controllers_test
import (
"context"
"encoding/json"
2025-01-15 19:45:51 +00:00
controllers "queryorchestration/api/queryRunner"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
documentsync "queryorchestration/internal/document/sync"
"queryorchestration/internal/job/collector"
2025-01-23 14:56:20 +00:00
"queryorchestration/internal/query"
"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()
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 := documentsync.New(db, &documentsync.Services{
2025-01-23 14:56:20 +00:00
Collector: collector.New(db, &collector.Services{
Query: query.New(db),
}),
})
runner := controllers.NewQueryRunner(validator.New(), &controllers.Services{
DocumentSync: svc,
})
assert.NotNil(t, runner)
doc := documentsync.Document{
ID: uuid.New(),
JobID: uuid.New(),
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)
qV := int32(1)
pool.ExpectQuery("name: GetCollectorByJobID :one").WithArgs(database.MustToDBUUID(doc.JobID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
2025-01-23 14:56:20 +00:00
AddRow(collectorID, database.MustToDBUUID(doc.JobID), &minCleanVersion, &minTextVersion, int32(1), int32(2), []byte("")),
)
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: ListCollectorQueries :many").WithArgs(collectorID).
WillReturnRows(
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "queryVersion", "requiredIds"}).
AddRow(collectorID, queryID, repository.QuerytypeContextFull, qV, []pgtype.UUID{}),
)
err = runner.Process(ctx, msg)
assert.Nil(t, err)
}