"""Fixtures for lambda testing.""" import os import mysql.connector import pytest from py.xml import html from pytest_html.extras import url from utils.dockerized_lambda_client import DockerizedLambdaAPIClient QA_BASE_URL = os.environ.get('QA_BASE_URL', 'http://sync-account-lambda:8080') VENDOR_ID = 123456 # reporting JIRA_PREFIX_URL = 'https://theorchard.atlassian.net/browse/' PYTEST_REPORT_PREFIX = 'sync_account lambda integration tests results' PYTEST_REPORT_SUMMARY = ( 'Lambda to create/update an ABACUS account.' 'Intended to use as a part of the KNR and AWAL ' 'accounts creation step functions, ' 'after the CreateVendor step.' ) def dockerized_lambda_api_client(headers): """Create sync_account dockerized lambda object.""" return DockerizedLambdaAPIClient(QA_BASE_URL, headers) @pytest.fixture(scope='session') def basic_headers(): """Return basic headers.""" return {'Content-Type': 'application/json'} def mysql_client(): """Mysql client for integration tests.""" return mysql.connector.connect( host=os.environ.get('MYSQL_HOST'), port=os.environ.get('MYSQL_PORT'), database=os.environ.get('MYSQL_DB'), user=os.environ.get('MYSQL_USER'), password=os.environ.get('MYSQL_PASS'), ) def db_delete_test_account(account_id): """Delete test account from abacus DB.""" cnx = mysql_client() cursor = cnx.cursor(buffered=True, dictionary=True) query = """DELETE FROM account where account_id=%(account_id)s;""" params = {'account_id': account_id} cursor.execute(query, params) cnx.commit() cnx.close() def db_select_account(account_id): """Query the DB.""" cnx = mysql_client() cursor = cnx.cursor(buffered=True, dictionary=True) query = """SELECT account_name FROM account WHERE account_id=%(account_id)s""" params = {'account_id': account_id} cursor.execute(query, params) result = cursor.fetchall() cnx.commit() cnx.close() return result def query_the_db(query): """Query the DB.""" cnx = mysql_client() cursor = cnx.cursor() cursor.execute(query) cnx.commit() cnx.close() def query_the_db_with_values(query, values): """Query the DB.""" cnx = mysql_client() cursor = cnx.cursor() cursor.execute(query, values) cnx.commit() cnx.close() @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 = []