0ac5ff9e15
Test Query * depstextandclean * startedcleaningresult * resulttidyup * roundone * cleaning * unsyncedquery * startedtestsandsimplification * api * querytests * resultprocessortests * unittests * cleanup
308 lines
6.7 KiB
Go
308 lines
6.7 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/job/collector"
|
|
"queryorchestration/internal/query/result"
|
|
resultprocessor "queryorchestration/internal/query/result/processor"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestBatchQueries(t *testing.T) {
|
|
svc := Service{}
|
|
|
|
in := []*resultprocessor.Query{}
|
|
out := svc.batchQueries(in)
|
|
assert.ElementsMatch(t, [][]*resultprocessor.Query{}, out)
|
|
|
|
in = []*resultprocessor.Query{
|
|
{
|
|
ID: uuid.New(),
|
|
},
|
|
}
|
|
out = svc.batchQueries(in)
|
|
assert.ElementsMatch(t, [][]*resultprocessor.Query{
|
|
{
|
|
in[0],
|
|
},
|
|
}, out)
|
|
|
|
in = []*resultprocessor.Query{
|
|
{
|
|
ID: uuid.New(),
|
|
RequiredQueryIDs: &[]uuid.UUID{uuid.New()},
|
|
},
|
|
}
|
|
out = svc.batchQueries(in)
|
|
assert.ElementsMatch(t, [][]*resultprocessor.Query{
|
|
{
|
|
in[0],
|
|
},
|
|
}, out)
|
|
|
|
in = []*resultprocessor.Query{
|
|
{
|
|
ID: uuid.New(),
|
|
},
|
|
{
|
|
ID: uuid.New(),
|
|
},
|
|
}
|
|
out = svc.batchQueries(in)
|
|
assert.ElementsMatch(t, [][]*resultprocessor.Query{
|
|
{
|
|
in[0],
|
|
in[1],
|
|
},
|
|
}, out)
|
|
|
|
idone := uuid.New()
|
|
in = []*resultprocessor.Query{
|
|
{
|
|
ID: idone,
|
|
},
|
|
{
|
|
ID: uuid.New(),
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
idone,
|
|
},
|
|
},
|
|
}
|
|
out = svc.batchQueries(in)
|
|
assert.ElementsMatch(t, [][]*resultprocessor.Query{
|
|
{
|
|
in[0],
|
|
},
|
|
{
|
|
in[1],
|
|
},
|
|
}, out)
|
|
|
|
in = []*resultprocessor.Query{
|
|
{
|
|
ID: uuid.New(),
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
idone,
|
|
},
|
|
},
|
|
{
|
|
ID: idone,
|
|
},
|
|
}
|
|
out = svc.batchQueries(in)
|
|
assert.ElementsMatch(t, [][]*resultprocessor.Query{
|
|
{
|
|
in[1],
|
|
},
|
|
{
|
|
in[0],
|
|
},
|
|
}, out)
|
|
|
|
in = []*resultprocessor.Query{
|
|
{
|
|
ID: idone,
|
|
},
|
|
{
|
|
ID: uuid.New(),
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
idone,
|
|
},
|
|
},
|
|
{
|
|
ID: uuid.New(),
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
idone,
|
|
},
|
|
},
|
|
{
|
|
ID: uuid.New(),
|
|
},
|
|
}
|
|
out = svc.batchQueries(in)
|
|
assert.ElementsMatch(t, [][]*resultprocessor.Query{
|
|
{
|
|
in[0],
|
|
in[3],
|
|
},
|
|
{
|
|
in[1],
|
|
in[2],
|
|
},
|
|
}, out)
|
|
|
|
idtwo := uuid.New()
|
|
idthree := uuid.New()
|
|
in = []*resultprocessor.Query{
|
|
{
|
|
ID: idone,
|
|
},
|
|
{
|
|
ID: idtwo,
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
idone,
|
|
},
|
|
},
|
|
{
|
|
ID: idthree,
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
idtwo,
|
|
},
|
|
},
|
|
{
|
|
ID: uuid.New(),
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
idthree,
|
|
},
|
|
},
|
|
}
|
|
out = svc.batchQueries(in)
|
|
assert.ElementsMatch(t, [][]*resultprocessor.Query{
|
|
{
|
|
in[0],
|
|
},
|
|
{
|
|
in[1],
|
|
},
|
|
{
|
|
in[2],
|
|
},
|
|
{
|
|
in[3],
|
|
},
|
|
}, out)
|
|
}
|
|
|
|
func TestProcessBatch(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,
|
|
}
|
|
svc := Service{
|
|
svc: &Services{
|
|
Result: result.New(db),
|
|
},
|
|
}
|
|
|
|
assert.Error(t, svc.processBatch(ctx, nil, nil))
|
|
|
|
doc := &Document{
|
|
ID: uuid.New(),
|
|
CleanVersion: 1,
|
|
TextVersion: 1,
|
|
}
|
|
|
|
assert.Nil(t, svc.processBatch(ctx, doc, nil))
|
|
|
|
qs := []*resultprocessor.Query{}
|
|
assert.Nil(t, svc.processBatch(ctx, doc, qs))
|
|
|
|
coll := &collector.Collector{
|
|
ID: uuid.New(),
|
|
MinCleanVersion: 1,
|
|
MinTextVersion: 1,
|
|
}
|
|
qs = []*resultprocessor.Query{
|
|
{
|
|
ID: uuid.New(),
|
|
Type: resultprocessor.TypeContextFull,
|
|
Version: 1,
|
|
},
|
|
}
|
|
pool.ExpectQuery("name: SetResult :one").WithArgs(database.MustToDBUUID(qs[0].ID), database.MustToDBUUID(doc.ID), pgxmock.AnyArg(), coll.MinCleanVersion, coll.MinTextVersion, qs[0].Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(pgtype.UUID{}),
|
|
)
|
|
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(doc.JobID), coll.MinCleanVersion, coll.MinTextVersion, int32(1), int32(2), []byte("")),
|
|
)
|
|
pool.ExpectQuery("name: ListCollectorQueries :many").WithArgs(database.MustToDBUUID(coll.ID)).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "queryVersion", "requiredIds"}).
|
|
AddRow(database.MustToDBUUID(coll.ID), database.MustToDBUUID(qs[0].ID), repository.QuerytypeContextFull, qs[0].Version, []pgtype.UUID{}),
|
|
)
|
|
assert.Nil(t, svc.processBatch(ctx, doc, qs))
|
|
}
|
|
|
|
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,
|
|
}
|
|
svc := Service{
|
|
svc: &Services{
|
|
Result: result.New(db),
|
|
},
|
|
}
|
|
|
|
assert.Error(t, svc.processBatch(ctx, nil, nil))
|
|
|
|
doc := &Document{
|
|
ID: uuid.New(),
|
|
CleanVersion: 1,
|
|
TextVersion: 1,
|
|
}
|
|
|
|
assert.Nil(t, svc.processBatch(ctx, doc, nil))
|
|
|
|
qs := []*resultprocessor.Query{}
|
|
assert.Nil(t, svc.processBatch(ctx, doc, qs))
|
|
|
|
coll := &collector.Collector{
|
|
ID: uuid.New(),
|
|
MinCleanVersion: 1,
|
|
MinTextVersion: 1,
|
|
}
|
|
qs = []*resultprocessor.Query{
|
|
{
|
|
ID: uuid.New(),
|
|
Type: resultprocessor.TypeContextFull,
|
|
Version: 1,
|
|
},
|
|
}
|
|
|
|
pool.ExpectQuery("name: ListUnsyncedQueriesByDocId :many").WithArgs(database.MustToDBUUID(doc.ID)).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
|
AddRow(database.MustToDBUUID(qs[0].ID), repository.QuerytypeContextFull, qs[0].Version, qs[0].Version, nil, []pgtype.UUID{}),
|
|
)
|
|
pool.ExpectQuery("name: SetResult :one").WithArgs(database.MustToDBUUID(qs[0].ID), database.MustToDBUUID(doc.ID), pgxmock.AnyArg(), coll.MinCleanVersion, coll.MinTextVersion, qs[0].Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(pgtype.UUID{}),
|
|
)
|
|
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(doc.JobID), coll.MinCleanVersion, coll.MinTextVersion, int32(1), int32(2), []byte("")),
|
|
)
|
|
pool.ExpectQuery("name: ListCollectorQueries :many").WithArgs(database.MustToDBUUID(coll.ID)).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "queryVersion", "requiredIds"}).
|
|
AddRow(database.MustToDBUUID(coll.ID), database.MustToDBUUID(qs[0].ID), repository.QuerytypeContextFull, qs[0].Version, []pgtype.UUID{}),
|
|
)
|
|
assert.Nil(t, svc.Sync(ctx, doc))
|
|
}
|