2025-02-07 14:15:06 +00:00
|
|
|
package doctextrunner_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2025-02-07 14:15:06 +00:00
|
|
|
doctextrunner "queryorchestration/api/docTextRunner"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/document"
|
|
|
|
|
documenttext "queryorchestration/internal/document/text"
|
2025-04-02 18:50:03 +00:00
|
|
|
"queryorchestration/internal/server/runner"
|
|
|
|
|
"queryorchestration/internal/serviceconfig/aws"
|
|
|
|
|
"queryorchestration/internal/serviceconfig/objectstore"
|
2025-02-07 14:15:06 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/queue/querysync"
|
2025-03-20 11:06:41 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/textract"
|
2025-04-02 18:50:03 +00:00
|
|
|
objectstoremock "queryorchestration/mocks/objectstore"
|
2025-02-07 14:15:06 +00:00
|
|
|
queuemock "queryorchestration/mocks/queue"
|
2025-04-02 18:50:03 +00:00
|
|
|
textractmock "queryorchestration/mocks/textract"
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
awstextract "github.com/aws/aws-sdk-go-v2/service/textract"
|
2025-02-07 14:15:06 +00:00
|
|
|
"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"
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2025-02-07 14:15:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DocTextConfig struct {
|
2025-04-02 18:50:03 +00:00
|
|
|
runner.BaseConfig[doctextrunner.Body]
|
|
|
|
|
aws.AWSConfig
|
2025-02-07 14:15:06 +00:00
|
|
|
querysync.QuerySyncConfig
|
2025-03-20 11:06:41 +00:00
|
|
|
textract.TextractConfig
|
2025-04-02 18:50:03 +00:00
|
|
|
objectstore.ObjectStoreConfig
|
2025-02-07 14:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDocCleanRunner(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
2025-03-04 15:51:03 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-07 14:15:06 +00:00
|
|
|
|
|
|
|
|
cfg := &DocTextConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
|
|
|
cfg.QueueClient = mockSQS
|
|
|
|
|
cfg.QuerySyncURL = "/i/am/here"
|
2025-04-02 18:50:03 +00:00
|
|
|
mockS3 := objectstoremock.NewMockS3Client(t)
|
|
|
|
|
cfg.StoreClient = mockS3
|
|
|
|
|
mockTextract := textractmock.NewMockTextractClient(t)
|
|
|
|
|
cfg.TextractClient = mockTextract
|
2025-02-07 14:15:06 +00:00
|
|
|
|
|
|
|
|
runner := doctextrunner.New(validator.New(), &doctextrunner.Services{
|
2025-03-11 18:15:49 +00:00
|
|
|
Text: documenttext.New(cfg),
|
2025-02-07 14:15:06 +00:00
|
|
|
})
|
|
|
|
|
assert.NotNil(t, runner)
|
|
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
|
|
|
doc := doctextrunner.Body{
|
2025-04-02 18:50:03 +00:00
|
|
|
DocumentID: uuid.New(),
|
2025-03-05 19:49:03 +00:00
|
|
|
}
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
cleanId := uuid.New()
|
2025-03-05 19:49:03 +00:00
|
|
|
inloc := document.Location{
|
|
|
|
|
Bucket: "bucket_name",
|
|
|
|
|
Key: "/i/am/here",
|
|
|
|
|
}
|
2025-04-02 18:50:03 +00:00
|
|
|
hash := "example"
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(doc.DocumentID).WillReturnRows(
|
2025-03-05 19:49:03 +00:00
|
|
|
pgxmock.NewRows([]string{"isextracted"}).
|
|
|
|
|
AddRow(false),
|
|
|
|
|
)
|
|
|
|
|
mimeType := "application/pdf"
|
|
|
|
|
dbmimetype := repository.NullCleanmimetype{
|
|
|
|
|
Valid: true,
|
|
|
|
|
Cleanmimetype: repository.Cleanmimetype(mimeType),
|
|
|
|
|
}
|
2025-04-02 18:50:03 +00:00
|
|
|
pool.ExpectQuery("name: GetCleanEntryByDocId :one").WithArgs(doc.DocumentID).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "hash", "mimetype", "fail", "externalClientId"}).
|
|
|
|
|
AddRow(cleanId, doc.DocumentID, &inloc.Bucket, &inloc.Key, int64(1), &hash, dbmimetype, repository.NullCleanfailtype{}, "clientId"),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
2025-04-02 18:50:03 +00:00
|
|
|
jobId := "hello"
|
|
|
|
|
triggerId := uuid.New()
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectBegin()
|
2025-04-02 18:50:03 +00:00
|
|
|
pool.ExpectQuery("name: AddDocumentTextTrigger :one").WithArgs(cleanId, pgxmock.AnyArg()).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id"}).AddRow(triggerId),
|
2025-03-20 11:06:41 +00:00
|
|
|
)
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
mockTextract.EXPECT().
|
|
|
|
|
StartDocumentTextDetection(
|
2025-03-05 19:49:03 +00:00
|
|
|
mock.Anything,
|
2025-04-02 18:50:03 +00:00
|
|
|
mock.MatchedBy(func(in *awstextract.StartDocumentTextDetectionInput) bool {
|
|
|
|
|
return true
|
2025-03-05 19:49:03 +00:00
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
2025-04-02 18:50:03 +00:00
|
|
|
Return(&awstextract.StartDocumentTextDetectionOutput{
|
|
|
|
|
JobId: &jobId,
|
|
|
|
|
}, nil)
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
pool.ExpectExec("name: AddDocumentTextTriggerJobId :exec").WithArgs(&jobId, triggerId).
|
|
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
|
pool.ExpectCommit()
|
2025-03-05 19:49:03 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
assert.True(t, runner.Process(ctx, doc))
|
2025-03-05 19:49:03 +00:00
|
|
|
})
|
2025-02-07 14:15:06 +00:00
|
|
|
}
|