Merged in bugfix/errhandling (pull request #89)

Error handling

* basic
This commit is contained in:
Michael McGuinness
2025-03-05 19:49:03 +00:00
parent c78ca26a6f
commit 8f409e60a8
36 changed files with 965 additions and 641 deletions
+178 -60
View File
@@ -1,4 +1,4 @@
package docinitrunner_test
package docinitrunner
import (
"context"
@@ -6,7 +6,6 @@ import (
"fmt"
"testing"
docinitrunner "queryorchestration/api/docInitRunner"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document"
@@ -43,73 +42,192 @@ func TestDocInitRunner(t *testing.T) {
mockSQS := queuemock.NewMockSQSClient(t)
cfg.QueueClient = mockSQS
runner := docinitrunner.New(validator.New(), &docinitrunner.Services{
runner := New(validator.New(), &Services{
Document: documentinit.New(cfg),
})
assert.NotNil(t, runner)
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 := docinitrunner.S3EventNotification{
Records: []docinitrunner.S3EventRecord{
{
EventName: "ObjectCreated:Put",
S3: docinitrunner.S3EventRecordDetails{
Bucket: docinitrunner.S3Bucket{
Name: bucketName,
},
Object: docinitrunner.S3Object{
Key: location,
ETag: docinfo.Hash,
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,
}
}
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"),
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)
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.Process(ctx, msg)
assert.NoError(t, err)
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"))
})
}