2025-02-12 19:00:25 +00:00
|
|
|
package docsyncrunner_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-03-10 11:03:00 +00:00
|
|
|
"fmt"
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2025-02-12 19:00:25 +00:00
|
|
|
docsyncrunner "queryorchestration/api/docSyncRunner"
|
|
|
|
|
"queryorchestration/internal/client"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/document"
|
|
|
|
|
documentsync "queryorchestration/internal/document/sync"
|
2025-04-02 18:50:03 +00:00
|
|
|
"queryorchestration/internal/server/runner"
|
2025-02-12 19:00:25 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/queue/documentclean"
|
2025-03-10 11:03:00 +00:00
|
|
|
queuemock "queryorchestration/mocks/queue"
|
2025-02-12 19:00:25 +00:00
|
|
|
|
2025-03-10 11:03:00 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
2025-02-12 19:00:25 +00:00
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2025-03-10 11:03:00 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2025-02-12 19:00:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DocSyncConfig struct {
|
2025-04-02 18:50:03 +00:00
|
|
|
runner.BaseConfig[docsyncrunner.Body]
|
2025-02-12 19:00:25 +00:00
|
|
|
documentclean.DocCleanConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDocInitRunner(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
2025-03-04 15:51:03 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-12 19:00:25 +00:00
|
|
|
|
|
|
|
|
cfg := &DocSyncConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-03-10 11:03:00 +00:00
|
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
|
|
|
cfg.QueueClient = mockSQS
|
2025-02-12 19:00:25 +00:00
|
|
|
|
|
|
|
|
runner := docsyncrunner.New(validator.New(), &docsyncrunner.Services{
|
|
|
|
|
Document: documentsync.New(cfg, &documentsync.Services{
|
|
|
|
|
Document: document.New(cfg),
|
2025-03-10 13:00:57 +00:00
|
|
|
Client: client.New(cfg),
|
2025-02-12 19:00:25 +00:00
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
assert.NotNil(t, runner)
|
|
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
t.Run("valid", func(t *testing.T) {
|
2025-03-10 11:03:00 +00:00
|
|
|
j := client.Client{
|
2025-04-02 18:50:03 +00:00
|
|
|
ID: "hello",
|
2025-03-05 19:49:03 +00:00
|
|
|
}
|
2025-03-17 18:14:15 +00:00
|
|
|
docinfo := document.DocumentSummary{
|
2025-03-10 11:03:00 +00:00
|
|
|
ID: uuid.New(),
|
|
|
|
|
ClientID: j.ID,
|
2025-03-05 19:49:03 +00:00
|
|
|
}
|
|
|
|
|
doc := docsyncrunner.Body{
|
2025-04-02 18:50:03 +00:00
|
|
|
DocumentID: docinfo.ID,
|
2025-03-05 19:49:03 +00:00
|
|
|
}
|
2025-02-12 19:00:25 +00:00
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(docinfo.ID).
|
2025-03-05 19:49:03 +00:00
|
|
|
WillReturnRows(
|
2025-03-10 11:03:00 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(docinfo.ID, docinfo.ClientID, "example"),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(j.ID).
|
2025-03-05 19:49:03 +00:00
|
|
|
WillReturnRows(
|
2025-04-02 18:50:03 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
|
|
|
|
AddRow(j.ID, "client_name", true),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
|
|
|
|
|
2025-03-10 11:03:00 +00:00
|
|
|
mockSQS.EXPECT().
|
|
|
|
|
SendMessage(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
2025-04-02 18:50:03 +00:00
|
|
|
return *in.QueueUrl == cfg.DocumentCleanURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.DocumentID)
|
2025-03-10 11:03:00 +00:00
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
|
|
|
|
|
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-12 19:00:25 +00:00
|
|
|
}
|