mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 06:57:47 +01:00
feat(docs): report.md update and added options to test-with-ephemeral-mariadb.sh
This commit is contained in:
41
7project/backend/test-with-ephemeral-mariadb.sh
Normal file → Executable file
41
7project/backend/test-with-ephemeral-mariadb.sh
Normal file → Executable file
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user