-
Notifications
You must be signed in to change notification settings - Fork 22
[GAIA-21588] Create a function to convert v2prime result into dataframe #379
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
Changes from all commits
255ba81
d7e5c5b
960b8f1
03de82e
deafd4d
87610a6
faa60b0
b9fd10b
bcee8a1
27fde3c
e8a67f6
c01d1b3
3f6961d
a58325b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| try: | ||
| # Python 3.3+ | ||
| from unittest.mock import patch | ||
| except ImportError: | ||
| # Python 2.7 | ||
| from mock import patch | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
|
|
||
| from pandas.testing import assert_frame_equal | ||
| from unittest import TestCase | ||
|
|
||
| from groclient import Experimental | ||
| from groclient.mock_data import mock_v2_prime_data_request, mock_v2_prime_data_response | ||
|
|
||
| MOCK_HOST = "pytest.groclient.url" | ||
| MOCK_TOKEN = "pytest.groclient.token" | ||
|
|
||
| class ExperimentalTests(TestCase): | ||
| def setUp(self): | ||
| self.client = Experimental(MOCK_HOST, MOCK_TOKEN) | ||
| self.assertTrue(isinstance(self.client, Experimental)) | ||
|
|
||
| @patch("groclient.lib.get_data_points_v2_prime") | ||
| def test_get_data_points(self, mock_get_data_points): | ||
| mock_get_data_points.return_value = mock_v2_prime_data_response.copy() | ||
|
|
||
| res = self.client.get_data_points(**mock_v2_prime_data_request) | ||
|
|
||
| self.assertEqual(len(res), len(mock_v2_prime_data_response)) | ||
| self.assertIn('data_points', res[0]) | ||
| self.assertIn('series_description', res[0]) | ||
| point = res[0]['data_points'][0] | ||
| self.assertTrue(isinstance(point["start_timestamp"], int)) | ||
| self.assertTrue(isinstance(point["end_timestamp"], int)) | ||
|
|
||
| @patch("groclient.lib.get_data_points_v2_prime") | ||
| def test_get_data_points_df(self, mock_get_data_points): | ||
| mock_get_data_points.return_value = mock_v2_prime_data_response.copy() | ||
| df = self.client.get_data_points_df(**mock_v2_prime_data_request) | ||
|
|
||
| expected_df = pd.DataFrame({ | ||
| "value": [33.20, 32.73], | ||
| "start_timestamp":['2023-05-01', '2023-05-01'], | ||
| "end_timestamp": ['2023-05-02', '2023-05-02'], | ||
| "metric_id": [2540047, 2540047], | ||
| "item_id": [3457, 3457], | ||
| "region_id": [12344, 12345], | ||
| "partner_region_id": [np.nan, np.nan], | ||
| "frequency_id": [1,1], | ||
| "source_id": [26, 26], | ||
| "unit_id": [36, 36] | ||
| }).astype({ | ||
| 'start_timestamp': 'datetime64', | ||
| 'end_timestamp': 'datetime64' | ||
| }) | ||
|
|
||
| print(df.dtypes) | ||
|
|
||
| assert_frame_equal(df, expected_df) | ||
|
|
||
|
|
||
| @patch("groclient.lib.get_data_points_v2_prime") | ||
| def test_get_data_points_df_no_data(self, mock_get_data_points): | ||
| mock_get_data_points.return_value = [] | ||
| df = self.client.get_data_points_df(**mock_v2_prime_data_request) | ||
|
|
||
| self.assertTrue(df.empty) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,3 +190,50 @@ | |
| "frequency_id": 4, | ||
| "source_id": 5, | ||
| } | ||
|
|
||
| mock_v2_prime_data_request = { | ||
| "metric_id": 2540047, | ||
| "item_ids": [3457], | ||
| "source_id": 26, | ||
| "frequency_id": 1, | ||
| "unit_id": 36, | ||
| "start_date": "2023-05-01", | ||
| "region_ids": [1215], | ||
| } | ||
|
|
||
| mock_v2_prime_data_response = [ | ||
| { | ||
| "data_points": [ | ||
| { | ||
| "value": 33.20, | ||
| "start_timestamp": "1682899200", | ||
| "end_timestamp": "1682985600" | ||
| } | ||
| ], | ||
| "series_description": { | ||
| "source_id": 26, | ||
| "item_id": 3457, | ||
| "metric_id": 2540047, | ||
| "frequency_id": 1, | ||
| "region_id": 12344, | ||
| "unit_id": 36 | ||
| } | ||
| }, | ||
| { | ||
| "data_points": [ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have more than one data points?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. data_points is the list of data points per series, yes you can have multiple series in response, but each series dic should only have one data_points attribute |
||
| { | ||
| "value": 32.73, | ||
| "start_timestamp": "1682899200", | ||
| "end_timestamp": "1682985600" | ||
| } | ||
| ], | ||
| "series_description": { | ||
| "source_id": 26, | ||
| "item_id": 3457, | ||
| "metric_id": 2540047, | ||
| "frequency_id": 1, | ||
| "region_id": 12345, | ||
| "unit_id": 36 | ||
| } | ||
| } | ||
| ] | ||
Uh oh!
There was an error while loading. Please reload this page.