stages:
  - test

tests:
  stage: test
  image: python:3.11
  variables:
    DJANGO_SETTINGS_MODULE: "la_chariotte.settings"
    DB_NAME: "postgres"
    DB_USER: "postgres"
    DB_PASSWORD: "mysecretpassword"
    DB_HOST: "postgres"
    DB_PORT: "5432"
    POSTGRES_HOST_AUTH_METHOD: "trust"
  services:
    - postgres:13
  script:
    - export CACHE_PATH="/venvs/${CI_PROJECT_NAME}/$(date +week-%V-%Y)"
    - if [[ "$CI_COMMIT_BRANCH" == *"--no-cache"* ]] ; then export CACHE_PATH="/venvs/$CI_COMMIT_SHORT_SHA"; fi
    - echo "Creating virtual env..."
    - if [ ! -d $CACHE_PATH/venv ] ; then python3.11 -m venv $CACHE_PATH/venv ; fi
    - $CACHE_PATH/venv/bin/pip install --upgrade pip
    - echo "Virtual env created."
    - echo "Running tests..."
    - source $CACHE_PATH/venv/bin/activate
    - pip cache purge
    - pip install -e ".[dev]"
    - pytest --create-db --cov --cov-report=xml
    - if [ "$CI_COMMIT_REF_NAME" = 'develop' ] ; then git fetch origin develop ; diff-cover coverage.xml --fail-under=90 --compare-branch origin/develop --diff-range-notation '..' && exit 0 ; fi
    - git fetch origin develop ; diff-cover coverage.xml --fail-under=90 --compare-branch origin/develop --diff-range-notation '..'
    - echo "Tests done."

