"""Database Utilities.""" from contextlib import contextmanager from contextlib import ExitStack import sys from unittest import mock from project_manager import config from project_manager.connector import mysql from project_manager.connector.mysql import pm_session_scope DROP_ARTIST_INFO_TABLE = """ DROP TABLE IF EXISTS artist_info """ CREATE_ARTIST_INFO_TABLE = """ CREATE TABLE IF NOT EXISTS artist_info ( artist_id INT(10) PRIMARY KEY, vendor_id INT(10), name VARCHAR(100), description VARCHAR(100), url VARCHAR(100) ) """ DROP_PRODUCT_GENRE_TABLE = """ DROP TABLE IF EXISTS genre """ CREATE_PRODUCT_GENRE_TABLE = """ CREATE TABLE genre ( genre_id TINYINT(3) PRIMARY KEY, genre VARCHAR(15) ) """ INSERT_PRODUCT_GENRES = """ INSERT INTO genre ( genre_id, genre ) VALUES (1, 'Rock'), (2, 'Electronic'), (3, 'Metal'), (4, 'Punk'), (5, 'Folk'), (6, 'Alternative') """ INSERT_PRODUCT_IMPRINT_ARTIST_INFO = """ INSERT INTO artist_info (artist_id, vendor_id) VALUES -- The following entries are used to test getting a vendor's imprints. (1, 1), (2, 1), (3, 2) """ INSERT_GENERIC_ARTIST_INFO = """ INSERT INTO artist_info (artist_id, vendor_id, name, description, url) VALUES -- The following entries are used to test getting a vendor's imprints. (1, 1, 'Billy Bob', 'The best honky tonky', 'http://www.billy.com/'), (123, 7123, 'Test Artist 1', 'Artist for testing', 'http://www.test.com/'), (311, 7123, 'Test Artist 2', 'Artist for testing', 'http://www.test.com/'), (4567, 7123, 'Test Artist 3', 'Artist for testing', 'http://www.test.com/') """ INSERT_PRODUCT_IMPRINT_PRODUCT_TYPES = """ INSERT INTO product_type (id, product_type) VALUES (1, 'Music') """ INSERT_PRODUCT_IMPRINT_PROJECTS = """ INSERT INTO project (created_date_utc, project_code, project_id, project_name, subaccount_id, updated_date_utc, vendor_id, artist_id, deletions) VALUES ('test', 'test', 1, 'test', 1, 'test', 123, 123, 'N'), -- This entry is -- used to test getting a subaccount's imprints. ('test', 'test', 2, 'test', 0, 'test', 1, 123, 'N') -- This entry is -- used to test getting a vendor's imprints. """ INSERT_PRODUCT_ARTIST_PROJECTS = """ INSERT INTO project ( project_id, project_code, vendor_id, subaccount_id, project_name, created_date_utc, updated_date_utc, correlation_id, description, artist_id, deletions) VALUES (1, 'code', 1, 1, 'Name', 'created', 'updated', 123, 'description', 1, 'N'); """ INSERT_PRODUCT_ARTIST_PROJECTS_WITH_DELETIONS = """ INSERT INTO project ( project_id, project_code, vendor_id, subaccount_id, project_name, created_date_utc, updated_date_utc, correlation_id, description, artist_id, deletions) VALUES (1, 'code', 1, 1, 'Name', 'created', 'updated', 123, 'description', 1, 'Y'); """ INSERT_PRODUCT_IMPRINT_RELEASES = """ INSERT INTO releases (artist_id, label, product_type_id, subaccount_id, upc) VALUES -- The following entries are used to test getting a subaccount's imprints. (NULL, 'Maple Records', 1, 1, 1), (NULL, 'Apple Records', 1, 1, 2), (NULL, 'Apple Records', 1, 1, 3), -- This entry's label value is a -- duplicate to test that each result is unique. (NULL, 'Banana Records', 1, 2, 4), (NULL, NULL, 1, 1, 5), -- This entry's label value is null to test that -- there are no null results. (NULL, '', 1, 1, 6), -- This entry's label value is an empty string to test -- that there are no empty string results. (NULL, ' ', 1, 1, 7), -- This entry's label value is a string made only -- of space characters to test that there are no results containing only -- spaces. -- The following entries are used to test getting a vendor's imprints. (1, 'Best Records', 1, NULL, 8), (2, 'Second Best Records', 1, NULL, 9), (2, 'Second Best Records', 1, NULL, 10), -- This entry's label value is a -- duplicate to test that each result is unique. (3, 'Going Records', 1, NULL, 11), (1, NULL, 1, NULL, 12), -- This entry's label value is null to test that -- there are no null results. (1, '', 1, NULL, 13), -- This entry's label value is an empty string to -- test that there are no empty string results. (1, ' ', 1, NULL, 14) -- This entry's label value is a string made only -- of space characters to test that there are no results containing only -- spaces. """ DROP_PRODUCT_SUBGENRE_TABLE = """ DROP TABLE IF EXISTS subgenre """ CREATE_PRODUCT_SUBGENRE_TABLE = """ CREATE TABLE subgenre ( orchard_id SMALLINT(5) PRIMARY KEY, name VARCHAR(80), genre_id TINYINT(3), composer TEXT DEFAULT NULL ) """ INSERT_PRODUCT_SUBGENRES = """ INSERT INTO subgenre ( orchard_id, name, genre_id, composer ) VALUES (7, 'Indie Rock', 1, 'N'), (36, 'Alternative', 3, 'N'), (355, 'Trance', 2, 'N'), (996, 'Electro House', 2, 'Y') """ DROP_PROJECT_TABLE = """ DROP TABLE IF EXISTS project """ CREATE_PROJECT_TABLE = """ CREATE TABLE IF NOT EXISTS project( project_id INTEGER PRIMARY KEY, project_code VARCHAR(255) NOT NULL, vendor_id INT(15) NOT NULL, subaccount_id INT(15), project_name VARCHAR(255) NOT NULL, created_date_utc datetime NOT NULL, updated_date_utc datetime NOT NULL, correlation_id VARCHAR(255) NULL, artist_id INT(12) NULL, description VARCHAR(255) NULL, last_modified_by VARCHAR(255) NULL, user_type VARCHAR(255) NULL, deletions TEXT DEFAULT 'N' ) """ CREATE_INDEX_PROJECT = """ CREATE UNIQUE INDEX IF NOT EXISTS project_idx0 ON project(project_code, vendor_id, subaccount_id) """ INSERT_PROJECT = """ INSERT INTO project ( project_code, vendor_id, subaccount_id, project_name, artist_id, created_date_utc, updated_date_utc, correlation_id, deletions ) VALUES ( 'ABC', 7123, 1, 'new project', 123, '2016-01-01 00:00:00.000000', '2016-01-01 00:00:00.000000', '2500b32e-7c1b-11e2-830a-70cd60f2c980', 'N' ) """ INSERT_DELETED_PROJECT = """ INSERT INTO project ( project_code, vendor_id, subaccount_id, project_name, artist_id, created_date_utc, updated_date_utc, correlation_id, deletions ) VALUES ( 'DELETED', 7123, 1, 'new project', 123, '2016-01-01 00:00:00.000000', '2016-01-01 00:00:00.000000', '2500b32e-7c1b-11e2-830a-70cd60f2c980', 'Y' ) """ INSERT_PROJECTS = """ INSERT INTO project ( project_code, vendor_id, subaccount_id, project_name, artist_id, created_date_utc, updated_date_utc, correlation_id, deletions ) VALUES ( 'ABC', 7123, 1, 'new project', 123, '2016-01-01 01:00:00.000000', '2016-01-01 01:00:00.000000', '2500b32e-7c1b-11e2-830a-70cd60f2c980', 'N' ), ( 'DEF', 7123, 1, 'new project 2', 311, '2016-01-01 02:00:00.000000', '2016-01-01 02:00:00.000000', '2500b32e-7c1b-11e2-830a-70cd60f2c981', 'N' ), ( 'GHI', 7123, 2, 'new project 3', 4567, '2016-01-01 03:00:00.000000', '2016-01-01 03:00:00.000000', '2500b32e-7c1b-11e2-830a-70cd60f2c982', 'N' ) """ DROP_MKT_PRIORITY_PROJECT_TABLE = """ DROP TABLE IF EXISTS mkt_priority_project """ CREATE_MKT_PRIORITY_PROJECT_TABLE = """ CREATE TABLE mkt_priority_project( mkt_priority_project_id INTEGER PRIMARY KEY, project_id INT(15) NOT NULL, priority varchar(15), country_id INT(15), created_by INT(10) DEFAULT NULL, updated_by INT(10) DEFAULT NULL, created_on datetime DEFAULT NULL, updated_on datetime DEFAULT NULL ) """ INSERT_MKT_PRIORITY_PROJECT = """ INSERT INTO mkt_priority_project ( project_id, priority, country_id ) VALUES ( 1, 'b', 1 ), ( 2, 'a', 3 ), ( 3, 'b', 5 ) """ INSERT_PROJECTS_WITHOUT_SUBACCOUNT = """ INSERT INTO project ( project_code, vendor_id, subaccount_id, project_name, artist_id, created_date_utc, updated_date_utc, correlation_id, deletions ) VALUES ( 'ABC', 7123, null, 'new project', 1234, '2016-01-01 01:00:00.000000', '2016-01-01 01:00:00.000000', '2500b32e-7c1b-11e2-830a-70cd60f2c980', 'N' ), ( 'DEF', 7123, null, 'new project 2', 311, '2016-01-01 02:00:00.000000', '2016-01-01 02:00:00.000000', '2500b32e-7c1b-11e2-830a-70cd60f2c981', 'N' ), ( 'GHI', 7123, null, 'new project 3', 4567, '2016-01-01 03:00:00.000000', '2016-01-01 03:00:00.000000', '2500b32e-7c1b-11e2-830a-70cd60f2c982', 'N' ) """ DROP_SUBACCOUNT_TABLE = """ DROP TABLE IF EXISTS subaccount """ CREATE_SUBACCOUNT_TABLE = """ CREATE TABLE subaccount( subaccount_id INT(15) NOT NULL, subaccount_uuid CHAR(36) NOT NULL, vendor_id INT(15) NOT NULL, subaccount_name VARCHAR(100) ) """ INSERT_SUBACCOUNT = """ INSERT INTO subaccount ( vendor_id, subaccount_id, subaccount_uuid, subaccount_name ) VALUES ( 7123, 1, 'a6a48264-7882-4ef4-9b7d-2500fb2c3ce4', 'subaccount_test_value' ), ( 7123, 2, '6e420fd1-2268-47a2-b5a9-ccacf4b46a98', 'subaccount_test_value' ) """ DROP_PRODUCT_TYPE_TABLE = """ DROP TABLE IF EXISTS product_type """ CREATE_PRODUCT_TYPE_TABLE = """ CREATE TABLE IF NOT EXISTS product_type ( id INTEGER PRIMARY KEY, product_type VARCHAR(10) NOT NULL ) """ INSERT_PRODUCT_TYPES = """ INSERT INTO product_type (id, product_type) VALUES (1,'Music'), (2,'TV Show'), (3,'Movie'), (4,'Video Etc'), (5,'Web Videos') """ DROP_RELEASE_ARTIST_TABLE = """ DROP TABLE IF EXISTS release_artist """ CREATE_RELEASE_ARTIST_TABLE = """ CREATE TABLE IF NOT EXISTS release_artist ( release_artist_id INTEGER PRIMARY KEY, upc INTEGER, role VARCHAR(25), release_id INTEGER, artist_name VARCHAR(255), url VARCHAR(156), artist_info_id INTEGER ) """ INSERT_RELEASE_ARTISTS = """ INSERT INTO release_artist (upc, role, release_id, artist_name, url, artist_info_id) VALUES (888831283041, 'performer', 1001, 'Mike Jones', 'http://mike.jones.com/me', NULL), (889845667810, 'performer', 1002, 'Mike Jones', 'http://mike.jones.com/me', NULL), (889845667872, 'performer', 1003, 'Mike Jones', 'http://mike.jones.com/me', NULL), (823623000680, 'performer', 1011, 'John Mike', 'http://john.mike.com/me', NULL), (803680422151, 'performer', 1012, 'John Mike', 'http://john.mike.com/me', NULL), (803680422555, 'performer', 1014, 'John Mike', 'http://john.mike.com/me', NULL) """ INSERT_MULTIPLE_PRIMARY_ARTISTS = """ INSERT INTO release_artist (upc, role, release_id, artist_name, url) VALUES (888831283041, 'performer', 1001, 'Billy Bones', 'http://billy.bones.com/'), (888831283041, 'performer', 1001, 'Jane Dune', 'http://jane.dune.com/me') """ DROP_RELEASES_TABLE = """ DROP TABLE IF EXISTS releases """ CREATE_RELEASES_TABLE = """ CREATE TABLE IF NOT EXISTS releases( release_id INTEGER PRIMARY KEY, release_name VARCHAR(255), version VARCHAR(255), delivered_version VARCHAR(255), display_upc VARCHAR(20), upc INTEGER NOT NULL, label VARCHAR(70), product_type_id TINYINT(3), artist_id INT(12), subaccount_id INT(12), release_status VARCHAR(20), project_id INTEGER NULL, distribution_format_id INTEGER NULL, deletions VARCHAR(1), format VARCHAR(16), not_for_distribution VARCHAR(20) ) """ INSERT_RELEASES = """ INSERT INTO releases( release_id, release_name, version, delivered_version, display_upc, upc, product_type_id, release_status, project_id, distribution_format_id, deletions, format, not_for_distribution ) VALUES ( 1001, 'Something Strange Here', 'Deluxe', 'Live at Wembley', '888831283041', 888831283041, 1, 'in_content', 1, 1, 'N', 'Single', 'N' ), ( 1002, 'Something Strange Here', 'Unremarkable', 'Live in New York', '889845667810', 889845667810, 1, 'in_content', 1, 2, 'N', 'EP', 'N' ), ( 1003, 'Something Strange Here', 'Target Special Edition', 'Live in Berlin', '889845667872', 889845667872, 1, 'transfer_to_content', 1, 1, 'N', 'Full Length', 'N' ), ( 1004, 'Something Strange Here', 'Best Buy Special Edition', 'Live in Paris', '889845667875', 889845667875, 1, 'in_content', 1, 2, 'Y', 'EP', 'N' ), ( 1011, 'Everything is Boring', 'Moms Best', 'Live', '823623000680', 823623000680, 1, 'in_content', 2, 1, 'N', 'Single', 'N' ), ( 1012, 'Everything is Boring', 'Christmas spectacular', 'Remixed', '803680422151', 803680422151, 1, 'label_processing', 2, 2, 'N', 'EP', 'N' ), ( 1013, 'Everything is Boring', 'Christmas spectacular', 'Remixed Digitally', '803680422152', 803680422152, 1, 'in_content', 2, 2, 'Y', 'Single', 'N' ), ( 1014, 'Herp Derp', 'Le 4th status', 'Remixed by Rem', '803680422555', 803680422555, 1, 'in_content', 1, 1, 'N', 'Single', 'N' ) """ INSERT_RELEASE_NO_ARTIST = """ INSERT INTO releases( release_id, release_name, version, delivered_version, display_upc, upc, product_type_id, release_status, project_id, distribution_format_id, deletions, not_for_distribution ) VALUES ( 1020, 'Dance Till You Drop', 'Deluxe', 'Live at Wembley', '555555555555', 555555555555, 1, 'label_processing', 2, 1, 'N', 'N' ) """ INSERT_RELEASE_LABEL_PROCESSING_REJECTED = """ INSERT INTO releases( release_id, release_name, version, delivered_version, display_upc, upc, product_type_id, release_status, project_id, distribution_format_id, deletions, not_for_distribution ) VALUES ( 2034, 'Midnight Town', 'Deluxe', 'Live at Wembley', '888831283644', 888831283644, 1, 'label_processing', 1, 1, 'N', 'N' ) """ INSERT_RELEASE_APPROVAL_QUEUE_STATUS_REJECTED = """ INSERT INTO release_approval_queue( release_id, status, last_updated, date_submitted ) VALUES ( 2034, 'rejected', '2016-02-02 01:00:00.000000', '2016-01-01 01:00:00.000000' ) """ INSERT_RELEASE_IN_CONTENT_SUBMITTED = """ INSERT INTO releases( release_id, release_name, version, delivered_version, display_upc, upc, product_type_id, release_status, project_id, distribution_format_id, deletions, not_for_distribution ) VALUES ( 1853456, 'Rich Boy', 'Deluxe', 'Live at Wembley', '190374791632', 190374791632, 1, 'in_content', 1, 1, 'N', 'N' ) """ INSERT_RELEASE_CORRECTION_SUBMITTED = """ INSERT INTO release_correction( release_id, status, last_updated, last_updated_by ) VALUES ( 1853456, 'submitted', '2016-01-01 01:00:00.000000', 1 ) """ DROP_RELEASE_CORRECTION_TABLE = """ DROP TABLE IF EXISTS release_correction """ CREATE_RELEASE_CORRECTION_TABLE = """ CREATE TABLE release_correction ( release_correction_id INTEGER PRIMARY KEY, release_id INTEGER NOT NULL, status VARCHAR(12) NOT NULL, last_updated datetime NOT NULL, last_updated_by INTEGER NOT NULL, last_updated_type VARCHAR(12) DEFAULT 'vendor' ) """ INSERT_RELEASE_CORRECTION = """ INSERT INTO release_correction( release_correction_id, release_id, status, last_updated, last_updated_by ) VALUES ( 2, 1001, 'active', '2016-01-01 01:00:00.000000', 1 ), ( 1, 1001, 'applied', '2015-01-01 01:00:00.000000', 1 ), ( 3, 1014, 'active', '2015-01-01 01:00:00.000000', 1 ) """ DROP_RELEASE_APPROVAL_QUEUE_TABLE = """ DROP TABLE IF EXISTS release_approval_queue """ CREATE_RELEASE_APPROVAL_QUEUE_TABLE = """ CREATE TABLE release_approval_queue ( release_approval_id INTEGER PRIMARY KEY, release_id INTEGER NOT NULL, release_correction_id INTEGER DEFAULT NULL, checked_out_by INTEGER DEFAULT NULL, admin_approval VARCHAR(1) DEFAULT NULL, status VARCHAR(12) NOT NULL, approved_by INTEGER DEFAULT NULL, last_updated datetime NOT NULL, date_submitted datetime NOT NULL, initiate_errorcorrection INTEGER NOT NULL DEFAULT 0, submitted_by INTEGER DEFAULT NULL ) """ INSERT_RELEASE_APPROVAL_QUEUE = """ INSERT INTO release_approval_queue( release_approval_id, release_id, status, release_correction_id, last_updated, date_submitted ) VALUES ( 1, 1002, 'rejected', NULL, '2016-02-02 01:00:00.000000', '2016-01-01 01:00:00.000000' ), ( 2, 1002, 'approved', NULL, '2015-02-02 01:00:00.000000', '2015-01-01 01:00:00.000000' ), ( 3, 1014, 'rejected', 123, '2016-02-02 01:00:00.000000', '2016-01-01 01:00:00.000000' ), ( 4, 1002, 'rejected', 123, '2016-02-02 01:00:00.000000', '2016-01-01 01:00:00.000000' ), ( 5, 1003, 'checked_in', 123, '2016-02-02 01:00:00.000000', '2016-01-01 01:00:00.000000' ) """ DROP_DISTRIBUTION_FORMAT_TABLE = """ DROP TABLE IF EXISTS distribution_format """ CREATE_DISTRIBUTION_FORMAT_TABLE = """ CREATE TABLE distribution_format ( distribution_format_id INTEGER PRIMARY KEY, display_flag VARCHAR(1) DEFAULT 'Y', context_type VARCHAR(64) DEFAULT 'physical', distribution_format_media_id INTEGER ) """ INSERT_DISTRIBUTION_FORMATS = """ INSERT INTO distribution_format ( distribution_format_id, context_type, display_flag, distribution_format_media_id ) VALUES ( 1, 'digital', 'Y', 68 ), ( 2, 'physical', 'Y', 1 ), ( 3, 'physical', 'N', 2 ) """ DROP_DISTRIBUTION_FORMAT_MEDIA_TABLE = """ DROP TABLE IF EXISTS distribution_format_media """ CREATE_DISTRIBUTION_FORMAT_MEDIA_TABLE = """ CREATE TABLE distribution_format_media ( distribution_format_media_id INTEGER PRIMARY KEY, name VARCHAR(128) NOT NULL ) """ INSERT_DISTRIBUTION_FORMAT_MEDIA = """ INSERT INTO distribution_format_media ( distribution_format_media_id, name ) VALUES ( 1, 'CD' ), ( 2, 'DVD' ), ( 68, 'Digital' ), ( 69, 'Music Video' ) """ CREATE_VENDOR_TABLE = """ CREATE TABLE IF NOT EXISTS vendor ( vendor_id INTEGER PRIMARY KEY, vendor_uuid CHAR(36) NOT NULL, company_brand_id int unsigned, name VARCHAR(100) NULL, owner VARCHAR(10) ) """ DROP_VENDOR_TABLE = """ DROP TABLE IF EXISTS vendor """ CREATE_COMPANY_BRAND_TABLE = """ CREATE TABLE company_brand ( id int unsigned NOT NULL, name varchar(50) DEFAULT NULL, uuid varchar(50) DEFAULT NULL, parent_company_id int unsigned DEFAULT NULL, display_name varchar(50) DEFAULT NULL, date_updated datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) ) """ DROP_COMPANY_BRAND_TABLE = """ DROP TABLE IF EXISTS company_brand """ CREATE_PARENT_COMPANY_TABLE = """ CREATE TABLE IF NOT EXISTS parent_company ( id int unsigned NOT NULL, name varchar(50) DEFAULT NULL, uuid varchar(50) DEFAULT NULL, display_name varchar(50) DEFAULT NULL, date_updated datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) ) """ DROP_PARENT_COMPANY_TABLE = """ DROP TABLE IF EXISTS parent_company """ INSERT_VENDOR_TABLE = """ INSERT INTO vendor (vendor_id, vendor_uuid, name, owner, company_brand_id) VALUES ( 7123, '369a8193-132c-4d8c-9d87-b8e7a61dbc25', "Awesome Vendor", "Great Owner", 1 ), ( 1, 'a8d36bb5-bca5-4e3e-80dc-1b292857351c', "One Vendor", "Great Owner", 2 ) """ INSERT_COMPANY_BRAND_TABLE = """ INSERT INTO company_brand (id, name, uuid, parent_company_id) VALUES ( 1, 'knr', "77c6c150-19f3-451f-a692-1a5d7ef30e85", 2 ), ( 2, 'theorchard', "d25a4cd1-e820-45f2-be5c-56edcfeb8298", 2 ) """ INSERT_PARENT_COMPANY_TABLE = """ INSERT INTO parent_company (id, name, uuid) VALUES ( 1, 'sme', "f1594122-7f99-4916-b103-08b0444c7b46" ), ( 2, 'theorchard', "955a1bbd-b623-4ea1-ab5f-8d6620c442fb" ) """ def drop_product_genre_table(session=None): """Drop the table that is used to construct product genre objects. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(DROP_PRODUCT_GENRE_TABLE) def create_product_genre_table(session=None): """Create the table that is used to construct product genre objects. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(CREATE_PRODUCT_GENRE_TABLE) def insert_product_genres(session=None): """Insert test entries into the table used to construct productgenres. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(INSERT_PRODUCT_GENRES) def drop_product_imprint_tables(session=None): """Drop the tables that are used to construct product imprint objects. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(DROP_ARTIST_INFO_TABLE) session.execute(DROP_PRODUCT_TYPE_TABLE) drop_project_table(session) session.execute(DROP_RELEASES_TABLE) def create_product_imprint_tables(session=None): """Create the tables that are used to construct product imprint objects. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(CREATE_ARTIST_INFO_TABLE) session.execute(CREATE_PRODUCT_TYPE_TABLE) create_project_table(session) session.execute(CREATE_RELEASES_TABLE) def insert_product_imprint_values(session=None): """Insert test entries into the tables that are used to construct product. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(INSERT_PRODUCT_IMPRINT_ARTIST_INFO) session.execute(INSERT_PRODUCT_IMPRINT_PRODUCT_TYPES) session.execute(INSERT_PRODUCT_IMPRINT_PROJECTS) session.execute(INSERT_PRODUCT_IMPRINT_RELEASES) def create_product_artist_tables(session=None): """Create the tables that are used to construct product/artist objects. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(CREATE_ARTIST_INFO_TABLE) create_project_table(session) def insert_product_artist_values(session=None): """Insert test entries that are used to construct product/artist. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(INSERT_GENERIC_ARTIST_INFO) session.execute(INSERT_PRODUCT_ARTIST_PROJECTS) session.execute(INSERT_SUBACCOUNT) def insert_product_artist_values_with_deletions(session=None): """Insert data to construct product/artist with deleted project. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(INSERT_GENERIC_ARTIST_INFO) session.execute(INSERT_PRODUCT_ARTIST_PROJECTS_WITH_DELETIONS) session.execute(INSERT_SUBACCOUNT) def drop_product_subgenre_table(session=None): """Drop the table that is used to construct product subgenre objects. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(DROP_PRODUCT_SUBGENRE_TABLE) def create_product_subgenre_table(session=None): """Create the table that is used to construct product subgenre objects. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(CREATE_PRODUCT_SUBGENRE_TABLE) def insert_product_subgenres(session=None): """Insert test entries into the subgenres table. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(INSERT_PRODUCT_SUBGENRES) def drop_project_table(session=None): """Drop the table that is used to construct project objects. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(DROP_PROJECT_TABLE) def create_project_table(session=None): """Create the table that is used to construct project objects. Args: session (connnector.mysql.pm_session_scope): session to execute sql in (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) session.execute(CREATE_PROJECT_TABLE) session.execute(CREATE_INDEX_PROJECT) def insert_project(): """Insert project.""" with pm_session_scope() as session: session.execute(INSERT_PROJECT) def insert_deleted_project(): """Insert deleted project.""" with pm_session_scope() as session: session.execute(INSERT_DELETED_PROJECT) def insert_projects(): """Insert projects.""" with pm_session_scope() as session: session.execute(INSERT_PROJECTS) def insert_projects_without_subaccount(): """Insert projects without subaccount.""" with pm_session_scope() as session: session.execute(INSERT_PROJECTS_WITHOUT_SUBACCOUNT) def drop_product_type_table(): """Drop product type table.""" with pm_session_scope() as session: session.execute(DROP_PRODUCT_TYPE_TABLE) def create_product_type_table(): """Create product type table.""" with pm_session_scope() as session: session.execute(DROP_PRODUCT_TYPE_TABLE) session.execute(CREATE_PRODUCT_TYPE_TABLE) def insert_product_types(): """Insert product types.""" with pm_session_scope() as session: session.execute(INSERT_PRODUCT_TYPES) def drop_release_artist_table(): """Drop release artist table.""" with pm_session_scope() as session: session.execute(DROP_RELEASE_ARTIST_TABLE) def create_release_artist_table(): """Create release artist table.""" with pm_session_scope() as session: session.execute(DROP_RELEASE_ARTIST_TABLE) session.execute(CREATE_RELEASE_ARTIST_TABLE) def insert_release_artists(): """Insert release artists.""" with pm_session_scope() as session: session.execute(INSERT_RELEASE_ARTISTS) def insert_multiple_primary_artists(): """Insert multiple primary artists.""" with pm_session_scope() as session: session.execute(INSERT_MULTIPLE_PRIMARY_ARTISTS) def insert_release_no_primary_artists(): """Insert releases without primary artists.""" with pm_session_scope() as session: session.execute(INSERT_RELEASE_NO_ARTIST) def drop_releases_table(): """Drop release table.""" with pm_session_scope() as session: session.execute(DROP_RELEASES_TABLE) def create_releases_table(): """Create releases table.""" with pm_session_scope() as session: session.execute(DROP_RELEASES_TABLE) session.execute(CREATE_RELEASES_TABLE) def insert_releases(): """Insert releases.""" with pm_session_scope() as session: session.execute(INSERT_RELEASES) def insert_release_label_processing_rejected(): """Insert release with status label_processing and release approval entry. Adds a release approval entry of rejected for the label_processing release. """ with pm_session_scope() as session: session.execute(INSERT_RELEASE_LABEL_PROCESSING_REJECTED) session.execute(INSERT_RELEASE_APPROVAL_QUEUE_STATUS_REJECTED) def insert_release_in_content_correction_submitted(): """Insert release with status in_content and error correction entry. Adds an in_content release and creates a corresponding error correction entry with a status of submitted. """ with pm_session_scope() as session: session.execute(INSERT_RELEASE_IN_CONTENT_SUBMITTED) session.execute(INSERT_RELEASE_CORRECTION_SUBMITTED) def drop_release_correction_table(): """Drop release correction table.""" with pm_session_scope() as session: session.execute(DROP_RELEASE_CORRECTION_TABLE) def create_release_correction_table(): """Create release correction table.""" with pm_session_scope() as session: session.execute(DROP_RELEASE_CORRECTION_TABLE) session.execute(CREATE_RELEASE_CORRECTION_TABLE) def insert_release_correction(): """Insert release correction.""" with pm_session_scope() as session: session.execute(INSERT_RELEASE_CORRECTION) def drop_release_approval_queue_table(): """Drop release approval queue table.""" with pm_session_scope() as session: session.execute(DROP_RELEASE_APPROVAL_QUEUE_TABLE) def create_release_approval_queue_table(): """Create release approval queue table.""" with pm_session_scope() as session: session.execute(DROP_RELEASE_APPROVAL_QUEUE_TABLE) session.execute(CREATE_RELEASE_APPROVAL_QUEUE_TABLE) def insert_release_approval_queue(): """Insert release approval queue.""" with pm_session_scope() as session: session.execute(INSERT_RELEASE_APPROVAL_QUEUE) def drop_distribution_format_table(): """Drop distribution format table.""" with pm_session_scope() as session: session.execute(DROP_DISTRIBUTION_FORMAT_TABLE) def create_distribution_format_table(): """Create distribution format table.""" with pm_session_scope() as session: session.execute(DROP_DISTRIBUTION_FORMAT_TABLE) session.execute(CREATE_DISTRIBUTION_FORMAT_TABLE) def insert_distribution_formats(): """Insert distribution formats.""" with pm_session_scope() as session: session.execute(INSERT_DISTRIBUTION_FORMATS) def drop_distribution_format_media_table(): """Drop distribution format media table.""" with pm_session_scope() as session: session.execute(DROP_DISTRIBUTION_FORMAT_MEDIA_TABLE) def create_distribution_format_media_table(): """Create distribution format media table.""" with pm_session_scope() as session: session.execute(DROP_DISTRIBUTION_FORMAT_MEDIA_TABLE) session.execute(CREATE_DISTRIBUTION_FORMAT_MEDIA_TABLE) def insert_distribution_format_media(): """Insert distribution format media.""" with pm_session_scope() as session: session.execute(INSERT_DISTRIBUTION_FORMAT_MEDIA) def create_subaccount_table(): """Create subaccount table.""" with pm_session_scope() as session: session.execute(DROP_SUBACCOUNT_TABLE) session.execute(CREATE_SUBACCOUNT_TABLE) def insert_subaccount(): """Insert subaccount.""" with pm_session_scope() as session: session.execute(INSERT_SUBACCOUNT) def create_vendor_table(): """Create vendor table.""" with pm_session_scope() as session: session.execute(CREATE_VENDOR_TABLE) def drop_vendor_table(): """Drop vendor table.""" with pm_session_scope() as session: session.execute(DROP_VENDOR_TABLE) def drop_subaccount_table(): """Drop subaccount table.""" with pm_session_scope() as session: session.execute(DROP_SUBACCOUNT_TABLE) def insert_vendor(): """Insert vendor.""" with pm_session_scope() as session: session.execute(INSERT_VENDOR_TABLE) def create_artist_info_table(): """Create artist_info table.""" with pm_session_scope() as session: session.execute(CREATE_ARTIST_INFO_TABLE) def drop_artist_info_table(): """Drop artist_info table.""" with pm_session_scope() as session: session.execute(DROP_ARTIST_INFO_TABLE) def create_company_brand_table(): """Create company_brand table.""" with pm_session_scope() as session: session.execute(CREATE_COMPANY_BRAND_TABLE) def drop_company_brand_table(): """Drop company_brand table.""" with pm_session_scope() as session: session.execute(DROP_COMPANY_BRAND_TABLE) def create_parent_company_table(): """Create parent_company table.""" with pm_session_scope() as session: session.execute(CREATE_PARENT_COMPANY_TABLE) def drop_parent_company_table(): """Drop parent_company table.""" with pm_session_scope() as session: session.execute(DROP_PARENT_COMPANY_TABLE) def insert_company_brand(): """Insert company_brand.""" with pm_session_scope() as session: session.execute(INSERT_COMPANY_BRAND_TABLE) def insert_parent_company(): """Insert parent_company.""" with pm_session_scope() as session: session.execute(INSERT_PARENT_COMPANY_TABLE) def create_mkt_priority_project(): """Create mkt_priority_project table.""" with pm_session_scope() as session: session.execute(DROP_MKT_PRIORITY_PROJECT_TABLE) session.execute(CREATE_MKT_PRIORITY_PROJECT_TABLE) def insert_mkt_priority_project(): """Insert mkt_priority_project.""" with pm_session_scope() as session: session.execute(INSERT_MKT_PRIORITY_PROJECT) def drop_mkt_priority_project_table(): """Drop mkt_priority_project table.""" with pm_session_scope() as session: session.execute(DROP_MKT_PRIORITY_PROJECT_TABLE) def mock_db_session(mocker): """Create a mock database sesssion. Also mock the db_session context manager to use the mock session. """ mock_session = mock.Mock(query=mock.Mock()) @contextmanager def fake_session_manager(): yield mock_session mocker.patch.object(mysql, 'pm_session_scope', fake_session_manager) return mock_session DROP_TRACK_TABLE = """ DROP TABLE IF EXISTS track """ CREATE_TRACK_TABLE = """ CREATE TABLE IF NOT EXISTS track ( id INTEGER PRIMARY KEY, release_id INTEGER NOT NULL, track_name VARCHAR(255) ) """ INSERT_TRACKS = """ INSERT INTO track (id, release_id, track_name) VALUES (5001, 1001, 'Track A'), (5002, 1001, 'Track B'), (5003, 1002, 'Track C') """ DROP_TRACK_ARTIST_TABLE = """ DROP TABLE IF EXISTS track_artist """ CREATE_TRACK_ARTIST_TABLE = """ CREATE TABLE IF NOT EXISTS track_artist ( id INTEGER PRIMARY KEY, track_id INTEGER NOT NULL, type VARCHAR(50), name VARCHAR(255), artist_info_id INTEGER ) """ INSERT_TRACK_ARTISTS = """ INSERT INTO track_artist (id, track_id, type, name, artist_info_id) VALUES (6001, 5001, 'performer', 'Mike Jones', NULL), (6002, 5002, 'performer', 'Mike Jones', NULL), (6003, 5003, 'performer', 'John Mike', NULL) """ DROP_TRACK_WRITER_TABLE = """ DROP TABLE IF EXISTS track_writer """ CREATE_TRACK_WRITER_TABLE = """ CREATE TABLE IF NOT EXISTS track_writer ( track_writer_id INTEGER PRIMARY KEY, writer_name VARCHAR(255), upc INTEGER, cd INTEGER, track_id INTEGER, unique_track_id INTEGER NOT NULL, artist_info_id INTEGER, label_participant_id INTEGER ) """ INSERT_TRACK_WRITERS = """ INSERT INTO track_writer (track_writer_id, writer_name, upc, cd, track_id, unique_track_id, artist_info_id) VALUES (7001, 'Mike Jones', 888831283041, 1, 1, 5001, NULL), (7002, 'Mike Jones', 888831283041, 2, 2, 5002, NULL), (7003, 'John Mike', 889845667810, 1, 1, 5003, NULL) """ def drop_track_table(): """Drop track table.""" with pm_session_scope() as session: session.execute(DROP_TRACK_TABLE) def create_track_table(): """Create track table.""" with pm_session_scope() as session: session.execute(DROP_TRACK_TABLE) session.execute(CREATE_TRACK_TABLE) def insert_tracks(): """Insert tracks.""" with pm_session_scope() as session: session.execute(INSERT_TRACKS) def drop_track_artist_table(): """Drop track_artist table.""" with pm_session_scope() as session: session.execute(DROP_TRACK_ARTIST_TABLE) def create_track_artist_table(): """Create track_artist table.""" with pm_session_scope() as session: session.execute(DROP_TRACK_ARTIST_TABLE) session.execute(CREATE_TRACK_ARTIST_TABLE) def insert_track_artists(): """Insert track_artist rows.""" with pm_session_scope() as session: session.execute(INSERT_TRACK_ARTISTS) def drop_track_writer_table(): """Drop track_writer table.""" with pm_session_scope() as session: session.execute(DROP_TRACK_WRITER_TABLE) def create_track_writer_table(): """Create track_writer table.""" with pm_session_scope() as session: session.execute(DROP_TRACK_WRITER_TABLE) session.execute(CREATE_TRACK_WRITER_TABLE) def insert_track_writers(): """Insert track_writer rows.""" with pm_session_scope() as session: session.execute(INSERT_TRACK_WRITERS) def exit_if_not_test_environment(session=None): """Exit application if not in test environment with a sqlite database. Args: session (connnector.mysql.pm_session_scope): session to check for sqlite driver (optional). """ with ExitStack() as stack: if not session: session = stack.enter_context(pm_session_scope()) if config.ENVIRONMENT != config.TEST_ENVIRONMENT: sys.exit('Environment must be set to {}'.format( config.TEST_ENVIRONMENT)) elif 'sqlite' not in session.bind.url.drivername: sys.exit('Tests must point to sqlite database')