Files
query-orchestration/api/docTextProcessRunner/runner_test.go
T

158 lines
4.2 KiB
Go
Raw Normal View History

package doctextprocessrunner
import (
"context"
"errors"
"fmt"
"io"
"strings"
"testing"
"time"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document"
documenttext "queryorchestration/internal/document/text"
"queryorchestration/internal/server/runner"
"queryorchestration/internal/serviceconfig/aws"
"queryorchestration/internal/serviceconfig/objectstore"
"queryorchestration/internal/serviceconfig/queue/querysync"
"queryorchestration/internal/serviceconfig/textract"
objectstoremock "queryorchestration/mocks/objectstore"
queuemock "queryorchestration/mocks/queue"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/go-playground/validator/v10"
"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[Body]
aws.AWSConfig
textract.TextractConfig
querysync.QuerySyncConfig
objectstore.ObjectStoreConfig
}
func TestDocTextProcessRunner(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
mockS3 := objectstoremock.NewMockS3Client(t)
cfg.StoreClient = mockS3
runner := New(validator.New(), &Services{
Text: documenttext.New(cfg),
})
assert.NotNil(t, runner)
t.Run("pass through", func(t *testing.T) {
triggerId := uuid.New()
docId := uuid.New()
textractId := "heeloo"
textId := uuid.New()
cleanId := uuid.New()
bucketName := "bucketName"
docinfo := document.DocumentSummary{
ID: uuid.New(),
ClientID: "hello",
Hash: `"5d41402abc4b2a76b9719d911017c592"`,
}
key := objectstore.BucketKey{
ClientID: docinfo.ClientID,
Location: objectstore.TextTextract,
Filename: triggerId.String(),
CreatedAt: time.Now().UTC(),
}
doc := Body{
Bucket: bucketName,
Key: key.String(),
Hash: docinfo.Hash,
ClientID: docinfo.ClientID,
}
pool.ExpectQuery("name: GetDocumentTextTrigger :one").WithArgs(triggerId).WillReturnRows(
pgxmock.NewRows([]string{"id", "cleanId", "version", "textractJobId", "documentId"}).
AddRow(triggerId, cleanId, int64(123), &textractId, docId),
)
pool.ExpectBegin()
pool.ExpectQuery("name: GetDocumentTextExtractionByHash :one").WithArgs(cleanId, docinfo.Hash).WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(textId),
)
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(textId, triggerId, pgxmock.AnyArg()).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
body := io.NopCloser(strings.NewReader("hello"))
mockS3.EXPECT().
GetObject(
mock.Anything,
mock.MatchedBy(func(in *s3.GetObjectInput) bool {
return *in.IfMatch == `"5d41402abc4b2a76b9719d911017c592"`
}),
mock.Anything,
).
Return(&s3.GetObjectOutput{
Body: body,
}, nil)
mockSQS.EXPECT().
SendMessage(
mock.Anything,
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
return *in.QueueUrl == cfg.QuerySyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docId.String())
}),
mock.Anything,
).
Return(&sqs.SendMessageOutput{}, nil)
assert.True(t, runner.Process(ctx, doc))
})
t.Run("invalid key", func(t *testing.T) {
key := objectstore.BucketKey{}
doc := Body{
Key: key.String(),
}
assert.True(t, runner.Process(ctx, doc))
})
t.Run("unable to process", func(t *testing.T) {
triggerId := uuid.New()
bucketName := "bucketName"
docinfo := document.DocumentSummary{
ID: uuid.New(),
ClientID: "hello",
Hash: `"5d41402abc4b2a76b9719d911017c592"`,
}
key := objectstore.BucketKey{
ClientID: docinfo.ClientID,
Location: objectstore.TextTextract,
Filename: triggerId.String(),
CreatedAt: time.Now().UTC(),
}
doc := Body{
Bucket: bucketName,
Key: key.String(),
Hash: docinfo.Hash,
ClientID: docinfo.ClientID,
}
pool.ExpectQuery("name: GetDocumentTextTrigger :one").WithArgs(triggerId).
WillReturnError(errors.New("db fail"))
assert.False(t, runner.Process(ctx, doc))
})
}