"""Config for tests.""" import sys import json from flexmock import flexmock import pytest from salessheets import config from salessheets.connectors import mysql from tests.utils import dbutils @pytest.fixture(autouse=True, scope='session') def _exit_if_not_test_environment(): """For safety, only run tests in test environment pointed to sqlite. Exit immediately if not in test environment or not pointed to sqlite. """ with mysql.salessheets_history_session_scope() as session: if config.ENVIRONMENT != config.ENVIRONMENT_TEST: sys.exit('Environment must be set to {}.'.format( config.ENVIRONMENT_TEST)) if 'sqlite' not in session.bind.url.drivername: sys.exit('Tests must point to sqlite database.') @pytest.fixture(scope='session') def create_test_dir(tmpdir_factory): """Create temp directory for testing of creation of pdf files in dir.""" fn = tmpdir_factory.mktemp('pdfs') return fn @pytest.fixture(scope='function') def set_bulk_sales_sheets_feature_flag_to_true(): """Set feature flag to true.""" (flexmock(config) .should_receive('BULK_SALES_SHEETS') .and_return(True)) def set_bulk_sales_sheets_feature_flag_to_false(): """Set feature flag to false.""" (flexmock(config) .should_receive('BULK_SALES_SHEETS') .and_return(False)) @pytest.fixture def marketing_highlights_from_ows(): """Fixture for marketing highlights data retrieved from ows.""" test_marketing_highlights = [ { 'scope': 'public', 'description': 'first_example_description', 'entity': 'release', 'entity_id': 1234567, 'client': 'example_client', 'mkt_program_id': 20, 'attachment': 'example_attachment', 'highlight_id': 123456 }, { 'scope': 'public', 'description': 'second_example_description', 'entity': 'example', 'entity_id': 1234567, 'client': 'example_client', 'mkt_program_id': 12345, 'attachment': 'example_attachment', 'highlight_id': 123457 }, { 'scope': 'public', 'description': 'third_example_description', 'entity': 'project', 'entity_id': 1234567, 'client': 'example_client', 'mkt_program_id': 20, 'attachment': 'example_attachment', 'highlight_id': 123458 }, { 'scope': 'public', 'description': '', 'entity': 'project', 'entity_id': 1234567, 'client': 'example_client', 'mkt_program_id': 20, 'attachment': 'example_attachment', 'highlight_id': 123459 } ] return test_marketing_highlights @pytest.fixture def filtered_marketing_highlights(): """Marketing highlights filtered by mkt_program_id=20.""" filtered_product_highlights = [{ 'scope': 'public', 'description': 'first_example_description', 'entity': 'release', 'entity_id': 1234567, 'client': 'example_client', 'mkt_program_id': 20, 'attachment': 'example_attachment', 'highlight_id': 123456 }, { 'scope': 'public', 'description': 'third_example_description', 'entity': 'project', 'entity_id': 1234567, 'client': 'example_client', 'mkt_program_id': 20, 'attachment': 'example_attachment', 'highlight_id': 123458 }] return filtered_product_highlights @pytest.fixture def create_sqs_message(): """Mock message creation.""" def create_message(msg): mock_message = {'Body': json.dumps(msg)} mock_message['MessageAttributes'] = { 'Correlation-Id': "a34362f83hg" } return mock_message return create_message @pytest.fixture def test_database(): """Initialize in-memory database for testing purposes.""" dbutils.db_setup_function() yield dbutils.db_teardown_function()