5b7160fe44
Job Collector * createstructure * mostupdatevalidation * repocollectorupdate * updateoutline * updatevalidation * scriptupdate * cleanupdockerignore * update * collectorupdateapi
83 lines
2.4 KiB
Go
83 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/job/collector"
|
|
"queryorchestration/internal/query"
|
|
"queryorchestration/internal/query/document"
|
|
"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 := document.New(db, &document.Services{
|
|
Collector: collector.New(db, &collector.Services{
|
|
Query: query.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)
|
|
|
|
qV := int32(1)
|
|
|
|
pool.ExpectQuery("name: GetCollectorByJobID :one").WithArgs(database.MustToDBUUID(doc.JobID)).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
|
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)
|
|
}
|