"""Conftest. This file gets picked up when running py.test tests: http://pytest.org/latest/writing_plugins.html#conftest """ import os from unittest.mock import MagicMock from unittest.mock import patch import boto from boto.dynamodb2 import layer1 from social_analytics import config config.ENVIRONMENT = config.TEST_ENVIRONMENT config.DB_URL = 'sqlite://' os.environ['Environment'] = 'qa' layer1.DynamoDBConnection = MagicMock() boto.connect_ses = MagicMock() items = dict(qa_social_analytics_user_interaction=[]) class Item(dict): """DynamoDB Item.""" def partial_save(self): """Partially saving an item.""" return True def put_item(self, data): """Mock put item operation. Args: data (dict): the data to save. Returns: dict: the data that has been saved. """ items.get(self.table_name).append(Item(data)) return data def get_item(self, **data): """Mock get item operation. Args: data (dict): named params. Returns: data (dict): the object if found. """ result = query_2(self, **data) if len(result) > 0: return result[0] return None def query_2(self, **kwargs): """Mock query_2 operation from DynamoDB. Args: kwargs (dict): the keys used to fetch the dataset. Returns: list: the list of objects (if found). """ kwargs.pop('index', None) kwargs.pop('reverse', None) kwargs.pop('limit', None) results = [] for item in items.get(self.table_name): found = False for key, val in kwargs.items(): if item.get(key.replace('__eq', '')) == val: found = True if found: results.append(item) return results # Patch the dynamodb table. patch('boto.dynamodb2.table.Table.put_item', put_item).start() patch('boto.dynamodb2.table.Table.get_item', get_item).start() patch('boto.dynamodb2.table.Table.query_2', query_2).start()