"""Utils for db tests.""" from ows_accounting import config from ows_accounting.models import accounting_period from ows_accounting.models import currency from ows_accounting.models import currency_exchange_rates from ows_accounting.utils import mysql session = mysql._db_session() currencies_data = [{ 'id': 1, 'ISO_4217_code': 'USD', 'symbol_html_entity_code': '$', 'currency_name': 'United States Dollar' }, { 'id': 4, 'ISO_4217_code': 'EUR', 'symbol_html_entity_code': '€', 'currency_name': 'European Euro' }] accounting_periods_data = [{ 'id': 1, 'year': 2017, 'quarter': '1', 'month': '1' }, { 'id': 2, 'year': 2017, 'quarter': '1', 'month': '2' }, { 'id': 3, 'year': 2017, 'quarter': '1', 'month': '3' }, { 'id': 4, 'year': 2017, 'quarter': '2', 'month': '4' }, { 'id': 5, 'year': 2017, 'quarter': '2', 'month': '5' }, { 'id': 6, 'year': 2017, 'quarter': '2', 'month': '6' }, { 'id': 7, 'year': 2017, 'quarter': '3', 'month': '7' }, { 'id': 8, 'year': 2017, 'quarter': '3', 'month': '8' }, { 'id': 9, 'year': 2017, 'quarter': '3', 'month': '9' }] currency_exchange_rates_data = [{ 'id': 1, 'period_id': 102, 'currency_from_id': 4, 'currency_to_id': 1, 'exchange_rate': 2.7 }, { 'id': 2, 'period_id': 105, 'currency_from_id': 3, 'currency_to_id': 1, 'exchange_rate': 1.5 }] def create_tables(): """Create tables defined in models.""" if config.ENVIRONMENT != config.TEST_ENVIRONMENT: return mysql.BaseModel.metadata.drop_all(mysql._db_engine) mysql.BaseModel.metadata.create_all(mysql._db_engine) def seed_currencies_table(): """Insert test data to currencies table.""" if config.ENVIRONMENT != config.TEST_ENVIRONMENT: return session.execute( currency.Currency.__table__.insert().values( currencies_data)) session.commit() session.close() def seed_accounting_periods_table(): """Insert test data to accounting periods table.""" if config.ENVIRONMENT != config.TEST_ENVIRONMENT: return session.execute( accounting_period.AccountingPeriod.__table__.insert().values( accounting_periods_data)) session.commit() session.close() def seed_currency_exchange_rates_table(): """Insert test data to currency exchange rates table.""" if config.ENVIRONMENT != config.TEST_ENVIRONMENT: return session.execute( (currency_exchange_rates.CurrencyExchangeRates .__table__.insert().values(currency_exchange_rates_data))) session.commit() session.close()