From 82edbdc6a98d7fcdf558c5baf85c0ddb340b921d Mon Sep 17 00:00:00 2001 From: RodneyCodess <160431998+RodneyCodess@users.noreply.github.com> Date: Wed, 5 Jun 2024 14:12:26 -0400 Subject: [PATCH] Fix bug with trailing slashes in `xdmod_host`. (#24) Co-authored-by: Aaron Weeden <31246768+aaronweeden@users.noreply.github.com> --- CHANGELOG.md | 1 + .../test_datawarehouse_integration.py | 43 ++++++++++++------- tests/unit/test_datawarehouse_unit.py | 6 ++- xdmod_data/_http_requester.py | 2 + 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aada8df2..9c0a77b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Remove limit on number of results returned from `get_filter_values()` ([\#21](https://github.com/ubccr/xdmod-data/pull/21)). - Add a "Feedback / Feature Requests" section to the README ([\#22](https://github.com/ubccr/xdmod-notebooks/pull/22)). - Improve performance of validation of filters and raw fields ([\#18](https://github.com/ubccr/xdmod-data/pull/18)). +- Fix bug with trailing slashes in `xdmod_host` ([\#24](https://github.com/ubccr/xdmod-data/pull/24)). ## v1.0.0 (2023-07-21) - Initial release. diff --git a/tests/integration/test_datawarehouse_integration.py b/tests/integration/test_datawarehouse_integration.py index 9e296fec..1688b5c0 100644 --- a/tests/integration/test_datawarehouse_integration.py +++ b/tests/integration/test_datawarehouse_integration.py @@ -35,7 +35,7 @@ VALID_DATE = '2020-01-01' VALID_DIMENSION = 'Resource' VALID_VALUES = { - 'duration': 'Previous month', + 'duration': 'Yesterday', 'realm': 'Jobs', 'metric': 'CPU Hours: Total', 'dimension': VALID_DIMENSION, @@ -44,7 +44,7 @@ 'aggregation_unit': 'Auto', 'parameter': 'duration', 'fields': ['Nodes'], - 'show_progress': True, + 'show_progress': False, } KEY_ERROR_TEST_VALUES_AND_MATCHES = { 'duration': (INVALID_STR, 'Invalid value for `duration`'), @@ -92,8 +92,16 @@ method + ':end_date', ] date_malformed_test_params += [ - (method, 'start_date', {'duration': (INVALID_STR, VALID_DATE)}), - (method, 'end_date', {'duration': (VALID_DATE, INVALID_STR)}), + ( + method, + 'start_date', + {'duration': (INVALID_STR, VALID_DATE)}, + ), + ( + method, + 'end_date', + {'duration': (VALID_DATE, INVALID_STR)}, + ), ] value_error_test_methods += [method] if 'filters' in METHOD_PARAMS[method]: @@ -107,8 +115,11 @@ @pytest.fixture(scope='module') -def dw_methods(): - with DataWarehouse(VALID_XDMOD_URL) as dw: +def dw_methods(request): + xdmod_host = VALID_XDMOD_URL + if hasattr(request, 'param'): + xdmod_host = request.param + with DataWarehouse(xdmod_host) as dw: yield __get_dw_methods(dw) @@ -152,16 +163,7 @@ def test_KeyError(dw_methods, method, params, match): @pytest.mark.parametrize( 'method', - [ - 'get_data', - 'get_raw_data', - 'describe_realms', - 'describe_metrics', - 'describe_dimensions', - 'get_filter_values', - 'describe_raw_realms', - 'describe_raw_fields', - ], + list(METHOD_PARAMS.keys()), ) def test_RuntimeError_outside_context( dw_methods_outside_runtime_context, @@ -439,3 +441,12 @@ def test_case_insensitive(dw_methods, method, param, value1, value2): data1 = __run_method(dw_methods, method, {param: value1}) data2 = __run_method(dw_methods, method, {param: value2}) assert data1.equals(data2) + + +@pytest.mark.parametrize( + 'dw_methods,method', + [(VALID_XDMOD_URL + '/', method) for method in list(METHOD_PARAMS.keys())], + indirect=['dw_methods'], +) +def test_trailing_slashes(dw_methods, method): + __run_method(dw_methods, method) diff --git a/tests/unit/test_datawarehouse_unit.py b/tests/unit/test_datawarehouse_unit.py index 7af2d989..95b0bf95 100644 --- a/tests/unit/test_datawarehouse_unit.py +++ b/tests/unit/test_datawarehouse_unit.py @@ -10,7 +10,11 @@ @pytest.fixture(scope='module', autouse=True) def set_environ(): - token = os.environ['XDMOD_API_TOKEN'] if 'XDMOD_API_TOKEN' in os.environ else '' + token = ( + os.environ['XDMOD_API_TOKEN'] + if 'XDMOD_API_TOKEN' in os.environ + else '' + ) os.environ['XDMOD_API_TOKEN'] = INVALID_STR yield os.environ['XDMOD_API_TOKEN'] = token diff --git a/xdmod_data/_http_requester.py b/xdmod_data/_http_requester.py index d90e3af7..cc089003 100644 --- a/xdmod_data/_http_requester.py +++ b/xdmod_data/_http_requester.py @@ -1,5 +1,6 @@ import json import os +import re import requests from urllib.parse import urlencode import xdmod_data._validator as _validator @@ -10,6 +11,7 @@ class _HttpRequester: def __init__(self, xdmod_host): self.__in_runtime_context = False _validator._assert_str('xdmod_host', xdmod_host) + xdmod_host = re.sub('/+$', '', xdmod_host) self.__xdmod_host = xdmod_host try: self.__api_token = os.environ['XDMOD_API_TOKEN']