2025-02-07 14:15:06 +00:00
|
|
|
package documenttext
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2025-02-07 14:15:06 +00:00
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/document"
|
|
|
|
|
"queryorchestration/internal/serviceconfig"
|
|
|
|
|
"queryorchestration/internal/serviceconfig/queue/querysync"
|
2025-03-20 11:06:41 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/textract"
|
2025-02-07 14:15:06 +00:00
|
|
|
queuemock "queryorchestration/mocks/queue"
|
|
|
|
|
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2025-02-07 14:15:06 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DocTextConfig struct {
|
|
|
|
|
serviceconfig.BaseConfig
|
|
|
|
|
querysync.QuerySyncConfig
|
2025-03-20 11:06:41 +00:00
|
|
|
textract.TextractConfig
|
2025-02-07 14:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCreate(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
|
|
|
|
|
|
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
|
|
|
|
|
|
|
|
cfg := &DocTextConfig{}
|
|
|
|
|
cfg.QueueClient = mockSQS
|
|
|
|
|
cfg.QuerySyncURL = "/i/am/here"
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
|
|
|
|
|
|
svc := Service{
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
|
|
|
id := uuid.New()
|
|
|
|
|
inloc := document.Location{
|
|
|
|
|
Bucket: "bucket_name",
|
|
|
|
|
Key: "/i/am/here",
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(id).WillReturnRows(
|
2025-03-05 19:49:03 +00:00
|
|
|
pgxmock.NewRows([]string{"isextracted"}).
|
|
|
|
|
AddRow(false),
|
|
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
cleanId := uuid.New()
|
2025-03-05 19:49:03 +00:00
|
|
|
mimeType := "application/pdf"
|
|
|
|
|
dbmimetype := repository.NullCleanmimetype{
|
|
|
|
|
Valid: true,
|
|
|
|
|
Cleanmimetype: repository.Cleanmimetype(mimeType),
|
|
|
|
|
}
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetCleanEntryByDocId :one").WithArgs(id).WillReturnRows(
|
2025-03-05 19:49:03 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "mimetype", "fail"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(cleanId, id, &inloc.Bucket, &inloc.Key, int32(1), dbmimetype, repository.NullCleanfailtype{}),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
|
|
|
|
pool.ExpectQuery("name: GetCleanEntry :one").WithArgs(cleanId).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "mimetype", "fail"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(cleanId, id, &inloc.Bucket, &inloc.Key, int32(1), dbmimetype, repository.NullCleanfailtype{}),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
textId := uuid.New()
|
|
|
|
|
pool.ExpectBegin()
|
|
|
|
|
pool.ExpectQuery("name: GetDocumentTextExtractionByHash :one").WithArgs(cleanId, "example").WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "hash", "cleanEntryId"}),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectQuery("name: AddDocumentText :one").WithArgs(inloc.Bucket, inloc.Key, cleanId, "example").
|
2025-03-17 18:14:15 +00:00
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(textId),
|
2025-03-17 18:14:15 +00:00
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(textId, pgxmock.AnyArg()).
|
|
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
|
pool.ExpectCommit()
|
2025-03-05 19:49:03 +00:00
|
|
|
|
|
|
|
|
mockSQS.EXPECT().
|
|
|
|
|
SendMessage(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
|
|
|
|
return *in.QueueUrl == cfg.QuerySyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", id)
|
|
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
|
|
|
|
|
|
|
|
|
err = svc.Extract(ctx, id)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-03-05 19:49:03 +00:00
|
|
|
})
|
|
|
|
|
t.Run("invalid clean", func(t *testing.T) {
|
|
|
|
|
id := uuid.New()
|
|
|
|
|
fail := repository.NullCleanfailtype{
|
|
|
|
|
Valid: true,
|
|
|
|
|
Cleanfailtype: repository.CleanfailtypeInvalidMimetype,
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(id).WillReturnRows(
|
2025-03-05 19:49:03 +00:00
|
|
|
pgxmock.NewRows([]string{"isextracted"}).AddRow(false),
|
|
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetCleanEntryByDocId :one").WithArgs(id).WillReturnRows(
|
2025-03-05 19:49:03 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "mimetype", "fail"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(id, uuid.New(), nil, nil, int64(1), nil, fail),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
err = svc.Extract(ctx, id)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-03-05 19:49:03 +00:00
|
|
|
})
|
2025-02-07 14:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInformClean(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
|
|
|
|
|
|
|
|
cfg := &DocTextConfig{}
|
|
|
|
|
cfg.QueueClient = mockSQS
|
|
|
|
|
cfg.QuerySyncURL = "/i/am/here"
|
|
|
|
|
svc := Service{
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
}
|
|
|
|
|
id := uuid.New()
|
|
|
|
|
|
|
|
|
|
mockSQS.EXPECT().
|
|
|
|
|
SendMessage(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
|
|
|
|
return *in.QueueUrl == cfg.QuerySyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", id)
|
|
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
|
|
|
|
|
|
|
|
|
err := svc.informExtraction(ctx, id)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-07 14:15:06 +00:00
|
|
|
}
|