name: Tests
on:
  pull_request:
    branches:
      - "*"
  push:
    branches:
      - "*"
  schedule:
    - cron: "0 1 * * *"
  workflow_dispatch:
jobs:
  unit-tests:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.8", "3.9", "3.10", "3.11", "pypy3.8", "3.12"]
        flask: ["<3.0.0", ">=3.0.0"]
    steps:
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          allow-prereleases: true
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install "flask${{ matrix.flask }}"
          pip install ".[test]"
      - name: Test with inv
        run: inv cover qa
      - name: Codecov
        uses: codecov/codecov-action@v1
        with:
          file: ./coverage.xml
  bench:
    needs: unit-tests
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
      - name: Set up Python 3.8
        uses: actions/setup-python@v4
        with:
          python-version: "3.8"
      - name: Checkout ${{ github.base_ref }}
        uses: actions/checkout@v3
        with:
          ref: ${{ github.base_ref}}
          path: base
      - name: Checkout ${{ github.ref }}
        uses: actions/checkout@v3
        with:
          ref: ${{ github.ref}}
          path: ref
      - name: Install dev dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e "base[dev]"
      - name: Install ci dependencies for ${{ github.base_ref }}
        run: pip install -e "base[ci]"
      - name: Benchmarks for ${{ github.base_ref }}
        run: |
          cd base
          inv benchmark --max-time 4 --save
          mv .benchmarks ../ref/
      - name: Install ci dependencies for ${{ github.ref }}
        run: pip install -e "ref[ci]"
      - name: Benchmarks for ${{ github.ref }}
        run: |
          cd ref
          inv benchmark --max-time 4 --compare
