7ce7c9df4d
Feature/ecr * nosave * repo * awscli * unzip * ignore * moreram * 14k * ref * deployment * 12k * uselocal * go * dockercomd * reorder * iamgename * installs * tart * cli * clideps * y * dockerce * nodock * multi * rmecr * dev
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package querysync
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
resultsync "queryorchestration/internal/query/result/sync"
|
|
"queryorchestration/internal/serviceconfig"
|
|
queryc "queryorchestration/internal/serviceconfig/queue/query"
|
|
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 QuerySyncConfig struct {
|
|
serviceconfig.BaseConfig
|
|
queryc.QueryConfig
|
|
}
|
|
|
|
func TestSync(t *testing.T) {
|
|
ctx := t.Context()
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
cfg := &QuerySyncConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
cfg.QueueClient = mockSQS
|
|
cfg.QueryURL = "/i/am/here"
|
|
svc := Service{
|
|
cfg: cfg,
|
|
svc: &Services{
|
|
ResultSync: resultsync.New(cfg),
|
|
},
|
|
}
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
id := uuid.New()
|
|
reqOne := uuid.New()
|
|
reqTwo := uuid.New()
|
|
|
|
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(id).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"isextracted"}).
|
|
AddRow(true),
|
|
)
|
|
pool.ExpectQuery("name: ListUnsyncedNoDepsQueriesByDocId :many").WithArgs(&id).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(&reqOne).
|
|
AddRow(&reqTwo),
|
|
)
|
|
|
|
mockSQS.EXPECT().
|
|
SendMessage(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
|
return *in.QueueUrl == cfg.QueryURL && *in.MessageBody == fmt.Sprintf("{\"document_id\":\"%s\",\"query_id\":\"%s\"}", id.String(), reqOne.String())
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
|
|
|
mockSQS.EXPECT().
|
|
SendMessage(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
|
return *in.QueueUrl == cfg.QueryURL && *in.MessageBody == fmt.Sprintf("{\"document_id\":\"%s\",\"query_id\":\"%s\"}", id.String(), reqTwo.String())
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
|
|
|
assert.Nil(t, svc.Sync(ctx, id))
|
|
})
|
|
t.Run("not extracted", func(t *testing.T) {
|
|
id := uuid.New()
|
|
|
|
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(id).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"isextracted"}).
|
|
AddRow(false),
|
|
)
|
|
|
|
assert.Nil(t, svc.Sync(ctx, id))
|
|
})
|
|
}
|