def mock_connection(): mock_connection = PropertyMock() class MockCursor(object): def __init__(self, *args, **kwargs): self.statements = [] self.return_rows = [] self.cursor_position = 0 def execute(self, statement, *args, **kwargs): self.statements.append(statement) def fetchone(self, *args, **kwargs): if self.cursor_position > len(self.return_rows) - 1: return None else: next_row = self.return_rows[self.cursor_position] self.cursor_position += 1 return next_row def __enter__(self, *args, **kwargs): return self def __exit__(self, *args, **kwargs): return def __iter__(self): for row in self.return_rows: yield row mock_cursor = MockCursor() mock_connection_enter = MagicMock() mock_connection_enter.cursor.return_value = mock_cursor mock_connection.return_value = mock_connection mock_connection.cursor.return_value = mock_cursor mock_connection.__enter__ = lambda x: mock_connection_enter mock_connection.__exit__ = MagicMock() return mock_connection
def mock_connection(): mock_connection = PropertyMock() mock_connection.return_value = mock_connection mock_connection.__enter__ = MagicMock() mock_connection.__exit__ = MagicMock() return mock_connection