def test_iterate_must_call_execute_with_query_params(self): mocked_cursor = self._create_mocked_cursor() with mock.patch('onmydesk.core.datasets.connection.cursor', return_value=mocked_cursor): dataset = datasets.SQLDataset('SELECT * FROM flunfa WHERE id = %s', [1]) with dataset: for i in dataset.iterate(): pass mocked_cursor.execute.assert_called_once_with( 'SELECT * FROM flunfa WHERE id = %s', [1])
def test_iterate_must_call_correct_db_alias(self): db_alias = 'my-db-alias' my_connection = mock.MagicMock() my_connection.cursor.return_value = self._create_mocked_cursor() connections = {db_alias: my_connection} with mock.patch('onmydesk.core.datasets.connections', connections): dataset = datasets.SQLDataset('SELECT * FROM flunfa WHERE id = %s', [1], db_alias=db_alias) with dataset: for i in dataset.iterate(): pass self.assertTrue(my_connection.cursor.called)
def test_iterate_with_no_context_manager_must_return_result(self): mocked_cursor = self._create_mocked_cursor() with mock.patch('onmydesk.core.datasets.connection.cursor', return_value=mocked_cursor): dataset = datasets.SQLDataset('SELECT * FROM flunfa') results = [] for item in dataset.iterate(): results.append(item) expected_result = [ OrderedDict([('name', 'Alisson'), ('age', 25)]), OrderedDict([('name', 'Joao'), ('age', 12)]), ] self.assertEqual(results, expected_result)
def dataset(self): return datasets.SQLDataset(self.query, self.query_params, self.db_alias)
def dataset(self): """Return SQLDataset to be used by this report.""" return datasets.SQLDataset(self.query, self.query_params, self.db_alias)