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

# Prepare Postgres and Mysql
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
echo "default-authentication-plugin=mysql_native_password" >> /etc/mysql/mysql.conf.d/mysqld.cnf
systemctl restart postgresql
systemctl restart mysql

# Cleanup existing test databases
psql -c 'drop database sqlalchemy_utils_test;' -U postgres
mysql -e 'drop database sqlalchemy_utils_test;'
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';"

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 -e 'create database sqlalchemy_utils_test;'

export SQLALCHEMY_UTILS_TEST_MYSQL_USER="root"
export SQLALCHEMY_UTILS_TEST_POSTGRESQL_USER="postgres"
export SQLALCHEMY_UTILS_TEST_DB="sqlalchemy_utils_test"

# Run tests
pytest-3 sqlalchemy_utils tests
