"""DB setup for testing. Database level utility class for test schema. """ import sys from images import config from images.connectors.mysql import db_session CREATE_IMPORT_ASSET = """ CREATE TABLE import_asset ( id INTEGER NOT NULL PRIMARY KEY, import_asset_batch_id INTEGER NOT NULL, foldername VARCHAR(255) DEFAULT NULL, filename VARCHAR(255) NOT NULL, -- 'audio' -- 'image' -- 'caption' -- 'subtitles' asset_type VARCHAR(20) NOT NULL, -- 'upload_complete' -- 'error' -- 'finished' -- 'new' -- 'deleted' status VARCHAR(20) DEFAULT 'new', upload_completed datetime DEFAULT NULL, encoding_completed datetime DEFAULT NULL, result TEXT ); """ DROP_IMPORT_ASSET = """ DROP TABLE IF EXISTS import_asset; """ def create_import_asset_table(): """Create the import_asset table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(CREATE_IMPORT_ASSET) def drop_import_asset_table(): """Drop the import_asset table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_IMPORT_ASSET) CREATE_IMAGE_ASSETS = """ CREATE TABLE image_assets ( id INTEGER NOT NULL PRIMARY KEY, category_id INTEGER NOT NULL, path VARCHAR(100) NOT NULL, filename VARCHAR(100) NOT NULL, height INT DEFAULT NULL, width INT DEFAULT NULL, mime_type VARCHAR(50), -- 'image/jpeg' -- 'image/png' -- 'image/gif' -- 'image/tiff' file_size INT NOT NULL, date_added DATETIME NOT NULL, date_modified DATETIME NOT NULL ); """ DROP_IMAGE_ASSETS = """ DROP TABLE IF EXISTS image_assets; """ def create_image_assets_table(): """Create the import_asset table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(CREATE_IMAGE_ASSETS) def drop_image_assets_table(): """Drop the import_asset table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_IMAGE_ASSETS) CREATE_IMPORT_ASSET_DETAIL = """ CREATE TABLE import_asset_detail ( id INTEGER NOT NULL PRIMARY KEY, import_asset_id INTEGER NOT NULL, upc BIGINT NOT NULL, track_id INTEGER NOT NULL ); """ DROP_IMPORT_ASSET_DETAIL = """ DROP TABLE IF EXISTS import_asset_detail; """ def create_import_asset_detail_table(): """Create the import_asset_detail table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(CREATE_IMPORT_ASSET_DETAIL) def drop_import_asset_detail_table(): """Drop the import_asset table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_IMPORT_ASSET_DETAIL) CREATE_RELEASE_COVER_IMAGES = """ CREATE TABLE release_cover_images ( release_cover_images_id INTEGER NOT NULL PRIMARY KEY, image_asset_id INTEGER NOT NULL, upc BIGINT NOT NULL ); """ DROP_RELEASE_COVER_IMAGES = """ DROP TABLE IF EXISTS release_cover_images; """ def create_release_cover_images_table(): """Create the release_cover_images table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(CREATE_RELEASE_COVER_IMAGES) def drop_release_cover_images_table(): """Drop the release_cover_images table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_RELEASE_COVER_IMAGES) def seed_models(models): """Save the given model(s) to the DB.""" if not hasattr(models, '__iter__'): models = [models] with db_session() as session: _exit_if_not_test_environment(session) for model in models: session.merge(model) session.commit() 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.')