name: Tests
permissions: read-all

on:
  pull_request:
    branches:
    - main
  workflow_dispatch:

jobs:
  unit-tests:
    name: Unit Tests
    runs-on: ${{ matrix.os }}
    strategy:
      max-parallel: 4
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]
    steps:
    - name: Checkout
      uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
    - name: Set up Go
      uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
      with:
        go-version: stable
    - name: Run unit tests with coverage and race conditions checking
      if: matrix.os == 'ubuntu-latest'
      run: make test
    - name: Run unit tests without coverage and race conditions checking
      if: matrix.os != 'ubuntu-latest'
      run: go test ./...

  integration-tests:
    name: Integration Tests
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:11
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: postgres
        ports:
        - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    steps:
    - name: Checkout
      uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
    - name: Set up Go
      uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
      with:
        go-version: stable
    - name: Install Postgres client
      run: sudo apt update && sudo apt install -y postgresql-client
    - name: Run integration tests
      run: make integration-test
      env:
        PGHOST: 127.0.0.1
        PGPASSWORD: postgres
