# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Maintainer: Carl Smedstad <carsme@archlinux.org>
# Contributor: Felix Yan <felixonmars@archlinux.org>
# Contributor: Karol 'Kenji Takahashi' Woźniak <kenji.sx>
# Contributor: Pierre Gueth <pierre.gueth@gmail.com>

pkgname=python-redis
pkgver=5.2.0
pkgrel=1
pkgdesc='The Python interface to the Redis key-value store'
arch=(any)
url='https://github.com/redis/redis-py'
license=('MIT')
depends=(
  'python'
  'python-async-timeout'
)
makedepends=(
  'git'
  'python-build'
  'python-installer'
  'python-setuptools'
  'python-wheel'
)
checkdepends=(
  'python-coverage'
  'python-cryptography'
  'python-hiredis'
  'python-numpy'
  'python-pyopenssl'
  'python-pytest'
  'python-pytest-asyncio'
  'python-requests'
  'redis'
)
optdepends=(
  'python-cryptography: OCSP certificate validation'
  'python-hiredis: faster performance via hiredis'
  'python-pyopenssl: OCSP certificate validation'
  'python-requests: OCSP certificate validation'
)
source=("$pkgname::git+$url#tag=v$pkgver")
b2sums=('2a7cd4348c11814cdd7076e26dad94970c0106e0a6d207350d7fac19a32ab5e6e4f28860a12fdf4bb83a7f3438d6753c1989f195a4cd5b87ceaaa0cc02e26a24')

prepare() {
  cd "$pkgname"
  # Compatibiltiy with recent python-pytest-asyncio.
  sed -i 's/@pytest.mark.asyncio(forbid_global_loop=True)/@pytest.mark.asyncio/g' \
    tests/test_asyncio/test_scripting.py
}

build() {
  cd "$pkgname"
  python -m build --wheel --no-isolation
}

check() {
  cd "$pkgname"
  # shellcheck disable=SC2317
  _teardown() {
    redis-cli -p 6379 shutdown
    redis-cli -p 6380 shutdown
    for port in $(seq 16379 16384); do
      redis-cli -p "$port" shutdown;
    done
    rm -rf cluster
  }
  trap '_teardown' EXIT

  # Setup single instance and replica.
  redis-server \
    --enable-debug-command yes \
    --enable-module-command yes \
    --daemonize yes \
    --port 6379
  redis-server \
    --replicaof 127.0.0.1 6379 \
    --daemonize yes \
    --port 6380

  # Setup cluster.
  for port in $(seq 16379 16384); do
    mkdir -p "cluster/$port"
    pushd "cluster/$port"
    redis-server \
      --cluster-enabled yes \
      --enable-debug-command yes \
      --dir "$PWD" \
      --logfile "$PWD/redis.log" \
      --daemonize yes \
      --port "$port"
    popd
  done
  yes yes | redis-cli --cluster create \
    127.0.0.1:16379 \
    127.0.0.1:16380 \
    127.0.0.1:16381 \
    127.0.0.1:16382 \
    127.0.0.1:16383 \
    127.0.0.1:16384 \
    --cluster-replicas 1

  local pytest_deselect_args=(
    # Tests that are testing, and as such depend on, modules that are part of
    # Redis Stack. These are not packaged since they are not OSS, only source
    # available.
    --deselect=tests/test_asyncio/test_bloom.py
    --deselect=tests/test_asyncio/test_graph.py
    --deselect=tests/test_asyncio/test_json.py
    --deselect=tests/test_asyncio/test_search.py
    --deselect=tests/test_asyncio/test_timeseries.py
    --deselect=tests/test_bloom.py
    --deselect=tests/test_cluster.py::TestClusterRedisCommands::test_tfcall
    --deselect=tests/test_cluster.py::TestClusterRedisCommands::test_tfunction_list
    --deselect=tests/test_cluster.py::TestClusterRedisCommands::test_tfunction_load_delete
    --deselect=tests/test_commands.py::TestRedisCommands::test_module
    --deselect=tests/test_commands.py::TestRedisCommands::test_module_loadex
    --deselect=tests/test_commands.py::TestRedisCommands::test_tfcall
    --deselect=tests/test_commands.py::TestRedisCommands::test_tfunction_list
    --deselect=tests/test_commands.py::TestRedisCommands::test_tfunction_load_delete
    --deselect=tests/test_graph.py
    --deselect=tests/test_json.py
    --deselect=tests/test_search.py
    --deselect=tests/test_timeseries.py

    # Tests that freezes, unsure of why.
    --deselect=tests/test_asyncio/test_cluster.py::TestRedisClusterObj::test_address_remap
  )

  # Run standalone test suite - targets the Redis server running :6379 and the
  # replica on :6380.
  pytest \
    -m "not onlycluster and not ssl" \
    "${pytest_deselect_args[@]}"

  # Run cluster tests suite - targets the Redis cluster running on
  # :16379-:16384.
  pytest \
    --redis-url="redis://localhost:16379/0" \
    -m "not onlynoncluster and not redismod and not ssl" \
    "${pytest_deselect_args[@]}"
}

package() {
  cd "$pkgname"
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE
}
