0815cb35fb
Basic Lint Checks * basic
99 lines
2.9 KiB
Go
99 lines
2.9 KiB
Go
package doctextrunner_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
|
|
doctextrunner "queryorchestration/api/docTextRunner"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/document"
|
|
documenttext "queryorchestration/internal/document/text"
|
|
textversion "queryorchestration/internal/document/text/version"
|
|
"queryorchestration/internal/serviceconfig"
|
|
"queryorchestration/internal/serviceconfig/queue/querysync"
|
|
queuemock "queryorchestration/mocks/queue"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
|
|
"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 DocTextConfig struct {
|
|
serviceconfig.BaseConfig
|
|
querysync.QuerySyncConfig
|
|
}
|
|
|
|
func TestDocCleanRunner(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
|
|
cfg := &DocTextConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
cfg.QueueClient = mockSQS
|
|
cfg.QuerySyncURL = "/i/am/here"
|
|
|
|
runner := doctextrunner.New(validator.New(), &doctextrunner.Services{
|
|
Text: documenttext.New(cfg, &documenttext.Services{
|
|
Version: textversion.New(cfg),
|
|
}),
|
|
})
|
|
assert.NotNil(t, runner)
|
|
|
|
doc := doctextrunner.Body{
|
|
ID: uuid.New(),
|
|
}
|
|
bodyBytes, err := json.Marshal(doc)
|
|
assert.NoError(t, err)
|
|
body := string(bodyBytes)
|
|
msg := &types.Message{
|
|
Body: &body,
|
|
}
|
|
|
|
inloc := document.Location{
|
|
Bucket: "bucket_name",
|
|
Key: "/i/am/here",
|
|
}
|
|
|
|
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(database.MustToDBUUID(doc.ID)).WillReturnRows(
|
|
pgxmock.NewRows([]string{"isextracted"}).
|
|
AddRow(false),
|
|
)
|
|
cleanId := database.MustToDBUUID(uuid.New())
|
|
mimeType := "application/pdf"
|
|
dbmimetype := repository.NullCleanmimetype{
|
|
Valid: true,
|
|
Cleanmimetype: repository.Cleanmimetype(mimeType),
|
|
}
|
|
pool.ExpectQuery("name: GetDocumentCleanEntry :one").WithArgs(database.MustToDBUUID(doc.ID)).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "mimetype", "fail"}).
|
|
AddRow(cleanId, database.MustToDBUUID(doc.ID), &inloc.Bucket, &inloc.Key, int32(1), dbmimetype, repository.NullCleanfailtype{}),
|
|
)
|
|
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(int32(1), inloc.Bucket, inloc.Key, cleanId).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
mockSQS.EXPECT().
|
|
SendMessage(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
|
return *in.QueueUrl == cfg.QuerySyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.ID.String())
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
|
|
|
err = runner.Process(ctx, msg)
|
|
assert.NoError(t, err)
|
|
}
|