|
| 1 | +import mssql_python |
| 2 | +import pandas as pd |
| 3 | +import pytest |
| 4 | + |
| 5 | +from nypl_py_utils.classes.azure_client import AzureClient, AzureClientError |
| 6 | +from pandas.testing import assert_frame_equal |
| 7 | + |
| 8 | + |
| 9 | +class TestAzureClient: |
| 10 | + @pytest.fixture |
| 11 | + def mock_azure_conn(self, mocker): |
| 12 | + return mocker.patch( |
| 13 | + "nypl_py_utils.classes.azure_client.mssql_python.connect") |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def test_instance(self): |
| 17 | + return AzureClient( |
| 18 | + server="test_server", |
| 19 | + database="test_database", |
| 20 | + user="test_user", |
| 21 | + password="test_password", |
| 22 | + ) |
| 23 | + |
| 24 | + def test_connect_success(self, mock_azure_conn, test_instance): |
| 25 | + test_instance.connect() |
| 26 | + |
| 27 | + assert test_instance.conn == mock_azure_conn.return_value |
| 28 | + mock_azure_conn.return_value.setencoding.assert_called_once_with( |
| 29 | + encoding="utf-8" |
| 30 | + ) |
| 31 | + mock_azure_conn.return_value.setdecoding.assert_called_once_with( |
| 32 | + sqltype=mssql_python.SQL_WCHAR, encoding="utf-8" |
| 33 | + ) |
| 34 | + # credentials are interpolated into connection string |
| 35 | + connection_str = mock_azure_conn.call_args.kwargs["connection_str"] |
| 36 | + assert connection_str == ( |
| 37 | + "Server=test_server;Database=test_database;" |
| 38 | + "UID=test_user;PWD=test_password;Encrypt=yes;" |
| 39 | + ) |
| 40 | + |
| 41 | + def test_connect_retry_success( |
| 42 | + self, mock_azure_conn, test_instance, mocker, caplog |
| 43 | + ): |
| 44 | + mock_sleep = mocker.patch( |
| 45 | + "nypl_py_utils.classes.azure_client.time.sleep") |
| 46 | + success_conn = mocker.MagicMock() |
| 47 | + mock_azure_conn.side_effect = [ |
| 48 | + mssql_python.OperationalError("busy", "ddbc busy"), |
| 49 | + success_conn, |
| 50 | + ] |
| 51 | + with caplog.at_level("ERROR"): |
| 52 | + test_instance.connect(retry_count=2, backoff_factor=2) |
| 53 | + |
| 54 | + assert test_instance.conn == success_conn |
| 55 | + assert mock_azure_conn.call_count == 2 |
| 56 | + mock_sleep.assert_called_once_with(2**0) |
| 57 | + assert caplog.text == "" |
| 58 | + |
| 59 | + def test_connect_retry_fail( |
| 60 | + self, mock_azure_conn, test_instance, mocker, caplog |
| 61 | + ): |
| 62 | + mocker.patch("nypl_py_utils.classes.azure_client.time.sleep") |
| 63 | + mock_azure_conn.side_effect = mssql_python.OperationalError( |
| 64 | + "still busy", "ddbc busy" |
| 65 | + ) |
| 66 | + |
| 67 | + with pytest.raises(AzureClientError): |
| 68 | + test_instance.connect(retry_count=2, backoff_factor=2) |
| 69 | + |
| 70 | + # retry_count=2 -> three attempts total before giving up |
| 71 | + assert mock_azure_conn.call_count == 3 |
| 72 | + assert "Error connecting to test_database database" in caplog.text |
| 73 | + |
| 74 | + def test_connect_unexpected_error( |
| 75 | + self, mock_azure_conn, test_instance, caplog |
| 76 | + ): |
| 77 | + mock_azure_conn.side_effect = ValueError("uh oh") |
| 78 | + |
| 79 | + with pytest.raises(AzureClientError): |
| 80 | + test_instance.connect(retry_count=3) |
| 81 | + |
| 82 | + assert mock_azure_conn.call_count == 1 |
| 83 | + assert ( |
| 84 | + "Error connecting to test_database database: uh oh" in caplog.text |
| 85 | + ) |
| 86 | + |
| 87 | + def test_execute_query_no_params_success( |
| 88 | + self, mock_azure_conn, test_instance, mocker |
| 89 | + ): |
| 90 | + test_instance.connect() |
| 91 | + mock_cursor = mocker.MagicMock() |
| 92 | + mock_cursor.fetchall.return_value = [(1, 2), (3, 4)] |
| 93 | + test_instance.conn.cursor.return_value = mock_cursor |
| 94 | + |
| 95 | + result = test_instance.execute_query("SELECT * FROM t") |
| 96 | + |
| 97 | + assert result == [(1, 2), (3, 4)] |
| 98 | + mock_cursor.execute.assert_called_once_with("SELECT * FROM t") |
| 99 | + mock_cursor.close.assert_called_once() |
| 100 | + |
| 101 | + def test_execute_query_with_params_success( |
| 102 | + self, mock_azure_conn, test_instance, mocker |
| 103 | + ): |
| 104 | + test_instance.connect() |
| 105 | + mock_cursor = mocker.MagicMock() |
| 106 | + mock_cursor.fetchall.return_value = [] |
| 107 | + test_instance.conn.cursor.return_value = mock_cursor |
| 108 | + |
| 109 | + test_instance.execute_query("SELECT ?", params=("a",)) |
| 110 | + |
| 111 | + mock_cursor.execute.assert_called_once_with("SELECT ?", ("a",)) |
| 112 | + |
| 113 | + def test_execute_query_no_params_returns_dataframe_success( |
| 114 | + self, mock_azure_conn, test_instance, mocker |
| 115 | + ): |
| 116 | + test_instance.connect() |
| 117 | + mock_cursor = mocker.MagicMock() |
| 118 | + mock_cursor.description = [("col1",), ("col2",)] |
| 119 | + mock_cursor.fetchall.return_value = [(1, 2), (3, 4)] |
| 120 | + test_instance.conn.cursor.return_value = mock_cursor |
| 121 | + |
| 122 | + result = test_instance.execute_query("SELECT * FROM t", dataframe=True) |
| 123 | + |
| 124 | + expected = pd.DataFrame({"col1": [1, 3], "col2": [2, 4]}) |
| 125 | + assert_frame_equal(result, expected) |
| 126 | + |
| 127 | + def test_execute_query_with_params_returns_dataframe_success( |
| 128 | + self, mock_azure_conn, test_instance, mocker |
| 129 | + ): |
| 130 | + test_instance.connect() |
| 131 | + mock_cursor = mocker.MagicMock() |
| 132 | + mock_cursor.description = [("col1",), ("col2",)] |
| 133 | + mock_cursor.fetchall.return_value = [(1, 2), (3, 4)] |
| 134 | + test_instance.conn.cursor.return_value = mock_cursor |
| 135 | + expected = pd.DataFrame({"col1": [1, 3], "col2": [2, 4]}) |
| 136 | + |
| 137 | + result = test_instance.execute_query( |
| 138 | + "SELECT * FROM t WHERE col1 = ?", params=("a",), dataframe=True |
| 139 | + ) |
| 140 | + |
| 141 | + mock_cursor.execute.assert_called_once_with( |
| 142 | + "SELECT * FROM t WHERE col1 = ?", ("a",) |
| 143 | + ) |
| 144 | + assert_frame_equal(result, expected) |
| 145 | + |
| 146 | + def test_execute_query_fail( |
| 147 | + self, mock_azure_conn, test_instance, mocker, caplog |
| 148 | + ): |
| 149 | + test_instance.connect() |
| 150 | + mock_conn = test_instance.conn |
| 151 | + mock_cursor = mocker.MagicMock() |
| 152 | + mock_cursor.execute.side_effect = Exception("bad query") |
| 153 | + mock_conn.cursor.return_value = mock_cursor |
| 154 | + |
| 155 | + with pytest.raises(AzureClientError): |
| 156 | + test_instance.execute_query("SELECT bad") |
| 157 | + |
| 158 | + mock_conn.rollback.assert_called_once() |
| 159 | + mock_conn.close.assert_called_once() |
| 160 | + assert test_instance.conn is None |
| 161 | + mock_cursor.close.assert_called_once() |
| 162 | + assert ( |
| 163 | + "Error executing test_database query 'SELECT bad'" in caplog.text |
| 164 | + ) |
| 165 | + |
| 166 | + def test_execute_query_fail_with_rollback_error( |
| 167 | + self, mock_azure_conn, test_instance, mocker, caplog |
| 168 | + ): |
| 169 | + test_instance.connect() |
| 170 | + mock_conn = test_instance.conn |
| 171 | + mock_cursor = mocker.MagicMock() |
| 172 | + mock_cursor.execute.side_effect = Exception("bad query") |
| 173 | + mock_conn.cursor.return_value = mock_cursor |
| 174 | + mock_conn.rollback.side_effect = Exception("rollback issue") |
| 175 | + |
| 176 | + with pytest.raises(AzureClientError): |
| 177 | + test_instance.execute_query("SELECT bad") |
| 178 | + |
| 179 | + mock_conn.close.assert_called_once() |
| 180 | + assert test_instance.conn is None |
| 181 | + assert "Error rolling back open transaction" in caplog.text |
| 182 | + assert ( |
| 183 | + "Error executing test_database query 'SELECT bad'" in caplog.text |
| 184 | + ) |
| 185 | + |
| 186 | + def test_execute_query_without_connection(self, test_instance, caplog): |
| 187 | + assert test_instance.conn is None |
| 188 | + |
| 189 | + with pytest.raises(AzureClientError): |
| 190 | + test_instance.execute_query("SELECT 1") |
| 191 | + |
| 192 | + assert "No active database connection" in caplog.text |
| 193 | + |
| 194 | + def test_close_connection_success(self, mock_azure_conn, test_instance): |
| 195 | + test_instance.connect() |
| 196 | + mock_conn = test_instance.conn |
| 197 | + |
| 198 | + test_instance.close_connection() |
| 199 | + |
| 200 | + mock_conn.close.assert_called_once() |
| 201 | + assert test_instance.conn is None |
| 202 | + |
| 203 | + def test_close_connection_when_already_closed(self, test_instance): |
| 204 | + # no connection -> nothing to close, so nothing happens & no error |
| 205 | + assert test_instance.conn is None |
| 206 | + test_instance.close_connection() |
| 207 | + assert test_instance.conn is None |
0 commit comments