Files
query-orchestration/api/docInitRunner/runner_test.go
T

115 lines
3.3 KiB
Go
Raw Normal View History

package docinitrunner_test
import (
"context"
"encoding/json"
"fmt"
docinitrunner "queryorchestration/api/docInitRunner"
"queryorchestration/internal/client"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document"
documentinit "queryorchestration/internal/document/init"
"queryorchestration/internal/job"
"queryorchestration/internal/job/collector"
"queryorchestration/internal/serviceconfig"
"queryorchestration/internal/serviceconfig/objectstore"
"queryorchestration/internal/serviceconfig/queue/documentclean"
objectstoremock "queryorchestration/mocks/objectstore"
"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
objectstore.ObjectStoreConfig
}
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)
mockStore := objectstoremock.NewMockS3Client(t)
cfg.StoreClient = mockStore
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(),
}
bucketName := "bucketName"
location := fmt.Sprintf("aaa/%s/aaa", j.ID.String())
docinfo := document.Document{
ID: uuid.New(),
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,
},
},
},
},
}
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: 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()
err = runner.Process(ctx, msg)
assert.NoError(t, err)
}