Merged in feature/checkbox (pull request #145)
Checkbox Primary Edge Cases * hascheckboxlogic * gen * preppinggen * exploringcheckbox * removeunselected * tests * failingtest * failintests * nomockqueryapi * db * name * nocontainer * deleterow * cnc * extradocsandupdatetextracts * moretables * appndedtables * baseversion
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package clientsyncrunner_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
@@ -10,11 +9,10 @@ import (
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/server/runner"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentsync"
|
||||
"queryorchestration/internal/test"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
|
||||
"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"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -26,14 +24,11 @@ type ClientSyncConfig struct {
|
||||
}
|
||||
|
||||
func TestQueryRunner(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Parallel()
|
||||
cfg := &ClientSyncConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
cfg.DocumentSyncURL = "/i/am/here"
|
||||
@@ -44,29 +39,30 @@ func TestQueryRunner(t *testing.T) {
|
||||
ClientSync: svc,
|
||||
})
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
bod := clientsyncrunner.Body{
|
||||
ClientID: "hello",
|
||||
}
|
||||
|
||||
docId := uuid.New()
|
||||
|
||||
total := int64(1)
|
||||
pool.ExpectQuery("name: ListDocumentIDsBatch :many").WithArgs(bod.ClientID, int32(100), int32(0)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "totalCount"}).
|
||||
AddRow(&docId, &total),
|
||||
)
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docId.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
assert.True(t, runner.Process(ctx, bod))
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: "client_id",
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
||||
Clientid: "client_id",
|
||||
Hash: "hash",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
bod := clientsyncrunner.Body{
|
||||
ClientID: "client_id",
|
||||
}
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docId.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
assert.True(t, runner.Process(t.Context(), bod))
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package doccleanrunner_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
@@ -10,18 +9,17 @@ import (
|
||||
|
||||
doccleanrunner "queryorchestration/api/docCleanRunner"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
documentclean "queryorchestration/internal/document/clean"
|
||||
"queryorchestration/internal/server/runner"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/documenttexttrigger"
|
||||
"queryorchestration/internal/test"
|
||||
objectstoremock "queryorchestration/mocks/objectstore"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"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"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -34,14 +32,11 @@ type DocCleanConfig struct {
|
||||
}
|
||||
|
||||
func TestDocCleanRunner(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Parallel()
|
||||
cfg := &DocCleanConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
mockStore := objectstoremock.NewMockS3Client(t)
|
||||
cfg.StoreClient = mockStore
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
@@ -52,96 +47,73 @@ func TestDocCleanRunner(t *testing.T) {
|
||||
Clean: documentclean.New(cfg),
|
||||
})
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
bod := doccleanrunner.Body{
|
||||
DocumentID: uuid.New(),
|
||||
}
|
||||
|
||||
doc := document.DocumentSummary{
|
||||
ID: bod.DocumentID,
|
||||
ClientID: "hello",
|
||||
Hash: "example_hash",
|
||||
}
|
||||
bucket := "hello"
|
||||
key := objectstore.BucketKey{
|
||||
CreatedAt: time.Now().UTC(),
|
||||
ClientID: "hi",
|
||||
EntityID: uuid.New(),
|
||||
Location: objectstore.Import,
|
||||
}
|
||||
keyStr := key.String()
|
||||
dbid := doc.ID
|
||||
|
||||
pool.ExpectQuery("name: HasDocumentCleanEntry :one").WithArgs(dbid).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"isclean"}).
|
||||
AddRow(false),
|
||||
)
|
||||
pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(dbid).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
|
||||
AddRow(dbid, doc.ClientID, doc.Hash),
|
||||
)
|
||||
pool.ExpectQuery("name: GetDocumentEntry :one").WithArgs(dbid).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"documentId", "bucket", "key"}).
|
||||
AddRow(dbid, bucket, keyStr),
|
||||
)
|
||||
mimeType := "application/pdf"
|
||||
dbmimetype := repository.NullCleanmimetype{
|
||||
Valid: true,
|
||||
Cleanmimetype: repository.Cleanmimetype(mimeType),
|
||||
}
|
||||
pool.ExpectBegin()
|
||||
cleanid := uuid.New()
|
||||
pool.ExpectQuery("name: GetMostRecentDocumentCleanEntry :one").WithArgs(dbid).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "hash", "mimetype", "fail"}),
|
||||
)
|
||||
pool.ExpectQuery("name: AddDocumentClean :one").WithArgs(dbid, &bucket, &keyStr, &doc.Hash, dbmimetype, repository.NullCleanfailtype{}).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(cleanid),
|
||||
)
|
||||
pool.ExpectExec("name: AddDocumentCleanEntry :exec").WithArgs(cleanid, pgxmock.AnyArg()).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentTextTriggerURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.ID.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
mockStore.EXPECT().
|
||||
HeadObject(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *s3.HeadObjectInput) bool {
|
||||
return *in.Bucket == bucket && *in.Key == keyStr
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&s3.HeadObjectOutput{
|
||||
ContentType: &mimeType,
|
||||
}, nil)
|
||||
|
||||
bodyMsg := io.NopCloser(strings.NewReader(pdfHelloWorld))
|
||||
mockStore.EXPECT().
|
||||
GetObject(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *s3.GetObjectInput) bool {
|
||||
return *in.Bucket == bucket && *in.Key == keyStr
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&s3.GetObjectOutput{
|
||||
Body: bodyMsg,
|
||||
}, nil)
|
||||
|
||||
assert.True(t, runner.Process(ctx, bod))
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: "clientid",
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
||||
Clientid: "clientid",
|
||||
Hash: "hash",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
part := uint16(1)
|
||||
filetype := "pdf"
|
||||
key := objectstore.BucketKey{
|
||||
CreatedAt: time.Now().UTC(),
|
||||
ClientID: "clientid",
|
||||
EntityID: uuid.New(),
|
||||
Location: objectstore.Import,
|
||||
Part: &part,
|
||||
FileType: &filetype,
|
||||
}
|
||||
err = cfg.GetDBQueries().AddDocumentEntry(t.Context(), &repository.AddDocumentEntryParams{
|
||||
Documentid: docId,
|
||||
Bucket: "bucket",
|
||||
Key: key.String(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentTextTriggerURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docId.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
mimeType := "application/pdf"
|
||||
mockStore.EXPECT().
|
||||
HeadObject(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *s3.HeadObjectInput) bool {
|
||||
return *in.Bucket == "bucket" && *in.Key == key.String()
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&s3.HeadObjectOutput{
|
||||
ContentType: &mimeType,
|
||||
}, nil)
|
||||
|
||||
bodyMsg := io.NopCloser(strings.NewReader(pdfHelloWorld))
|
||||
mockStore.EXPECT().
|
||||
GetObject(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *s3.GetObjectInput) bool {
|
||||
return *in.Bucket == "bucket" && *in.Key == key.String()
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&s3.GetObjectOutput{
|
||||
Body: bodyMsg,
|
||||
}, nil)
|
||||
|
||||
bod := doccleanrunner.Body{
|
||||
DocumentID: docId,
|
||||
}
|
||||
assert.True(t, runner.Process(t.Context(), bod))
|
||||
}
|
||||
|
||||
const pdfHelloWorld = `%PDF-1.4
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
package docinitrunner_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
docinitrunner "queryorchestration/api/docInitRunner"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
documentinit "queryorchestration/internal/document/init"
|
||||
"queryorchestration/internal/server/runner"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentsync"
|
||||
"queryorchestration/internal/test"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
|
||||
"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"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -29,14 +25,11 @@ type DocInitConfig struct {
|
||||
}
|
||||
|
||||
func TestDocInitRunner(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Parallel()
|
||||
cfg := &DocInitConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
|
||||
@@ -44,53 +37,32 @@ func TestDocInitRunner(t *testing.T) {
|
||||
Document: documentinit.New(cfg),
|
||||
})
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
clientId := "clientid"
|
||||
bucketName := "bucketName"
|
||||
location := objectstore.BucketKey{
|
||||
Location: objectstore.Import,
|
||||
ClientID: clientId,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
docinfo := document.DocumentSummary{
|
||||
ID: uuid.New(),
|
||||
ClientID: "hello",
|
||||
Hash: "example_hash",
|
||||
}
|
||||
doc := docinitrunner.Body{
|
||||
Bucket: bucketName,
|
||||
Key: location.String(),
|
||||
Hash: docinfo.Hash,
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, clientId).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}),
|
||||
)
|
||||
pool.ExpectBegin()
|
||||
pool.ExpectQuery("name: CreateDocument :one").WithArgs(clientId, docinfo.Hash).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(docinfo.ID),
|
||||
)
|
||||
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(docinfo.ID, bucketName, location.String()).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(docinfo.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
|
||||
AddRow(docinfo.ID, docinfo.ClientID, "example"),
|
||||
)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docinfo.ID.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: "clientid",
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
location := objectstore.BucketKey{
|
||||
Location: objectstore.Import,
|
||||
ClientID: "clientid",
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
doc := docinitrunner.Body{
|
||||
Bucket: "bucket",
|
||||
Key: location.String(),
|
||||
Hash: "hash",
|
||||
}
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentSyncURL
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
assert.True(t, runner.Process(t.Context(), doc))
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
docsyncrunner "queryorchestration/api/docSyncRunner"
|
||||
"queryorchestration/internal/client"
|
||||
@@ -11,12 +12,13 @@ import (
|
||||
"queryorchestration/internal/document"
|
||||
documentsync "queryorchestration/internal/document/sync"
|
||||
"queryorchestration/internal/server/runner"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentclean"
|
||||
"queryorchestration/internal/test"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
|
||||
"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"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -27,15 +29,13 @@ type DocSyncConfig struct {
|
||||
documentclean.DocCleanConfig
|
||||
}
|
||||
|
||||
func TestDocInitRunner(t *testing.T) {
|
||||
func TestDocSyncRunner(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := &DocSyncConfig{}
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg := &DocSyncConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
|
||||
@@ -46,39 +46,51 @@ func TestDocInitRunner(t *testing.T) {
|
||||
}),
|
||||
})
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
j := client.Client{
|
||||
ID: "hello",
|
||||
}
|
||||
docinfo := document.DocumentSummary{
|
||||
ID: uuid.New(),
|
||||
ClientID: j.ID,
|
||||
}
|
||||
doc := docsyncrunner.Body{
|
||||
DocumentID: docinfo.ID,
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(docinfo.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
|
||||
AddRow(docinfo.ID, docinfo.ClientID, "example"),
|
||||
)
|
||||
pool.ExpectQuery("name: GetClient :one").WithArgs(j.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
||||
AddRow(j.ID, "client_name", true),
|
||||
)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentCleanURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.DocumentID)
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: "clientid",
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = cfg.GetDBQueries().AddClientCanSync(t.Context(), &repository.AddClientCanSyncParams{
|
||||
Clientid: "clientid",
|
||||
Cansync: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
||||
Clientid: "clientid",
|
||||
Hash: "hash",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
part := uint16(1)
|
||||
filetype := "pdf"
|
||||
key := objectstore.BucketKey{
|
||||
CreatedAt: time.Now().UTC(),
|
||||
ClientID: "clientid",
|
||||
EntityID: uuid.New(),
|
||||
Location: objectstore.Import,
|
||||
Part: &part,
|
||||
FileType: &filetype,
|
||||
}
|
||||
err = cfg.GetDBQueries().AddDocumentEntry(t.Context(), &repository.AddDocumentEntryParams{
|
||||
Documentid: docId,
|
||||
Bucket: "bucket",
|
||||
Key: key.String(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentCleanURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docId)
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
doc := docsyncrunner.Body{
|
||||
DocumentID: docId,
|
||||
}
|
||||
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package doctextrunner_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
@@ -25,7 +24,6 @@ import (
|
||||
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
||||
awstextract "github.com/aws/aws-sdk-go-v2/service/textract"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -39,14 +37,12 @@ type DocTextConfig struct {
|
||||
objectstore.ObjectStoreConfig
|
||||
}
|
||||
|
||||
func TestDocCleanRunner(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
|
||||
func TestDocTextRunner(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := &DocTextConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
mockStore := objectstoremock.NewMockS3Client(t)
|
||||
cfg.StoreClient = mockStore
|
||||
mockTextract := textractmock.NewMockTextractClient(t)
|
||||
@@ -58,84 +54,99 @@ func TestDocCleanRunner(t *testing.T) {
|
||||
runner := doctextrunner.New(&doctextrunner.Services{
|
||||
Text: documenttext.New(cfg),
|
||||
})
|
||||
doc := doctextrunner.Body{
|
||||
DocumentID: uuid.New(),
|
||||
}
|
||||
|
||||
cleanId := uuid.New()
|
||||
bucket := "bucket"
|
||||
hash := `"a06d5e0e795adeb6d0b3732330dbcc15"`
|
||||
textId := uuid.New()
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: "clientid",
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
||||
Clientid: "clientid",
|
||||
Hash: "hash",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
part := uint16(1)
|
||||
filetype := "pdf"
|
||||
key := objectstore.BucketKey{
|
||||
ClientID: "aa",
|
||||
Location: objectstore.Import,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
EntityID: cleanId,
|
||||
ClientID: "clientid",
|
||||
EntityID: uuid.New(),
|
||||
Location: objectstore.Import,
|
||||
Part: &part,
|
||||
FileType: &filetype,
|
||||
}
|
||||
err = cfg.GetDBQueries().AddDocumentEntry(t.Context(), &repository.AddDocumentEntryParams{
|
||||
Documentid: docId,
|
||||
Bucket: "bucket",
|
||||
Key: key.String(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
bucket := "bucket"
|
||||
hash := "hash"
|
||||
keyStr := key.String()
|
||||
filename := "../../assets/textract/helloWorld.gen"
|
||||
textractBaseOut := test.GetTextractFileResponse(t, filename)
|
||||
cleanId, err := cfg.GetDBQueries().AddDocumentClean(t.Context(), &repository.AddDocumentCleanParams{
|
||||
Documentid: docId,
|
||||
Bucket: &bucket,
|
||||
Hash: &hash,
|
||||
Key: &keyStr,
|
||||
Mimetype: repository.NullCleanmimetype{
|
||||
Valid: true,
|
||||
Cleanmimetype: repository.CleanmimetypeApplicationPdf,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = cfg.GetDBQueries().AddDocumentCleanEntry(t.Context(), &repository.AddDocumentCleanEntryParams{
|
||||
Cleanid: cleanId,
|
||||
Version: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
mimetype := repository.NullCleanmimetype{
|
||||
Valid: true,
|
||||
Cleanmimetype: repository.CleanmimetypeApplicationPdf,
|
||||
}
|
||||
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(doc.DocumentID).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"isextracted"}).
|
||||
AddRow(false),
|
||||
)
|
||||
pool.ExpectQuery("name: GetCleanEntryByDocId :one").WithArgs(doc.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "hash", "mimetype", "fail", "clientId"}).
|
||||
AddRow(cleanId, doc.DocumentID, &bucket, &keyStr, int64(123), &hash, mimetype, nil, key.ClientID),
|
||||
)
|
||||
bodyMsg := io.NopCloser(strings.NewReader(pdfHelloWorld))
|
||||
mockStore.EXPECT().
|
||||
GetObject(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *s3.GetObjectInput) bool {
|
||||
return *in.Bucket == bucket && *in.Key == keyStr
|
||||
return *in.Bucket == "bucket" && *in.Key == key.String()
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&s3.GetObjectOutput{
|
||||
Body: bodyMsg,
|
||||
}, nil)
|
||||
sent := 0
|
||||
filename := "../../assets/textract/helloWorld.gen"
|
||||
textractBaseOut := test.GetTextractFileResponse(t, filename)
|
||||
mockTextract.EXPECT().
|
||||
AnalyzeDocument(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *awstextract.AnalyzeDocumentInput) bool {
|
||||
if sent == 0 {
|
||||
sent++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return true
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(textractBaseOut[0]["base"], nil).Once()
|
||||
|
||||
pool.ExpectBegin()
|
||||
pool.ExpectQuery("name: GetDocumentTextExtractionByHash :one").WithArgs(cleanId, hash).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(textId),
|
||||
)
|
||||
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(textId, pgxmock.AnyArg()).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
mockStore.EXPECT().
|
||||
PutObject(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *s3.PutObjectInput) bool {
|
||||
return *in.Bucket == "bucket"
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&s3.PutObjectOutput{}, nil)
|
||||
mockQueue.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.QuerySyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.DocumentID.String())
|
||||
return *in.QueueUrl == cfg.QuerySyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docId.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
doc := doctextrunner.Body{
|
||||
DocumentID: docId,
|
||||
}
|
||||
assert.True(t, runner.Process(t.Context(), doc))
|
||||
}
|
||||
|
||||
const pdfHelloWorld = `%PDF-1.4
|
||||
|
||||
+76
-67
@@ -2,7 +2,6 @@ package queryapi_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -14,25 +13,23 @@ import (
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/clientsync"
|
||||
"queryorchestration/internal/test"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type ClientConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
clientsync.ConfigProvider
|
||||
clientsync.ClientSyncConfig
|
||||
}
|
||||
|
||||
func TestCreateClient(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
Client: client.New(cfg),
|
||||
@@ -42,71 +39,62 @@ func TestCreateClient(t *testing.T) {
|
||||
Name: "example_name",
|
||||
Id: "external_id",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(body)
|
||||
require.NoError(t, err)
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
|
||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
ctx, rec := createContextWithBody(t, body)
|
||||
|
||||
pool.ExpectExec("name: CreateClient :exec").WithArgs(body.Id, body.Name).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
|
||||
err = cons.CreateClient(ctx)
|
||||
err := cons.CreateClient(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusCreated, rec.Code)
|
||||
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", body.Id), rec.Body.String())
|
||||
assertBody(t, rec, queryapi.ClientIDBody{
|
||||
Id: body.Id,
|
||||
})
|
||||
|
||||
client, err := cfg.GetDBQueries().GetClient(t.Context(), "external_id")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "example_name", client.Name)
|
||||
assert.Equal(t, "external_id", client.Clientid)
|
||||
}
|
||||
|
||||
func TestGetClient(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
Client: client.New(cfg),
|
||||
})
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
|
||||
id := "client_id"
|
||||
|
||||
ctx.Set("id", id)
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: id,
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = cfg.GetDBQueries().AddClientCanSync(t.Context(), &repository.AddClientCanSyncParams{
|
||||
Clientid: id,
|
||||
Cansync: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
pool.ExpectQuery("name: GetClient :one").WithArgs(id).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
||||
AddRow("client_id", "client_name", true),
|
||||
)
|
||||
ctx, rec := createContext(t)
|
||||
|
||||
err = cons.GetClient(ctx, id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.DocClient
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
require.NoError(t, err)
|
||||
assert.EqualExportedValues(t, queryapi.DocClient{
|
||||
assertBody(t, rec, queryapi.DocClient{
|
||||
Id: id,
|
||||
Name: "client_name",
|
||||
CanSync: true,
|
||||
}, res)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdateClient(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Parallel()
|
||||
cfg := &ClientConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
ClientUpdate: clientupdate.New(cfg, &clientupdate.Services{
|
||||
@@ -114,10 +102,36 @@ func TestUpdateClient(t *testing.T) {
|
||||
}),
|
||||
})
|
||||
|
||||
cs := false
|
||||
id := "client_id"
|
||||
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: id,
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
newClientName := "new_name"
|
||||
body := queryapi.ClientUpdate{
|
||||
CanSync: &cs,
|
||||
Name: &newClientName,
|
||||
}
|
||||
|
||||
ctx, rec := createContextWithBody(t, body)
|
||||
|
||||
err = cons.UpdateClient(ctx, id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Empty(t, rec.Body.String())
|
||||
|
||||
client, err := cfg.GetDBQueries().GetClient(t.Context(), "client_id")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "new_name", client.Name)
|
||||
}
|
||||
|
||||
func createContext(t testing.TB) (echo.Context, *httptest.ResponseRecorder) {
|
||||
return createContextWithBody(t, struct{}{})
|
||||
}
|
||||
|
||||
func createContextWithBody(t testing.TB, body interface{}) (echo.Context, *httptest.ResponseRecorder) {
|
||||
bodyBytes, err := json.Marshal(body)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -125,23 +139,18 @@ func TestUpdateClient(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
|
||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
|
||||
id := "clientid"
|
||||
|
||||
ctx.Set("id", id)
|
||||
|
||||
pool.ExpectQuery("name: GetClient :one").WithArgs(id).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
||||
AddRow("clientid", "client_name", true),
|
||||
)
|
||||
pool.ExpectBegin()
|
||||
pool.ExpectExec("name: AddClientCanSync :exec").WithArgs(*body.CanSync, id).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
err = cons.UpdateClient(ctx, id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Empty(t, rec.Body.String())
|
||||
return e.NewContext(req, rec), rec
|
||||
}
|
||||
|
||||
func assertBody[T any](t testing.TB, rec *httptest.ResponseRecorder, expected T) {
|
||||
res := getBody[T](t, rec)
|
||||
assert.EqualExportedValues(t, expected, res)
|
||||
}
|
||||
|
||||
func getBody[T any](t testing.TB, rec *httptest.ResponseRecorder) T {
|
||||
var res T
|
||||
err := json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
require.NoError(t, err)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package queryapi_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
queryapi "queryorchestration/api/queryAPI"
|
||||
@@ -13,39 +10,36 @@ import (
|
||||
collectorset "queryorchestration/internal/collector/set"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/clientsync"
|
||||
"queryorchestration/internal/test"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSetCollector(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &struct {
|
||||
serviceconfig.BaseConfig
|
||||
clientsync.ClientSyncConfig
|
||||
}{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
t.Parallel()
|
||||
cfg := &ClientConfig{}
|
||||
cfg.ClientSyncURL = "example"
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
|
||||
id := "clientid"
|
||||
current := collector.Collector{
|
||||
ClientID: id,
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 4,
|
||||
}
|
||||
|
||||
av := int32(2)
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: id,
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
queryId, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeContextFull)
|
||||
require.NoError(t, err)
|
||||
|
||||
av := int32(1)
|
||||
cv := int64(1)
|
||||
body := queryapi.CollectorSet{
|
||||
ActiveVersion: &av,
|
||||
@@ -53,18 +47,11 @@ func TestSetCollector(t *testing.T) {
|
||||
Fields: &[]queryapi.CollectorField{
|
||||
{
|
||||
Name: "a",
|
||||
QueryId: uuid.New(),
|
||||
QueryId: queryId,
|
||||
},
|
||||
},
|
||||
}
|
||||
bodyBytes, err := json.Marshal(body)
|
||||
require.NoError(t, err)
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
|
||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
ctx, rec := createContextWithBody(t, body)
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
CollectorSet: collectorset.New(cfg, &collectorset.Services{
|
||||
@@ -72,36 +59,11 @@ func TestSetCollector(t *testing.T) {
|
||||
}),
|
||||
})
|
||||
|
||||
ctx.Set("id", id)
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByClientID :one").WithArgs(current.ClientID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"clientId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(current.ClientID, current.MinCleanVersion, current.MinTextVersion, current.ActiveVersion, current.LatestVersion, []byte("")),
|
||||
)
|
||||
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs([]uuid.UUID{(*body.Fields)[0].QueryId}).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"exist"}).
|
||||
AddRow(true),
|
||||
)
|
||||
pool.ExpectBeginTx(pgx.TxOptions{})
|
||||
pool.ExpectQuery("name: AddLatestCollectorVersion :one").WithArgs(current.ClientID).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"version"}).
|
||||
AddRow(int32(5)),
|
||||
)
|
||||
pool.ExpectExec("name: SetActiveCollectorVersion :exec").WithArgs(current.ClientID, int32(2)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: SetCollectorCleanVersion :exec").WithArgs(current.ClientID, int32(5), *body.MinimumCleanerVersion).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(current.ClientID, "a", (*body.Fields)[0].QueryId, int32(5)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.ClientSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", current.ClientID)
|
||||
return *in.QueueUrl == cfg.GetClientSyncURL() && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", id)
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
@@ -113,50 +75,35 @@ func TestSetCollector(t *testing.T) {
|
||||
assert.Empty(t, rec.Body.String())
|
||||
}
|
||||
|
||||
func TestGetCollectorByclientId(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
func TestGetCollectorByClientId(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
|
||||
svc := collector.New(cfg)
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
Collector: svc,
|
||||
Collector: collector.New(cfg),
|
||||
})
|
||||
|
||||
coll := collector.Collector{
|
||||
ClientID: "hello",
|
||||
MinCleanVersion: 1,
|
||||
MinTextVersion: 2,
|
||||
}
|
||||
|
||||
id := "clientid"
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByClientID :one").WithArgs(id).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"clientId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(coll.ClientID, coll.MinCleanVersion, coll.MinTextVersion, int32(1), int32(2), []byte("")),
|
||||
)
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: id,
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx, rec := createContext(t)
|
||||
|
||||
err = cons.GetCollectorByClientId(ctx, id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.Collector
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
require.NoError(t, err)
|
||||
assert.EqualExportedValues(t, queryapi.Collector{
|
||||
assertBody(t, rec, queryapi.Collector{
|
||||
ClientId: id,
|
||||
MinimumCleanerVersion: coll.MinCleanVersion,
|
||||
MinimumTextVersion: coll.MinTextVersion,
|
||||
ActiveVersion: int32(1),
|
||||
LatestVersion: int32(2),
|
||||
MinimumCleanerVersion: 0,
|
||||
MinimumTextVersion: 0,
|
||||
ActiveVersion: 0,
|
||||
LatestVersion: 0,
|
||||
Fields: []queryapi.CollectorField{},
|
||||
}, res)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
package queryapi_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
queryapi "queryorchestration/api/queryAPI"
|
||||
@@ -12,109 +9,82 @@ import (
|
||||
"queryorchestration/internal/document"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/clientsync"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
"queryorchestration/internal/test"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestListDocumentsByClientId(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
t.Parallel()
|
||||
cfg := &struct {
|
||||
serviceconfig.BaseConfig
|
||||
clientsync.ClientSyncConfig
|
||||
}{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
|
||||
clientId := "clientid"
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
Document: document.New(cfg),
|
||||
})
|
||||
|
||||
ctx.Set("id", clientId)
|
||||
doc := queryapi.ListDocuments{
|
||||
{
|
||||
Id: uuid.New(),
|
||||
Hash: "hash",
|
||||
},
|
||||
}
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: "client_id",
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
||||
Clientid: "client_id",
|
||||
Hash: "hash",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
pool.ExpectQuery("name: ListDocumentsByClient :many").WithArgs(clientId).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "hash"}).
|
||||
AddRow(doc[0].Id, "hash"),
|
||||
)
|
||||
ctx, rec := createContext(t)
|
||||
|
||||
err = cons.ListDocumentsByClientId(ctx, clientId)
|
||||
err = cons.ListDocumentsByClientId(ctx, "client_id")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.ListDocuments
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
require.NoError(t, err)
|
||||
assert.EqualExportedValues(t, doc, res)
|
||||
assertBody(t, rec, queryapi.ListDocuments{
|
||||
{
|
||||
Id: docId,
|
||||
Hash: "hash",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetDocument(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
t.Parallel()
|
||||
cfg := &struct {
|
||||
serviceconfig.BaseConfig
|
||||
clientsync.ClientSyncConfig
|
||||
}{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
|
||||
docId := uuid.New()
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
Document: document.New(cfg),
|
||||
})
|
||||
|
||||
ctx.Set("id", docId)
|
||||
doc := queryapi.Document{
|
||||
Id: docId,
|
||||
ClientId: "externalID",
|
||||
Hash: "example",
|
||||
Fields: map[string]interface{}{
|
||||
"json": "hello",
|
||||
},
|
||||
}
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: "client_id",
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
||||
Clientid: "client_id",
|
||||
Hash: "hash",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
pool.ExpectQuery("name: GetDocumentExternal :one").WithArgs(docId).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "clientId", "hash", "fields"}).
|
||||
AddRow(doc.Id, "externalID", "example", []byte(`{"json": "hello"}`)),
|
||||
)
|
||||
ctx, rec := createContext(t)
|
||||
|
||||
err = cons.GetDocument(ctx, docId)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.Document
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
require.NoError(t, err)
|
||||
assert.EqualExportedValues(t, doc, res)
|
||||
assertBody(t, rec, queryapi.Document{
|
||||
Id: docId,
|
||||
ClientId: "client_id",
|
||||
Hash: "hash",
|
||||
Fields: map[string]interface{}{},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,23 +2,17 @@ package queryapi_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
queryapi "queryorchestration/api/queryAPI"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTriggerExport(t *testing.T) {
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
ctx, rec := createContext(t)
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{})
|
||||
|
||||
@@ -29,10 +23,7 @@ func TestTriggerExport(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExportState(t *testing.T) {
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
ctx, rec := createContext(t)
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{})
|
||||
|
||||
|
||||
+121
-154
@@ -1,12 +1,10 @@
|
||||
package queryapi_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
queryapi "queryorchestration/api/queryAPI"
|
||||
"queryorchestration/internal/collector"
|
||||
@@ -14,29 +12,28 @@ import (
|
||||
"queryorchestration/internal/document"
|
||||
"queryorchestration/internal/query"
|
||||
"queryorchestration/internal/query/result"
|
||||
resultprocessor "queryorchestration/internal/query/result/processor"
|
||||
querytest "queryorchestration/internal/query/test"
|
||||
queryupdate "queryorchestration/internal/query/update"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/queryversionsync"
|
||||
"queryorchestration/internal/test"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestCreateQuery(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
Query: query.New(cfg),
|
||||
@@ -45,125 +42,79 @@ func TestCreateQuery(t *testing.T) {
|
||||
body := queryapi.QueryCreate{
|
||||
Type: queryapi.CONTEXTFULL,
|
||||
}
|
||||
bodyBytes, err := json.Marshal(body)
|
||||
require.NoError(t, err)
|
||||
ctx, rec := createContextWithBody(t, body)
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
|
||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
|
||||
id := uuid.New()
|
||||
|
||||
pool.ExpectBeginTx(pgx.TxOptions{})
|
||||
pool.ExpectQuery("name: CreateQuery :one").WithArgs(repository.QuerytypeContextFull).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(id),
|
||||
)
|
||||
pool.ExpectQuery("name: AddLatestQueryVersion :one").WithArgs(id).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"version"}).
|
||||
AddRow(int32(1)),
|
||||
)
|
||||
pool.ExpectExec("name: AddActiveQueryVersion :exec").WithArgs(id, int32(1)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
err = cons.CreateQuery(ctx)
|
||||
err := cons.CreateQuery(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusCreated, rec.Code)
|
||||
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", id), rec.Body.String())
|
||||
res := getBody[queryapi.IdMessage](t, rec)
|
||||
assert.NotEqual(t, uuid.Nil, res.Id)
|
||||
}
|
||||
|
||||
func TestListQueries(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
Query: query.New(cfg),
|
||||
})
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
ctx, rec := createContext(t)
|
||||
|
||||
id := uuid.New()
|
||||
|
||||
pool.ExpectQuery("name: ListQueries :many").WithArgs().WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
||||
AddRow(id, repository.QuerytypeContextFull, int32(1), int32(2), []byte(""), []uuid.UUID{}),
|
||||
)
|
||||
id, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeContextFull)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = cons.ListQueries(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.ListQueries
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
require.NoError(t, err)
|
||||
assert.ElementsMatch(t, res.Queries, []queryapi.Query{
|
||||
{
|
||||
Id: id,
|
||||
Type: queryapi.CONTEXTFULL,
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 2,
|
||||
assertBody(t, rec, queryapi.ListQueries{
|
||||
Queries: []queryapi.Query{
|
||||
{
|
||||
Id: id,
|
||||
Type: queryapi.CONTEXTFULL,
|
||||
ActiveVersion: 0,
|
||||
LatestVersion: 0,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetQuery(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
Query: query.New(cfg),
|
||||
})
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
ctx, rec := createContext(t)
|
||||
|
||||
id := uuid.New()
|
||||
|
||||
ctx.Set("id", id)
|
||||
|
||||
pool.ExpectQuery("name: GetQuery :one").WithArgs(id).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
||||
AddRow(id, repository.QuerytypeContextFull, int32(1), int32(2), nil, []uuid.UUID{}),
|
||||
)
|
||||
id, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeContextFull)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = cons.GetQuery(ctx, id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.Query
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
require.NoError(t, err)
|
||||
assert.EqualExportedValues(t, queryapi.Query{
|
||||
assertBody(t, rec, queryapi.Query{
|
||||
Id: id,
|
||||
Type: queryapi.CONTEXTFULL,
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 2,
|
||||
}, res)
|
||||
ActiveVersion: 0,
|
||||
LatestVersion: 0,
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdateQuery(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
t.Parallel()
|
||||
cfg := &struct {
|
||||
serviceconfig.BaseConfig
|
||||
queryversionsync.QueryVersionSyncConfig
|
||||
}{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
cfg.QueryVersionSyncURL = "here"
|
||||
@@ -174,32 +125,17 @@ func TestUpdateQuery(t *testing.T) {
|
||||
}),
|
||||
})
|
||||
|
||||
av := int32(2)
|
||||
body := queryapi.QueryUpdate{
|
||||
ActiveVersion: &av,
|
||||
}
|
||||
bodyBytes, err := json.Marshal(body)
|
||||
id, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeJsonExtractor)
|
||||
require.NoError(t, err)
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
|
||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
av := int32(1)
|
||||
c := `{"path": "val"}`
|
||||
body := queryapi.QueryUpdate{
|
||||
ActiveVersion: &av,
|
||||
Config: &c,
|
||||
}
|
||||
ctx, rec := createContextWithBody(t, body)
|
||||
|
||||
id := uuid.New()
|
||||
|
||||
ctx.Set("id", id)
|
||||
|
||||
pool.ExpectQuery("name: GetQuery :one").WithArgs(id).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
||||
AddRow(id, repository.QuerytypeContextFull, int32(1), int32(2), []byte(""), []uuid.UUID{}),
|
||||
)
|
||||
|
||||
pool.ExpectBeginTx(pgx.TxOptions{})
|
||||
pool.ExpectExec("name: AddActiveQueryVersion :exec").WithArgs(id, av).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
@@ -217,11 +153,10 @@ func TestUpdateQuery(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTestQuery(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
docsvc := document.New(cfg)
|
||||
col := collector.New(cfg)
|
||||
@@ -238,51 +173,83 @@ func TestTestQuery(t *testing.T) {
|
||||
}),
|
||||
})
|
||||
|
||||
doc := document.DocumentSummary{
|
||||
ID: uuid.New(),
|
||||
}
|
||||
params := &result.Process{
|
||||
QueryID: uuid.New(),
|
||||
DocumentID: doc.ID,
|
||||
QueryVersion: int32(3),
|
||||
}
|
||||
body := queryapi.QueryTestRequest{
|
||||
DocumentId: params.DocumentID,
|
||||
QueryVersion: params.QueryVersion,
|
||||
}
|
||||
bodyBytes, err := json.Marshal(body)
|
||||
contextQueryId, err := que.Create(t.Context(), &resultprocessor.Create{
|
||||
Type: resultprocessor.TypeContextFull,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
|
||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
err = cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: "client_id",
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
||||
Clientid: "client_id",
|
||||
Hash: "hash",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
fill := "fill"
|
||||
cleanId, err := cfg.GetDBQueries().AddDocumentClean(t.Context(), &repository.AddDocumentCleanParams{
|
||||
Documentid: docId,
|
||||
Bucket: &fill,
|
||||
Key: &fill,
|
||||
Hash: &fill,
|
||||
Mimetype: repository.NullCleanmimetype{
|
||||
Valid: true,
|
||||
Cleanmimetype: repository.CleanmimetypeApplicationPdf,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = cfg.GetDBQueries().AddDocumentCleanEntry(t.Context(), &repository.AddDocumentCleanEntryParams{
|
||||
Cleanid: cleanId,
|
||||
Version: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
textId, err := cfg.GetDBQueries().AddDocumentText(t.Context(), &repository.AddDocumentTextParams{
|
||||
Cleanid: cleanId,
|
||||
Bucket: fill,
|
||||
Key: fill,
|
||||
Hash: fill,
|
||||
Createdat: pgtype.Timestamp{
|
||||
Time: time.Now(),
|
||||
Valid: true,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = cfg.GetDBQueries().AddDocumentTextEntry(t.Context(), &repository.AddDocumentTextEntryParams{
|
||||
Textid: textId,
|
||||
Version: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
strVal := `{"mykey": "example_value", "oldkey": "old_value"}`
|
||||
_, err = cfg.GetDBQueries().AddResult(t.Context(), &repository.AddResultParams{
|
||||
Queryid: contextQueryId,
|
||||
Value: strVal,
|
||||
Textentryid: textId,
|
||||
Queryversion: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
reqID := uuid.New()
|
||||
pool.ExpectQuery("name: GetQueryWithVersion :one").WithArgs(¶ms.QueryID, ¶ms.QueryVersion).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
||||
AddRow(params.QueryID, repository.QuerytypeJsonExtractor, int32(1), params.QueryVersion+1, []byte("{\"path\":\"oldkey\"}"), []uuid.UUID{reqID}),
|
||||
)
|
||||
strVal := "{\"mykey\":\"example_value\",\"oldkey\":\"old_value\"}"
|
||||
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(¶ms.QueryID, ¶ms.QueryVersion, ¶ms.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
||||
AddRow(&uuid.UUID{}, reqID, repository.QuerytypeContextFull, &strVal),
|
||||
)
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(params.QueryID).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte("{\"path\":\"oldkey\"}")),
|
||||
)
|
||||
c := `{"path": "oldkey"}`
|
||||
queryId, err := que.Create(t.Context(), &resultprocessor.Create{
|
||||
Type: resultprocessor.TypeJsonExtractor,
|
||||
Config: &c,
|
||||
RequiredQueryIDs: &[]uuid.UUID{
|
||||
contextQueryId,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = cons.TestQuery(ctx, params.QueryID)
|
||||
body := queryapi.QueryTestRequest{
|
||||
DocumentId: docId,
|
||||
QueryVersion: 1,
|
||||
}
|
||||
ctx, rec := createContextWithBody(t, body)
|
||||
|
||||
err = cons.TestQuery(ctx, queryId)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.QueryTestResponse
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
require.NoError(t, err)
|
||||
assert.EqualExportedValues(t, queryapi.QueryTestResponse{
|
||||
assertBody(t, rec, queryapi.QueryTestResponse{
|
||||
Value: "old_value",
|
||||
}, res)
|
||||
})
|
||||
}
|
||||
|
||||
+50
-29
@@ -1,60 +1,81 @@
|
||||
package queryapi_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
queryapi "queryorchestration/api/queryAPI"
|
||||
"queryorchestration/internal/client"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/export"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/test"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetClientStatus(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
t.Parallel()
|
||||
cfg := &ClientConfig{}
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
Client: client.New(cfg),
|
||||
Export: export.New(),
|
||||
})
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
ctx, rec := createContext(t)
|
||||
|
||||
clientId := "clientid"
|
||||
|
||||
pool.ExpectQuery("name: GetClient :one").WithArgs(clientId).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "name", "can_sync"}).
|
||||
AddRow(clientId, "name", true),
|
||||
)
|
||||
pool.ExpectQuery("name: IsClientSynced :one").WithArgs(&clientId).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"issynced"}).
|
||||
AddRow(true),
|
||||
)
|
||||
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
||||
Clientid: clientId,
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = cfg.GetDBQueries().AddClientCanSync(t.Context(), &repository.AddClientCanSyncParams{
|
||||
Clientid: clientId,
|
||||
Cansync: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = cons.GetStatusByClientId(ctx, clientId)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.ClientStatusBody
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
require.NoError(t, err)
|
||||
assert.EqualExportedValues(t, queryapi.ClientStatusBody{
|
||||
assertBody(t, rec, queryapi.ClientStatusBody{
|
||||
Status: queryapi.INSYNC,
|
||||
}, res)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkGetClientStatus(b *testing.B) {
|
||||
cfg := &ClientConfig{}
|
||||
net := test.GetNetwork(b)
|
||||
test.CreateDB(b, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
cons := queryapi.NewControllers(&queryapi.Services{
|
||||
Client: client.New(cfg),
|
||||
Export: export.New(),
|
||||
})
|
||||
|
||||
ctx, _ := createContext(b)
|
||||
|
||||
clientId := "clientid"
|
||||
|
||||
err := cfg.GetDBQueries().CreateClient(b.Context(), &repository.CreateClientParams{
|
||||
Clientid: clientId,
|
||||
Name: "client_name",
|
||||
})
|
||||
require.NoError(b, err)
|
||||
err = cfg.GetDBQueries().AddClientCanSync(b.Context(), &repository.AddClientCanSyncParams{
|
||||
Clientid: clientId,
|
||||
Cansync: true,
|
||||
})
|
||||
require.NoError(b, err)
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
for b.Loop() {
|
||||
_ = cons.GetStatusByClientId(ctx, clientId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,80 +53,78 @@ func TestQueryRunner(t *testing.T) {
|
||||
}),
|
||||
})
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
doc := queryrunner.Body{
|
||||
DocumentID: uuid.New(),
|
||||
QueryID: uuid.New(),
|
||||
}
|
||||
doc := queryrunner.Body{
|
||||
DocumentID: uuid.New(),
|
||||
QueryID: uuid.New(),
|
||||
}
|
||||
|
||||
qcfg := "{\"path\":\"examplekey\"}"
|
||||
reqQuery := uuid.New()
|
||||
query := &resultprocessor.Query{
|
||||
ID: doc.QueryID,
|
||||
Version: 2,
|
||||
RequiredQueryIDs: &[]uuid.UUID{reqQuery},
|
||||
Config: &qcfg,
|
||||
}
|
||||
params := &resultset.Set{
|
||||
DocumentID: doc.DocumentID,
|
||||
}
|
||||
qcfg := "{\"path\":\"examplekey\"}"
|
||||
reqQuery := uuid.New()
|
||||
query := &resultprocessor.Query{
|
||||
ID: doc.QueryID,
|
||||
Version: 2,
|
||||
RequiredQueryIDs: &[]uuid.UUID{reqQuery},
|
||||
Config: &qcfg,
|
||||
}
|
||||
params := &resultset.Set{
|
||||
DocumentID: doc.DocumentID,
|
||||
}
|
||||
|
||||
textEntryId := uuid.New()
|
||||
pool.ExpectQuery("name: GetTextEntryByDocId :one").WithArgs(params.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "hash", "cleanId", "extractionVersion"}).
|
||||
AddRow(textEntryId, params.DocumentID, "buket", "/i/am/here", "example", uuid.New(), int64(543)),
|
||||
)
|
||||
pool.ExpectQuery("name: GetQuery :one").WithArgs(query.ID).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
||||
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.Version, query.Version, []byte(*query.Config), *query.RequiredQueryIDs),
|
||||
textEntryId := uuid.New()
|
||||
pool.ExpectQuery("name: GetTextEntryByDocId :one").WithArgs(params.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "hash", "cleanId", "extractionVersion"}).
|
||||
AddRow(textEntryId, params.DocumentID, "buket", "/i/am/here", "example", uuid.New(), int64(543)),
|
||||
)
|
||||
pool.ExpectQuery("name: GetQueryWithVersion :one").WithArgs(&query.ID, &query.Version).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
||||
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.Version, query.Version, []byte(*query.Config), *query.RequiredQueryIDs),
|
||||
pool.ExpectQuery("name: GetQuery :one").WithArgs(query.ID).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
||||
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.Version, query.Version, []byte(*query.Config), *query.RequiredQueryIDs),
|
||||
)
|
||||
pool.ExpectQuery("name: GetQueryWithVersion :one").WithArgs(&query.ID, &query.Version).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
||||
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.Version, query.Version, []byte(*query.Config), *query.RequiredQueryIDs),
|
||||
)
|
||||
requiredResultId := uuid.New()
|
||||
strVal := "{\"examplekey\":\"example_value\"}"
|
||||
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(&query.ID, &query.Version, ¶ms.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
||||
AddRow(&requiredResultId, (*query.RequiredQueryIDs)[0], repository.QuerytypeContextFull, &strVal),
|
||||
)
|
||||
requiredResultId := uuid.New()
|
||||
strVal := "{\"examplekey\":\"example_value\"}"
|
||||
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(&query.ID, &query.Version, ¶ms.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
||||
AddRow(&requiredResultId, (*query.RequiredQueryIDs)[0], repository.QuerytypeContextFull, &strVal),
|
||||
)
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte(qcfg)),
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte(qcfg)),
|
||||
)
|
||||
pool.ExpectBegin()
|
||||
resultId := uuid.New()
|
||||
pool.ExpectQuery("name: AddResult :one").WithArgs(query.ID, pgxmock.AnyArg(), textEntryId, query.Version).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(resultId),
|
||||
)
|
||||
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(&query.ID, &query.Version, ¶ms.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
||||
AddRow(&requiredResultId, (*query.RequiredQueryIDs)[0], repository.QuerytypeContextFull, &strVal),
|
||||
)
|
||||
pool.ExpectExec("name: AddResultDependency :exec").WithArgs(resultId, requiredResultId).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
pool.ExpectQuery("name: ListQueryDirectDependentsByDocumentID :many").WithArgs(&query.ID, &doc.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"queryId"}).
|
||||
AddRow(&reqQuery),
|
||||
)
|
||||
pool.ExpectBegin()
|
||||
resultId := uuid.New()
|
||||
pool.ExpectQuery("name: AddResult :one").WithArgs(query.ID, pgxmock.AnyArg(), textEntryId, query.Version).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(resultId),
|
||||
)
|
||||
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(&query.ID, &query.Version, ¶ms.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
||||
AddRow(&requiredResultId, (*query.RequiredQueryIDs)[0], repository.QuerytypeContextFull, &strVal),
|
||||
)
|
||||
pool.ExpectExec("name: AddResultDependency :exec").WithArgs(resultId, requiredResultId).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
pool.ExpectQuery("name: ListQueryDirectDependentsByDocumentID :many").WithArgs(&query.ID, &doc.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"queryId"}).
|
||||
AddRow(&reqQuery),
|
||||
)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.QueryURL && *in.MessageBody == fmt.Sprintf("{\"document_id\":\"%s\",\"query_id\":\"%s\"}", params.DocumentID.String(), (*query.RequiredQueryIDs)[0].String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.QueryURL && *in.MessageBody == fmt.Sprintf("{\"document_id\":\"%s\",\"query_id\":\"%s\"}", params.DocumentID.String(), (*query.RequiredQueryIDs)[0].String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
})
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
}
|
||||
|
||||
@@ -47,37 +47,35 @@ func TestQueryRunner(t *testing.T) {
|
||||
QuerySync: svc,
|
||||
})
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
doc := querysyncrunner.Body{
|
||||
DocumentID: uuid.New(),
|
||||
}
|
||||
doc := querysyncrunner.Body{
|
||||
DocumentID: uuid.New(),
|
||||
}
|
||||
|
||||
reqId := uuid.New()
|
||||
qs := []uuid.UUID{
|
||||
reqId,
|
||||
}
|
||||
reqId := uuid.New()
|
||||
qs := []uuid.UUID{
|
||||
reqId,
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(doc.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"isextracted"}).
|
||||
AddRow(true),
|
||||
)
|
||||
pool.ExpectQuery("name: ListUnsyncedNoDepsQueriesByDocId :many").WithArgs(&doc.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(&reqId),
|
||||
)
|
||||
pool.ExpectQuery("name: IsDocumentTextExtracted :one").WithArgs(doc.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"isextracted"}).
|
||||
AddRow(true),
|
||||
)
|
||||
pool.ExpectQuery("name: ListUnsyncedNoDepsQueriesByDocId :many").WithArgs(&doc.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(&reqId),
|
||||
)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.QueryURL && *in.MessageBody == fmt.Sprintf("{\"document_id\":\"%s\",\"query_id\":\"%s\"}", doc.DocumentID.String(), qs[0].String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.QueryURL && *in.MessageBody == fmt.Sprintf("{\"document_id\":\"%s\",\"query_id\":\"%s\"}", doc.DocumentID.String(), qs[0].String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
})
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
}
|
||||
|
||||
@@ -44,44 +44,42 @@ func TestQueryRunner(t *testing.T) {
|
||||
Sync: svc,
|
||||
})
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
doc := queryversionsyncrunner.Body{
|
||||
QueryID: uuid.New(),
|
||||
}
|
||||
doc := queryversionsyncrunner.Body{
|
||||
QueryID: uuid.New(),
|
||||
}
|
||||
|
||||
clientOne := "hello"
|
||||
clientTwo := "bye"
|
||||
clientIds := []string{
|
||||
clientOne,
|
||||
clientTwo,
|
||||
}
|
||||
clientOne := "hello"
|
||||
clientTwo := "bye"
|
||||
clientIds := []string{
|
||||
clientOne,
|
||||
clientTwo,
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: ListQueryClientIDs :many").WithArgs(&doc.QueryID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"clientId"}).
|
||||
AddRow(clientOne).
|
||||
AddRow(clientTwo),
|
||||
)
|
||||
pool.ExpectQuery("name: ListQueryClientIDs :many").WithArgs(&doc.QueryID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"clientId"}).
|
||||
AddRow(clientOne).
|
||||
AddRow(clientTwo),
|
||||
)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.ClientSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", clientIds[0])
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.ClientSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", clientIds[1])
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.ClientSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", clientIds[0])
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.ClientSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", clientIds[1])
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
})
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
}
|
||||
|
||||
@@ -43,64 +43,62 @@ func TestDocInitRunner(t *testing.T) {
|
||||
documentstore.New(cfg),
|
||||
})
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
clientId := "hi"
|
||||
bucketName := "bucketName"
|
||||
location := objectstore.BucketKey{
|
||||
Location: objectstore.Import,
|
||||
ClientID: clientId,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
docinfo := document.DocumentSummary{
|
||||
ID: uuid.New(),
|
||||
ClientID: clientId,
|
||||
Hash: "example_hash",
|
||||
}
|
||||
doc := S3EventNotification{
|
||||
Records: []S3EventRecord{
|
||||
{
|
||||
EventName: EventS3ObjectCreatedPut,
|
||||
S3: S3EventRecordDetails{
|
||||
Bucket: S3Bucket{
|
||||
Name: bucketName,
|
||||
},
|
||||
Object: S3Object{
|
||||
Key: location.String(),
|
||||
ETag: docinfo.Hash,
|
||||
},
|
||||
clientId := "hi"
|
||||
bucketName := "bucketName"
|
||||
location := objectstore.BucketKey{
|
||||
Location: objectstore.Import,
|
||||
ClientID: clientId,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
docinfo := document.DocumentSummary{
|
||||
ID: uuid.New(),
|
||||
ClientID: clientId,
|
||||
Hash: "example_hash",
|
||||
}
|
||||
doc := S3EventNotification{
|
||||
Records: []S3EventRecord{
|
||||
{
|
||||
EventName: EventS3ObjectCreatedPut,
|
||||
S3: S3EventRecordDetails{
|
||||
Bucket: S3Bucket{
|
||||
Name: bucketName,
|
||||
},
|
||||
Object: S3Object{
|
||||
Key: location.String(),
|
||||
ETag: docinfo.Hash,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, clientId).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}),
|
||||
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, clientId).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}),
|
||||
)
|
||||
pool.ExpectBegin()
|
||||
pool.ExpectQuery("name: CreateDocument :one").WithArgs(clientId, docinfo.Hash).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(docinfo.ID),
|
||||
)
|
||||
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(docinfo.ID, bucketName, location.String()).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(docinfo.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
|
||||
AddRow(docinfo.ID, docinfo.ClientID, "example"),
|
||||
)
|
||||
pool.ExpectBegin()
|
||||
pool.ExpectQuery("name: CreateDocument :one").WithArgs(clientId, docinfo.Hash).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(docinfo.ID),
|
||||
)
|
||||
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(docinfo.ID, bucketName, location.String()).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(docinfo.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
|
||||
AddRow(docinfo.ID, docinfo.ClientID, "example"),
|
||||
)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocInitURL
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocInitURL
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
})
|
||||
assert.True(t, runner.Process(ctx, doc))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user