Files
query-orchestration/internal/job/sync/sync_test.go
T
Michael McGuinness 0815cb35fb Merged in feature/lint (pull request #87)
Basic Lint Checks

* basic
2025-03-05 12:05:46 +00:00

117 lines
3.0 KiB
Go

package jobsync
import (
"context"
"fmt"
"testing"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document"
"queryorchestration/internal/serviceconfig"
"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 JobSyncConfig struct {
serviceconfig.BaseConfig
documentsync.DocSyncConfig
}
func TestTrigger(t *testing.T) {
t.Run("two batches", func(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &JobSyncConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
mockSQS := queuemock.NewMockSQSClient(t)
cfg.QueueClient = mockSQS
cfg.DocumentSyncURL = "/i/am/here"
svc := New(cfg)
jobId := uuid.New()
docs := []document.Document{
{
ID: uuid.New(),
JobID: jobId,
},
{
ID: uuid.New(),
JobID: jobId,
},
}
svc.batchSize = 1
total := int64(2)
pool.ExpectQuery("name: ListJobDocumentIDsBatch :many").WithArgs(database.MustToDBUUID(jobId), svc.batchSize, int32(0)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "totalCount"}).
AddRow(database.MustToDBUUID(docs[0].ID), &total),
)
mockSQS.EXPECT().
SendMessage(
mock.Anything,
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docs[0].ID.String())
}),
mock.Anything,
).
Return(&sqs.SendMessageOutput{}, nil)
pool.ExpectQuery("name: ListJobDocumentIDsBatch :many").WithArgs(database.MustToDBUUID(jobId), svc.batchSize, int32(1)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "totalCount"}).
AddRow(database.MustToDBUUID(docs[1].ID), &total),
)
mockSQS.EXPECT().
SendMessage(
mock.Anything,
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docs[1].ID.String())
}),
mock.Anything,
).
Return(&sqs.SendMessageOutput{}, nil)
err = svc.Sync(ctx, jobId)
assert.NoError(t, err)
})
t.Run("empty batch", func(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &JobSyncConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
mockSQS := queuemock.NewMockSQSClient(t)
cfg.QueueClient = mockSQS
cfg.DocumentSyncURL = "/i/am/here"
svc := New(cfg)
jobId := uuid.New()
svc.batchSize = 1
pool.ExpectQuery("name: ListJobDocumentIDsBatch :many").WithArgs(database.MustToDBUUID(jobId), svc.batchSize, int32(0)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "totalCount"}),
)
err = svc.Sync(ctx, jobId)
assert.NoError(t, err)
})
}