2025-02-03 17:30:50 +00:00
|
|
|
package docinitrunner_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2025-02-07 12:12:51 +00:00
|
|
|
"fmt"
|
2025-02-03 17:30:50 +00:00
|
|
|
docinitrunner "queryorchestration/api/docInitRunner"
|
|
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
"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"
|
|
|
|
|
"queryorchestration/internal/job"
|
|
|
|
|
"queryorchestration/internal/serviceconfig"
|
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
|
|
|
"testing"
|
|
|
|
|
|
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/aws/aws-sdk-go-v2/service/sqs/types"
|
|
|
|
|
"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 {
|
|
|
|
|
serviceconfig.BaseConfig
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
j := job.Job{
|
|
|
|
|
ID: uuid.New(),
|
|
|
|
|
ClientID: uuid.New(),
|
|
|
|
|
}
|
2025-02-07 12:12:51 +00:00
|
|
|
bucketName := "bucketName"
|
2025-02-12 19:00:25 +00:00
|
|
|
location := fmt.Sprintf("%s/%s/aaa", j.ClientID.String(), j.ID.String())
|
2025-02-07 12:12:51 +00:00
|
|
|
docinfo := document.Document{
|
2025-02-12 19:00:25 +00:00
|
|
|
ID: uuid.New(),
|
|
|
|
|
JobID: j.ID,
|
|
|
|
|
Hash: "example_hash",
|
2025-02-07 12:12:51 +00:00
|
|
|
}
|
|
|
|
|
doc := docinitrunner.S3EventNotification{
|
|
|
|
|
Records: []docinitrunner.S3EventRecord{
|
|
|
|
|
{
|
|
|
|
|
EventName: "ObjectCreated:Put",
|
|
|
|
|
S3: docinitrunner.S3EventRecordDetails{
|
|
|
|
|
Bucket: docinitrunner.S3Bucket{
|
|
|
|
|
Name: bucketName,
|
|
|
|
|
},
|
|
|
|
|
Object: docinitrunner.S3Object{
|
|
|
|
|
Key: location,
|
|
|
|
|
ETag: docinfo.Hash,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-02-03 17:30:50 +00:00
|
|
|
}
|
|
|
|
|
bodyBytes, err := json.Marshal(doc)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
body := string(bodyBytes)
|
|
|
|
|
msg := &types.Message{
|
|
|
|
|
Body: &body,
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 12:12:51 +00:00
|
|
|
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, database.MustToDBUUID(j.ID)).WillReturnRows(
|
2025-02-05 17:44:01 +00:00
|
|
|
pgxmock.NewRows([]string{"id"}),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectBegin()
|
2025-02-07 12:12:51 +00:00
|
|
|
pool.ExpectQuery("name: CreateDocument :one").WithArgs(database.MustToDBUUID(j.ID), docinfo.Hash).
|
2025-02-03 17:30:50 +00:00
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id"}).
|
2025-02-05 17:44:01 +00:00
|
|
|
AddRow(database.MustToDBUUID(docinfo.ID)),
|
2025-02-03 17:30:50 +00:00
|
|
|
)
|
2025-02-07 12:12:51 +00:00
|
|
|
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(database.MustToDBUUID(docinfo.ID), bucketName, location).
|
2025-02-05 17:44:01 +00:00
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
|
pool.ExpectCommit()
|
2025-02-12 19:00:25 +00:00
|
|
|
pool.ExpectQuery("name: GetDocument :one").WithArgs(database.MustToDBUUID(docinfo.ID)).
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "jobId", "hash"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(docinfo.ID), database.MustToDBUUID(docinfo.JobID), "example"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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-02-05 17:44:01 +00:00
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
err = runner.Process(ctx, msg)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
}
|