From 7fe5e246436f2ae92389f4492622bbfabe5f1995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katarina=20Vardi=C4=87?= <85176631+katarina-vardic-4ss@users.noreply.github.com> Date: Thu, 16 Feb 2023 13:40:47 +0100 Subject: [PATCH] maint: return native data types as received from the REST API --- fourinsight/campaigns/api.py | 139 ++++-------- tests/test_api.py | 394 +++++++++++++++++------------------ tests/test_campaign.py | 12 +- tests/test_client.py | 8 +- tests/testdata/get_data.py | 36 ++-- 5 files changed, 254 insertions(+), 335 deletions(-) diff --git a/fourinsight/campaigns/api.py b/fourinsight/campaigns/api.py index 3b60fc1..c1c2c92 100644 --- a/fourinsight/campaigns/api.py +++ b/fourinsight/campaigns/api.py @@ -1,5 +1,3 @@ -import pandas as pd - import fourinsight.campaigns as fc @@ -33,7 +31,6 @@ def _dict_rename(dict_org, dict_map): dict_new = {} for key_org, value_org in dict_org.items(): - label_mapper = dict(dict_map.keys()) try: key_new = label_mapper[key_org.lower()] @@ -58,86 +55,27 @@ def _dict_rename(dict_org, dict_map): return dict_new -class JSONSpecialParse: - def __init__(self, datetime_keys=(), location_keys=(), float_keys=(), int_keys=()): - self._datetime_keys = [key.lower() for key in datetime_keys] - self._location_keys = [key.lower() for key in location_keys] - - # Remove when endpoints start returning native values - self._float_keys = [key.lower() for key in float_keys] - self._int_keys = [key.lower() for key in int_keys] +def _loc_to_float(value): + """ + Attempt to cast "location" string to float. Also converts "null" to ``None``. + """ + try: + value = float(value) + except (TypeError, ValueError): + value = None if value == "null" else value + finally: + return value - def __call__(self, dct): - dct_update = { - key: pd.to_datetime(value) - for key, value in dct.items() - if key.lower() in self._datetime_keys - } - dct.update(dct_update) - - dct_update = {} - for key, value in dct.items(): - if key.lower() in self._location_keys: - try: - val1, val2 = value.split("#", 1) - except (AttributeError, ValueError): - dct_update[key] = value - else: - dct_update[key] = (self._float(val1), self._float(val2)) - dct.update(dct_update) - - # Remove when endpoints start returning native values - dct_update = { - key: None if value is None else float(value) - for key, value in dct.items() - if key.lower() in self._float_keys - } - dct.update(dct_update) - dct_update = { - key: None if value is None else int(value) - for key, value in dct.items() - if key.lower() in self._int_keys - } - dct.update(dct_update) - return dct +def _location_convert(value): + try: + val1, val2 = value.split("#", 1) + except (AttributeError, ValueError): + converted_value = value + else: + converted_value = (_loc_to_float(val1), _loc_to_float(val2)) - @staticmethod - def _float(value): - """ - Attempt to cast "location" string to float. Also converts "null" to ``None``. - """ - try: - value = float(value) - except (TypeError, ValueError): - value = None if value == "null" else value - finally: - return value - - -json_special_hook = JSONSpecialParse( - datetime_keys=( - "start", - "stop", - "startdate", - "enddate", - "stopdate", - "attached", - "detached", - "dashboardclosedate", - ), - location_keys=("location", "geolocation"), - float_keys=( - "distancefromwellhead", - "samplingrate", - "mass", - "submergedweight", - "height", - "addedmasscoefficient", - "alpha", - "waterdepth", - ), -) + return converted_value class CampaignsAPI: @@ -226,7 +164,13 @@ def get_campaigns(self): ("wdtimeseriesid", "Wd Timeseries ID"): None, } - response = self._get_payload(self._url(""), object_hook=json_special_hook) + response = self._get_payload(self._url("")) + + location_keys = ["location", "geolocation"] + for campaign_item in response: + for key in campaign_item.keys(): + if key.lower() in location_keys: + campaign_item[key] = _location_convert(campaign_item[key]) response_out = [ _dict_rename(campaign_item, response_map) for campaign_item in response @@ -266,9 +210,12 @@ def get_campaign(self, campaign_id): # change to v1.1 when available response = self._get_payload_legacy( self._url(f"/{campaign_id}", api_version="v1.0"), - object_hook=json_special_hook, ) + for key in response.keys(): + if key.lower() in "location": + response[key] = _location_convert(response[key]) + response_out = _dict_rename(response, response_map) return response_out @@ -295,8 +242,7 @@ def get_geotrack(self, campaign_id): # change to v1.1 when available response = self._get_payload_legacy( - self._url(f"/{campaign_id}", api_version="v1.0"), - object_hook=json_special_hook, + self._url(f"/{campaign_id}", api_version="v1.0") ) response_out = _dict_rename(response, response_map) @@ -323,9 +269,7 @@ def get_events(self, campaign_id): ("comment", "Comment"): None, } - response = self._get_payload( - self._url(f"/{campaign_id}/Events"), object_hook=json_special_hook - ) + response = self._get_payload(self._url(f"/{campaign_id}/Events")) response_out = [ _dict_rename(event_item, response_map) for event_item in response @@ -359,9 +303,7 @@ def _get_sensors(self, campaign_id): ("detached", "Detached Time"): None, } - response = self._get_payload( - self._url(f"/{campaign_id}/Sensors"), object_hook=json_special_hook - ) + response = self._get_payload(self._url(f"/{campaign_id}/Sensors")) response_out = [ _dict_rename(sensor_item, response_map) for sensor_item in response @@ -392,8 +334,7 @@ def _get_channels(self, campaign_id, sensor_id): } response = self._get_payload( - self._url(f"/{campaign_id}/Sensors/{sensor_id}/channels"), - object_hook=json_special_hook, + self._url(f"/{campaign_id}/Sensors/{sensor_id}/channels") ) response_out = [ @@ -449,8 +390,7 @@ def get_lowerstack(self, campaign_id): # change to v1.1 when available response = self._get_payload_legacy( - self._url(f"/{campaign_id}/LowerStack", api_version="v1.0"), - object_hook=json_special_hook, + self._url(f"/{campaign_id}/LowerStack", api_version="v1.0") ) response_out = _dict_rename(response, response_map) @@ -488,9 +428,7 @@ def get_swimops_campaign(self, campaign_id): ("servicesavailable", "Services Available"): None, } - response = self._get_payload( - self._url(f"/{campaign_id}/Swimops"), object_hook=json_special_hook - ) + response = self._get_payload(self._url(f"/{campaign_id}/Swimops")) response_out = _dict_rename(response[0], response_map) return response_out @@ -522,9 +460,7 @@ def get_swimops(self): ("servicesavailable", "Services Available"): None, } - response = self._get_payload( - self._url("/Swimops"), object_hook=json_special_hook - ) + response = self._get_payload(self._url("/Swimops")) response_out = [ _dict_rename(swim_ops_item, response_map) for swim_ops_item in response ] @@ -549,8 +485,7 @@ def get_campaign_type(self, campaign_id): } # change to v1.1 when available response = self._get_payload_legacy( - self._url(f"/{campaign_id}", api_version="v1.0"), - object_hook=json_special_hook, + self._url(f"/{campaign_id}", api_version="v1.0") ) response = _dict_rename(response, response_map) return response["CampaignType"].lower() diff --git a/tests/test_api.py b/tests/test_api.py index 7cbe911..5641831 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -5,7 +5,12 @@ import pytest import fourinsight.campaigns as fc -from fourinsight.campaigns.api import CampaignsAPI, JSONSpecialParse, _dict_rename +from fourinsight.campaigns.api import ( + CampaignsAPI, + _dict_rename, + _loc_to_float, + _location_convert, +) @pytest.fixture @@ -116,8 +121,8 @@ def test_get_campaigns(self, campaigns_api, auth_session, response, headers_expe "Water Depth": 0.0, "Location": (1.3, 2.4), "Main Data Provider": "string", - "Start Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), - "End Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), + "Start Date": "2021-08-12T11:38:16.509Z", + "End Date": "2021-08-12T11:38:16.509Z", "GeoTrack Position ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "GeoTrack Location": (3.2, 4.5), "GeoTrack Title": "string", @@ -139,8 +144,8 @@ def test_get_campaigns(self, campaigns_api, auth_session, response, headers_expe "Water Depth": 0.0, "Location": (1.3, 2.4), "Main Data Provider": "string", - "Start Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), - "End Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), + "Start Date": "2021-08-12T11:38:16.509Z", + "End Date": "2021-08-12T11:38:16.509Z", "GeoTrack Position ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "GeoTrack Location": (3.2, 4.5), "GeoTrack Title": "string", @@ -187,8 +192,8 @@ def test_get_campaigns_camelcase( "Water Depth": 0.0, "Location": (1.3, 2.4), "Main Data Provider": "string", - "Start Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), - "End Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), + "Start Date": "2021-08-12T11:38:16.509Z", + "End Date": "2021-08-12T11:38:16.509Z", "GeoTrack Position ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "GeoTrack Location": (3.2, 4.5), "GeoTrack Title": "string", @@ -210,8 +215,8 @@ def test_get_campaigns_camelcase( "Water Depth": 0.0, "Location": (1.3, 2.4), "Main Data Provider": "string", - "Start Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), - "End Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), + "Start Date": "2021-08-12T11:38:16.509Z", + "End Date": "2021-08-12T11:38:16.509Z", "GeoTrack Position ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "GeoTrack Location": (3.2, 4.5), "GeoTrack Title": "string", @@ -241,8 +246,8 @@ def test_get_campaign(self, campaigns_api, auth_session, response, headers_expec "Water Depth": 100.0, "Location": None, "Main Data Provider": "4Subsea", - "Start Date": pd.to_datetime("2017-04-08T00:00:00+00:00"), - "End Date": pd.to_datetime("2017-05-13T00:00:00+00:00"), + "Start Date": "2017-04-08T00:00:00+00:00", + "End Date": "2017-05-13T00:00:00+00:00", } assert expect == out @@ -271,8 +276,8 @@ def test_get_campaign_camelcase( "Water Depth": 100.0, "Location": None, "Main Data Provider": "4Subsea", - "Start Date": pd.to_datetime("2017-04-08T00:00:00+00:00"), - "End Date": pd.to_datetime("2017-05-13T00:00:00+00:00"), + "Start Date": "2017-04-08T00:00:00+00:00", + "End Date": "2017-05-13T00:00:00+00:00", } assert expect == out @@ -286,13 +291,13 @@ def test_get_events(self, campaigns_api, auth_session, response, headers_expect) response.json.assert_called() expect = [ { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), - "End": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", + "End": "2021-08-12T11:49:38.286Z", "Event Type": "string", "Comment": "string", }, { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", "End": None, "Event Type": "WLR connected", "Comment": None, @@ -304,7 +309,7 @@ def test_get_events(self, campaigns_api, auth_session, response, headers_expect) "Comment": None, }, { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", "End": None, "Event Type": "Connect-Disconnect", "Comment": None, @@ -328,13 +333,13 @@ def test_get_events_camelcase( response_camelcase.json.assert_called() expect = [ { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), - "End": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", + "End": "2021-08-12T11:49:38.286Z", "Event Type": "string", "Comment": "string", }, { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", "End": None, "Event Type": "WLR connected", "Comment": None, @@ -346,7 +351,7 @@ def test_get_events_camelcase( "Comment": None, }, { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", "End": None, "Event Type": "Connect-Disconnect", "Comment": None, @@ -379,8 +384,8 @@ def test_get_sensors(self, campaigns_api, auth_session, response, headers_expect "Direction Z Axis": "string", "Sampling Rate": 0.0, "Sensor Vendor": "string", - "Attached Time": pd.to_datetime("2021-08-12T11:51:19.667Z"), - "Detached Time": pd.to_datetime("2021-08-12T11:51:19.667Z"), + "Attached Time": "2021-08-12T11:51:19.667Z", + "Detached Time": "2021-08-12T11:51:19.667Z", "Channels": [ { "Channel": "string", @@ -444,8 +449,8 @@ def test_get_sensors_camelcase( "Direction Z Axis": "string", "Sampling Rate": 0.0, "Sensor Vendor": "string", - "Attached Time": pd.to_datetime("2021-08-12T11:51:19.667Z"), - "Detached Time": pd.to_datetime("2021-08-12T11:51:19.667Z"), + "Attached Time": "2021-08-12T11:51:19.667Z", + "Detached Time": "2021-08-12T11:51:19.667Z", "Channels": [ { "Channel": "string", @@ -496,8 +501,8 @@ def test__get_sensors(self, campaigns_api, auth_session, response, headers_expec "Direction Z Axis": "string", "Sampling Rate": 0.0, "Sensor Vendor": "string", - "Attached Time": pd.to_datetime("2021-08-12T11:51:19.667Z"), - "Detached Time": pd.to_datetime("2021-08-12T11:51:19.667Z"), + "Attached Time": "2021-08-12T11:51:19.667Z", + "Detached Time": "2021-08-12T11:51:19.667Z", }, { "SensorID": "", @@ -538,8 +543,8 @@ def test__get_sensors_camelcase( "Direction Z Axis": "string", "Sampling Rate": 0.0, "Sensor Vendor": "string", - "Attached Time": pd.to_datetime("2021-08-12T11:51:19.667Z"), - "Detached Time": pd.to_datetime("2021-08-12T11:51:19.667Z"), + "Attached Time": "2021-08-12T11:51:19.667Z", + "Detached Time": "2021-08-12T11:51:19.667Z", }, { "SensorID": "", @@ -666,7 +671,7 @@ def test_get_swimops_campaign( "SLA Level": "string", "Customer Contact": "string", "Comments": "string", - "Dashboard Close Date": pd.to_datetime("2021-08-12T11:56:23.069Z"), + "Dashboard Close Date": "2021-08-12T11:56:23.069Z", "SWIM Instance Status": "string", "Report Made": "string", "Report Sent": "string", @@ -699,7 +704,7 @@ def test_get_swimops_campaign_camelcase( "SLA Level": "string", "Customer Contact": "string", "Comments": "string", - "Dashboard Close Date": pd.to_datetime("2021-08-12T11:56:23.069Z"), + "Dashboard Close Date": "2021-08-12T11:56:23.069Z", "SWIM Instance Status": "string", "Report Made": "string", "Report Sent": "string", @@ -727,7 +732,7 @@ def test_get_swimops(self, campaigns_api, auth_session, response, headers_expect "SLA Level": "string", "Customer Contact": "string", "Comments": "string", - "Dashboard Close Date": pd.to_datetime("2021-08-12T11:54:42.513Z"), + "Dashboard Close Date": "2021-08-12T11:54:42.513Z", "SWIM Instance Status": "string", "Report Made": "string", "Report Sent": "string", @@ -762,7 +767,7 @@ def test_get_swimops_camelcase( "SLA Level": "string", "Customer Contact": "string", "Comments": "string", - "Dashboard Close Date": pd.to_datetime("2021-08-12T11:54:42.513Z"), + "Dashboard Close Date": "2021-08-12T11:54:42.513Z", "SWIM Instance Status": "string", "Report Made": "string", "Report Sent": "string", @@ -816,191 +821,170 @@ def test_rename(self): assert dict_expected == dict_out -class Test_JSONSpecialParse: - def test_datetime(self): - json_str = """{ - "a_datetime": "2020-01-01 00:01:00Z", - "b_other": "something", - "nested": [ - { - "nested_datetime_1": "2020-01-01 00:02:00Z", - "nested_datetime_2": "2020-01-01 00:03:00Z" - } - ] - }""" +def test_loc_to_float_string(): + value_in = "5.678900000" + value_out = _loc_to_float(value_in) + value_expected = 5.6789 - dict_expected = { - "a_datetime": pd.to_datetime("2020-01-01 00:01:00Z"), - "b_other": "something", - "nested": [ - { - "nested_datetime_1": pd.to_datetime("2020-01-01 00:02:00Z"), - "nested_datetime_2": pd.to_datetime("2020-01-01 00:03:00Z"), - } - ], - } - - json_special_hook = JSONSpecialParse( - datetime_keys=("a_datetime", "nested_datetime_1", "nested_datetime_2") - ) - - dict_out = json.loads(json_str, object_hook=json_special_hook) - assert dict_expected == dict_out - - def test_location(self): - json_str = """{ - "a_location": "1.23#4.56", - "b_other": "something", - "nested": [ - { - "nested_location_1": "7.89#10.11", - "nested_location_2": "12.13#14.15" - } - ] - }""" - - dict_expected = { - "a_location": (1.23, 4.56), - "b_other": "something", - "nested": [ - { - "nested_location_1": (7.89, 10.11), - "nested_location_2": (12.13, 14.15), - } - ], - } + assert value_out == value_expected - json_special_hook = JSONSpecialParse( - location_keys=("a_location", "nested_location_1", "nested_location_2") - ) - dict_out = json.loads(json_str, object_hook=json_special_hook) - assert dict_expected == dict_out +def test_loc_to_float_digits(): + value_in = 5.123456789000000 + value_out = _loc_to_float(value_in) + value_expected = 5.123456789 - def test_location_null(self): - json_str = """{ - "a_location": "null#null", - "b_other": "something", - "nested": [ - { - "nested_location_1": "null#10.11", - "nested_location_2": "12.13#null" - } - ] - }""" + assert value_out == value_expected - dict_expected = { - "a_location": (None, None), - "b_other": "something", - "nested": [ - { - "nested_location_1": (None, 10.11), - "nested_location_2": (12.13, None), - } - ], - } - json_special_hook = JSONSpecialParse( - location_keys=("a_location", "nested_location_1", "nested_location_2") - ) +def test_loc_to_float_null(): + value_in = "null" + value_out = _loc_to_float(value_in) + value_expected = None - dict_out = json.loads(json_str, object_hook=json_special_hook) - assert dict_expected == dict_out + assert value_out == value_expected - def test_location_invalid(self): - json_str = """{ - "a_location": "1.23::4.56", - "b_other": "something", - "nested": [ - { - "nested_location_1": "7.89#10.11a#eight", - "nested_location_2": "twelve#14.15" - } - ] - }""" - dict_expected = { - "a_location": "1.23::4.56", - "b_other": "something", - "nested": [ - { - "nested_location_1": (7.89, "10.11a#eight"), - "nested_location_2": ("twelve", 14.15), - } - ], - } - - json_special_hook = JSONSpecialParse( - location_keys=("a_location", "nested_location_1", "nested_location_2") - ) - - dict_out = json.loads(json_str, object_hook=json_special_hook) - assert dict_expected == dict_out - - def test_numbers(self): - """Deprecate when REST API endpoint starts returning native values""" - json_str = """{ - "a_float": "1.23", - "b_other": "something", - "nested": [ - { - "nested_float_1": "7.89", - "nested_int_2": "12" - } - ] - }""" - - dict_expected = { - "a_float": 1.23, - "b_other": "something", - "nested": [{"nested_float_1": 7.89, "nested_int_2": 12}], - } - - json_special_hook = JSONSpecialParse( - float_keys=("a_float", "nested_float_1"), int_keys=("nested_int_2",) - ) - - dict_out = json.loads(json_str, object_hook=json_special_hook) - assert dict_expected == dict_out +def test_location_convert(): + dict_in = { + "CampaignID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Name": "string", + "Type": "string", + "Client": "string", + "PO Number": "string", + "Project Number": "string", + "Vessel": "string", + "Vessel Contractor": "string", + "Well Name": "string", + "Well ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Water Depth": 0.0, + "Location": "1.3#2.4", + } - def test_mixed(self): - json_str = """{ - "a_location": "1.23#4.56", - "b_other": "something", - "nested": [ - { - "nested_location_1": "7.89#10.11", - "nested_datetime_2": "2020-01-01 04:00:12Z" - } - ] - }""" + dict_expect = { + "CampaignID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Name": "string", + "Type": "string", + "Client": "string", + "PO Number": "string", + "Project Number": "string", + "Vessel": "string", + "Vessel Contractor": "string", + "Well Name": "string", + "Well ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Water Depth": 0.0, + "Location": (1.3, 2.4), + } - dict_expected = { - "a_location": (1.23, 4.56), - "b_other": "something", - "nested": [ - { - "nested_location_1": (7.89, 10.11), - "nested_datetime_2": pd.to_datetime("2020-01-01 04:00:12Z"), - } - ], - } + dict_in["Location"] = _location_convert(dict_in["Location"]) + assert dict_in == dict_expect + + +def test_location_convert_none(): + dict_in = { + "CampaignID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Name": "string", + "Type": "string", + "Client": "string", + "PO Number": "string", + "Project Number": "string", + "Vessel": "string", + "Vessel Contractor": "string", + "Well Name": "string", + "Well ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Water Depth": 0.0, + "Location": None, + "Main Data Provider": "string", + "Start Date": "2021-08-12T11:38:16.509Z", + "End Date": "2021-08-12T11:38:16.509Z", + "GeoTrack Position ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "GeoTrack Location": "3.2#4.5", + "GeoTrack Title": "string", + "Hs Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Tp Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Wd Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + } - json_special_hook = JSONSpecialParse( - location_keys=("a_location", "nested_location_1"), - datetime_keys=("nested_datetime_2",), - ) + dict_expect = { + "CampaignID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Name": "string", + "Type": "string", + "Client": "string", + "PO Number": "string", + "Project Number": "string", + "Vessel": "string", + "Vessel Contractor": "string", + "Well Name": "string", + "Well ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Water Depth": 0.0, + "Location": None, + "Main Data Provider": "string", + "Start Date": "2021-08-12T11:38:16.509Z", + "End Date": "2021-08-12T11:38:16.509Z", + "GeoTrack Position ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "GeoTrack Location": (3.2, 4.5), + "GeoTrack Title": "string", + "Hs Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Tp Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Wd Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + } - dict_out = json.loads(json_str, object_hook=json_special_hook) - assert dict_expected == dict_out + dict_in["Location"] = _location_convert(dict_in["Location"]) + dict_in["GeoTrack Location"] = _location_convert(dict_in["GeoTrack Location"]) + + assert dict_in == dict_expect + + +def test_location_convert_null(): + dict_in = { + "CampaignID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Name": "string", + "Type": "string", + "Client": "string", + "PO Number": "string", + "Project Number": "string", + "Vessel": "string", + "Vessel Contractor": "string", + "Well Name": "string", + "Well ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Water Depth": 0.0, + "Location": "null#10.11", + "Main Data Provider": "string", + "Start Date": "2021-08-12T11:38:16.509Z", + "End Date": "2021-08-12T11:38:16.509Z", + "GeoTrack Position ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "GeoTrack Location": "12.13#null", + "GeoTrack Title": "string", + "Hs Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Tp Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Wd Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + } - def test__float_valid(self): - assert JSONSpecialParse._float(12.0) == 12.0 - assert JSONSpecialParse._float(12) == 12.0 - assert JSONSpecialParse._float("12") == 12.0 + dict_expect = { + "CampaignID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Name": "string", + "Type": "string", + "Client": "string", + "PO Number": "string", + "Project Number": "string", + "Vessel": "string", + "Vessel Contractor": "string", + "Well Name": "string", + "Well ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Water Depth": 0.0, + "Location": (None, 10.11), + "Main Data Provider": "string", + "Start Date": "2021-08-12T11:38:16.509Z", + "End Date": "2021-08-12T11:38:16.509Z", + "GeoTrack Position ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "GeoTrack Location": (12.13, None), + "GeoTrack Title": "string", + "Hs Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Tp Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + "Wd Timeseries ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", + } - def test__float_null(self): - assert JSONSpecialParse._float("null") is None + dict_in["Location"] = _location_convert(dict_in["Location"]) + dict_in["GeoTrack Location"] = _location_convert(dict_in["GeoTrack Location"]) - def test__float_invalid(self): - assert JSONSpecialParse._float("12a") == "12a" + assert dict_in == dict_expect diff --git a/tests/test_campaign.py b/tests/test_campaign.py index 898d1d3..1f50013 100644 --- a/tests/test_campaign.py +++ b/tests/test_campaign.py @@ -57,20 +57,20 @@ def test_events_value_none(self, generic_campaign): events_out = generic_campaign.events() events_expected = [ { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), - "End": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", + "End": "2021-08-12T11:49:38.286Z", "Event Type": "string", "Comment": "string", }, { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", "End": None, "Event Type": "WLR connected", "Comment": None, }, {"Start": None, "End": None, "Event Type": "Artifact", "Comment": None}, { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", "End": None, "Event Type": "Connect-Disconnect", "Comment": None, @@ -84,7 +84,7 @@ def test_events_value_connect_disconnect(self, generic_campaign): ) events_expected = [ { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", "End": None, "Event Type": "Connect-Disconnect", "Comment": None, @@ -103,7 +103,7 @@ def test_events_value_wlr_connected(self, generic_campaign): events_out = generic_campaign.events(value="WLR connected", by="Event Type") events_expected = [ { - "Start": pd.to_datetime("2021-08-12T11:49:38.286Z"), + "Start": "2021-08-12T11:49:38.286Z", "End": None, "Event Type": "WLR connected", "Comment": None, diff --git a/tests/test_client.py b/tests/test_client.py index 6d2f036..cfdddb2 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -35,8 +35,8 @@ def test_overview(self, camp_client, auth_session): "Water Depth": 0.0, "Location": (1.3, 2.4), "Main Data Provider": "string", - "Start Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), - "End Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), + "Start Date": "2021-08-12T11:38:16.509Z", + "End Date": "2021-08-12T11:38:16.509Z", "GeoTrack Position ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "GeoTrack Location": (3.2, 4.5), "GeoTrack Title": "string", @@ -58,8 +58,8 @@ def test_overview(self, camp_client, auth_session): "Water Depth": 0.0, "Location": (1.3, 2.4), "Main Data Provider": "string", - "Start Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), - "End Date": pd.to_datetime("2021-08-12T11:38:16.509Z"), + "Start Date": "2021-08-12T11:38:16.509Z", + "End Date": "2021-08-12T11:38:16.509Z", "GeoTrack Position ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "GeoTrack Location": (3.2, 4.5), "GeoTrack Title": "string", diff --git a/tests/testdata/get_data.py b/tests/testdata/get_data.py index ef26a8f..a05e827 100644 --- a/tests/testdata/get_data.py +++ b/tests/testdata/get_data.py @@ -14,7 +14,7 @@ "vesselContractor": "string", "wellName": "string", "wellId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", - "waterDepth": 0, + "waterDepth": 0.0, "location": "1.3#2.4", "mainDataProvider": "string", "startDate": "2021-08-12T11:38:16.509Z", @@ -46,7 +46,7 @@ "VesselContractor": "string", "WellName": "string", "WellId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", - "WaterDepth": 0, + "WaterDepth": 0.0, "Location": "1.3#2.4", "MainDataProvider": "string", "StartDate": "2021-08-12T11:38:16.509Z", @@ -78,7 +78,7 @@ "vesselContractor": "string", "wellName": "string", "wellId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", - "waterDepth": 0, + "waterDepth": 0.0, "location": "1.3#2.4", "mainDataProvider": "string", "startDate": "2021-08-12T11:38:16.509Z", @@ -110,7 +110,7 @@ "VesselContractor": "string", "WellName": "string", "WellId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", - "WaterDepth": 0, + "WaterDepth": 0.0, "Location": "1.3#2.4", "MainDataProvider": "string", "StartDate": "2021-08-12T11:38:16.509Z", @@ -339,16 +339,16 @@ LOWERSTACK_DATA_CAMELCASE = { "campaignId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "anyFetchFailure": False, - "alpha": "0.1", + "alpha": 0.1, "elements": [ { "campaignId": "string", "name": "string", "elementId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", - "mass": "100.0", - "submergedWeight": "1000.0", - "height": "10.0", - "addedMassCoefficient": "2.0", + "mass": 100.0, + "submergedWeight": 1000.0, + "height": 10.0, + "addedMassCoefficient": 2.0, } ], } @@ -356,16 +356,16 @@ LOWERSTACK_DATA = { "CampaignId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "AnyFetchFailure": False, - "Alpha": "0.1", + "Alpha": 0.1, "Elements": [ { "CampaignId": "string", "Name": "string", "ElementId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", - "Mass": "100.0", - "SubmergedWeight": "1000.0", - "Height": "10.0", - "AddedMassCoefficient": "2.0", + "Mass": 100.0, + "SubmergedWeight": 1000.0, + "Height": 10.0, + "AddedMassCoefficient": 2.0, } ], } @@ -496,7 +496,7 @@ "wellName": "string", "wellId": "2f9356d1-e32c-4916-8f80-bbf8dfaef1e8", "fieldTitle": "Janice", - "waterDepth": "100.0", + "waterDepth": 100.0, "location": None, "mainDataProvider": "4Subsea", "startDate": "2017-04-08T00:00:00+00:00", @@ -524,7 +524,7 @@ "WellName": "string", "WellId": "2f9356d1-e32c-4916-8f80-bbf8dfaef1e8", "FieldTitle": "Janice", - "WaterDepth": "100.0", + "WaterDepth": 100.0, "Location": None, "MainDataProvider": "4Subsea", "StartDate": "2017-04-08T00:00:00+00:00", @@ -552,7 +552,7 @@ "wellName": "string", "wellId": "2f9356d1-e32c-4916-8f80-bbf8dfaef1e8", "fieldTitle": "Janice", - "waterDepth": "100.0", + "waterDepth": 100.0, "location": None, "mainDataProvider": "4Subsea", "startDate": "2017-04-08T00:00:00+00:00", @@ -580,7 +580,7 @@ "WellName": "string", "WellId": "2f9356d1-e32c-4916-8f80-bbf8dfaef1e8", "FieldTitle": "Janice", - "WaterDepth": "100.0", + "WaterDepth": 100.0, "Location": None, "MainDataProvider": "4Subsea", "StartDate": "2017-04-08T00:00:00+00:00",