Files
query-orchestration/internal/document/init/create_test.go
T
Michael McGuinness 62886dbba8 Merged in feature/removejob (pull request #95)
Remove Job

* removejob

* rmjob

* sync

* cleanup

* precommit

* startslow

* startslow

* startslow

* openapi

* clean

* test

* scripts

* littlecleanercmds

* mermaid
2025-03-10 11:03:00 +00:00

302 lines
7.7 KiB
Go

package documentinit
import (
"context"
"errors"
"fmt"
"testing"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document"
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 := 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
cfg.DocumentSyncURL = "/i/am/here"
svc := New(cfg)
clientId := uuid.New()
doc := document.Document{
ID: uuid.New(),
ClientID: clientId,
Hash: "example_hash",
}
location := document.Location{
Bucket: "example_bucket",
Key: "/i/am/here",
}
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(doc.Hash, database.MustToDBUUID(doc.ClientID)).WillReturnRows(
pgxmock.NewRows([]string{"id"}),
)
pool.ExpectBegin()
pool.ExpectQuery("name: CreateDocument :one").WithArgs(database.MustToDBUUID(doc.ClientID), doc.Hash).
WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(doc.ID)),
)
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(database.MustToDBUUID(doc.ID), location.Bucket, location.Key).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
pool.ExpectQuery("name: GetDocument :one").WithArgs(database.MustToDBUUID(doc.ID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
AddRow(database.MustToDBUUID(doc.ID), database.MustToDBUUID(doc.ClientID), doc.Hash),
)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(clientId)).WillReturnRows(
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
AddRow(database.MustToDBUUID(clientId), database.MustToDBUUID(clientId), true),
)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(clientId)).WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(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{
ClientID: doc.ClientID,
Location: location,
Hash: doc.Hash,
})
assert.NoError(t, err)
assert.Equal(t, doc.ID, id)
}
func TestGetCreateParams(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &DocInitConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := New(cfg)
doc := document.Document{
ClientID: uuid.New(),
Hash: "example_hash",
}
location := document.Location{
Bucket: "example_bucket",
Key: "/i/am/here",
}
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(doc.Hash, database.MustToDBUUID(doc.ClientID)).WillReturnRows(
pgxmock.NewRows([]string{"id"}),
)
params, err := svc.getCreateParams(ctx, &Create{
ClientID: doc.ClientID,
Location: location,
Hash: doc.Hash,
})
assert.NoError(t, err)
assert.Equal(t, &createDocumentParams{
ClientID: database.MustToDBUUID(doc.ClientID),
Hash: doc.Hash,
Location: location,
}, params)
}
func TestGetCreateParamsExisting(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &DocInitConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := New(cfg)
doc := document.Document{
ID: uuid.New(),
ClientID: uuid.New(),
Hash: "example_hash",
}
location := document.Location{
Bucket: "example_bucket",
Key: "/i/am/here",
}
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(doc.Hash, database.MustToDBUUID(doc.ClientID)).WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(doc.ID)),
)
params, err := svc.getCreateParams(ctx, &Create{
ClientID: doc.ClientID,
Location: location,
Hash: doc.Hash,
})
assert.NoError(t, err)
dbid := database.MustToDBUUID(doc.ID)
assert.Equal(t, &createDocumentParams{
ID: &dbid,
ClientID: database.MustToDBUUID(doc.ClientID),
Hash: doc.Hash,
Location: location,
}, params)
}
func TestGetCreateParamsCurrentDocErr(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &DocInitConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := New(cfg)
doc := document.Document{
ClientID: uuid.New(),
Hash: "example_hash",
}
location := document.Location{
Bucket: "example_bucket",
Key: "/i/am/here",
}
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(doc.Hash, database.MustToDBUUID(doc.ClientID)).
WillReturnError(errors.New("db err"))
_, err = svc.getCreateParams(ctx, &Create{
ClientID: doc.ClientID,
Location: location,
})
assert.Error(t, err)
}
func TestSubmitCreate(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &DocInitConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := New(cfg)
doc := document.Document{
ID: uuid.New(),
ClientID: uuid.New(),
Hash: "example_hash",
}
location := document.Location{
Bucket: "example_bucket",
Key: "/i/am/here",
}
pool.ExpectBegin()
pool.ExpectQuery("name: CreateDocument :one").WithArgs(database.MustToDBUUID(doc.ClientID), doc.Hash).
WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(doc.ID)),
)
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(database.MustToDBUUID(doc.ID), location.Bucket, location.Key).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
id, err := svc.submitCreate(ctx, &createDocumentParams{
ClientID: database.MustToDBUUID(doc.ClientID),
Location: location,
Hash: doc.Hash,
})
assert.NoError(t, err)
assert.Equal(t, doc.ID, id)
}
func TestSubmitCreateExists(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &DocInitConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := New(cfg)
doc := document.Document{
ID: uuid.New(),
ClientID: uuid.New(),
Hash: "example_hash",
}
location := document.Location{
Bucket: "example_bucket",
Key: "/i/am/here",
}
pool.ExpectBegin()
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(database.MustToDBUUID(doc.ID), location.Bucket, location.Key).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
docid := database.MustToDBUUID(doc.ID)
id, err := svc.submitCreate(ctx, &createDocumentParams{
ID: &docid,
ClientID: database.MustToDBUUID(doc.ClientID),
Location: location,
Hash: doc.Hash,
})
assert.NoError(t, err)
assert.Equal(t, doc.ID, id)
}
func TestGetClientIDFromKey(t *testing.T) {
svc := Service{}
_, err := svc.GetClientIDFromKey("")
assert.Error(t, err)
_, err = svc.GetClientIDFromKey("jkhaskhweuifhwhieh")
assert.Error(t, err)
_, err = svc.GetClientIDFromKey("aaa/bbb/ccc")
assert.Error(t, err)
id := uuid.New()
_, err = svc.GetClientIDFromKey(fmt.Sprintf("aaa/%s/bbb", id.String()))
assert.Error(t, err)
aid, err := svc.GetClientIDFromKey(fmt.Sprintf("%s/%s/bbb", id.String(), uuid.NewString()))
assert.NoError(t, err)
assert.Equal(t, id, aid)
aid, err = svc.GetClientIDFromKey(fmt.Sprintf("%s/bbb", id.String()))
assert.NoError(t, err)
assert.Equal(t, id, aid)
}