name: Unit Tests

on:
  push:
    branches:
      - develop
      - master
      - release-*.*
  pull_request:
    branches:
      - develop
      - release-*.*

jobs:
  unittests:
    name: "py${{ matrix.python-version }}-${{ matrix.django-version }}-${{ matrix.db }}"
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 7
      matrix:
        python-version: [3.6, 3.7, 3.8, 3.9]
        django-version: [django210, django220, django300, django310]
        db: [sqlite]
        include:
          - python-version: 3.8
            django-version: django300
            db: mysql
          - python-version: 3.8
            django-version: django300
            db: mariadb
          - python-version: 3.8
            django-version: django300
            db: postgres
          - python-version: 3.8
            django-version: django310
            db: mysql
          - python-version: 3.8
            django-version: django310
            db: mariadb
          - python-version: 3.8
            django-version: django310
            db: postgres

    steps:
      - name: Install packages used to setup testenv
        run: |
          sudo apt update
          sudo apt-get install gcc python3-dev libkrb5-dev libmariadb-dev libxcrypt-dev

      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install tox "tox-docker==1.7.0" coveralls

      - run: |
          echo "tox_pyver=${pyver}" | sed "s/\.//" >> $GITHUB_ENV
        env:
          pyver: ${{ matrix.python-version }}

      - name: Tox
        run: |
          tox -e py$tox_pyver-${{ matrix.django-version }}-${{ matrix.db }}

      - name: Report coverage
        run: coveralls
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          COVERALLS_FLAG_NAME: testenv-py${{ matrix.python-version }}-${{ matrix.django-version }}-${{ matrix.db }}
          COVERALLS_PARALLEL: true

  coveralls:
    name: Finish Coveralls
    needs: unittests
    runs-on: ubuntu-latest
    steps:
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.8
      - name: Finished
        run: |
          python -m pip install --upgrade coveralls
          coveralls --finish
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
