#! /bin/bash

set -e

# Wait for postgres to be up

PYTHONUNBUFFERED=1 python -c 'import psycopg2
import sys

from tracker import config

if "localhost" not in config.db_connection_uri:
    print("Non local database. Not safe for test!")
    sys.exit(45)

print("Will wait for db to be ready.")
for _ in range(10):
    try:
        psycopg2.connect(config.db_connection_uri)
        print("Db ready!")
        sys.exit(0)
    except Exception as e:
        print("DB not available. Sleeping...", e)
        import time; time.sleep(1)
print("Failed to find db after a long time trying.  FAtaLITY")
sys.exit(100)
'

echo -e "-- Running Alembic --\n\nhead revision:\n`alembic show head`\n"
alembic upgrade head || exit 5

echo "Beginning tests"

# /testlogs should be a mounted volume.
py.test ./tracker/test/unit -vv | tee /testlogs/test_results_`date +%s`.txt

echo "Running API Tests"
