From aade78bf3f20497d8779075e82e8b032eaa07b94 Mon Sep 17 00:00:00 2001 From: ribardej Date: Wed, 12 Nov 2025 14:12:04 +0100 Subject: [PATCH] feat(docs): report.md update and added options to test-with-ephemeral-mariadb.sh --- .../backend/test-with-ephemeral-mariadb.sh | 43 +++++++++++++++++-- 7project/report.md | 19 +++++--- 2 files changed, 52 insertions(+), 10 deletions(-) mode change 100644 => 100755 7project/backend/test-with-ephemeral-mariadb.sh diff --git a/7project/backend/test-with-ephemeral-mariadb.sh b/7project/backend/test-with-ephemeral-mariadb.sh old mode 100644 new mode 100755 index 650edd2..f9b9645 --- a/7project/backend/test-with-ephemeral-mariadb.sh +++ b/7project/backend/test-with-ephemeral-mariadb.sh @@ -5,7 +5,12 @@ set -euo pipefail # Requirements: Docker, docker compose plugin, Python, Alembic, pytest. # Usage: # chmod +x ./test-with-ephemeral-mariadb.sh -# ./test-with-ephemeral-mariadb.sh +# # From 7project/backend directory +# ./test-with-ephemeral-mariadb.sh [--only-unit|--only-integration|--only-e2e] [pytest-args...] +# # Examples: +# ./test-with-ephemeral-mariadb.sh --only-unit -q +# ./test-with-ephemeral-mariadb.sh --only-integration -k "login" +# ./test-with-ephemeral-mariadb.sh --only-e2e -vv # # This script will: # 1) Start a MariaDB 11.4 container (ephemeral storage, port 3307) @@ -64,13 +69,45 @@ export MARIADB_PASSWORD=apppass export DATABASE_URL="mysql+asyncmy://$MARIADB_USER:$MARIADB_PASSWORD@$MARIADB_HOST:$MARIADB_PORT/$MARIADB_DB" export PYTEST_RUN_CONFIG="True" +# Determine which tests to run based on flags +UNIT_TESTS="tests/test_unit_user_service.py" +INTEGRATION_TESTS="tests/test_integration_app.py" +E2E_TESTS="tests/test_e2e.py" + +FLAG_COUNT=0 +TEST_TARGET="" +declare -a PYTEST_ARGS=() +for arg in "$@"; do + case "$arg" in + --only-unit) + TEST_TARGET="$UNIT_TESTS"; FLAG_COUNT=$((FLAG_COUNT+1));; + --only-integration) + TEST_TARGET="$INTEGRATION_TESTS"; FLAG_COUNT=$((FLAG_COUNT+1));; + --only-e2e) + TEST_TARGET="$E2E_TESTS"; FLAG_COUNT=$((FLAG_COUNT+1));; + *) + PYTEST_ARGS+=("$arg");; + esac +done + +if [ "$FLAG_COUNT" -gt 1 ]; then + echo "Error: Use only one of --only-unit, --only-integration, or --only-e2e" >&2 + exit 2 +fi + # Run Alembic migrations then tests pushd . >/dev/null echo "Running Alembic migrations..." alembic upgrade head echo "Running pytest..." - pytest "$@" + if [ -n "$TEST_TARGET" ]; then + # Use "${PYTEST_ARGS[@]:-}" to safely expand empty array with 'set -u' + pytest "$TEST_TARGET" "${PYTEST_ARGS[@]:-}" + else + # Use "${PYTEST_ARGS[@]:-}" to safely expand empty array with 'set -u' + pytest "${PYTEST_ARGS[@]:-}" + fi popd >/dev/null -# Cleanup handled by trap +# Cleanup handled by trap \ No newline at end of file diff --git a/7project/report.md b/7project/report.md index 2a7e718..ba7b4a0 100644 --- a/7project/report.md +++ b/7project/report.md @@ -269,28 +269,33 @@ open http://localhost:5173 ``` ## Testing Instructions -The tests are located in 7project/backend/tests directory -If you want to test locally, you have to have the DB running locally as well (start the docker compose in /backend). +The tests are located in 7project/backend/tests directory. All tests are run by GitHub actions on every pull request and push to main. +See the workflow [here](../.github/workflows/run-tests.yml). + +If you want to run the tests locally, the preferred is to use a [bash script](backend/test-with-ephemeral-mariadb.sh) +that will start a [test DB container](backend/docker-compose.test.yml) and remove it afterward. ```bash -cd backend +cd 7project/backend +bash test-with-ephemeral-mariadb.sh ``` + ### Unit Tests There are only 3 basic unit tests, since our services logic is very simple ```bash -pytest tests/test_unit_user_service.py +bash test-with-ephemeral-mariadb.sh --only-unit ``` ### Integration Tests There are 11 basic unit tests, testing the individual backend API logic ```bash -pytest tests/test_integration_app.py +bash test-with-ephemeral-mariadb.sh --only-integration ``` ### End-to-End Tests -There are 7 e2e tests testing more complex app logic +There are 7 e2e tests, testing more complex app logic ```bash -pytest tests/test_e2e.py +bash test-with-ephemeral-mariadb.sh --only-e2e ``` ## Usage Examples