5c253b3592
Feature/docinit * createunittests * cleanup * skip * cleanup
116 lines
3.4 KiB
Go
116 lines
3.4 KiB
Go
package docinitrunner_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
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/s3"
|
|
"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"
|
|
)
|
|
|
|
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(),
|
|
}
|
|
doc := documentinit.Create{
|
|
JobID: j.ID,
|
|
Bucket: "bucket_name",
|
|
Location: "/I/am/here",
|
|
}
|
|
bodyBytes, err := json.Marshal(doc)
|
|
assert.NoError(t, err)
|
|
body := string(bodyBytes)
|
|
msg := &types.Message{
|
|
Body: &body,
|
|
}
|
|
|
|
docinfo := document.Document{
|
|
ID: uuid.New(),
|
|
Hash: "example_hash",
|
|
}
|
|
|
|
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(doc.JobID)).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}),
|
|
)
|
|
pool.ExpectBegin()
|
|
pool.ExpectQuery("name: CreateDocument :one").WithArgs(database.MustToDBUUID(doc.JobID), docinfo.Hash).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(database.MustToDBUUID(docinfo.ID)),
|
|
)
|
|
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(database.MustToDBUUID(docinfo.ID), doc.Bucket, doc.Location).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
mockStore.EXPECT().
|
|
HeadObject(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *s3.HeadObjectInput) bool {
|
|
return *in.Bucket == doc.Bucket && *in.Key == doc.Location
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&s3.HeadObjectOutput{
|
|
ETag: &docinfo.Hash,
|
|
}, nil)
|
|
|
|
err = runner.Process(ctx, msg)
|
|
assert.NoError(t, err)
|
|
}
|