"""Conftest. This file gets picked up when running py.test tests: http://pytest.org/latest/writing_plugins.html#conftest """ import re from unittest.mock import MagicMock import boto3 import pytest class SubstringMatcher: """Class which allows to check for SQL calls by the list of substrings.""" def __init__(self, containing): """Initialise object. Args: containing (list): A list of substrings to check. """ self.containing = [el.lower() for el in containing] def __eq__(self, sql): """Equal magic method. Check if all the substrings from the passed list are present in SQL. Args: sql (str): SQL statement from a call. """ sql = re.sub('\\s+', ' ', sql.lower()).strip() return all(el in sql for el in self.containing) def __repr__(self): """Represent string magic method to play nice with py.test messages.""" return 'SQL containing: {}'.format(', '.join(self.containing)) @pytest.fixture def sf_config_mock(): """Fixture returning the dict with Snowflake connection params.""" return { 'account': 'test_acc', 'role': 'test_role', 'host': 'test_host', 'warehouse': 'test_wh', 'port': 10, 'user': 'test_user', 'password': 'test_pass', 'db': 'test_db', 'schema': 'test_schema', 'private_key': b'10' } @pytest.fixture(autouse=True) def mock_swf_base(monkeypatch): """Mock all connections to SWF.""" monkeypatch.setattr( 'boto.swf.layer2.SWFBase.__init__', MagicMock(return_value=None)) @pytest.fixture(autouse=True) def mock_boto3_client(monkeypatch): """Mock all connections via boto3.""" session_obj_mock = MagicMock() client_mock = MagicMock() session_obj_mock.client = MagicMock(return_value=client_mock) session_mock = MagicMock(return_value=session_obj_mock) monkeypatch.setattr(boto3.session, 'Session', session_mock) return client_mock