59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
|
import os
|
||
|
from unittest import TestCase
|
||
|
from bootstrap import app
|
||
|
from hub.persistence.base_repo import BaseRepo
|
||
|
from sqlalchemy import create_engine
|
||
|
from hub.persistence.models import City
|
||
|
from hub.persistence.models import User
|
||
|
from sqlalchemy.exc import ProgrammingError
|
||
|
|
||
|
|
||
|
# function to ensure tests run in order shown in fle
|
||
|
def arrange():
|
||
|
order = {}
|
||
|
|
||
|
def ordered(f):
|
||
|
order[f.__name__] = len(order)
|
||
|
return f
|
||
|
|
||
|
def compare(a, b):
|
||
|
return [1, -1][order[a] < order[b]]
|
||
|
|
||
|
return ordered, compare
|
||
|
|
||
|
|
||
|
class BaseTest(TestCase):
|
||
|
"""
|
||
|
Tests for payment resource
|
||
|
"""
|
||
|
|
||
|
@classmethod
|
||
|
def setUpClass(cls):
|
||
|
os.environ['FLASK_ENV'] = 'testing'
|
||
|
cls.app = app
|
||
|
cls.client = cls.app.test_client()
|
||
|
|
||
|
# Create test database
|
||
|
repo = BaseRepo(db_name='test_db', app_env='TEST', dotenv_path="{}/.env".format(os.path.expanduser('~')))
|
||
|
eng = create_engine(f'postgresql://{repo.config.get_db_user()}@/{repo.config.get_db_user()}')
|
||
|
|
||
|
try:
|
||
|
# delete test database if it exists
|
||
|
conn = eng.connect()
|
||
|
conn.execute('commit')
|
||
|
conn.execute('DROP DATABASE test_db')
|
||
|
conn.close()
|
||
|
except ProgrammingError as err:
|
||
|
print(f'Database does not exist. Nothing to delete')
|
||
|
|
||
|
cnn = eng.connect()
|
||
|
cnn.execute('commit')
|
||
|
cnn.execute("CREATE DATABASE test_db")
|
||
|
cnn.close()
|
||
|
User.__table__.create(bind=repo.engine, checkfirst=True)
|
||
|
City.__table__.create(bind=repo.engine, checkfirst=True)
|
||
|
|
||
|
@classmethod
|
||
|
def tearDownClass(cls) -> None:
|
||
|
pass
|