7ce7c9df4d
Feature/ecr * nosave * repo * awscli * unzip * ignore * moreram * 14k * ref * deployment * 12k * uselocal * go * dockercomd * reorder * iamgename * installs * tart * cli * clideps * y * dockerce * nodock * multi * rmecr * dev
295 lines
6.9 KiB
Go
295 lines
6.9 KiB
Go
package documentinit
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/document"
|
|
"queryorchestration/internal/serviceconfig/objectstore"
|
|
queuemock "queryorchestration/mocks/queue"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
|
"github.com/google/uuid"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
func TestCreate(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
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
|
|
cfg.DocumentSyncURL = "/i/am/here"
|
|
|
|
svc := New(cfg)
|
|
|
|
clientId := "hi"
|
|
doc := document.DocumentSummary{
|
|
ID: uuid.New(),
|
|
ClientID: clientId,
|
|
Hash: "example_hash",
|
|
}
|
|
bucket := "example_bucket"
|
|
key := objectstore.BucketKey{
|
|
ClientID: doc.ClientID,
|
|
CreatedAt: time.Now().UTC(),
|
|
EntityID: uuid.New(),
|
|
Location: objectstore.Import,
|
|
}
|
|
|
|
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(doc.Hash, doc.ClientID).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}),
|
|
)
|
|
pool.ExpectBegin()
|
|
pool.ExpectQuery("name: CreateDocument :one").WithArgs(doc.ClientID, doc.Hash).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(doc.ID),
|
|
)
|
|
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(doc.ID, bucket, key.String()).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(doc.ID).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
|
|
AddRow(doc.ID, doc.ClientID, doc.Hash),
|
|
)
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(clientId).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
|
|
AddRow(clientId, clientId, true),
|
|
)
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(clientId).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
|
AddRow(clientId, "client_name", true),
|
|
)
|
|
|
|
mockSQS.EXPECT().
|
|
SendMessage(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
|
return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.ID.String())
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
|
|
|
id, err := svc.Create(ctx, &Create{
|
|
Hash: doc.Hash,
|
|
Key: key,
|
|
Bucket: bucket,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, doc.ID, id)
|
|
}
|
|
|
|
func TestGetCreateParams(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
cfg := &DocInitConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
svc := New(cfg)
|
|
|
|
doc := document.DocumentSummary{
|
|
ClientID: "hello",
|
|
Hash: "example_hash",
|
|
}
|
|
bucket := "example_bucket"
|
|
key := objectstore.BucketKey{
|
|
ClientID: doc.ClientID,
|
|
CreatedAt: time.Now().UTC(),
|
|
EntityID: uuid.New(),
|
|
Location: objectstore.Import,
|
|
}
|
|
|
|
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(doc.Hash, doc.ClientID).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}),
|
|
)
|
|
|
|
params, err := svc.getCreateParams(ctx, &Create{
|
|
Hash: doc.Hash,
|
|
Bucket: bucket,
|
|
Key: key,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &createDocumentParams{
|
|
Hash: doc.Hash,
|
|
Bucket: bucket,
|
|
Key: key,
|
|
}, params)
|
|
}
|
|
|
|
func TestGetCreateParamsExisting(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
cfg := &DocInitConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
svc := New(cfg)
|
|
|
|
doc := document.DocumentSummary{
|
|
ID: uuid.New(),
|
|
ClientID: "hello",
|
|
Hash: "example_hash",
|
|
}
|
|
bucket := "example_bucket"
|
|
key := objectstore.BucketKey{
|
|
ClientID: doc.ClientID,
|
|
CreatedAt: time.Now().UTC(),
|
|
EntityID: uuid.New(),
|
|
Location: objectstore.Import,
|
|
}
|
|
|
|
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(doc.Hash, doc.ClientID).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(doc.ID),
|
|
)
|
|
|
|
params, err := svc.getCreateParams(ctx, &Create{
|
|
Bucket: bucket,
|
|
Key: key,
|
|
Hash: doc.Hash,
|
|
})
|
|
require.NoError(t, err)
|
|
dbid := doc.ID
|
|
assert.Equal(t, &createDocumentParams{
|
|
ID: &dbid,
|
|
Hash: doc.Hash,
|
|
Bucket: bucket,
|
|
Key: key,
|
|
}, params)
|
|
}
|
|
|
|
func TestGetCreateParamsCurrentDocErr(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
cfg := &DocInitConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
svc := New(cfg)
|
|
|
|
doc := document.DocumentSummary{
|
|
ClientID: "hello",
|
|
Hash: "example_hash",
|
|
}
|
|
bucket := "example_bucket"
|
|
key := objectstore.BucketKey{
|
|
ClientID: doc.ClientID,
|
|
CreatedAt: time.Now().UTC(),
|
|
EntityID: uuid.New(),
|
|
Location: objectstore.Import,
|
|
}
|
|
|
|
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(doc.Hash, doc.ClientID).
|
|
WillReturnError(errors.New("db err"))
|
|
|
|
_, err = svc.getCreateParams(ctx, &Create{
|
|
Bucket: bucket,
|
|
Key: key,
|
|
})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestSubmitCreate(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
cfg := &DocInitConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
svc := New(cfg)
|
|
|
|
doc := document.DocumentSummary{
|
|
ID: uuid.New(),
|
|
ClientID: "hello",
|
|
Hash: "example_hash",
|
|
}
|
|
bucket := "example_bucket"
|
|
key := objectstore.BucketKey{
|
|
ClientID: doc.ClientID,
|
|
CreatedAt: time.Now().UTC(),
|
|
EntityID: uuid.New(),
|
|
Location: objectstore.Import,
|
|
}
|
|
|
|
pool.ExpectBegin()
|
|
pool.ExpectQuery("name: CreateDocument :one").WithArgs(doc.ClientID, doc.Hash).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(doc.ID),
|
|
)
|
|
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(doc.ID, bucket, key.String()).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
id, err := svc.submitCreate(ctx, &createDocumentParams{
|
|
Hash: doc.Hash,
|
|
Bucket: bucket,
|
|
Key: key,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, doc.ID, id)
|
|
}
|
|
|
|
func TestSubmitCreateExists(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
cfg := &DocInitConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
svc := New(cfg)
|
|
|
|
doc := document.DocumentSummary{
|
|
ID: uuid.New(),
|
|
ClientID: "hello",
|
|
Hash: "example_hash",
|
|
}
|
|
bucket := "example_bucket"
|
|
key := objectstore.BucketKey{
|
|
ClientID: doc.ClientID,
|
|
CreatedAt: time.Now().UTC(),
|
|
EntityID: uuid.New(),
|
|
Location: objectstore.Import,
|
|
}
|
|
|
|
pool.ExpectBegin()
|
|
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(doc.ID, bucket, key.String()).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
docid := doc.ID
|
|
id, err := svc.submitCreate(ctx, &createDocumentParams{
|
|
ID: &docid,
|
|
Hash: doc.Hash,
|
|
Bucket: bucket,
|
|
Key: key,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, doc.ID, id)
|
|
}
|