name: Test qlib from source slow

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    timeout-minutes: 720
    # we may retry for 3 times for `Unit tests with Pytest`

    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        # Since macos-latest changed from 12.7.4 to 14.4.1,
        # the minimum python version that matches a 14.4.1 version of macos is 3.10,
        # so we limit the macos version to macos-12.
        os: [windows-latest, ubuntu-20.04, ubuntu-22.04, macos-11, macos-12]
        # not supporting 3.6 due to annotations is not supported https://stackoverflow.com/a/52890129
        python-version: [3.7, 3.8]

    steps:
    - name: Test qlib from source slow
      uses: actions/checkout@v3

    # Since version 3.7 of python for MacOS is installed in CI, version 3.7.17, this version causes "_bz not found error".
    # So we make the version number of python 3.7 for MacOS more specific.
    # refs: https://github.com/actions/setup-python/issues/682
    - name: Set up Python ${{ matrix.python-version }}
      if: (matrix.os == 'macos-latest' && matrix.python-version == '3.7') || (matrix.os == 'macos-11' && matrix.python-version == '3.7')
      uses: actions/setup-python@v4
      with:
        python-version: "3.7.16"

    - name: Set up Python ${{ matrix.python-version }}
      if: (matrix.os != 'macos-latest' || matrix.python-version != '3.7') && (matrix.os != 'macos-11' || matrix.python-version != '3.7')
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}

    - name: Set up Python tools
      run: |
        python -m pip install --upgrade pip
        pip install --upgrade cython numpy
        pip install -e .[dev]

    - name: Downloads dependencies data
      run: |
        python scripts/get_data.py qlib_data --name qlib_data_simple --target_dir ~/.qlib/qlib_data/cn_data --interval 1d --region cn

    - name: Install Lightgbm for MacOS
      if: ${{ matrix.os == 'macos-11' || matrix.os == 'macos-latest' }}
      run: |
        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Microsoft/qlib/main/.github/brew_install.sh)"
        HOMEBREW_NO_AUTO_UPDATE=1 brew install lightgbm
        # FIX MacOS error: Segmentation fault
        # reference: https://github.com/microsoft/LightGBM/issues/4229
        wget https://raw.githubusercontent.com/Homebrew/homebrew-core/fb8323f2b170bd4ae97e1bac9bf3e2983af3fdb0/Formula/libomp.rb
        brew unlink libomp
        brew install libomp.rb

    - name: Unit tests with Pytest
      uses: nick-fields/retry@v2
      with:
        timeout_minutes: 240
        max_attempts: 3
        command: |
          cd tests
          python -m pytest . -m "slow" --durations=0
