7ce7c9df4d
Feature/ecr * nosave * repo * awscli * unzip * ignore * moreram * 14k * ref * deployment * 12k * uselocal * go * dockercomd * reorder * iamgename * installs * tart * cli * clideps * y * dockerce * nodock * multi * rmecr * dev
142 lines
3.7 KiB
Go
142 lines
3.7 KiB
Go
package query_test
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/query"
|
|
resultprocessor "queryorchestration/internal/query/result/processor"
|
|
"queryorchestration/internal/serviceconfig"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCreate(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
svc := query.New(cfg)
|
|
|
|
config := "{\"path\":\"example_path\"}"
|
|
q := query.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)
|
|
require.NoError(t, err)
|
|
|
|
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(*create.RequiredQueryIDs).WillReturnRows(
|
|
pgxmock.NewRows([]string{"all_exist"}).AddRow(true),
|
|
)
|
|
|
|
pool.ExpectBeginTx(pgx.TxOptions{})
|
|
pool.ExpectQuery("name: CreateQuery :one").WithArgs(dbType).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(q.ID),
|
|
)
|
|
pool.ExpectQuery("name: AddLatestQueryVersion :one").WithArgs(q.ID).WillReturnRows(
|
|
pgxmock.NewRows([]string{"version"}).
|
|
AddRow(int32(1)),
|
|
)
|
|
pool.ExpectExec("name: AddActiveQueryVersion :exec").WithArgs(q.ID, int32(1)).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
for _, req := range *create.RequiredQueryIDs {
|
|
pool.ExpectExec("name: AddRequiredQuery :exec").WithArgs(q.ID, req, int32(1)).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
}
|
|
pool.ExpectExec("name: SetQueryConfig :exec").WithArgs(q.ID, []byte(*create.Config), int32(1)).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
id, err := svc.Create(ctx, create)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, q.ID, id)
|
|
}
|
|
|
|
func TestCreateMinimal(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
svc := query.New(cfg)
|
|
|
|
q := query.Query{
|
|
ID: uuid.New(),
|
|
Type: resultprocessor.TypeContextFull,
|
|
}
|
|
create := &resultprocessor.Create{
|
|
Type: q.Type,
|
|
}
|
|
|
|
dbType, err := resultprocessor.ToDBQueryType(create.Type)
|
|
require.NoError(t, err)
|
|
|
|
pool.ExpectBeginTx(pgx.TxOptions{})
|
|
pool.ExpectQuery("name: CreateQuery :one").WithArgs(dbType).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(q.ID),
|
|
)
|
|
pool.ExpectQuery("name: AddLatestQueryVersion :one").WithArgs(q.ID).WillReturnRows(
|
|
pgxmock.NewRows([]string{"version"}).
|
|
AddRow(int32(1)),
|
|
)
|
|
pool.ExpectExec("name: AddActiveQueryVersion :exec").WithArgs(q.ID, int32(1)).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
id, err := svc.Create(ctx, create)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, q.ID, id)
|
|
}
|
|
|
|
func TestCreateRollback(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
svc := query.New(cfg)
|
|
|
|
ccfg := "{}"
|
|
create := &resultprocessor.Create{
|
|
Type: resultprocessor.TypeJsonExtractor,
|
|
Config: &ccfg,
|
|
}
|
|
|
|
dbType, err := resultprocessor.ToDBQueryType(create.Type)
|
|
require.NoError(t, err)
|
|
|
|
pool.ExpectBeginTx(pgx.TxOptions{})
|
|
msg := "database failure"
|
|
pool.ExpectQuery("name: CreateQuery :one").WithArgs(dbType).WillReturnError(errors.New(msg))
|
|
pool.ExpectRollback()
|
|
|
|
_, err = svc.Create(ctx, create)
|
|
assert.EqualError(t, err, msg)
|
|
}
|