2025-02-07 14:15:06 +00:00
|
|
|
package doctextrunner_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-04-22 14:40:16 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
2025-04-03 19:17:24 +00:00
|
|
|
"time"
|
2025-03-05 12:05:46 +00:00
|
|
|
|
2025-02-07 14:15:06 +00:00
|
|
|
doctextrunner "queryorchestration/api/docTextRunner"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
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-22 14:40:16 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
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/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)
|
2025-04-22 14:40:16 +00:00
|
|
|
mockStore := objectstoremock.NewMockS3Client(t)
|
|
|
|
|
cfg.StoreClient = mockStore
|
2025-04-02 18:50:03 +00:00
|
|
|
mockTextract := textractmock.NewMockTextractClient(t)
|
|
|
|
|
cfg.TextractClient = mockTextract
|
2025-04-22 14:40:16 +00:00
|
|
|
mockQueue := queuemock.NewMockSQSClient(t)
|
|
|
|
|
cfg.QueueClient = mockQueue
|
|
|
|
|
cfg.QuerySyncURL = "ex"
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-04-22 14:40:16 +00:00
|
|
|
runner := doctextrunner.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-04-22 14:40:16 +00:00
|
|
|
doc := doctextrunner.Body{
|
|
|
|
|
DocumentID: uuid.New(),
|
|
|
|
|
}
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-04-22 14:40:16 +00:00
|
|
|
cleanId := uuid.New()
|
|
|
|
|
bucket := "bucket"
|
|
|
|
|
hash := `"a06d5e0e795adeb6d0b3732330dbcc15"`
|
|
|
|
|
textId := uuid.New()
|
|
|
|
|
key := objectstore.BucketKey{
|
|
|
|
|
ClientID: "aa",
|
|
|
|
|
Location: objectstore.Import,
|
|
|
|
|
CreatedAt: time.Now().UTC(),
|
|
|
|
|
EntityID: cleanId,
|
|
|
|
|
}
|
|
|
|
|
keyStr := key.String()
|
|
|
|
|
filename := "../../assets/sampleGeneration/generated/helloWorld.gen"
|
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
defer file.Close()
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-04-22 14:40:16 +00:00
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
|
var textractBaseOut map[string][]awstextract.AnalyzeDocumentOutput
|
|
|
|
|
err = decoder.Decode(&textractBaseOut)
|
|
|
|
|
require.NoError(t, err)
|
2025-04-08 12:44:09 +00:00
|
|
|
|
2025-04-22 14:40:16 +00:00
|
|
|
mimetype := repository.NullCleanmimetype{
|
|
|
|
|
Valid: true,
|
|
|
|
|
Cleanmimetype: repository.CleanmimetypeApplicationPdf,
|
|
|
|
|
}
|
|
|
|
|
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(doc.DocumentID).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"isextracted"}).
|
|
|
|
|
AddRow(false),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectQuery("name: GetCleanEntryByDocId :one").WithArgs(doc.DocumentID).
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "hash", "mimetype", "fail", "clientId"}).
|
|
|
|
|
AddRow(cleanId, doc.DocumentID, &bucket, &keyStr, int64(123), &hash, mimetype, nil, key.ClientID),
|
2025-03-20 11:06:41 +00:00
|
|
|
)
|
2025-04-22 14:40:16 +00:00
|
|
|
bodyMsg := io.NopCloser(strings.NewReader(pdfHelloWorld))
|
|
|
|
|
mockStore.EXPECT().
|
|
|
|
|
GetObject(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *s3.GetObjectInput) bool {
|
|
|
|
|
return *in.Bucket == bucket && *in.Key == keyStr
|
|
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&s3.GetObjectOutput{
|
|
|
|
|
Body: bodyMsg,
|
|
|
|
|
}, nil)
|
|
|
|
|
sent := 0
|
|
|
|
|
mockTextract.EXPECT().
|
|
|
|
|
AnalyzeDocument(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *awstextract.AnalyzeDocumentInput) bool {
|
|
|
|
|
if sent == 0 {
|
|
|
|
|
sent++
|
2025-04-02 18:50:03 +00:00
|
|
|
return true
|
2025-04-22 14:40:16 +00:00
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&textractBaseOut["base"][0], nil).Once()
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-04-22 14:40:16 +00:00
|
|
|
pool.ExpectBegin()
|
|
|
|
|
pool.ExpectQuery("name: GetDocumentTextExtractionByHash :one").WithArgs(cleanId, hash).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id"}).
|
|
|
|
|
AddRow(textId),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(textId, pgxmock.AnyArg()).
|
|
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
|
pool.ExpectCommit()
|
2025-03-05 19:49:03 +00:00
|
|
|
|
2025-04-22 14:40:16 +00:00
|
|
|
mockQueue.EXPECT().
|
|
|
|
|
SendMessage(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
|
|
|
|
return *in.QueueUrl == cfg.QuerySyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.DocumentID.String())
|
|
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
|
|
|
|
|
|
|
|
|
assert.True(t, runner.Process(ctx, doc))
|
2025-02-07 14:15:06 +00:00
|
|
|
}
|
2025-04-22 14:40:16 +00:00
|
|
|
|
|
|
|
|
const pdfHelloWorld = `%PDF-1.4
|
|
|
|
|
%����
|
|
|
|
|
1 0 obj
|
|
|
|
|
<<
|
|
|
|
|
/Type /Catalog
|
|
|
|
|
/Pages 2 0 R
|
|
|
|
|
/Version /1.4
|
|
|
|
|
>>
|
|
|
|
|
endobj
|
|
|
|
|
2 0 obj
|
|
|
|
|
<<
|
|
|
|
|
/Type /Pages
|
|
|
|
|
/Kids [3 0 R]
|
|
|
|
|
/Count 1
|
|
|
|
|
>>
|
|
|
|
|
endobj
|
|
|
|
|
3 0 obj
|
|
|
|
|
<<
|
|
|
|
|
/Type /Page
|
|
|
|
|
/Parent 2 0 R
|
|
|
|
|
/MediaBox [0 0 612 792]
|
|
|
|
|
/Resources <<
|
|
|
|
|
/Font <<
|
|
|
|
|
/F1 <<
|
|
|
|
|
/Type /Font
|
|
|
|
|
/Subtype /Type1
|
|
|
|
|
/BaseFont /Helvetica
|
|
|
|
|
>>
|
|
|
|
|
>>
|
|
|
|
|
>>
|
|
|
|
|
/Contents 4 0 R
|
|
|
|
|
>>
|
|
|
|
|
endobj
|
|
|
|
|
4 0 obj
|
|
|
|
|
<<
|
|
|
|
|
/Length
|
|
|
|
|
44
|
|
|
|
|
>>
|
|
|
|
|
stream
|
|
|
|
|
BT
|
|
|
|
|
/F1 24 Tf
|
|
|
|
|
100 700 Td
|
|
|
|
|
(Hello World!) Tj
|
|
|
|
|
ET
|
|
|
|
|
endstream
|
|
|
|
|
endobj
|
|
|
|
|
xref
|
|
|
|
|
0 5
|
|
|
|
|
0000000000 65535 f
|
|
|
|
|
0000000015 00000 n
|
|
|
|
|
0000000086 00000 n
|
|
|
|
|
0000000151 00000 n
|
|
|
|
|
0000000376 00000 n
|
|
|
|
|
trailer
|
|
|
|
|
<<
|
|
|
|
|
/Size 5
|
|
|
|
|
/Root 1 0 R
|
|
|
|
|
>>
|
|
|
|
|
startxref
|
|
|
|
|
472
|
|
|
|
|
%%EOF`
|