Files
query-orchestration/api/docInitRunner/runner_test.go
T
Michael McGuinness 8f409e60a8 Merged in bugfix/errhandling (pull request #89)
Error handling

* basic
2025-03-05 19:49:03 +00:00

234 lines
6.2 KiB
Go

package docinitrunner
import (
"context"
"encoding/json"
"fmt"
"testing"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document"
documentinit "queryorchestration/internal/document/init"
"queryorchestration/internal/job"
"queryorchestration/internal/serviceconfig"
"queryorchestration/internal/serviceconfig/queue/documentsync"
queuemock "queryorchestration/mocks/queue"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"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"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type DocInitConfig struct {
serviceconfig.BaseConfig
documentsync.DocSyncConfig
}
func TestDocInitRunner(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &DocInitConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
mockSQS := queuemock.NewMockSQSClient(t)
cfg.QueueClient = mockSQS
runner := New(validator.New(), &Services{
Document: documentinit.New(cfg),
})
assert.NotNil(t, runner)
t.Run("valid", func(t *testing.T) {
j := job.Job{
ID: uuid.New(),
ClientID: uuid.New(),
}
bucketName := "bucketName"
location := fmt.Sprintf("%s/%s/aaa", j.ClientID.String(), j.ID.String())
docinfo := document.Document{
ID: uuid.New(),
JobID: j.ID,
Hash: "example_hash",
}
doc := S3EventNotification{
Records: []S3EventRecord{
{
EventName: "ObjectCreated:Put",
S3: S3EventRecordDetails{
Bucket: S3Bucket{
Name: bucketName,
},
Object: S3Object{
Key: location,
ETag: docinfo.Hash,
},
},
},
},
}
bodyBytes, err := json.Marshal(doc)
assert.NoError(t, err)
body := string(bodyBytes)
msg := &types.Message{
Body: &body,
}
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, database.MustToDBUUID(j.ID)).WillReturnRows(
pgxmock.NewRows([]string{"id"}),
)
pool.ExpectBegin()
pool.ExpectQuery("name: CreateDocument :one").WithArgs(database.MustToDBUUID(j.ID), docinfo.Hash).
WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(docinfo.ID)),
)
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(database.MustToDBUUID(docinfo.ID), bucketName, location).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
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)
assert.True(t, runner.Process(ctx, msg))
})
t.Run("invalid body", func(t *testing.T) {
body := "definitely invalid"
msgId := "id"
msg := &types.Message{
Body: &body,
MessageId: &msgId,
}
assert.True(t, runner.Process(ctx, msg))
})
}
func TestProcessRecord(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &DocInitConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
mockSQS := queuemock.NewMockSQSClient(t)
cfg.QueueClient = mockSQS
runner := New(validator.New(), &Services{
Document: documentinit.New(cfg),
})
assert.NotNil(t, runner)
t.Run("valid", func(t *testing.T) {
j := job.Job{
ID: uuid.New(),
ClientID: uuid.New(),
}
bucketName := "bucketName"
location := fmt.Sprintf("%s/%s/aaa", j.ClientID.String(), j.ID.String())
docinfo := document.Document{
ID: uuid.New(),
JobID: j.ID,
Hash: "example_hash",
}
record := S3EventRecord{
EventName: S3EventObjectCreatedPut,
S3: S3EventRecordDetails{
Bucket: S3Bucket{
Name: bucketName,
},
Object: S3Object{
Key: location,
ETag: docinfo.Hash,
},
},
}
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, database.MustToDBUUID(j.ID)).WillReturnRows(
pgxmock.NewRows([]string{"id"}),
)
pool.ExpectBegin()
pool.ExpectQuery("name: CreateDocument :one").WithArgs(database.MustToDBUUID(j.ID), docinfo.Hash).
WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(docinfo.ID)),
)
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(database.MustToDBUUID(docinfo.ID), bucketName, location).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
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)
err = runner.processRecord(ctx, record)
assert.NoError(t, err)
})
t.Run("invalid_event", func(t *testing.T) {
record := S3EventRecord{
EventName: "invalid_event",
}
err = runner.processRecord(ctx, record)
assert.NoError(t, err)
})
t.Run("invalid id", func(t *testing.T) {
location := fmt.Sprintf("%s/cc/aaa", uuid.New())
record := S3EventRecord{
EventName: S3EventObjectCreatedPut,
S3: S3EventRecordDetails{
Object: S3Object{
Key: location,
},
},
}
err = runner.processRecord(ctx, record)
assert.NoError(t, err)
})
}
func TestIsSupportedEvent(t *testing.T) {
runner := Runner{}
t.Run("put", func(t *testing.T) {
assert.True(t, runner.isSupportedEvent(S3EventObjectCreatedPut))
})
t.Run("invalid", func(t *testing.T) {
assert.False(t, runner.isSupportedEvent("invalid"))
})
}