"""Fixtures for lambda testing.""" import os import mysql.connector import pytest from py.xml import html from pytest_html.extras import url from tests.integration.utils.dockerized_lambda_client import DockerizedLambdaAPIClient QA_BASE_URL = os.environ.get('QA_BASE_URL', 'http://sync-contract-lambda:8080') JIRA_PREFIX_URL = 'https://theorchard.atlassian.net/browse/' PYTEST_REPORT_PREFIX = 'sync_contract lambda integration tests results' PYTEST_REPORT_SUMMARY = 'This lambda creates/updates contract info in legacy system' def dockerized_lambda_api_client(headers): """Create snapshot_adjustments dockerized lambda object.""" return DockerizedLambdaAPIClient(QA_BASE_URL, headers) @pytest.fixture(scope='session') def basic_headers(): """Return basic headers.""" return {'Content-Type': 'application/json'} @pytest.fixture(autouse='True') def create_vendor_contract_distribution_type(): """Create currencies table.""" cnx = db_art_relations_connector() cursor = cnx.cursor() query = """CREATE TABLE IF NOT EXISTS `vendor_contract_distribution_type` ( `id` int(12) NOT NULL AUTO_INCREMENT COMMENT 'Primary key', `vendor_contract_id` int(10) DEFAULT NULL COMMENT 'Foreign key referencing vendor contract table', `distribution_type_id` int(10) DEFAULT NULL COMMENT 'Reference to distribution_type table.', `new_store_default` enum('Y','N') DEFAULT 'N' COMMENT 'Flag indicating default distribution type value when new store is added to the system', PRIMARY KEY (`id`), KEY `vendor_contract_id` (`vendor_contract_id`), KEY `distribution_type_id` (`distribution_type_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Holds distribution type allowed in the corresponding label c' """ cursor.execute(query) cnx.commit() cnx.close() @pytest.fixture(autouse='True') def create_distribution_type(): """Create distribution_type table.""" cnx = db_art_relations_connector() cursor = cnx.cursor() query = """CREATE TABLE `distribution_type` ( `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key', `name` varchar(20) NOT NULL COMMENT 'distribution_type of a vendor, e.g. full track, ringtone, etc', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='Distribution type of labels, e.g. ringtone, full track, etc' """ cursor.execute(query) cnx.commit() cnx.close() @pytest.fixture(autouse='True') def create_art_rel_currencies(): """Create currencies table.""" cnx = db_art_relations_connector() cursor = cnx.cursor() query = """ CREATE TABLE IF NOT EXISTS `currencies` ( `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, `ISO_4217_code` varchar(12) DEFAULT NULL, `symbol_html_entity_code` varchar(45) DEFAULT NULL COMMENT 'Html entity of symbol', `currency_name` varchar(45) DEFAULT NULL COMMENT 'Name of currency.', `decimal_mark` char(1) DEFAULT NULL COMMENT 'Decimal mark', `thousands_separator` char(1) DEFAULT NULL COMMENT 'Thousand separator', `symbol_infront` char(1) DEFAULT NULL COMMENT 'Place symbol infront of dollar amount\n', `subunit_to_unit` smallint(6) DEFAULT NULL COMMENT 'Subunit of unit', `subunit_name` varchar(45) DEFAULT NULL COMMENT 'Name of subunit', `supported_payout_currency` char(1) DEFAULT 'N' COMMENT 'Orchard supported payout currency\n', PRIMARY KEY (`id`), UNIQUE KEY `ISO_4217_code_UNIQUE` (`ISO_4217_code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; """ cursor.execute(query) cnx.commit() cnx.close() @pytest.fixture(autouse='True') def create_art_rel_vendor_contract(): """Create vendor_contract table.""" cnx = db_art_relations_connector() cursor = cnx.cursor() query = """ CREATE TABLE IF NOT EXISTS `vendor_contract` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `vendor_id` int(11) DEFAULT NULL, `cont_start` date DEFAULT NULL, `cont_end` date DEFAULT NULL, `cont_version` varchar(50) DEFAULT NULL, `exclusive` enum('yes_digital','yes_physical','yes_both','no_both','y','n') DEFAULT 'no_both', `orchrep_name` varchar(55) DEFAULT NULL, `carve_out` varchar(255) DEFAULT NULL, `qualification_level` varchar(44) DEFAULT NULL, `territory_carve_out` text, `dms_carve_out` mediumtext, `dms_master_carve_out` text, `encoding_fees_cap_back_ctlg` float DEFAULT NULL, `encoding_fees_cap_new_release` float DEFAULT NULL, `digital_split` float DEFAULT NULL, `physical_split` float DEFAULT NULL, `contract_type` enum('vendor_term','per_release_term') DEFAULT 'vendor_term', `release_term` int(11) DEFAULT NULL, `advance_payment` float DEFAULT NULL, `advance_recoupable_percentage` float DEFAULT NULL, `dig_distribution_type` enum('digital_mobile','digital_only','mobile_only') DEFAULT 'digital_mobile', `possible_track_restrictions` enum('Y','N') DEFAULT 'N', `oms_type` enum('none','both','orchard','label') DEFAULT 'none', `oms_fee_percentage` float DEFAULT NULL, `negotiated_changes` enum('Y','N') DEFAULT NULL, `negotiated_change_comments` text, `contract_complete` enum('Y','N') DEFAULT 'N', `signature_date` date DEFAULT NULL, `marketing_restrictions` enum('Y','N') DEFAULT 'N', `orchard_assignment_right_restriction` enum('Y','N') DEFAULT 'N', `currency_id` smallint(5) unsigned DEFAULT '1', `third_party_responsibility` enum('standard','other') DEFAULT 'standard', `third_party_responsibility_detail` text, `possible_track_restriction_detail` text, `is_amendment` enum('Y','N') DEFAULT 'N', `extend_until_recouped` enum('Y','N') DEFAULT 'N', `sync_admin_territory` text, `sync_admin_commission` text, `sync_admin_type_of_deal` enum('master_admin','master_or_publishing_admin','publishing_admin_only') DEFAULT NULL, `royalty_collection_territory` text, `royalty_collection_commission` float DEFAULT NULL, `publishing_admin_commission` float DEFAULT NULL, `publishing_admin_territory` text, `publishing_admin_limit_grant_of_rights` varchar(50) DEFAULT NULL, `publishing_admin_misc_provisions` varchar(50) DEFAULT NULL, `parent_vendor_contract_id` int(11) DEFAULT NULL, `vendor_type` enum('vendor','oms_client') DEFAULT 'vendor', `vendor_proposed_term_id` int(11) DEFAULT NULL, `sync_admin_response_time` int(11) DEFAULT NULL, `ringtone_publishing_type` enum('both','label','orchard','none') DEFAULT 'none', `physical_track_publishing_type` enum('none','both','orchard','label') DEFAULT 'none', `unlimited_roll` enum('Y','N') DEFAULT 'N', `rollover_length_in_months` int(11) DEFAULT NULL, `can_terminate` enum('Y','N') DEFAULT 'N', `youtube_composition_clause` enum('N','Y') DEFAULT 'N', `sx_royalty_collection_commission` float DEFAULT '0', `topspin_rate` float DEFAULT '0', `topspin_rate_territory` text, `notice_required_in_days` int(11) DEFAULT NULL, `term_continues_until_recouped` enum('Y','N') DEFAULT 'N', `brand_split` float DEFAULT NULL, `other_rights_option` enum('any_and_all','other','none') DEFAULT 'any_and_all', `other_rights_text` varchar(50) DEFAULT NULL, `special_product_split` float DEFAULT NULL, `special_product_carve_out` text, `payment_interval` enum('month','quarter') DEFAULT 'quarter', `pay_after` enum('30','45','60','90') DEFAULT '45', `show_credit_card` enum('N','Y') DEFAULT 'N', `opt_out` enum('Y','N') DEFAULT 'N', `apply_fx_spread` enum('Y','N') CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT 'N', `reserve_rate` float(10,2) unsigned NOT NULL DEFAULT '0.00', `number_of_months_before_payout` tinyint(1) unsigned DEFAULT '0', `number_of_installments` tinyint(1) unsigned DEFAULT '0', `contract_terms` text, `service_type_id` int(11) unsigned DEFAULT NULL, `is_automatic_rollover` enum('Y','N') DEFAULT NULL, PRIMARY KEY (`id`), KEY `vendor_id` (`vendor_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Holds all contracts of labels'; """ cursor.execute(query) cnx.commit() cnx.close() @pytest.fixture(autouse='True') def currency_fixture(): """Insert a fixture to the currencies table.""" cnx = db_art_relations_connector() cursor = cnx.cursor() query = """INSERT INTO art_relations.currencies (ISO_4217_code, symbol_html_entity_code, currency_name, decimal_mark, thousands_separator, symbol_infront, subunit_to_unit, subunit_name, supported_payout_currency) VALUES ('USD', null, 'United States Dollar', null, null, null, null, null, DEFAULT) """ cursor.execute(query) cnx.commit() cnx.close() @pytest.fixture(autouse='True') def distribution_type_fixture(): """Insert a fixture to the distribution_type table.""" cnx = db_art_relations_connector() cursor = cnx.cursor() query = """INSERT INTO art_relations.distribution_type (id, name) VALUES (1, 'Full Track'), (2, 'Tone'), (3, 'Video Master'), (4, 'Wallpaper'), (5, 'Segment'), (6, 'Video Ringtone'), (7, 'Trailer'), (8, 'Sound Recording') """ cursor.execute(query) cnx.commit() cnx.close() def db_art_relations_connector(): """Connector to the art_relations db (docker).""" cnx = mysql.connector.connect( host='sync-contract-legacy-mysql', port='3306', database='art_relations', user='db_user', password='db_pass', ) return cnx def db_royalty_accounting_connector(): """Connector to the royalty_accounting db (docker).""" cnx = mysql.connector.connect( host='sync-contract-mysql', port='3306', database='royalty_accounting', user='db_user', password='db_pass', ) return cnx @pytest.mark.optionalhook def pytest_html_results_summary(prefix, summary, postfix): """Populate report with info and summary.""" prefix.extend([html.p(PYTEST_REPORT_PREFIX)]) summary.extend([html.p(PYTEST_REPORT_SUMMARY)]) def pytest_html_results_table_header(cells): """Create results table header.""" cells.insert(1, html.th('Jira ID')) cells.insert(2, html.th('Description')) cells.pop() def pytest_html_results_table_row(report, cells): """Populate results table row.""" jira_ids = getattr(report, 'jira_ids', []) jira_links = [] for index, item in enumerate(jira_ids): link = url('{}{}'.format(JIRA_PREFIX_URL, item), item) if index == 0: jira_links.append(html.a(link['name'], href=link['content'])) else: jira_links.append(', ') jira_links.append(html.a(link['name'], href=link['content'])) jira_links_html = html.td(*jira_links) cells.insert(1, jira_links_html) cells.insert(2, html.td(report.description)) cells.pop() @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): """Populate results table.""" outcome = yield report = outcome.get_result() report.description = str(item.function.__doc__) if item.get_closest_marker('jira') is not None: to_populate = [] for item in item.get_closest_marker('jira').args: to_populate.append(item) report.jira_ids = to_populate else: report.jira_id = []