name: Test django-treebeard

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

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint-python:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
        with:
          ref: ${{ github.ref }}
      - uses: astral-sh/ruff-action@v3
        with:
          version: "0.14.x"
      - run: ruff check --fix
      - run: ruff format --check --diff
  test-sqlite:
    name: SQlite, Python ${{ matrix.python-version }}, Django ${{ matrix.django-version }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        python-version: [3.12, 3.14]
        django-version: [52, 60]
    steps:
    - uses: actions/checkout@v5
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
        cache: 'pip'
    - name: Install dependencies
      run: pip install tox
    - name: Run tests
      run: tox -e "py-dj${{ matrix.django-version }}-sqlite"
  test-postgres:
    name: Postgres, Python ${{ matrix.python-version }}, Django ${{ matrix.django-version }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        python-version: [3.12, 3.14]
        django-version: [52, 60]
    services:
      postgres:
        image: postgres:15
        ports:
          - 5432/tcp
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
        env:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: treebeard
          POSTGRES_DB: treebeard
    steps:
    - uses: actions/checkout@v5
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
        cache: 'pip'
    - name: Install dependencies
      run: pip install tox
    - name: Run tests
      env:
        DATABASE_USER_POSTGRES: root
        DATABASE_PASSWORD: treebeard
        DATABASE_HOST: 127.0.0.1
        DATABASE_PORT_POSTGRES: ${{ job.services.postgres.ports[5432] }} # get randomly assigned published port
      # Postgres covers the entire codebase, so we enforce a coverage check
      run: tox -e "py-dj${{ matrix.django-version }}-postgres" -- --cov-fail-under 96
  test-mysql:
    name: MySQL, Python ${{ matrix.python-version }}, Django ${{ matrix.django-version }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        python-version: [3.12, 3.14]
        django-version: [52, 60]
    services:
      mysql:
        image: mysql:9.5
        options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 5
        env:
          MYSQL_ROOT_PASSWORD: treebeard
        ports:
          - 3306/tcp
    steps:
    - uses: actions/checkout@v5
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
        cache: 'pip'
    - name: Install dependencies
      run: pip install tox
    - name: Run tests
      env:
        DATABASE_USER_MYSQL: root
        DATABASE_PASSWORD: treebeard
        DATABASE_HOST: 127.0.0.1
        DATABASE_PORT_MYSQL: ${{ job.services.mysql.ports[3306] }} # get randomly assigned published port
      run: tox -e "py-dj${{ matrix.django-version }}-mysql"
  test-mariadb:
    name: MariaDB, Python ${{ matrix.python-version }}, Django ${{ matrix.django-version }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        python-version: [3.12, 3.14]
        django-version: [52, 60]
    services:
      mysql:
        image: mariadb:10.6
        options: --health-cmd="healthcheck.sh --connect" --health-interval 10s --health-timeout 5s --health-retries 5
        env:
          MYSQL_ROOT_PASSWORD: treebeard
        ports:
          - 3306/tcp
    steps:
    - uses: actions/checkout@v5
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
        cache: 'pip'
    - name: Install dependencies
      run: pip install tox
    - name: Run tests
      env:
        DATABASE_USER_MYSQL: root
        DATABASE_PASSWORD: treebeard
        DATABASE_HOST: 127.0.0.1
        DATABASE_PORT_MYSQL: ${{ job.services.mysql.ports[3306] }} # get randomly assigned published port
      run: tox -e "py-dj${{ matrix.django-version }}-mysql"
  test-mssql:
    name: MSSQL, Python ${{ matrix.python-version }}, Django ${{ matrix.django-version }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        python-version: [3.12, 3.14]
        django-version: [52]    # mssql-django doesn't yet support Django 6
    services:
      mssql:
        image: mcr.microsoft.com/mssql/server:2019-latest
        env:
          SA_PASSWORD: Password12!
          ACCEPT_EULA: 'Y'
        ports:
          - 1433/tcp
    steps:
    - uses: actions/checkout@v5
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
        cache: 'pip'
    - name: Install dependencies
      run: pip install tox
    - name: Install ODBC driver for MSSQL
      run: |
        curl -sSL -O https://packages.microsoft.com/config/ubuntu/$(grep VERSION_ID /etc/os-release | cut -d '"' -f 2)/packages-microsoft-prod.deb  
        sudo dpkg -i packages-microsoft-prod.deb
        sudo apt-get update
        sudo ACCEPT_EULA=Y apt-get install msodbcsql18 mssql-tools18 -y
    - name: Run tests
      env:
        DATABASE_PORT_MSSQL: ${{ job.services.mssql.ports[1433] }} # get randomly assigned published port
      run: tox -e "py-dj${{ matrix.django-version }}-mssql"