Merged in feature/shorttestsanddirtidy (pull request #26)
Add short tests and Tidy internal directories * complete the tasks
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package queryprocessor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
)
|
||||
|
||||
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.GetCollectorQueriesRow) (*Query, error) {
|
||||
reqQueryIDs := database.MustToUUIDArray(q.Requiredids)
|
||||
|
||||
qType, err := ParseDBNullType(q.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Query{
|
||||
ID: database.MustToUUID(q.Queryid),
|
||||
Version: *q.Queryversion,
|
||||
Type: qType,
|
||||
RequiredQueryIDs: reqQueryIDs,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package queryprocessor_test
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/database/repository"
|
||||
queryprocessor "queryorchestration/internal/query/processor"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseDBCollectorQuery(t *testing.T) {
|
||||
qV := int32(0)
|
||||
dbResult := repository.GetCollectorQueriesRow{
|
||||
Collectorid: pgtype.UUID{},
|
||||
Queryid: pgtype.UUID{},
|
||||
Requiredids: []pgtype.UUID{},
|
||||
Type: repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeJsonExtractor},
|
||||
Queryversion: &qV,
|
||||
}
|
||||
value, err := queryprocessor.ParseDBCollectorQuery(&dbResult)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uuid.Nil, value.ID)
|
||||
assert.Equal(t, []uuid.UUID{}, value.RequiredQueryIDs)
|
||||
assert.Equal(t, int32(0), value.Version)
|
||||
assert.Equal(t, queryprocessor.Type(queryprocessor.TypeJsonExtractor), value.Type)
|
||||
|
||||
dbResult.Type = repository.NullQuerytype{}
|
||||
_, err = queryprocessor.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 := queryprocessor.ParseDBNullType(qType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, queryprocessor.Type(queryprocessor.TypeJsonExtractor), value)
|
||||
|
||||
qType = repository.NullQuerytype{}
|
||||
_, err = queryprocessor.ParseDBNullType(qType)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
|
||||
qType = repository.NullQuerytype{Valid: true}
|
||||
_, err = queryprocessor.ParseDBNullType(qType)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
}
|
||||
|
||||
func TestParseDBType(t *testing.T) {
|
||||
qType := repository.QuerytypeJsonExtractor
|
||||
value, err := queryprocessor.ParseDBType(qType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, queryprocessor.Type(queryprocessor.TypeJsonExtractor), value)
|
||||
|
||||
qType = repository.QuerytypeContextFull
|
||||
value, err = queryprocessor.ParseDBType(qType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, queryprocessor.Type(queryprocessor.TypeContextFull), value)
|
||||
}
|
||||
|
||||
func TestToDBQueryType(t *testing.T) {
|
||||
dbQueryType := queryprocessor.Type(queryprocessor.TypeJsonExtractor)
|
||||
value, err := queryprocessor.ToDBQueryType(dbQueryType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, repository.Querytype(repository.QuerytypeJsonExtractor), value)
|
||||
|
||||
dbQueryType = queryprocessor.Type(-1)
|
||||
_, err = queryprocessor.ToDBQueryType(dbQueryType)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
|
||||
dbQueryType = queryprocessor.Type(queryprocessor.TypeContextFull)
|
||||
value, err = queryprocessor.ToDBQueryType(dbQueryType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, repository.Querytype(repository.QuerytypeContextFull), value)
|
||||
}
|
||||
|
||||
func TestToDBQueryTypeArray(t *testing.T) {
|
||||
inArr := []queryprocessor.Type{
|
||||
queryprocessor.TypeJsonExtractor,
|
||||
queryprocessor.TypeContextFull,
|
||||
}
|
||||
value, err := queryprocessor.ToDBQueryTypeArray(inArr)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []repository.Querytype{
|
||||
repository.QuerytypeJsonExtractor,
|
||||
repository.QuerytypeContextFull,
|
||||
}, value)
|
||||
|
||||
inArr = []queryprocessor.Type{
|
||||
queryprocessor.Type(-1),
|
||||
}
|
||||
_, err = queryprocessor.ToDBQueryTypeArray(inArr)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
}
|
||||
|
||||
func TestToDBNullQueryType(t *testing.T) {
|
||||
dbQueryType := queryprocessor.Type(queryprocessor.TypeJsonExtractor)
|
||||
value, err := queryprocessor.ToDBNullQueryType(dbQueryType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeJsonExtractor}, value)
|
||||
|
||||
dbQueryType = queryprocessor.Type(-1)
|
||||
_, err = queryprocessor.ToDBNullQueryType(dbQueryType)
|
||||
assert.EqualError(t, err, "invalid database query type")
|
||||
|
||||
dbQueryType = queryprocessor.Type(queryprocessor.TypeContextFull)
|
||||
value, err = queryprocessor.ToDBNullQueryType(dbQueryType)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeContextFull}, value)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package queryprocessor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"queryorchestration/internal/query/result"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Type int
|
||||
|
||||
const (
|
||||
TypeJsonExtractor = iota
|
||||
TypeContextFull
|
||||
)
|
||||
|
||||
type Create struct {
|
||||
Type Type
|
||||
RequiredQueryIDs []uuid.UUID
|
||||
Config string
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
ID uuid.UUID
|
||||
RequiredQueryIDs []uuid.UUID
|
||||
Config string
|
||||
}
|
||||
|
||||
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 []result.Value) (string, error)
|
||||
}
|
||||
Reference in New Issue
Block a user