Files
query-orchestration/internal/query/create_test.go
T
Michael McGuinness 7001ca854c Merged in feature/docinitialisation (pull request #41)
Queuing Changes and Cfg Testing

* staarting

* staarting

* startedpush

* note

* save

* mocking

* removederrs

* fixtests

* cleanuperrs

* newenvsetup

* preppingtests

* queue

* mmovetocfgpassunittests

* sortoutconfig

* passinginteg

* deps

* fixtests
2025-02-03 17:30:50 +00:00

133 lines
3.5 KiB
Go

package query_test
import (
"context"
"errors"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/query"
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 TestCreate(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 := query.New(cfg, &query.Services{})
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)
assert.NoError(t, err)
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray(*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(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.Create(ctx, create)
assert.NoError(t, err)
assert.Equal(t, q.ID, id)
}
func TestCreateMinimal(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 := query.New(cfg, &query.Services{})
q := query.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.Create(ctx, create)
assert.NoError(t, err)
assert.Equal(t, q.ID, id)
}
func TestCreateRollback(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 := query.New(cfg, &query.Services{})
create := &resultprocessor.Create{
Type: resultprocessor.TypeJsonExtractor,
}
dbType, err := resultprocessor.ToDBQueryType(create.Type)
assert.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)
}