2025-02-07 14:15:06 +00:00
|
|
|
package documenttext
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/document"
|
2025-02-12 19:00:25 +00:00
|
|
|
textversion "queryorchestration/internal/document/text/version"
|
2025-02-07 14:15:06 +00:00
|
|
|
"queryorchestration/internal/serviceconfig"
|
|
|
|
|
"queryorchestration/internal/serviceconfig/queue/querysync"
|
|
|
|
|
queuemock "queryorchestration/mocks/queue"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DocTextConfig struct {
|
|
|
|
|
serviceconfig.BaseConfig
|
|
|
|
|
querysync.QuerySyncConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
svc: &Services{
|
2025-02-12 19:00:25 +00:00
|
|
|
Version: textversion.New(cfg),
|
2025-02-07 14:15:06 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id := uuid.New()
|
|
|
|
|
inloc := document.Location{
|
|
|
|
|
Bucket: "bucket_name",
|
|
|
|
|
Key: "/i/am/here",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"isextracted"}).
|
|
|
|
|
AddRow(false),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectQuery("name: GetDocumentCleanEntry :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
|
2025-02-11 15:22:59 +00:00
|
|
|
pgxmock.NewRows([]string{"documentId", "bucket", "key", "version"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(id), inloc.Bucket, inloc.Key, int32(1)),
|
2025-02-07 14:15:06 +00:00
|
|
|
)
|
|
|
|
|
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(database.MustToDBUUID(id), int32(1), inloc.Bucket, inloc.Key).
|
|
|
|
|
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\"}", id)
|
|
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
|
|
|
|
|
2025-02-11 15:22:59 +00:00
|
|
|
err = svc.Extract(ctx, id)
|
2025-02-07 14:15:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
}
|