#!/bin/bash
#---------------------
# Testing sqlalchemy-utils
#---------------------
ret=0

# Prepare Postgres
sed  -i 's/local\s\+all\s\+postgres\s\+peer/local all postgres trust\nhost all postgres 127.0.0.1\/32 trust/' /etc/postgresql/*/main/pg_hba.conf
systemctl restart postgresql

# Prepare MySQL
systemctl restart mysql
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';"

# Cleanup existing test databases (ignore errors on first run)
psql -c 'drop database sqlalchemy_utils_test;' -U postgres || true
psql -c "ALTER USER postgres PASSWORD 'password';" -U postgres
mysql -u root -ppassword -e 'drop database sqlalchemy_utils_test;' || true

set -e
# Setup test databases
psql -c 'create database sqlalchemy_utils_test;' -U postgres
psql -c 'create extension hstore;' -d sqlalchemy_utils_test -U postgres
mysql -u root -ppassword -e 'create database sqlalchemy_utils_test;'

export SQLALCHEMY_UTILS_TEST_MYSQL_USER="root"
export SQLALCHEMY_UTILS_TEST_MYSQL_PASSWORD="password"
export SQLALCHEMY_UTILS_TEST_POSTGRESQL_USER="postgres"
export SQLALCHEMY_UTILS_TEST_DB="sqlalchemy_utils_test"
export SQLALCHEMY_UTILS_TEST_POSTGRESQL_PASSWORD="password"

# Run tests
# test_timezone.py requires dateutil's bundled zoneinfo tarball which is
# stripped from the Ubuntu package per policy (LP: #1887664)
pytest-3 sqlalchemy_utils tests --ignore=tests/types/test_timezone.py
