2025-04-02 18:50:03 +00:00
|
|
|
package docinitrunner_test
|
2025-02-03 17:30:50 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-02-07 12:12:51 +00:00
|
|
|
"fmt"
|
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-04-02 18:50:03 +00:00
|
|
|
docinitrunner "queryorchestration/api/docInitRunner"
|
2025-02-03 17:30:50 +00:00
|
|
|
"queryorchestration/internal/database/repository"
|
2025-02-05 17:44:01 +00:00
|
|
|
"queryorchestration/internal/document"
|
2025-02-03 17:30:50 +00:00
|
|
|
documentinit "queryorchestration/internal/document/init"
|
2025-04-02 18:50:03 +00:00
|
|
|
"queryorchestration/internal/server/runner"
|
2025-04-03 12:13:16 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/objectstore"
|
2025-02-12 19:00:25 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/queue/documentsync"
|
|
|
|
|
queuemock "queryorchestration/mocks/queue"
|
2025-02-03 17:30:50 +00:00
|
|
|
|
2025-02-12 19:00:25 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
2025-02-03 17:30:50 +00:00
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2025-02-12 19:00:25 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2025-02-03 17:30:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DocInitConfig struct {
|
2025-04-02 18:50:03 +00:00
|
|
|
runner.BaseConfig[docinitrunner.Body]
|
2025-02-12 19:00:25 +00:00
|
|
|
documentsync.DocSyncConfig
|
2025-02-03 17:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-03 17:30:50 +00:00
|
|
|
|
|
|
|
|
cfg := &DocInitConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-02-12 19:00:25 +00:00
|
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
|
|
|
cfg.QueueClient = mockSQS
|
2025-02-03 17:30:50 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
runner := docinitrunner.New(validator.New(), &docinitrunner.Services{
|
2025-02-12 19:00:25 +00:00
|
|
|
Document: documentinit.New(cfg),
|
2025-02-03 17:30:50 +00:00
|
|
|
})
|
|
|
|
|
assert.NotNil(t, runner)
|
|
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
t.Run("valid", func(t *testing.T) {
|
2025-04-02 18:50:03 +00:00
|
|
|
clientId := "clientid"
|
2025-03-05 19:49:03 +00:00
|
|
|
bucketName := "bucketName"
|
2025-04-03 12:13:16 +00:00
|
|
|
location := objectstore.BucketKey{
|
2025-04-03 19:17:24 +00:00
|
|
|
Location: objectstore.Import,
|
|
|
|
|
ClientID: clientId,
|
|
|
|
|
CreatedAt: time.Now().UTC(),
|
2025-04-03 12:13:16 +00:00
|
|
|
}
|
2025-03-17 18:14:15 +00:00
|
|
|
docinfo := document.DocumentSummary{
|
2025-03-10 11:03:00 +00:00
|
|
|
ID: uuid.New(),
|
2025-04-02 18:50:03 +00:00
|
|
|
ClientID: "hello",
|
2025-03-10 11:03:00 +00:00
|
|
|
Hash: "example_hash",
|
2025-03-05 19:49:03 +00:00
|
|
|
}
|
2025-04-02 18:50:03 +00:00
|
|
|
doc := docinitrunner.Body{
|
2025-04-03 19:17:24 +00:00
|
|
|
Bucket: bucketName,
|
|
|
|
|
Key: location.String(),
|
|
|
|
|
Hash: docinfo.Hash,
|
2025-03-05 19:49:03 +00:00
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, clientId).WillReturnRows(
|
2025-03-05 19:49:03 +00:00
|
|
|
pgxmock.NewRows([]string{"id"}),
|
2025-02-03 17:30:50 +00:00
|
|
|
)
|
2025-03-05 19:49:03 +00:00
|
|
|
pool.ExpectBegin()
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: CreateDocument :one").WithArgs(clientId, docinfo.Hash).
|
2025-03-05 19:49:03 +00:00
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(docinfo.ID),
|
2025-03-05 19:49:03 +00:00
|
|
|
)
|
2025-04-03 12:13:16 +00:00
|
|
|
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(docinfo.ID, bucketName, location.String()).
|
2025-03-05 19:49:03 +00:00
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
|
pool.ExpectCommit()
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
|
mockSQS.EXPECT().
|
|
|
|
|
SendMessage(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
|
|
|
|
return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docinfo.ID.String())
|
|
|
|
|
}),
|
|
|
|
|
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-03 17:30:50 +00:00
|
|
|
}
|