Skip to content

Commit

Permalink
Merge pull request #53 from PrediktorAS/fix/handle-cases-when-stored-…
Browse files Browse the repository at this point in the history
…procedure-does-not-return-results

We now handle the case when the called Stored Procedure does not retu…
  • Loading branch information
jNormaster authored Nov 28, 2023
2 parents bdf5d23 + c4486fe commit 5afe9d9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/pyprediktormapclient/dwh/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ def execute(self, query: str, *args, **kwargs) -> List[Any]:
self.__connect()
self.cursor.execute(query, *args, **kwargs)

result = self.cursor.fetchall()
result = []
try:
result = self.cursor.fetchall()
except Exception:
pass

self.__commit()

return result
Expand Down
36 changes: 33 additions & 3 deletions tests/dwh/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,7 @@ def test_execute_when_init_db_connection_is_successfull_but_fails_when_calling_e
db.execute(query)


def test_execute_when_parameter_passed_then_fetch_results_and_return_data(
monkeypatch,
):
def test_execute_when_parameter_passed_then_fetch_results_and_return_data(monkeypatch):
query = "INSERT INTO mytable VALUES (?, ?)"
param_one = "John"
param_two = "Smith"
Expand Down Expand Up @@ -756,3 +754,35 @@ def test_execute_when_parameter_passed_then_fetch_results_and_return_data(
mock_execute.assert_called_once_with(query, param_one, param_two)
mock_fetch.assert_called_once()
assert actual_result == expected_result


def test_execute_when_fetchall_throws_error_then_return_empty_list(monkeypatch):
query = "INSERT INTO mytable VALUES (?, ?)"
param_one = "John"
param_two = "Smith"
driver_index = 0

# Mock the cursor and execute
mock_cursor = Mock()
mock_execute = Mock()
mock_fetchall = Mock(side_effect=Exception("Error occurred"))

# Mock the connection method to return a mock connection with a mock cursor
mock_connection = Mock()
mock_connection.cursor.return_value = mock_cursor

monkeypatch.setattr(
"pyodbc.connect",
Mock(return_value=mock_connection),
)

# Mock the fetchall method
mock_cursor.execute = mock_execute
mock_cursor.fetchall = mock_fetchall

db = Db(grs(), grs(), grs(), grs(), driver_index)
actual_result = db.execute(query, param_one, param_two)

mock_execute.assert_called_once_with(query, param_one, param_two)
mock_fetchall.assert_called_once()
assert actual_result == []

0 comments on commit 5afe9d9

Please sign in to comment.