Files
query-orchestration/api/docInitRunner/runner_test.go
T
Michael McGuinness ee776d2681 Merged in feature/mockserver (pull request #135)
Single Mock Server

* mockserver

* mockserver

* reqs

* mockserver

* slowrunner

* someoptimisedqueries

* passedfullsuite

* passedfullsuite
2025-05-06 01:59:52 +00:00

97 lines
2.6 KiB
Go

package docinitrunner_test
import (
"context"
"fmt"
"testing"
"time"
docinitrunner "queryorchestration/api/docInitRunner"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document"
documentinit "queryorchestration/internal/document/init"
"queryorchestration/internal/server/runner"
"queryorchestration/internal/serviceconfig/objectstore"
"queryorchestration/internal/serviceconfig/queue/documentsync"
queuemock "queryorchestration/mocks/queue"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/google/uuid"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type DocInitConfig struct {
runner.BaseConfig[docinitrunner.Body]
documentsync.DocSyncConfig
}
func TestDocInitRunner(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &DocInitConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
mockSQS := queuemock.NewMockSQSClient(t)
cfg.QueueClient = mockSQS
runner := docinitrunner.New(&docinitrunner.Services{
Document: documentinit.New(cfg),
})
t.Run("valid", func(t *testing.T) {
clientId := "clientid"
bucketName := "bucketName"
location := objectstore.BucketKey{
Location: objectstore.Import,
ClientID: clientId,
CreatedAt: time.Now().UTC(),
}
docinfo := document.DocumentSummary{
ID: uuid.New(),
ClientID: "hello",
Hash: "example_hash",
}
doc := docinitrunner.Body{
Bucket: bucketName,
Key: location.String(),
Hash: docinfo.Hash,
}
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, clientId).WillReturnRows(
pgxmock.NewRows([]string{"id"}),
)
pool.ExpectBegin()
pool.ExpectQuery("name: CreateDocument :one").WithArgs(clientId, docinfo.Hash).
WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(docinfo.ID),
)
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(docinfo.ID, bucketName, location.String()).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(docinfo.ID).
WillReturnRows(
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
AddRow(docinfo.ID, docinfo.ClientID, "example"),
)
mockSQS.EXPECT().
SendMessage(
mock.Anything,
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docinfo.ID.String())
}),
mock.Anything,
).
Return(&sqs.SendMessageOutput{}, nil)
assert.True(t, runner.Process(ctx, doc))
})
}