"""db.py. Database level utility class for testing against the assets related tebles. 1. asset 2. asset_location 3. asset_location_detail """ from functools import wraps import sys from assets import config from assets.connectors.mysql import dd_db_session DROP_TABLE_ASSET = """ DROP TABLE IF EXISTS asset; """ DROP_TABLE_ASSET_LOCATION = """ DROP TABLE IF EXISTS asset_location; """ DROP_TABLE_ASSET_LOCATION_DETAIL = """ DROP TABLE IF EXISTS asset_location_detail; """ CREATE_TABLE_ASSET = """ CREATE TABLE asset ( asset_id int(10), upc bigint(20)); """ CREATE_TABLE_ASSET_LOCATION = """ CREATE TABLE asset_location ( asset_location_id int(11), asset_id int(10)); """ CREATE_TABLE_ASSET_LOCATION_DETAIL = """ CREATE TABLE asset_location_detail ( asset_location_detail_id int(11), asset_location_id int(11), bits_per_sample tinyint(3)); """ INSERT_INTO_ASSET = """ INSERT INTO asset(asset_id,upc) VALUES('1','191018729677'), ('2','191018729678'); """ INSERT_INTO_ASSET_LOCATION = """ INSERT INTO asset_location(asset_location_id,asset_id) VALUES('1','1'), ('2','1'), ('3','1'), ('4','2'), ('5','2'), ('6','2'); """ INSERT_INTO_ASSET_LOCATION_DETAIL = """ INSERT INTO asset_location_detail (asset_location_detail_id,asset_location_id,bits_per_sample) VALUES ('1','1','64'), ('2','2','64'), ('3','3','64'), ('4','4','64'), ('5','5','64'), ('6','6','16'); """ def drop_asset_table(): """DROP command for asset table.""" with dd_db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_TABLE_ASSET) def drop_asset_location_table(): """DROP command for asset_location table.""" with dd_db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_TABLE_ASSET_LOCATION) def drop_asset_location_detail_table(): """DROP command for asset_location_detail table.""" with dd_db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_TABLE_ASSET_LOCATION_DETAIL) def create_and_insert_into_asset(): """Create and Insert into asset table.""" with dd_db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_TABLE_ASSET) session.execute(CREATE_TABLE_ASSET) session.execute(INSERT_INTO_ASSET) def create_and_insert_into_asset_location(): """Create and Insert into asset_location table.""" with dd_db_session() as session: session.execute(DROP_TABLE_ASSET_LOCATION) session.execute(CREATE_TABLE_ASSET_LOCATION) session.execute(INSERT_INTO_ASSET_LOCATION) def create_and_insert_into_asset_location_detail(): """Create and Insert into asset_location_detail table.""" with dd_db_session() as session: session.execute(DROP_TABLE_ASSET_LOCATION_DETAIL) session.execute(CREATE_TABLE_ASSET_LOCATION_DETAIL) session.execute(INSERT_INTO_ASSET_LOCATION_DETAIL) def _exit_if_not_test_environment(session): """For safety, only run tests in test environment pointed to sqlite. Exit immediately if not in test environment or not pointed to sqlite. """ if config.ENVIRONMENT != config.TEST_ENVIRONMENT: sys.exit('Environment must be set to {}.'.format( config.TEST_ENVIRONMENT)) if 'sqlite' not in session.bind.url.drivername: sys.exit('Tests must point to sqlite database.') def test_schema(function): """Create and tear down the test DB schema around a function call. This just creates the schema and seed data. Args: function (callable): the function to be called after creating the test schema. Returns: function (callable): The decorated function. """ @wraps(function) def call_function_within_db_context(*args, **kwargs): create_and_insert_into_asset() create_and_insert_into_asset_location() create_and_insert_into_asset_location_detail() try: function_return = function(*args, **kwargs) finally: drop_asset_location_detail_table() drop_asset_location_table() drop_asset_table() return function_return return call_function_within_db_context