Merged in feature/shorttestsanddirtidy (pull request #26)
Add short tests and Tidy internal directories * complete the tasks
This commit is contained in:
@@ -1,74 +0,0 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"context"
|
||||
"queryorchestration/internal/collector"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
queryqueue "queryorchestration/internal/queryQueue"
|
||||
"queryorchestration/internal/result"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Document struct {
|
||||
ID uuid.UUID `json:"id" validate:"required,uuid"`
|
||||
JobID uuid.UUID `json:"jobId" validate:"required,uuid"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
CleanVersion int32 `json:"cleanVersion" validate:"required,gt=0"`
|
||||
TextVersion int32 `json:"textVersion" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
db *database.Connection
|
||||
}
|
||||
|
||||
func New(db *database.Connection) *Service {
|
||||
return &Service{
|
||||
db,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Sync(ctx context.Context, doc *Document) error {
|
||||
collector, err := collector.NewByJobId(ctx, s.db, doc.JobID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
results, err := s.getResults(ctx, doc.ID, collector)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
queue, err := queryqueue.New(ctx, s.db, collector, results, doc.ID, doc.CleanVersion, 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
|
||||
}
|
||||
@@ -1,199 +0,0 @@
|
||||
package document_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
"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 := document.Document{
|
||||
ID: uuid.New(),
|
||||
JobID: uuid.New(),
|
||||
Name: "document_name",
|
||||
}
|
||||
|
||||
dbCollectorId := database.MustToDBUUID(uuid.New())
|
||||
dbJobID := database.MustToDBUUID(doc.JobID)
|
||||
minCleanVersion := int32(1)
|
||||
minTextVersion := int32(1)
|
||||
qV := int32(1)
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}).
|
||||
AddRow(dbCollectorId, dbJobID, minCleanVersion, minTextVersion),
|
||||
)
|
||||
pool.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}).
|
||||
AddRow(pgtype.UUID{}, pgtype.UUID{}, qV),
|
||||
)
|
||||
pool.ExpectQuery("name: GetCollectorQueries :many").WithArgs(dbCollectorId).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "queryVersion", "requiredIds"}).
|
||||
AddRow(dbCollectorId, pgtype.UUID{}, repository.NullQuerytype{Querytype: repository.QuerytypeJsonExtractor, Valid: true}, &qV, []pgtype.UUID{}),
|
||||
)
|
||||
|
||||
docSvc := document.New(db)
|
||||
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 := document.Document{
|
||||
ID: uuid.New(),
|
||||
JobID: uuid.New(),
|
||||
Name: "document_name",
|
||||
}
|
||||
|
||||
dbCollectorId := database.MustToDBUUID(uuid.New())
|
||||
dbJobID := database.MustToDBUUID(doc.JobID)
|
||||
dbQueryID := database.MustToDBUUID(uuid.New())
|
||||
minCleanVersion := int32(1)
|
||||
minTextVersion := int32(1)
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}),
|
||||
)
|
||||
|
||||
docSvc := document.New(db)
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, "no rows in result set")
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}).
|
||||
AddRow(dbCollectorId, dbJobID, minCleanVersion, minTextVersion),
|
||||
)
|
||||
dbErr := "database failure"
|
||||
pool.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
|
||||
WillReturnError(errors.New(dbErr))
|
||||
|
||||
docSvc = document.New(db)
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, dbErr)
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}).
|
||||
AddRow(dbCollectorId, dbJobID, minCleanVersion, minTextVersion),
|
||||
)
|
||||
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: GetCollectorQueries :many").WithArgs(dbCollectorId).
|
||||
WillReturnError(errors.New(dbErr))
|
||||
|
||||
docSvc = document.New(db)
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, dbErr)
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}).
|
||||
AddRow(dbCollectorId, dbJobID, minCleanVersion, minTextVersion),
|
||||
)
|
||||
pool.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}),
|
||||
)
|
||||
dbErr = "database failure"
|
||||
qV := int32(1)
|
||||
pool.ExpectQuery("name: GetCollectorQueries :many").WithArgs(dbCollectorId).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "queryVersion", "requiredIds"}).
|
||||
AddRow(dbCollectorId, dbQueryID, repository.NullQuerytype{Querytype: repository.QuerytypeJsonExtractor, Valid: true}, &qV, []pgtype.UUID{}),
|
||||
)
|
||||
pool.ExpectQuery("name: ListResultValuesByID :many").WithArgs([]pgtype.UUID{}).
|
||||
WillReturnError(errors.New(dbErr))
|
||||
|
||||
docSvc = document.New(db)
|
||||
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 := document.Document{
|
||||
ID: uuid.New(),
|
||||
JobID: uuid.New(),
|
||||
Name: "document_name",
|
||||
}
|
||||
|
||||
dbCollectorId := database.MustToDBUUID(uuid.New())
|
||||
dbJobID := database.MustToDBUUID(doc.JobID)
|
||||
dbQueryID := database.MustToDBUUID(uuid.New())
|
||||
minCleanVersion := int32(1)
|
||||
minTextVersion := int32(1)
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}).
|
||||
AddRow(dbCollectorId, dbJobID, minCleanVersion, minTextVersion),
|
||||
)
|
||||
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: GetCollectorQueries :many").WithArgs(dbCollectorId).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "queryVersion", "requiredIds"}).
|
||||
AddRow(dbCollectorId, dbQueryID, repository.NullQuerytype{Querytype: repository.QuerytypeJsonExtractor, Valid: true}, &qV, []pgtype.UUID{}),
|
||||
)
|
||||
pool.ExpectQuery("name: ListResultValuesByID :many").WithArgs([]pgtype.UUID{}).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "value"}),
|
||||
)
|
||||
|
||||
docSvc := document.New(db)
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, "JSON Extraction requires 1 result")
|
||||
}
|
||||
Reference in New Issue
Block a user