Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We now handle the case when the called Stored Procedure does not retu… #53

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 == []