"""Integration test configuration.""" import os import pytest from abacus_common_logic.connectors.database import db from py.xml import html from pytest_html.extras import url from abacus_legacy_sync.api import create_app from abacus_legacy_sync.config import Config from tests.integration.utils.ows_abacus_legacy_sync_api_client import ( AbacusLegacySyncAPIClient, ) from tests.utils.sql_templates import ( CREATE_COUNTRY_TABLE, CREATE_CURRENCIES_TABLE, CREATE_CUSTOMER_MASTER_MASTER, CREATE_DISTRIBUTION_TYPE_TABLE, CREATE_VENDOR_CONTRACT_DISTRIBUTION_TYPE_TABLE, CREATE_VENDOR_CONTRACT_TABLE, CREATE_VENDOR_DMS_MASTER_RESTRICTION, ) QA_BASE_URL = os.environ.get( 'QA_BASE_URL', 'http://abacus-legacy-sync-ows-abacus-legacy-sync:8080' ) JIRA_PREFIX_URL = 'https://theorchard.atlassian.net/browse/' PYTEST_REPORT_PREFIX = 'ows-abacus-legacy-sync microservice integration tests results' PYTEST_REPORT_SUMMARY = 'integration tests of ows-abacus-legacy-sync microservice' class TestConfig(Config): """Test configuration.""" AR_MYSQL_DB_NAME = os.environ.get('AR_MYSQL_TEST_DB_NAME', Config.AR_MYSQL_DB_NAME) @pytest.fixture(scope='session', autouse=True) def test_app(): """Create a test application.""" return create_app(TestConfig) @pytest.fixture(autouse=True) def test_app_in_context(test_app): """Push the test app onto the context.""" with test_app.app_context(): yield test_app @pytest.fixture def basic_headers(): """Return basic headers.""" return {'Content-Type': 'application/json'} def ows_abacus_legacy_sync_api_client(basic_headers): """Create ows-abacus-legacy-sync client.""" return AbacusLegacySyncAPIClient(QA_BASE_URL, basic_headers) @pytest.fixture(autouse=True) def create_tables(): """Create fixture tables in db.""" db.engine.execute(CREATE_VENDOR_CONTRACT_TABLE) db.engine.execute(CREATE_CURRENCIES_TABLE) db.engine.execute(CREATE_COUNTRY_TABLE) db.engine.execute(CREATE_DISTRIBUTION_TYPE_TABLE) db.engine.execute(CREATE_VENDOR_CONTRACT_DISTRIBUTION_TYPE_TABLE) db.engine.execute(CREATE_CUSTOMER_MASTER_MASTER) db.engine.execute(CREATE_VENDOR_DMS_MASTER_RESTRICTION) @pytest.fixture def create_fixtures(): """Create fixture entries in db.""" distribution_type_query = """ INSERT INTO distribution_type ( `id`, `name` ) VALUES ( 1, 'test' ) """ db.engine.execute(distribution_type_query) query_country = """ INSERT INTO country ( `name`, `country_code`, `abbrivation`, `continent`, `latitude`, `longitude`, `iso3166a3`, `continent_id` ) VALUES ( 'Poland', 'PL', 'PL', 'Europe', 52.000000, 21.000000, 'POL', 1 ), ( 'United States', 'US', 'US', 'North America', 42.000000, 24.000000, 'USA', 2 ) """ db.engine.execute(query_country) query_currency = """ INSERT INTO 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 ( 'PLN', null, 'Zloty', null, null, null, null, null, DEFAULT ) """ db.engine.execute(query_currency) query_customer_master_master = """ INSERT INTO customer_master_master ( customer_master_master_id, customer_name, sony_dms_code, ci_dms_code, delivery_option, show_delivery_info, product_type_id, delivery_cap, weekly_limit, eo_limit, encoding_order_status, mv_delivery_option, ddex_party_id, optin_date, required_genre_code, required_subgenre_code, required_subgenre, instant_grat, hd_only, status, label_store ) VALUES ( 1, 'test1', null, null, DEFAULT, DEFAULT, null, null, null, null, DEFAULT, DEFAULT, null, null, null, null, null, null, DEFAULT, DEFAULT, DEFAULT ), ( 2, 'test2', null, null, DEFAULT, DEFAULT, null, null, null, null, DEFAULT, DEFAULT, null, null, null, null, null, null, DEFAULT, DEFAULT, DEFAULT ) """ db.engine.execute(query_customer_master_master) @pytest.fixture def clean_db(): """Clean tables between runs.""" tables = [ 'country', 'currencies', 'customer_master_master', 'distribution_type', 'vendor_contract_distribution_type', 'vendor_contract', 'vendor_dms_master_restriction', ] for table in tables: db.engine.execute("""TRUNCATE TABLE {};""".format(table)) @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 = []