Merged in feature/testquery (pull request #39)
Test Query * depstextandclean * startedcleaningresult * resulttidyup * roundone * cleaning * unsyncedquery * startedtestsandsimplification * api * querytests * resultprocessortests * unittests * cleanup
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
package resultprocessor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func ParseDBNullType(qType repository.NullQuerytype) (Type, error) {
|
||||
if !qType.Valid {
|
||||
return TypeJsonExtractor, fmt.Errorf("invalid database query type")
|
||||
}
|
||||
|
||||
return ParseDBType(qType.Querytype)
|
||||
}
|
||||
|
||||
func ParseDBType(qType repository.Querytype) (Type, error) {
|
||||
switch qType {
|
||||
case repository.QuerytypeJsonExtractor:
|
||||
return TypeJsonExtractor, nil
|
||||
case repository.QuerytypeContextFull:
|
||||
return TypeContextFull, nil
|
||||
default:
|
||||
return TypeJsonExtractor, fmt.Errorf("invalid database query type")
|
||||
}
|
||||
}
|
||||
|
||||
func ToDBQueryType(t Type) (repository.Querytype, error) {
|
||||
var dbType repository.Querytype
|
||||
|
||||
switch t {
|
||||
case TypeJsonExtractor:
|
||||
dbType = repository.QuerytypeJsonExtractor
|
||||
case TypeContextFull:
|
||||
dbType = repository.QuerytypeContextFull
|
||||
default:
|
||||
return repository.QuerytypeContextFull, fmt.Errorf("invalid database query type")
|
||||
}
|
||||
|
||||
return dbType, nil
|
||||
}
|
||||
|
||||
func ToDBQueryTypeArray(t []Type) ([]repository.Querytype, error) {
|
||||
arr := make([]repository.Querytype, len(t))
|
||||
for index, value := range t {
|
||||
v, err := ToDBQueryType(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
arr[index] = v
|
||||
}
|
||||
|
||||
return arr, nil
|
||||
}
|
||||
|
||||
func ToDBNullQueryType(t Type) (repository.NullQuerytype, error) {
|
||||
dbType, err := ToDBQueryType(t)
|
||||
if err != nil {
|
||||
return repository.NullQuerytype{}, err
|
||||
}
|
||||
|
||||
return repository.NullQuerytype{Querytype: dbType, Valid: true}, nil
|
||||
}
|
||||
|
||||
func ParseDBCollectorQuery(q *repository.Collectorquerydependencytree) (*Query, error) {
|
||||
var reqQueryIDs *[]uuid.UUID
|
||||
if len(q.Requiredids) > 0 {
|
||||
ids := database.MustToUUIDArray(q.Requiredids)
|
||||
reqQueryIDs = &ids
|
||||
}
|
||||
|
||||
qType, err := ParseDBType(q.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Query{
|
||||
ID: database.MustToUUID(q.Queryid),
|
||||
Version: q.Queryversion,
|
||||
Type: qType,
|
||||
RequiredQueryIDs: reqQueryIDs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ParseFullQuery(qs *repository.Fullactivequery) (*Query, error) {
|
||||
if qs == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
qt, err := ParseDBType(qs.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var rids *[]uuid.UUID
|
||||
if len(qs.Requiredids) > 0 {
|
||||
r := database.MustToUUIDArray(qs.Requiredids)
|
||||
rids = &r
|
||||
}
|
||||
|
||||
var cfg *string
|
||||
if qs.Config != nil {
|
||||
c := string(qs.Config)
|
||||
cfg = &c
|
||||
}
|
||||
|
||||
return &Query{
|
||||
ID: database.MustToUUID(qs.ID),
|
||||
Type: qt,
|
||||
Version: qs.Activeversion,
|
||||
RequiredQueryIDs: rids,
|
||||
Config: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ParseFullQueryArray(qs []*repository.Fullactivequery) ([]*Query, error) {
|
||||
parsed := make([]*Query, len(qs))
|
||||
for i, q := range qs {
|
||||
p, err := ParseFullQuery(q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
parsed[i] = p
|
||||
}
|
||||
|
||||
return parsed, nil
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package resultprocessor_test
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
resultprocessor "queryorchestration/internal/query/result/processor"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseDBCollectorQuery(t *testing.T) {
|
||||
dbResult := repository.Collectorquerydependencytree{
|
||||
Collectorid: pgtype.UUID{},
|
||||
Queryid: pgtype.UUID{},
|
||||
Requiredids: []pgtype.UUID{},
|
||||
Type: repository.QuerytypeJsonExtractor,
|
||||
Queryversion: 1,
|
||||
}
|
||||
value, err := resultprocessor.ParseDBCollectorQuery(&dbResult)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uuid.Nil, value.ID)
|
||||
assert.Nil(t, value.RequiredQueryIDs)
|
||||
assert.Equal(t, int32(1), value.Version)
|
||||
assert.Equal(t, resultprocessor.Type(resultprocessor.TypeJsonExtractor), value.Type)
|
||||
|
||||
dbResult.Type = repository.Querytype("")
|
||||
_, err = resultprocessor.ParseDBCollectorQuery(&dbResult)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
}
|
||||
|
||||
func TestParseDBNullType(t *testing.T) {
|
||||
qType := repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeJsonExtractor}
|
||||
value, err := resultprocessor.ParseDBNullType(qType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, resultprocessor.Type(resultprocessor.TypeJsonExtractor), value)
|
||||
|
||||
qType = repository.NullQuerytype{}
|
||||
_, err = resultprocessor.ParseDBNullType(qType)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
|
||||
qType = repository.NullQuerytype{Valid: true}
|
||||
_, err = resultprocessor.ParseDBNullType(qType)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
}
|
||||
|
||||
func TestParseDBType(t *testing.T) {
|
||||
qType := repository.QuerytypeJsonExtractor
|
||||
value, err := resultprocessor.ParseDBType(qType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, resultprocessor.Type(resultprocessor.TypeJsonExtractor), value)
|
||||
|
||||
qType = repository.QuerytypeContextFull
|
||||
value, err = resultprocessor.ParseDBType(qType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, resultprocessor.Type(resultprocessor.TypeContextFull), value)
|
||||
}
|
||||
|
||||
func TestToDBQueryType(t *testing.T) {
|
||||
dbQueryType := resultprocessor.Type(resultprocessor.TypeJsonExtractor)
|
||||
value, err := resultprocessor.ToDBQueryType(dbQueryType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, repository.Querytype(repository.QuerytypeJsonExtractor), value)
|
||||
|
||||
dbQueryType = resultprocessor.Type(-1)
|
||||
_, err = resultprocessor.ToDBQueryType(dbQueryType)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
|
||||
dbQueryType = resultprocessor.Type(resultprocessor.TypeContextFull)
|
||||
value, err = resultprocessor.ToDBQueryType(dbQueryType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, repository.Querytype(repository.QuerytypeContextFull), value)
|
||||
}
|
||||
|
||||
func TestToDBQueryTypeArray(t *testing.T) {
|
||||
inArr := []resultprocessor.Type{
|
||||
resultprocessor.TypeJsonExtractor,
|
||||
resultprocessor.TypeContextFull,
|
||||
}
|
||||
value, err := resultprocessor.ToDBQueryTypeArray(inArr)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []repository.Querytype{
|
||||
repository.QuerytypeJsonExtractor,
|
||||
repository.QuerytypeContextFull,
|
||||
}, value)
|
||||
|
||||
inArr = []resultprocessor.Type{
|
||||
resultprocessor.Type(-1),
|
||||
}
|
||||
_, err = resultprocessor.ToDBQueryTypeArray(inArr)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
}
|
||||
|
||||
func TestToDBNullQueryType(t *testing.T) {
|
||||
dbQueryType := resultprocessor.Type(resultprocessor.TypeJsonExtractor)
|
||||
value, err := resultprocessor.ToDBNullQueryType(dbQueryType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeJsonExtractor}, value)
|
||||
|
||||
dbQueryType = resultprocessor.Type(-1)
|
||||
_, err = resultprocessor.ToDBNullQueryType(dbQueryType)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
|
||||
dbQueryType = resultprocessor.Type(resultprocessor.TypeContextFull)
|
||||
value, err = resultprocessor.ToDBNullQueryType(dbQueryType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeContextFull}, value)
|
||||
}
|
||||
|
||||
func TestParseFullQuery(t *testing.T) {
|
||||
var q *repository.Fullactivequery
|
||||
out, err := resultprocessor.ParseFullQuery(q)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, out)
|
||||
|
||||
q = &repository.Fullactivequery{
|
||||
ID: database.MustToDBUUID(uuid.New()),
|
||||
Type: repository.QuerytypeContextFull,
|
||||
Activeversion: 1,
|
||||
Latestversion: 2,
|
||||
}
|
||||
|
||||
out, err = resultprocessor.ParseFullQuery(q)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, &resultprocessor.Query{
|
||||
ID: database.MustToUUID(q.ID),
|
||||
Type: resultprocessor.TypeContextFull,
|
||||
Version: 1,
|
||||
}, out)
|
||||
|
||||
q = &repository.Fullactivequery{
|
||||
ID: database.MustToDBUUID(uuid.New()),
|
||||
Type: repository.QuerytypeContextFull,
|
||||
Activeversion: 1,
|
||||
Latestversion: 2,
|
||||
Requiredids: []pgtype.UUID{},
|
||||
}
|
||||
|
||||
out, err = resultprocessor.ParseFullQuery(q)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, &resultprocessor.Query{
|
||||
ID: database.MustToUUID(q.ID),
|
||||
Type: resultprocessor.TypeContextFull,
|
||||
Version: 1,
|
||||
}, out)
|
||||
|
||||
q = &repository.Fullactivequery{
|
||||
ID: database.MustToDBUUID(uuid.New()),
|
||||
Type: repository.QuerytypeContextFull,
|
||||
Activeversion: 1,
|
||||
Latestversion: 2,
|
||||
Config: []byte("hello"),
|
||||
Requiredids: []pgtype.UUID{
|
||||
database.MustToDBUUID(uuid.New()),
|
||||
},
|
||||
}
|
||||
|
||||
out, err = resultprocessor.ParseFullQuery(q)
|
||||
assert.Nil(t, err)
|
||||
cfg := "hello"
|
||||
assert.EqualExportedValues(t, &resultprocessor.Query{
|
||||
ID: database.MustToUUID(q.ID),
|
||||
Type: resultprocessor.TypeContextFull,
|
||||
Version: 1,
|
||||
Config: &cfg,
|
||||
RequiredQueryIDs: &[]uuid.UUID{database.MustToUUID(q.Requiredids[0])},
|
||||
}, out)
|
||||
}
|
||||
|
||||
func TestParseFullQueryArray(t *testing.T) {
|
||||
var q []*repository.Fullactivequery
|
||||
out, err := resultprocessor.ParseFullQueryArray(q)
|
||||
assert.Nil(t, err)
|
||||
assert.ElementsMatch(t, []*resultprocessor.Query{}, out)
|
||||
|
||||
q = []*repository.Fullactivequery{
|
||||
{
|
||||
ID: database.MustToDBUUID(uuid.New()),
|
||||
Type: repository.QuerytypeContextFull,
|
||||
Activeversion: 1,
|
||||
Latestversion: 2,
|
||||
},
|
||||
}
|
||||
|
||||
out, err = resultprocessor.ParseFullQueryArray(q)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, []*resultprocessor.Query{
|
||||
{
|
||||
ID: database.MustToUUID(q[0].ID),
|
||||
Type: resultprocessor.TypeContextFull,
|
||||
Version: 1,
|
||||
},
|
||||
}, out)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package resultprocessor
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Value interface {
|
||||
GetValue(ctx context.Context) (string, error)
|
||||
GetStoreValue() string
|
||||
}
|
||||
|
||||
type Type int
|
||||
|
||||
const (
|
||||
TypeJsonExtractor = iota
|
||||
TypeContextFull
|
||||
)
|
||||
|
||||
type Create struct {
|
||||
Type Type
|
||||
RequiredQueryIDs *[]uuid.UUID
|
||||
Config *string
|
||||
}
|
||||
|
||||
func (s *Create) GetConfig() *string {
|
||||
return s.Config
|
||||
}
|
||||
|
||||
func (s *Create) SetConfig(cfg *string) {
|
||||
s.Config = cfg
|
||||
}
|
||||
|
||||
func (s *Create) GetRequiredQueryIDs() *[]uuid.UUID {
|
||||
return s.RequiredQueryIDs
|
||||
}
|
||||
|
||||
func (s *Create) SetRequiredQueryIDs(ids *[]uuid.UUID) {
|
||||
s.RequiredQueryIDs = ids
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
ID uuid.UUID
|
||||
ActiveVersion *int32
|
||||
RequiredQueryIDs *[]uuid.UUID
|
||||
Config *string
|
||||
}
|
||||
|
||||
func (s *Update) GetConfig() *string {
|
||||
return s.Config
|
||||
}
|
||||
|
||||
func (s *Update) SetConfig(cfg *string) {
|
||||
s.Config = cfg
|
||||
}
|
||||
|
||||
func (s *Update) GetRequiredQueryIDs() *[]uuid.UUID {
|
||||
return s.RequiredQueryIDs
|
||||
}
|
||||
|
||||
func (s *Update) SetRequiredQueryIDs(ids *[]uuid.UUID) {
|
||||
s.RequiredQueryIDs = ids
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
ID uuid.UUID
|
||||
Type Type
|
||||
Version int32
|
||||
RequiredQueryIDs *[]uuid.UUID
|
||||
Config *string
|
||||
}
|
||||
|
||||
type Creator interface {
|
||||
Validate(ctx context.Context, entity *Create) error
|
||||
}
|
||||
|
||||
type Updator interface {
|
||||
Validate(ctx context.Context, current *Query, entity *Update) error
|
||||
}
|
||||
|
||||
type Processor interface {
|
||||
Process(ctx context.Context, query *Query, values *[]Value) (string, error)
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package resultprocessor_test
|
||||
|
||||
import (
|
||||
resultprocessor "queryorchestration/internal/query/result/processor"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCreateGetConfig(t *testing.T) {
|
||||
entity := resultprocessor.Create{}
|
||||
|
||||
assert.Nil(t, entity.GetConfig())
|
||||
|
||||
cfg := "example_config"
|
||||
entity.Config = &cfg
|
||||
assert.NotNil(t, entity.GetConfig())
|
||||
assert.Equal(t, cfg, *entity.GetConfig())
|
||||
}
|
||||
|
||||
func TestCreateSetConfig(t *testing.T) {
|
||||
entity := resultprocessor.Create{}
|
||||
|
||||
assert.Nil(t, entity.Config)
|
||||
|
||||
cfg := "example_config"
|
||||
entity.SetConfig(&cfg)
|
||||
assert.Equal(t, cfg, *entity.Config)
|
||||
}
|
||||
|
||||
func TestUpdateGetConfig(t *testing.T) {
|
||||
entity := resultprocessor.Update{}
|
||||
|
||||
assert.Nil(t, entity.GetConfig())
|
||||
|
||||
cfg := "example_config"
|
||||
entity.Config = &cfg
|
||||
assert.NotNil(t, entity.GetConfig())
|
||||
assert.Equal(t, cfg, *entity.GetConfig())
|
||||
}
|
||||
|
||||
func TestUpdateSetConfig(t *testing.T) {
|
||||
entity := resultprocessor.Update{}
|
||||
|
||||
assert.Nil(t, entity.Config)
|
||||
|
||||
cfg := "example_config"
|
||||
entity.SetConfig(&cfg)
|
||||
assert.Equal(t, cfg, *entity.Config)
|
||||
}
|
||||
|
||||
func TestCreateGetRequiredQueryIDs(t *testing.T) {
|
||||
entity := resultprocessor.Create{}
|
||||
|
||||
assert.Nil(t, entity.GetRequiredQueryIDs())
|
||||
|
||||
ids := []uuid.UUID{uuid.New()}
|
||||
entity.RequiredQueryIDs = &ids
|
||||
assert.NotNil(t, entity.GetRequiredQueryIDs())
|
||||
assert.Equal(t, ids, *entity.GetRequiredQueryIDs())
|
||||
}
|
||||
|
||||
func TestCreateSetRequiredQueryIDs(t *testing.T) {
|
||||
entity := resultprocessor.Create{}
|
||||
|
||||
assert.Nil(t, entity.RequiredQueryIDs)
|
||||
|
||||
ids := []uuid.UUID{uuid.New()}
|
||||
entity.SetRequiredQueryIDs(&ids)
|
||||
assert.Equal(t, ids, *entity.RequiredQueryIDs)
|
||||
}
|
||||
|
||||
func TestUpdateGetRequiredQueryIDs(t *testing.T) {
|
||||
entity := resultprocessor.Update{}
|
||||
|
||||
assert.Nil(t, entity.GetRequiredQueryIDs())
|
||||
|
||||
ids := []uuid.UUID{uuid.New()}
|
||||
entity.RequiredQueryIDs = &ids
|
||||
assert.NotNil(t, entity.GetRequiredQueryIDs())
|
||||
assert.Equal(t, ids, *entity.GetRequiredQueryIDs())
|
||||
}
|
||||
|
||||
func TestUpdateSetRequiredQueryIDs(t *testing.T) {
|
||||
entity := resultprocessor.Update{}
|
||||
|
||||
assert.Nil(t, entity.RequiredQueryIDs)
|
||||
|
||||
ids := []uuid.UUID{uuid.New()}
|
||||
entity.SetRequiredQueryIDs(&ids)
|
||||
assert.Equal(t, ids, *entity.RequiredQueryIDs)
|
||||
}
|
||||
Reference in New Issue
Block a user