From 259797a76850dbea73ee71ee80c9e67d767ded2d Mon Sep 17 00:00:00 2001 From: akash5100 Date: Thu, 8 Sep 2022 15:31:27 +0530 Subject: [PATCH 1/4] added validator for source_id and id --- hvpy/api_groups/jpeg2000/get_jp2_header.py | 9 +++++++-- hvpy/api_groups/jpeg2000/get_jp2_image.py | 7 +++++-- .../jpeg2000/get_jpx_closest_to_mid_point.py | 8 +++++--- hvpy/facade.py | 19 ++++++++++--------- hvpy/utils.py | 19 +++++++++++++++++++ 5 files changed, 46 insertions(+), 16 deletions(-) diff --git a/hvpy/api_groups/jpeg2000/get_jp2_header.py b/hvpy/api_groups/jpeg2000/get_jp2_header.py index e14c227..7a9fc1b 100644 --- a/hvpy/api_groups/jpeg2000/get_jp2_header.py +++ b/hvpy/api_groups/jpeg2000/get_jp2_header.py @@ -1,6 +1,10 @@ -from typing import Optional +from typing import Union, Optional +from pydantic import validator + +from hvpy.datasource import DataSource from hvpy.io import HvpyParameters, OutputType +from hvpy.utils import _data_source_to_int class getJP2HeaderInputParameters(HvpyParameters): @@ -22,8 +26,9 @@ class getJP2HeaderInputParameters(HvpyParameters): {Shared} """ - id: int + id: Union[int, DataSource] callback: Optional[str] = None + _id_validator = validator("id", allow_reuse=True)(_data_source_to_int) def get_output_type(self) -> OutputType: """ diff --git a/hvpy/api_groups/jpeg2000/get_jp2_image.py b/hvpy/api_groups/jpeg2000/get_jp2_image.py index c9b1548..196b1ce 100644 --- a/hvpy/api_groups/jpeg2000/get_jp2_image.py +++ b/hvpy/api_groups/jpeg2000/get_jp2_image.py @@ -1,9 +1,11 @@ +from typing import Union from datetime import datetime from pydantic import Field, validator +from hvpy.datasource import DataSource from hvpy.io import HvpyParameters, OutputType -from hvpy.utils import convert_date_to_isoformat +from hvpy.utils import _data_source_to_int, convert_date_to_isoformat class getJP2ImageInputParameters(HvpyParameters): @@ -31,10 +33,11 @@ class getJP2ImageInputParameters(HvpyParameters): """ date: datetime - sourceId: int + sourceId: Union[int, DataSource] jpip: bool = False Json: bool = Field(False, alias="json") _date_validator = validator("date", allow_reuse=True)(convert_date_to_isoformat) + _source_id_validator = validator("sourceId", allow_reuse=True)(_data_source_to_int) def get_output_type(self) -> OutputType: """ diff --git a/hvpy/api_groups/jpeg2000/get_jpx_closest_to_mid_point.py b/hvpy/api_groups/jpeg2000/get_jpx_closest_to_mid_point.py index 213799c..a2fc936 100644 --- a/hvpy/api_groups/jpeg2000/get_jpx_closest_to_mid_point.py +++ b/hvpy/api_groups/jpeg2000/get_jpx_closest_to_mid_point.py @@ -1,10 +1,11 @@ -from typing import List +from typing import List, Union from datetime import datetime from pydantic import validator +from hvpy.datasource import DataSource from hvpy.io import HvpyParameters, OutputType -from hvpy.utils import convert_date_to_unix +from hvpy.utils import _data_source_to_int, convert_date_to_unix class getJPXClosestToMidPointInputParameters(HvpyParameters): @@ -38,11 +39,12 @@ class getJPXClosestToMidPointInputParameters(HvpyParameters): startTimes: List[datetime] endTimes: List[datetime] - sourceId: int + sourceId: Union[int, DataSource] linked: bool = True verbose: bool = False jpip: bool = False _date_validator = validator("startTimes", "endTimes", allow_reuse=True)(convert_date_to_unix) + _source_id_validator = validator("sourceId", allow_reuse=True)(_data_source_to_int) def get_output_type(self) -> OutputType: """ diff --git a/hvpy/facade.py b/hvpy/facade.py index 781aa26..df3986d 100644 --- a/hvpy/facade.py +++ b/hvpy/facade.py @@ -2,6 +2,7 @@ from datetime import datetime from hvpy.core import execute_api_call +from hvpy.datasource import DataSource from hvpy.parameters import * from hvpy.utils import _add_shared_docstring @@ -28,7 +29,7 @@ @_add_shared_docstring(getJP2ImageInputParameters) def getJP2Image( date: datetime, - sourceId: int, + sourceId: Union[int, DataSource], jpip: bool = False, json: bool = False, ) -> Union[bytes, str, Dict[str, Any]]: @@ -51,7 +52,7 @@ def getJP2Image( @_add_shared_docstring(getJP2HeaderInputParameters) def getJP2Header( - id: int, + id: Union[int, DataSource], callback: Optional[str] = None, ) -> Union[bytes, str, Dict[str, Any]]: """ @@ -75,7 +76,7 @@ def getJP2Header( def getJPXClosestToMidPoint( startTimes: List[datetime], endTimes: List[datetime], - sourceId: int, + sourceId: Union[int, DataSource], linked: bool = True, verbose: bool = False, jpip: bool = False, @@ -115,7 +116,7 @@ def getJPXClosestToMidPoint( def getJPX( startTime: List[datetime], endTime: List[datetime], - sourceId: int, + sourceId: Union[int, DataSource], linked: bool = True, verbose: bool = False, jpip: bool = False, @@ -175,7 +176,7 @@ def getStatus() -> Union[bytes, str, Dict[str, Any]]: @_add_shared_docstring(getClosestImageInputParameters) def getClosestImage( date: datetime, - sourceId: int, + sourceId: Union[int, DataSource], callback: Optional[str] = None, ) -> Union[bytes, str, Dict[str, Any]]: """ @@ -400,7 +401,7 @@ def queueMovie( @_add_shared_docstring(reQueueMovieInputParameters) def reQueueMovie( - id: str, + id: Union[int, DataSource], force: Optional[bool] = False, ) -> Union[bytes, str, Dict[str, Any]]: """ @@ -424,7 +425,7 @@ def reQueueMovie( @_add_shared_docstring(getMovieStatusInputParameters) def getMovieStatus( - id: str, + id: Union[int, DataSource], format: str, verbose: Optional[bool] = False, callback: Optional[str] = None, @@ -454,7 +455,7 @@ def getMovieStatus( @_add_shared_docstring(downloadMovieInputParameters) def downloadMovie( - id: str, + id: Union[int, DataSource], format: str, hq: bool = False, ) -> Union[bytes, str, Dict[str, Any]]: @@ -527,7 +528,7 @@ def shortenURL( @_add_shared_docstring(getTileInputParameters) def getTile( - id: int, + id: Union[int, DataSource], x: int, y: int, imageScale: int, diff --git a/hvpy/utils.py b/hvpy/utils.py index 2d4d031..47451b2 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -41,6 +41,25 @@ def convert_date_to_unix(v: list) -> str: return ",".join([str(int(datetime.timestamp(d))) for d in v]) +def _data_source_to_int(source: Union[int, DataSource]) -> int: + """ + Converts a `~hvpy.DataSource` to an integer. + + Parameters + ---------- + source + The `~hvpy.DataSource` to convert. + + Returns + ------- + int + The integer representation of the `~hvpy.DataSource`. + """ + if isinstance(source, DataSource): + return source.value + return source + + def _to_datasource(val: Union[int, DataSource]) -> DataSource: """ Validates the input and converts it to a DataSource enum. From 14bbb576a2a67f34d28b2598444a50d94731969e Mon Sep 17 00:00:00 2001 From: akash5100 Date: Thu, 8 Sep 2022 18:23:44 +0530 Subject: [PATCH 2/4] validator for source id --- hvpy/api_groups/jpeg2000/get_jp2_header.py | 9 +--- hvpy/api_groups/jpeg2000/get_jpx.py | 8 +-- .../jpeg2000/tests/test_get_jp2_image.py | 8 +++ .../api_groups/jpeg2000/tests/test_get_jpx.py | 54 +++++++++++++++++++ .../test_get_jpx_closest_to_mid_point.py | 30 +++++++++++ .../official_clients/get_closest_image.py | 8 +-- .../tests/test_get_closest_image.py | 12 +++++ hvpy/utils.py | 5 +- 8 files changed, 120 insertions(+), 14 deletions(-) diff --git a/hvpy/api_groups/jpeg2000/get_jp2_header.py b/hvpy/api_groups/jpeg2000/get_jp2_header.py index 7a9fc1b..e14c227 100644 --- a/hvpy/api_groups/jpeg2000/get_jp2_header.py +++ b/hvpy/api_groups/jpeg2000/get_jp2_header.py @@ -1,10 +1,6 @@ -from typing import Union, Optional +from typing import Optional -from pydantic import validator - -from hvpy.datasource import DataSource from hvpy.io import HvpyParameters, OutputType -from hvpy.utils import _data_source_to_int class getJP2HeaderInputParameters(HvpyParameters): @@ -26,9 +22,8 @@ class getJP2HeaderInputParameters(HvpyParameters): {Shared} """ - id: Union[int, DataSource] + id: int callback: Optional[str] = None - _id_validator = validator("id", allow_reuse=True)(_data_source_to_int) def get_output_type(self) -> OutputType: """ diff --git a/hvpy/api_groups/jpeg2000/get_jpx.py b/hvpy/api_groups/jpeg2000/get_jpx.py index 7613b12..63b0ecc 100644 --- a/hvpy/api_groups/jpeg2000/get_jpx.py +++ b/hvpy/api_groups/jpeg2000/get_jpx.py @@ -1,10 +1,11 @@ -from typing import Optional +from typing import Union, Optional from datetime import datetime from pydantic import validator +from hvpy.datasource import DataSource from hvpy.io import HvpyParameters, OutputType -from hvpy.utils import convert_date_to_isoformat +from hvpy.utils import _data_source_to_int, convert_date_to_isoformat class getJPXInputParameters(HvpyParameters): @@ -43,12 +44,13 @@ class getJPXInputParameters(HvpyParameters): startTime: datetime endTime: datetime - sourceId: int + sourceId: Union[int, DataSource] linked: bool = True verbose: bool = False jpip: bool = False cadence: Optional[int] = None _date_validator = validator("startTime", "endTime", allow_reuse=True)(convert_date_to_isoformat) + _source_id_validator = validator("sourceId", allow_reuse=True)(_data_source_to_int) def get_output_type(self) -> OutputType: """ diff --git a/hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py b/hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py index 855f800..f81ef8b 100644 --- a/hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py +++ b/hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py @@ -2,6 +2,7 @@ from hvpy import getJP2Image from hvpy.api_groups.jpeg2000.get_jp2_image import getJP2ImageInputParameters +from hvpy.datasource import DataSource def test_str_response(date): @@ -41,3 +42,10 @@ def test_url_property(date): params = {"date": date, "sourceId": 14, "jpip": True, "json": True} params = getJP2ImageInputParameters(**params) assert params.url == "https://api.beta.helioviewer.org/v2/getJP2Image/" + + +def test_getJP2Image_with_datasource_enum(date): + response = getJP2Image(date=date, sourceId=DataSource.COR1_A, jpip=True, json=True) + assert isinstance(response, dict) + assert "uri" in response + assert response["uri"].startswith("jpip://") diff --git a/hvpy/api_groups/jpeg2000/tests/test_get_jpx.py b/hvpy/api_groups/jpeg2000/tests/test_get_jpx.py index 80fe3d5..54871b9 100644 --- a/hvpy/api_groups/jpeg2000/tests/test_get_jpx.py +++ b/hvpy/api_groups/jpeg2000/tests/test_get_jpx.py @@ -2,6 +2,7 @@ from hvpy import getJPX from hvpy.api_groups.jpeg2000.get_jpx import getJPXInputParameters +from hvpy.datasource import DataSource def test_raw_response(start_time, end_time): @@ -57,6 +58,59 @@ def test_json_response(start_time, end_time): assert response["uri"].startswith("https://") +def test_raw_response_with_datasource_enum(start_time, end_time): + response = getJPX( + startTime=start_time, + endTime=end_time, + sourceId=DataSource.COR1_A, + linked=False, + verbose=False, + jpip=False, + cadence=60, + ) + assert isinstance(response, bytes) + + +def test_str_response_with_datasource_enum(start_time, end_time): + response = getJPX( + startTime=start_time, + endTime=end_time, + sourceId=DataSource.COR1_A, + linked=False, + verbose=False, + jpip=True, + cadence=60, + ) + assert isinstance(response, str) + assert response.startswith("jpip://") + + +def test_json_response_with_datasource_enum(start_time, end_time): + response = getJPX( + startTime=start_time, + endTime=end_time, + sourceId=DataSource.COR1_A, + linked=False, + verbose=True, + jpip=True, + cadence=60, + ) + assert isinstance(response, dict) + assert response["uri"].startswith("jpip://") + + response = getJPX( + startTime=start_time, + endTime=end_time, + sourceId=DataSource.COR1_A, + linked=False, + verbose=True, + jpip=False, + cadence=60, + ) + assert isinstance(response, dict) + assert response["uri"].startswith("https://") + + def test_error_handling(start_time, end_time): with pytest.raises(TypeError, match="missing 1 required positional argument: 'startTime'"): getJPX(endTime=end_time, sourceId=14) diff --git a/hvpy/api_groups/jpeg2000/tests/test_get_jpx_closest_to_mid_point.py b/hvpy/api_groups/jpeg2000/tests/test_get_jpx_closest_to_mid_point.py index a97224b..cecdb0e 100644 --- a/hvpy/api_groups/jpeg2000/tests/test_get_jpx_closest_to_mid_point.py +++ b/hvpy/api_groups/jpeg2000/tests/test_get_jpx_closest_to_mid_point.py @@ -2,6 +2,7 @@ from hvpy import getJPXClosestToMidPoint from hvpy.api_groups.jpeg2000.get_jpx_closest_to_mid_point import getJPXClosestToMidPointInputParameters +from hvpy.datasource import DataSource def test_raw_response(start_times, end_times): @@ -45,3 +46,32 @@ def test_error_handling(start_times, end_times): def test_url_property(start_times, end_times): params = getJPXClosestToMidPointInputParameters(startTimes=start_times, endTimes=end_times, sourceId=14) assert params.url == "https://api.beta.helioviewer.org/v2/getJPXClosestToMidPoint/" + + +def test_raw_response_with_datasource(start_times, end_times): + response = getJPXClosestToMidPoint( + startTimes=start_times, endTimes=end_times, sourceId=DataSource.AIA_131, linked=False, verbose=False, jpip=False + ) + assert isinstance(response, bytes) + + +def test_str_response_with_datasource(start_times, end_times): + response = getJPXClosestToMidPoint( + startTimes=start_times, endTimes=end_times, sourceId=DataSource.AIA_131, linked=False, verbose=False, jpip=True + ) + assert isinstance(response, str) + assert response.startswith("jpip://") + + +def test_json_response_with_datasource(start_times, end_times): + response = getJPXClosestToMidPoint( + startTimes=start_times, endTimes=end_times, sourceId=DataSource.AIA_131, linked=False, verbose=True, jpip=True + ) + assert isinstance(response, dict) + assert response["uri"].startswith("jpip://") + + response = getJPXClosestToMidPoint( + startTimes=start_times, endTimes=end_times, sourceId=DataSource.AIA_131, linked=False, verbose=True, jpip=False + ) + assert isinstance(response, dict) + assert response["uri"].startswith("https://") diff --git a/hvpy/api_groups/official_clients/get_closest_image.py b/hvpy/api_groups/official_clients/get_closest_image.py index 17049eb..c4f380b 100644 --- a/hvpy/api_groups/official_clients/get_closest_image.py +++ b/hvpy/api_groups/official_clients/get_closest_image.py @@ -1,10 +1,11 @@ -from typing import Optional +from typing import Union, Optional from datetime import datetime from pydantic import validator +from hvpy.datasource import DataSource from hvpy.io import HvpyParameters, OutputType -from hvpy.utils import convert_date_to_isoformat +from hvpy.utils import _data_source_to_int, convert_date_to_isoformat class getClosestImageInputParameters(HvpyParameters): @@ -29,9 +30,10 @@ class getClosestImageInputParameters(HvpyParameters): """ date: datetime - sourceId: int + sourceId: Union[int, DataSource] callback: Optional[str] = None _date_validator = validator("date", allow_reuse=True)(convert_date_to_isoformat) + _source_id_validator = validator("sourceId", allow_reuse=True)(_data_source_to_int) def get_output_type(self) -> OutputType: """ diff --git a/hvpy/api_groups/official_clients/tests/test_get_closest_image.py b/hvpy/api_groups/official_clients/tests/test_get_closest_image.py index 5f659e6..6e73e7e 100644 --- a/hvpy/api_groups/official_clients/tests/test_get_closest_image.py +++ b/hvpy/api_groups/official_clients/tests/test_get_closest_image.py @@ -2,6 +2,7 @@ from hvpy import getClosestImage from hvpy.api_groups.official_clients.get_closest_image import getClosestImageInputParameters +from hvpy.datasource import DataSource def test_json_res(date): @@ -15,6 +16,17 @@ def test_str_res(date): assert response.startswith("callback") +def test_json_res_with_datasource_enum(date): + response = getClosestImage(date=date, sourceId=DataSource.COR1_A) + assert isinstance(response, dict) + + +def test_str_res_with_datasource_enum(date): + response = getClosestImage(date=date, sourceId=DataSource.COR1_A, callback="callback") + assert isinstance(response, str) + assert response.startswith("callback") + + def test_error_handling(date): with pytest.raises(TypeError, match="missing 1 required positional argument: 'date'"): getClosestImage(sourceId=14) diff --git a/hvpy/utils.py b/hvpy/utils.py index 47451b2..da201aa 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -57,7 +57,10 @@ def _data_source_to_int(source: Union[int, DataSource]) -> int: """ if isinstance(source, DataSource): return source.value - return source + elif isinstance(source, int) and source in [x.value for x in DataSource]: + return source + else: + raise ValueError(f"{source} is not a valid DataSource.") def _to_datasource(val: Union[int, DataSource]) -> DataSource: From 588b185140345bca8a428922a216e79c534b5691 Mon Sep 17 00:00:00 2001 From: akash5100 Date: Thu, 8 Sep 2022 18:28:13 +0530 Subject: [PATCH 3/4] fix typehint error --- hvpy/facade.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hvpy/facade.py b/hvpy/facade.py index df3986d..b10c6e0 100644 --- a/hvpy/facade.py +++ b/hvpy/facade.py @@ -52,7 +52,7 @@ def getJP2Image( @_add_shared_docstring(getJP2HeaderInputParameters) def getJP2Header( - id: Union[int, DataSource], + id: int, callback: Optional[str] = None, ) -> Union[bytes, str, Dict[str, Any]]: """ From 6f2646d6290b2870c87856f6154281c66f9ce266 Mon Sep 17 00:00:00 2001 From: akash5100 Date: Thu, 8 Sep 2022 20:36:57 +0530 Subject: [PATCH 4/4] fix the failing test --- .../jpeg2000/tests/test_get_jp2_image.py | 13 +---- .../api_groups/jpeg2000/tests/test_get_jpx.py | 57 +------------------ .../test_get_jpx_closest_to_mid_point.py | 57 ++++++++----------- .../tests/test_get_closest_image.py | 13 +---- hvpy/utils.py | 12 +--- 5 files changed, 31 insertions(+), 121 deletions(-) diff --git a/hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py b/hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py index f81ef8b..ba53e64 100644 --- a/hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py +++ b/hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py @@ -6,7 +6,7 @@ def test_str_response(date): - response = getJP2Image(date=date, sourceId=14, jpip=True, json=False) + response = getJP2Image(date=date, sourceId=DataSource.AIA_335, jpip=True, json=False) assert isinstance(response, str) assert response.startswith("jpip://") @@ -19,7 +19,7 @@ def test_json_response(date): def test_raw_response(date): - response = getJP2Image(date=date, sourceId=14, jpip=False, json=False) + response = getJP2Image(date=date, sourceId=DataSource.AIA_335, jpip=False, json=False) assert isinstance(response, bytes) @@ -29,7 +29,7 @@ def test_raw_response_with_json(date): def test_default_response(date): - response = getJP2Image(date=date, sourceId=14) + response = getJP2Image(date=date, sourceId=DataSource.AIA_335) assert isinstance(response, bytes) @@ -42,10 +42,3 @@ def test_url_property(date): params = {"date": date, "sourceId": 14, "jpip": True, "json": True} params = getJP2ImageInputParameters(**params) assert params.url == "https://api.beta.helioviewer.org/v2/getJP2Image/" - - -def test_getJP2Image_with_datasource_enum(date): - response = getJP2Image(date=date, sourceId=DataSource.COR1_A, jpip=True, json=True) - assert isinstance(response, dict) - assert "uri" in response - assert response["uri"].startswith("jpip://") diff --git a/hvpy/api_groups/jpeg2000/tests/test_get_jpx.py b/hvpy/api_groups/jpeg2000/tests/test_get_jpx.py index 54871b9..4fa3c96 100644 --- a/hvpy/api_groups/jpeg2000/tests/test_get_jpx.py +++ b/hvpy/api_groups/jpeg2000/tests/test_get_jpx.py @@ -9,7 +9,7 @@ def test_raw_response(start_time, end_time): response = getJPX( startTime=start_time, endTime=end_time, - sourceId=14, + sourceId=DataSource.AIA_335, linked=False, verbose=False, jpip=False, @@ -22,7 +22,7 @@ def test_str_response(start_time, end_time): response = getJPX( startTime=start_time, endTime=end_time, - sourceId=14, + sourceId=DataSource.AIA_335, linked=False, verbose=False, jpip=True, @@ -58,59 +58,6 @@ def test_json_response(start_time, end_time): assert response["uri"].startswith("https://") -def test_raw_response_with_datasource_enum(start_time, end_time): - response = getJPX( - startTime=start_time, - endTime=end_time, - sourceId=DataSource.COR1_A, - linked=False, - verbose=False, - jpip=False, - cadence=60, - ) - assert isinstance(response, bytes) - - -def test_str_response_with_datasource_enum(start_time, end_time): - response = getJPX( - startTime=start_time, - endTime=end_time, - sourceId=DataSource.COR1_A, - linked=False, - verbose=False, - jpip=True, - cadence=60, - ) - assert isinstance(response, str) - assert response.startswith("jpip://") - - -def test_json_response_with_datasource_enum(start_time, end_time): - response = getJPX( - startTime=start_time, - endTime=end_time, - sourceId=DataSource.COR1_A, - linked=False, - verbose=True, - jpip=True, - cadence=60, - ) - assert isinstance(response, dict) - assert response["uri"].startswith("jpip://") - - response = getJPX( - startTime=start_time, - endTime=end_time, - sourceId=DataSource.COR1_A, - linked=False, - verbose=True, - jpip=False, - cadence=60, - ) - assert isinstance(response, dict) - assert response["uri"].startswith("https://") - - def test_error_handling(start_time, end_time): with pytest.raises(TypeError, match="missing 1 required positional argument: 'startTime'"): getJPX(endTime=end_time, sourceId=14) diff --git a/hvpy/api_groups/jpeg2000/tests/test_get_jpx_closest_to_mid_point.py b/hvpy/api_groups/jpeg2000/tests/test_get_jpx_closest_to_mid_point.py index cecdb0e..55d9293 100644 --- a/hvpy/api_groups/jpeg2000/tests/test_get_jpx_closest_to_mid_point.py +++ b/hvpy/api_groups/jpeg2000/tests/test_get_jpx_closest_to_mid_point.py @@ -7,14 +7,24 @@ def test_raw_response(start_times, end_times): response = getJPXClosestToMidPoint( - startTimes=start_times, endTimes=end_times, sourceId=14, linked=False, verbose=False, jpip=False + startTimes=start_times, + endTimes=end_times, + sourceId=DataSource.AIA_335, + linked=False, + verbose=False, + jpip=False, ) assert isinstance(response, bytes) def test_str_response(start_times, end_times): response = getJPXClosestToMidPoint( - startTimes=start_times, endTimes=end_times, sourceId=14, linked=False, verbose=False, jpip=True + startTimes=start_times, + endTimes=end_times, + sourceId=14, + linked=False, + verbose=False, + jpip=True, ) assert isinstance(response, str) assert response.startswith("jpip://") @@ -22,13 +32,23 @@ def test_str_response(start_times, end_times): def test_json_response(start_times, end_times): response = getJPXClosestToMidPoint( - startTimes=start_times, endTimes=end_times, sourceId=14, linked=False, verbose=True, jpip=True + startTimes=start_times, + endTimes=end_times, + sourceId=DataSource.AIA_335, + linked=False, + verbose=True, + jpip=True, ) assert isinstance(response, dict) assert response["uri"].startswith("jpip://") response = getJPXClosestToMidPoint( - startTimes=start_times, endTimes=end_times, sourceId=14, linked=False, verbose=True, jpip=False + startTimes=start_times, + endTimes=end_times, + sourceId=14, + linked=False, + verbose=True, + jpip=False, ) assert isinstance(response, dict) assert response["uri"].startswith("https://") @@ -46,32 +66,3 @@ def test_error_handling(start_times, end_times): def test_url_property(start_times, end_times): params = getJPXClosestToMidPointInputParameters(startTimes=start_times, endTimes=end_times, sourceId=14) assert params.url == "https://api.beta.helioviewer.org/v2/getJPXClosestToMidPoint/" - - -def test_raw_response_with_datasource(start_times, end_times): - response = getJPXClosestToMidPoint( - startTimes=start_times, endTimes=end_times, sourceId=DataSource.AIA_131, linked=False, verbose=False, jpip=False - ) - assert isinstance(response, bytes) - - -def test_str_response_with_datasource(start_times, end_times): - response = getJPXClosestToMidPoint( - startTimes=start_times, endTimes=end_times, sourceId=DataSource.AIA_131, linked=False, verbose=False, jpip=True - ) - assert isinstance(response, str) - assert response.startswith("jpip://") - - -def test_json_response_with_datasource(start_times, end_times): - response = getJPXClosestToMidPoint( - startTimes=start_times, endTimes=end_times, sourceId=DataSource.AIA_131, linked=False, verbose=True, jpip=True - ) - assert isinstance(response, dict) - assert response["uri"].startswith("jpip://") - - response = getJPXClosestToMidPoint( - startTimes=start_times, endTimes=end_times, sourceId=DataSource.AIA_131, linked=False, verbose=True, jpip=False - ) - assert isinstance(response, dict) - assert response["uri"].startswith("https://") diff --git a/hvpy/api_groups/official_clients/tests/test_get_closest_image.py b/hvpy/api_groups/official_clients/tests/test_get_closest_image.py index 6e73e7e..c65739a 100644 --- a/hvpy/api_groups/official_clients/tests/test_get_closest_image.py +++ b/hvpy/api_groups/official_clients/tests/test_get_closest_image.py @@ -6,7 +6,7 @@ def test_json_res(date): - response = getClosestImage(date=date, sourceId=14) + response = getClosestImage(date=date, sourceId=DataSource.AIA_335) assert isinstance(response, dict) @@ -16,17 +16,6 @@ def test_str_res(date): assert response.startswith("callback") -def test_json_res_with_datasource_enum(date): - response = getClosestImage(date=date, sourceId=DataSource.COR1_A) - assert isinstance(response, dict) - - -def test_str_res_with_datasource_enum(date): - response = getClosestImage(date=date, sourceId=DataSource.COR1_A, callback="callback") - assert isinstance(response, str) - assert response.startswith("callback") - - def test_error_handling(date): with pytest.raises(TypeError, match="missing 1 required positional argument: 'date'"): getClosestImage(sourceId=14) diff --git a/hvpy/utils.py b/hvpy/utils.py index da201aa..8a4dae8 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -49,18 +49,8 @@ def _data_source_to_int(source: Union[int, DataSource]) -> int: ---------- source The `~hvpy.DataSource` to convert. - - Returns - ------- - int - The integer representation of the `~hvpy.DataSource`. """ - if isinstance(source, DataSource): - return source.value - elif isinstance(source, int) and source in [x.value for x in DataSource]: - return source - else: - raise ValueError(f"{source} is not a valid DataSource.") + return _to_datasource(source).value def _to_datasource(val: Union[int, DataSource]) -> DataSource: