test: fix: migrator: multifile test

This commit is contained in:
2025-11-17 01:57:32 +00:00
parent 00f05cc3db
commit 5b1f05589d
6 changed files with 153 additions and 53 deletions

View File

@@ -62,7 +62,10 @@ export DATABASE_URL
log info "run test ${WHITE}${test_name}${NC}"
# run test
. "${test}/run.sh"
mkdir './test'
cp -r "$test"/* './test/'
cd './test'
. './run.sh'
HECTIC_NAMESPACE=test-laucher

View File

@@ -2,63 +2,65 @@
HECTIC_NAMESPACE=test-create-migration
log notice "case: ${WHITE}first migration"
log notice "test case: ${WHITE}first migration"
if ! migrator create; then
log error "test failed: error on migration creation"
log error "test failed: ${WHITE}error on migration creation"
exit 1
fi
if ! [ -d ./migration ]; then
log error "test failed: migration directory not created"
log error "test failed: ${WHITE}migration directory not created"
exit 1
fi
if [ "$(find ./migration -maxdepth 1 -type f | wc -l)" -eq 0 ]; then
log error "test failed: migration not created"
log error "test failed: ${WHITE}migration not created"
exit 1
fi
log notice "case: ${WHITE}next migration"
log notice "test case: ${WHITE}next migration"
if ! migrator create; then
log error "test failed: error on migration creation"
log error "test failed: ${WHITE}error on migration creation"
exit 1
fi
if [ "$(find ./migration -maxdepth 1 -type f | wc -l)" -eq 1 ]; then
log error "test failed: migration not created"
log error "test failed: ${WHITE}migration not created"
exit 1
fi
log notice "case: ${WHITE}migration with custom name"
log notice "test case: ${WHITE}migration with custom name"
if ! migrator create --name test; then
log error "test failed: error on migration creation"
log error "test failed: ${WHITE}error on migration creation"
exit 1
fi
if [ "$(find ./migration -maxdepth 1 -type f | wc -l)" -eq 2 ]; then
log error "test failed: migration not created"
log error "test failed: ${WHITE}migration not created"
exit 1
fi
if ! find ./migration -maxdepth 1 -type f -name '*test.sql' \
| grep -Eq '/[0-9]{14}-test\.sql$'; then
log eror "test failed: migration have unexpected name"
log eror "test failed: ${WHITE}migration have unexpected name"
exit 1
fi
log notice "case: ${WHITE}migration with custom name that contains space"
log notice "test case: ${WHITE}migration with custom name that contains space"
if ! migrator create --name 'test name'; then
log error "test failed: error on migration creation"
log error "test failed: ${WHITE}error on migration creation"
exit 1
fi
if [ "$(find ./migration -maxdepth 1 -type f | wc -l)" -eq 3 ]; then
log error "test failed: migration not created"
log error "test failed: ${WHITE}migration not created"
exit 1
fi
if ! find ./migration -maxdepth 1 -type f -name '*test name.sql' \
| grep -Eq '/[0-9]{14}-test name\.sql$'; then
log eror "test failed: migration have unexpected name"
log eror "test failed: ${WHITE}migration have unexpected name"
exit 1
fi
log notice "test passed"

View File

@@ -2,17 +2,57 @@
HECTIC_NAMESPACE=test-init-migrator
log info "hectic.migration table inheritance"
### CASE 1
log notice "test case: ${WHITE}table inherit tables that not exists"
if ! migration_table_sql="$(migrator --inherits tablename --inherits 'table name' init --dry-run)"; then
log error "test failed: error on migration table init dry run"
log error "test failed: ${WHITE}error on migration table init dry run"
exit 1
fi
printf '%s' "$migration_table_sql" | grep -Eq 'INHERITS[[:space:]]*\([[:space:]]*"tablename"[[:space:]]*,[[:space:]]*"table name"[[:space:]]*\)' ||
{ log error "not correct migration table inherits"; exit 1; }
{ log error "test failed: ${WHITE}not correct migration table inherits"; exit 1; }
log info "init"
if ! migrator --inherits tablename --inherits 'table name' init; then
log error "test failed: error on init sql"
### CASE 2
log notice "test case: ${WHITE}error: table inherit tables that not exists"
set +e
migrator --inherits tablename --inherits 'table name' init --db-url "$DATABASE_URL"
error_code=$?
set -e
if [ "$error_code" = 0 ]; then
log error "test failed: ${WHITE}no error handler"
exit 1
elif [ "$error_code" != 5 ]; then
log error "test failed: ${WHITE}unexpected error code"
exit 1
fi
printf 'SELECT * FROM hectic.migration' | psql -v ON_ERROR_STOP=1 "$DATABASE_URL"
### CASE 3
log notice "test case: ${WHITE}error: not provided --db-url"
set +e
migrator --inherits tablename --inherits 'table name' init
error_code=$?
set -e
if [ "$error_code" = 0 ]; then
log error "test failed: ${WHITE}no error handler"
exit 1
elif [ "$error_code" != 3 ]; then
log error "test failed: ${WHITE}unexpected error code"
exit 1
fi
### CASE 4
log notice "test case: ${WHITE}normal init"
psql "$DATABASE_URL" -c 'CREATE TABLE "table name"(); CREATE TABLE tablename();'
if ! migrator --inherits tablename --inherits 'table name' init --db-url "$DATABASE_URL"; then
log error "test failed: ${WHITE}error on init sql"
fi
psql -v ON_ERROR_STOP=1 "$DATABASE_URL" -c 'SELECT * FROM hectic.migration'
log notice "test passed"

View File

@@ -2,4 +2,15 @@
HECTIC_NAMESPACE=test-migration-list
#migrator list
log notice "test case: ${WHITE}getting list of local migrations"
if ! list="$(migrator list)"; then
log error "test failed: ${WHITE}error during execution"
exit 1
fi
ls
ls migration
exit 1
log notice "test passed"

View File

@@ -2,6 +2,8 @@
HECTIC_NAMESPACE=test-missing-psql
log notice "test case: ${WHITE}error: run migrator without postgresql tools installed"
# remove psql from $PATH
dir=$(dirname -- "$(command -v psql)")
PATH=$(printf '%s' "$PATH" | awk -v RS=: -v ORS=: -v d="$dir" '$0!=d{print}')
@@ -18,8 +20,10 @@ set -e
log debug "migrator error code: $migrator_error_code"
if [ "$migrator_error_code" -eq 127 ]; then
log notice "test passed"
else
log error "test failed"
if [ "$migrator_error_code" -eq 0 ]; then
log error "test failed: ${WHITE}no error handled"
elif [ "$migrator_error_code" -ne 127 ]; then
log error "test failed: ${WHITE}unexpected error code"
fi
log notice "test passed"