Merged in feature/docinitialisation (pull request #41)
Queuing Changes and Cfg Testing * staarting * staarting * startedpush * note * save * mocking * removederrs * fixtests * cleanuperrs * newenvsetup * preppingtests * queue * mmovetocfgpassunittests * sortoutconfig * passinginteg * deps * fixtests
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package docinitrunner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
documentinit "queryorchestration/internal/document/init"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
|
||||
)
|
||||
|
||||
const Name = "docInitRunner"
|
||||
|
||||
type Services struct {
|
||||
Document *documentinit.Service
|
||||
}
|
||||
|
||||
type Runner struct {
|
||||
validator *validator.Validate
|
||||
svc *Services
|
||||
}
|
||||
|
||||
func New(validator *validator.Validate, svc *Services) Runner {
|
||||
return Runner{
|
||||
validator: validator,
|
||||
svc: svc,
|
||||
}
|
||||
}
|
||||
|
||||
func (s Runner) Process(ctx context.Context, req *types.Message) error {
|
||||
var body documentinit.Create
|
||||
err := json.Unmarshal([]byte(*req.Body), &body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.validator.Struct(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = s.svc.Document.Create(ctx, &body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package docinitrunner_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
docinitrunner "queryorchestration/api/docInitRunner"
|
||||
"queryorchestration/internal/client"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
documentinit "queryorchestration/internal/document/init"
|
||||
"queryorchestration/internal/job"
|
||||
"queryorchestration/internal/job/collector"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentclean"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type DocInitConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
documentclean.DocCleanConfig
|
||||
}
|
||||
|
||||
func TestDocInitRunner(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open pgxmock database: %v", err)
|
||||
}
|
||||
|
||||
cfg := &DocInitConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
runner := docinitrunner.New(validator.New(), &docinitrunner.Services{
|
||||
Document: documentinit.New(cfg, &documentinit.Services{
|
||||
Job: job.New(cfg, &job.Services{
|
||||
Collector: collector.New(cfg, &collector.Services{}),
|
||||
Client: client.New(cfg),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
assert.NotNil(t, runner)
|
||||
|
||||
j := job.Job{
|
||||
ID: uuid.New(),
|
||||
ClientID: uuid.New(),
|
||||
}
|
||||
doc := documentinit.Create{
|
||||
JobID: j.ID,
|
||||
Location: "/I/am/here",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(doc)
|
||||
assert.NoError(t, err)
|
||||
body := string(bodyBytes)
|
||||
msg := &types.Message{
|
||||
Body: &body,
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
|
||||
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
|
||||
)
|
||||
pool.ExpectQuery("-- name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
||||
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
|
||||
)
|
||||
pool.ExpectQuery("name: CreateDocument :one").WithArgs(database.MustToDBUUID(doc.JobID), pgxmock.AnyArg(), doc.Location).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(database.MustToDBUUID(uuid.New())),
|
||||
)
|
||||
|
||||
err = runner.Process(ctx, msg)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user