Merged in feature/document (pull request #36)
Document CRUD * doccreate * dochashlocandget * depsandtodo
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package documentsync
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/job/collector"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Document struct {
|
||||
ID uuid.UUID `json:"id" validate:"required,uuid"`
|
||||
JobID uuid.UUID `json:"jobId" validate:"required,uuid"`
|
||||
CleanVersion int32 `json:"cleanVersion" validate:"required,gt=0"`
|
||||
TextVersion int32 `json:"textVersion" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
type Services struct {
|
||||
Collector *collector.Service
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
db *database.Connection
|
||||
svc *Services
|
||||
}
|
||||
|
||||
func New(db *database.Connection, svc *Services) *Service {
|
||||
return &Service{
|
||||
db,
|
||||
svc,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package documentsync_test
|
||||
|
||||
import (
|
||||
documentsync "queryorchestration/internal/document/sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
svc := documentsync.New(nil, nil)
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package documentsync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/job/collector"
|
||||
queryqueue "queryorchestration/internal/query/queue"
|
||||
"queryorchestration/internal/query/result"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (s *Service) Sync(ctx context.Context, doc *Document) error {
|
||||
coll, err := s.svc.Collector.GetByJobID(ctx, doc.JobID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
results, err := s.getResults(ctx, doc.ID, coll)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
queue, err := queryqueue.New(ctx, &queryqueue.NewConfig{
|
||||
DB: s.db,
|
||||
Collector: coll,
|
||||
Results: results,
|
||||
DocumentID: doc.ID,
|
||||
CleanVersion: doc.CleanVersion,
|
||||
TextVersion: doc.TextVersion,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = queue.Execute(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) getResults(ctx context.Context, id uuid.UUID, coll *collector.Collector) ([]*result.Result, error) {
|
||||
docID := database.MustToDBUUID(id)
|
||||
|
||||
results, err := s.db.Queries.ListResultsByDocumentID(ctx, &repository.ListResultsByDocumentIDParams{
|
||||
Documentid: docID,
|
||||
Textversion: coll.MinTextVersion,
|
||||
Cleanversion: coll.MinCleanVersion,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cleanResults := make([]*result.Result, len(results))
|
||||
for index, dbResult := range results {
|
||||
cleanResults[index] = result.Parse(dbResult)
|
||||
}
|
||||
|
||||
return cleanResults, nil
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package documentsync_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
documentsync "queryorchestration/internal/document/sync"
|
||||
"queryorchestration/internal/job/collector"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSyncIsSynced(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open pgxmock database: %v", err)
|
||||
}
|
||||
queries := repository.New(pool)
|
||||
db := &database.Connection{
|
||||
Queries: queries,
|
||||
Pool: pool,
|
||||
}
|
||||
|
||||
doc := documentsync.Document{
|
||||
ID: uuid.New(),
|
||||
JobID: uuid.New(),
|
||||
}
|
||||
|
||||
queryVersion := int32(1)
|
||||
coll := collector.Collector{
|
||||
ID: uuid.New(),
|
||||
JobID: doc.JobID,
|
||||
MinCleanVersion: int32(1),
|
||||
MinTextVersion: int32(2),
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByJobID :one").WithArgs(database.MustToDBUUID(doc.JobID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(database.MustToDBUUID(coll.ID), database.MustToDBUUID(coll.JobID), &coll.MinCleanVersion, &coll.MinTextVersion, int32(1), int32(2), []byte("")),
|
||||
)
|
||||
pool.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), coll.MinCleanVersion, coll.MinTextVersion).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}).
|
||||
AddRow(pgtype.UUID{}, pgtype.UUID{}, queryVersion),
|
||||
)
|
||||
pool.ExpectQuery("name: ListCollectorQueries :many").WithArgs(database.MustToDBUUID(coll.ID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "queryVersion", "requiredIds"}).
|
||||
AddRow(database.MustToDBUUID(coll.ID), pgtype.UUID{}, repository.QuerytypeJsonExtractor, queryVersion, []pgtype.UUID{}),
|
||||
)
|
||||
|
||||
docSvc := documentsync.New(db, &documentsync.Services{
|
||||
Collector: collector.New(db, &collector.Services{}),
|
||||
})
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestSyncDBFail(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open pgxmock database: %v", err)
|
||||
}
|
||||
queries := repository.New(pool)
|
||||
db := &database.Connection{
|
||||
Queries: queries,
|
||||
Pool: pool,
|
||||
}
|
||||
|
||||
doc := documentsync.Document{
|
||||
ID: uuid.New(),
|
||||
JobID: uuid.New(),
|
||||
}
|
||||
|
||||
dbCollectorId := database.MustToDBUUID(uuid.New())
|
||||
dbJobID := database.MustToDBUUID(doc.JobID)
|
||||
dbQueryID := database.MustToDBUUID(uuid.New())
|
||||
minCleanVersion := int32(1)
|
||||
minTextVersion := int32(1)
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}),
|
||||
)
|
||||
|
||||
docSvc := documentsync.New(db, &documentsync.Services{
|
||||
Collector: collector.New(db, &collector.Services{}),
|
||||
})
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, "no rows in result set")
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(dbCollectorId, dbJobID, &minCleanVersion, &minTextVersion, int32(1), int32(2), []byte("")),
|
||||
)
|
||||
dbErr := "database failure"
|
||||
pool.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
|
||||
WillReturnError(errors.New(dbErr))
|
||||
|
||||
docSvc = documentsync.New(db, &documentsync.Services{
|
||||
Collector: collector.New(db, &collector.Services{}),
|
||||
})
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, dbErr)
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(dbCollectorId, dbJobID, &minCleanVersion, &minTextVersion, int32(1), int32(2), []byte("")),
|
||||
)
|
||||
pool.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}),
|
||||
)
|
||||
dbErr = "database failure"
|
||||
pool.ExpectQuery("name: ListCollectorQueries :many").WithArgs(dbCollectorId).
|
||||
WillReturnError(errors.New(dbErr))
|
||||
|
||||
docSvc = documentsync.New(db, &documentsync.Services{
|
||||
Collector: collector.New(db, &collector.Services{}),
|
||||
})
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, dbErr)
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(dbCollectorId, dbJobID, &minCleanVersion, &minTextVersion, int32(1), int32(2), []byte("")),
|
||||
)
|
||||
qV := int32(1)
|
||||
reqID := database.MustToDBUUID(uuid.New())
|
||||
resID := database.MustToDBUUID(uuid.New())
|
||||
pool.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}).
|
||||
AddRow(resID, reqID, qV),
|
||||
)
|
||||
dbErr = "database failure"
|
||||
pool.ExpectQuery("name: ListCollectorQueries :many").WithArgs(dbCollectorId).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "queryVersion", "requiredIds"}).
|
||||
AddRow(dbCollectorId, dbQueryID, repository.QuerytypeJsonExtractor, qV, []pgtype.UUID{reqID}).
|
||||
AddRow(dbCollectorId, reqID, repository.QuerytypeContextFull, qV, []pgtype.UUID{}),
|
||||
)
|
||||
pool.ExpectQuery("name: ListResultValuesByID :many").WithArgs([]pgtype.UUID{resID}).
|
||||
WillReturnError(errors.New(dbErr))
|
||||
|
||||
docSvc = documentsync.New(db, &documentsync.Services{
|
||||
Collector: collector.New(db, &collector.Services{}),
|
||||
})
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, dbErr)
|
||||
}
|
||||
|
||||
func TestSync(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open pgxmock database: %v", err)
|
||||
}
|
||||
queries := repository.New(pool)
|
||||
db := &database.Connection{
|
||||
Queries: queries,
|
||||
Pool: pool,
|
||||
}
|
||||
|
||||
doc := documentsync.Document{
|
||||
ID: uuid.New(),
|
||||
JobID: uuid.New(),
|
||||
}
|
||||
|
||||
dbCollectorId := database.MustToDBUUID(uuid.New())
|
||||
dbJobID := database.MustToDBUUID(doc.JobID)
|
||||
dbQueryID := database.MustToDBUUID(uuid.New())
|
||||
minCleanVersion := int32(1)
|
||||
minTextVersion := int32(1)
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(dbCollectorId, dbJobID, &minCleanVersion, &minTextVersion, int32(1), int32(2), []byte("")),
|
||||
)
|
||||
pool.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}),
|
||||
)
|
||||
qV := int32(1)
|
||||
pool.ExpectQuery("name: ListCollectorQueries :many").WithArgs(dbCollectorId).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "queryVersion", "requiredIds"}).
|
||||
AddRow(dbCollectorId, dbQueryID, repository.QuerytypeJsonExtractor, qV, []pgtype.UUID{}),
|
||||
)
|
||||
pool.ExpectQuery("name: ListResultValuesByID :many").WithArgs([]pgtype.UUID{}).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "value"}),
|
||||
)
|
||||
|
||||
docSvc := documentsync.New(db, &documentsync.Services{
|
||||
Collector: collector.New(db, &collector.Services{}),
|
||||
})
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, "JSON Extraction requires 1 result")
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package documentsync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/job/collector"
|
||||
"queryorchestration/internal/query/result"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetResults(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open pgxmock database: %v", err)
|
||||
}
|
||||
queries := repository.New(pool)
|
||||
db := &database.Connection{
|
||||
Queries: queries,
|
||||
Pool: pool,
|
||||
}
|
||||
|
||||
doc := Document{
|
||||
ID: uuid.New(),
|
||||
JobID: uuid.New(),
|
||||
}
|
||||
|
||||
coll := collector.Collector{
|
||||
ID: uuid.New(),
|
||||
JobID: doc.JobID,
|
||||
MinCleanVersion: int32(1),
|
||||
MinTextVersion: int32(2),
|
||||
}
|
||||
results := []*result.Result{
|
||||
{
|
||||
ID: uuid.New(),
|
||||
QueryID: uuid.New(),
|
||||
QueryVersion: 2,
|
||||
},
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), coll.MinCleanVersion, coll.MinTextVersion).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}).
|
||||
AddRow(database.MustToDBUUID(results[0].ID), database.MustToDBUUID(results[0].QueryID), results[0].QueryVersion),
|
||||
)
|
||||
|
||||
docSvc := Service{
|
||||
db: db,
|
||||
}
|
||||
res, err := docSvc.getResults(ctx, doc.ID, &coll)
|
||||
assert.Nil(t, err)
|
||||
assert.ElementsMatch(t, results, res)
|
||||
}
|
||||
Reference in New Issue
Block a user