7001ca854c
Queuing Changes and Cfg Testing * staarting * staarting * startedpush * note * save * mocking * removederrs * fixtests * cleanuperrs * newenvsetup * preppingtests * queue * mmovetocfgpassunittests * sortoutconfig * passinginteg * deps * fixtests
192 lines
5.0 KiB
Go
192 lines
5.0 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
resultprocessor "queryorchestration/internal/query/result/processor"
|
|
"queryorchestration/internal/serviceconfig"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetCreator(t *testing.T) {
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
svc := New(cfg, &Services{})
|
|
|
|
queryType := resultprocessor.Type(resultprocessor.TypeContextFull)
|
|
creator, err := svc.getCreator(queryType)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, creator)
|
|
|
|
queryType = resultprocessor.Type(resultprocessor.TypeJsonExtractor)
|
|
creator, err = svc.getCreator(queryType)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, creator)
|
|
|
|
queryType = resultprocessor.Type(-1)
|
|
_, err = svc.getCreator(queryType)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestParseCreateQuery(t *testing.T) {
|
|
cfg := "{\"key\":\"value\"}"
|
|
cQuery := &resultprocessor.Create{
|
|
Type: resultprocessor.TypeContextFull,
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
uuid.New(),
|
|
},
|
|
Config: &cfg,
|
|
}
|
|
|
|
resultQuery, err := parseCreateQuery(cQuery)
|
|
assert.NoError(t, err)
|
|
rQIDs := database.MustToDBUUIDArray(*cQuery.RequiredQueryIDs)
|
|
qcfg := []byte(*cQuery.Config)
|
|
assert.EqualExportedValues(t, createQuery{
|
|
Type: repository.QuerytypeContextFull,
|
|
RequiredQueryIDs: &rQIDs,
|
|
Config: &qcfg,
|
|
}, *resultQuery)
|
|
}
|
|
|
|
func TestParseCreateQueryInvalidType(t *testing.T) {
|
|
cfg := "{\"key\":\"value\"}"
|
|
cQuery := &resultprocessor.Create{
|
|
Type: resultprocessor.Type(-1),
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
uuid.New(),
|
|
},
|
|
Config: &cfg,
|
|
}
|
|
|
|
_, err := parseCreateQuery(cQuery)
|
|
assert.EqualError(t, err, "invalid database query type")
|
|
}
|
|
|
|
func TestSubmitCreate(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
svc := New(cfg, &Services{})
|
|
|
|
config := "{\"path\":\"example_path\"}"
|
|
q := Query{
|
|
ID: uuid.New(),
|
|
Type: resultprocessor.TypeJsonExtractor,
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
uuid.New(),
|
|
},
|
|
Config: &config,
|
|
}
|
|
create := &resultprocessor.Create{
|
|
Type: q.Type,
|
|
RequiredQueryIDs: q.RequiredQueryIDs,
|
|
Config: q.Config,
|
|
}
|
|
|
|
dbType, err := resultprocessor.ToDBQueryType(create.Type)
|
|
assert.NoError(t, err)
|
|
|
|
pool.ExpectBeginTx(pgx.TxOptions{})
|
|
pool.ExpectQuery("name: CreateQuery :one").WithArgs(dbType).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(database.MustToDBUUID(q.ID)),
|
|
)
|
|
for _, req := range *create.RequiredQueryIDs {
|
|
pool.ExpectExec("name: AddRequiredQuery :exec").WithArgs(database.MustToDBUUID(q.ID), database.MustToDBUUID(req), int32(1)).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
}
|
|
pool.ExpectExec("name: AddQueryConfig :exec").WithArgs(database.MustToDBUUID(q.ID), []byte(*create.Config), int32(1)).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
id, err := svc.submitCreate(ctx, create)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, q.ID, id)
|
|
}
|
|
|
|
func TestSubmitCreateNoReqsOrConfig(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
svc := New(cfg, &Services{})
|
|
|
|
q := Query{
|
|
ID: uuid.New(),
|
|
Type: resultprocessor.TypeJsonExtractor,
|
|
}
|
|
create := &resultprocessor.Create{
|
|
Type: q.Type,
|
|
}
|
|
|
|
dbType, err := resultprocessor.ToDBQueryType(create.Type)
|
|
assert.NoError(t, err)
|
|
|
|
pool.ExpectBeginTx(pgx.TxOptions{})
|
|
pool.ExpectQuery("name: CreateQuery :one").WithArgs(dbType).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(database.MustToDBUUID(q.ID)),
|
|
)
|
|
pool.ExpectCommit()
|
|
|
|
id, err := svc.submitCreate(ctx, create)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, q.ID, id)
|
|
}
|
|
|
|
func TestNormalizeCreate(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
svc := New(cfg, &Services{})
|
|
|
|
create := &resultprocessor.Create{
|
|
Type: resultprocessor.TypeJsonExtractor,
|
|
RequiredQueryIDs: &[]uuid.UUID{},
|
|
}
|
|
|
|
dbids := database.MustToDBUUIDArray(*create.RequiredQueryIDs)
|
|
|
|
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(dbids).WillReturnRows(
|
|
pgxmock.NewRows([]string{"all_exist"}).
|
|
AddRow(true),
|
|
)
|
|
|
|
err = svc.normalizeCreate(ctx, create)
|
|
assert.NoError(t, err)
|
|
assert.EqualExportedValues(t, resultprocessor.Create{
|
|
Type: resultprocessor.TypeJsonExtractor,
|
|
Config: nil,
|
|
RequiredQueryIDs: nil,
|
|
}, *create)
|
|
}
|