"""Test configuration.""" import os from unittest.mock import MagicMock from unittest.mock import patch from boto.dynamodb2 import layer1 os.environ['Environment'] = 'qa' layer1.DynamoDBConnection = MagicMock() items = dict( qa_ows_auth_user=[], qa_ows_auth_token=[], qa_ows_auth_rsa_key=[]) 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) for item in items.get(self.table_name): found = True for key, val in kwargs.items(): if item.get(key.replace('__eq', '')) == val: continue found = False if found: return [item] return [] # 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()