|
4 | 4 | import types |
5 | 5 | from encodings.utf_8 import encode |
6 | 6 | from pathlib import Path |
| 7 | +from unittest.mock import MagicMock, patch |
7 | 8 |
|
8 | 9 | import pandas as pd |
9 | 10 | import pytest |
@@ -58,6 +59,10 @@ def fail_with_invalid_json_error(self, url, timeout): |
58 | 59 | raise InvalidJSONError() |
59 | 60 |
|
60 | 61 |
|
| 62 | +def _mock_blob_sequence_days(response_json): |
| 63 | + return {1: "file1", 2: "file2"} |
| 64 | + |
| 65 | + |
61 | 66 | class Test_Client: |
62 | 67 | """ |
63 | 68 | Tests the ``datareservoirio.Client`` class. |
@@ -158,6 +163,53 @@ def test_get_raise_empty(self, client): |
158 | 163 | with pytest.raises(ValueError): |
159 | 164 | client.get("e3d82cda-4737-4af9-8d17-d9dfda8703d0", raise_empty=True) |
160 | 165 |
|
| 166 | + def test_get_keyerror(self, client): |
| 167 | + series_id = "test_series_id" |
| 168 | + start = "2023-01-01" |
| 169 | + end = "2023-01-02" |
| 170 | + |
| 171 | + response_mock = MagicMock() |
| 172 | + response_mock.status_code = 200 |
| 173 | + response_mock.json.return_value = { |
| 174 | + "Files": [ |
| 175 | + {"Chunks": "file1"}, |
| 176 | + {"Chunks": "file2"}, |
| 177 | + ] # mock files with correct structure |
| 178 | + } |
| 179 | + |
| 180 | + client._auth_session.get = MagicMock(return_value=response_mock) |
| 181 | + |
| 182 | + def mock_storage_get(blob_sequence_i): |
| 183 | + if blob_sequence_i == "file1": |
| 184 | + return pd.DataFrame( |
| 185 | + { |
| 186 | + "index": [1672358410000000000, 1672358400000000000], |
| 187 | + "values": [100, 200], |
| 188 | + } |
| 189 | + ) |
| 190 | + elif blob_sequence_i == "file2": |
| 191 | + return pd.DataFrame( |
| 192 | + { |
| 193 | + "index": [1672358410000000000, 1672358420000000000], |
| 194 | + "values": [200, 400], |
| 195 | + } |
| 196 | + ) |
| 197 | + else: |
| 198 | + raise ValueError("Unexpected blob_sequence_i value") |
| 199 | + |
| 200 | + client._storage.get = MagicMock(side_effect=mock_storage_get) |
| 201 | + |
| 202 | + with patch( |
| 203 | + "datareservoirio.client._blob_sequence_days", |
| 204 | + side_effect=_mock_blob_sequence_days, |
| 205 | + ): |
| 206 | + with patch("datareservoirio.logging.warning") as mock_logging_warning: |
| 207 | + result = client.get(series_id, start, end) |
| 208 | + mock_logging_warning.assert_called_once_with( |
| 209 | + "The time series you requested is not properly ordered. The data will be sorted to attempt to resolve the issue. Please note that this operation may take some time." |
| 210 | + ) |
| 211 | + assert isinstance(result, pd.Series) |
| 212 | + |
161 | 213 | def test_get_raises_end_not_after_start(self, client): |
162 | 214 | start = 1672358400000000000 |
163 | 215 | end = start - 1 |
@@ -427,6 +479,22 @@ def test_create_upload_raises(self, client, data_float, response_cases): |
427 | 479 | with pytest.raises(HTTPError): |
428 | 480 | client.create(series=data_float.as_series(), wait_on_verification=True) |
429 | 481 |
|
| 482 | + def test_create_raises_valueerror_unsorted_index(self, client): |
| 483 | + data = pd.Series( |
| 484 | + [1, 2, 3], |
| 485 | + index=[ |
| 486 | + pd.to_datetime("2022-04-04"), |
| 487 | + pd.to_datetime("2022-04-03"), |
| 488 | + pd.to_datetime("2022-04-05"), |
| 489 | + ], |
| 490 | + ) |
| 491 | + with pytest.raises(ValueError) as e: |
| 492 | + client.create(data) |
| 493 | + assert ( |
| 494 | + str(e.value) |
| 495 | + == "Index not sorted. Please sort series on index before creating a timeseries." |
| 496 | + ) |
| 497 | + |
430 | 498 | def test_append( |
431 | 499 | self, client, data_float, mock_requests, bytesio_with_memory, response_cases |
432 | 500 | ): |
@@ -501,6 +569,23 @@ def test_append_upload_raises(self, client, data_float, response_cases): |
501 | 569 | with pytest.raises(HTTPError): |
502 | 570 | client.append(data_float.as_series(), series_id, wait_on_verification=True) |
503 | 571 |
|
| 572 | + def test_append_raises_valueerror_unsorted_index(self, client): |
| 573 | + series_id = "d30519af-5035-4093-a425-dafd857ad0ef" |
| 574 | + data = pd.Series( |
| 575 | + [1, 2, 3], |
| 576 | + index=[ |
| 577 | + pd.to_datetime("2022-04-04"), |
| 578 | + pd.to_datetime("2022-04-03"), |
| 579 | + pd.to_datetime("2022-04-05"), |
| 580 | + ], |
| 581 | + ) |
| 582 | + with pytest.raises(ValueError) as e: |
| 583 | + client.append(data, series_id) |
| 584 | + assert ( |
| 585 | + str(e.value) |
| 586 | + == "Index not sorted. Please sort series on index before appending data." |
| 587 | + ) |
| 588 | + |
504 | 589 | @pytest.mark.parametrize("data", ("data_float", "data_string")) |
505 | 590 | def test__verify_and_prepare_series(self, client, data, request): |
506 | 591 | data = request.getfixturevalue(data) |
|
0 commit comments