name: Tests

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

permissions:
  contents: read

jobs:
  test:
    name: Python ${{ matrix.python-version }}, Django ${{ matrix.django-version }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        python-version: ['3.8', '3.9', '3.10', '3.11']
        django-version: ['3.2', '4.0', '4.1', '4.2']
    services:
      postgres:
        image: postgres:14
        ports:
          - 5432/tcp
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: postgres
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pytest
        pip install -e .[test]
        pip install -r requirements.txt
        pip install "django~=${{ matrix.django-version }}.0"
    - name: Run tests
      env:
        DATABASE_USER: postgres
        DATABASE_PASSWORD: postgres
        DATABASE_HOST: localhost
        DATABASE_PORT: ${{ job.services.postgres.ports[5432] }} # get randomly assigned published port
      run: |
        coverage run --parallel -m pytest -x
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v4.5.0
      with:
        fail_ci_if_error: false
  lint_python:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: '3.11'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -e .[test]
        pip install -r requirements.txt
    - name: Run linters
      run: make lint
  lint_js:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up NodeJS
      uses: actions/setup-node@v4
      with:
        node-version: '16'
    - name: Install dependencies
      run: |
        npm ci
    - name: Run linters
      run: |
        npm run eslint
  sandbox:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: '3.11'
    - name: Build sandbox
      run: |
        python -m pip install --upgrade pip
        make sandbox
  docs:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: '3.11'
    - name: Build docs
      run: |
        make docs
