From 53726f702e6e40d0edf09fe132f338b6d79212dd Mon Sep 17 00:00:00 2001 From: Victor Engmark Date: Sat, 19 Jun 2021 15:30:33 +1200 Subject: [PATCH 1/2] tests: Verify empty observation_direction property --- tests/extensions/test_sar.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/extensions/test_sar.py b/tests/extensions/test_sar.py index 23311bced..d2f939e04 100644 --- a/tests/extensions/test_sar.py +++ b/tests/extensions/test_sar.py @@ -1,9 +1,12 @@ """Tests for pystac.extensions.sar.""" import datetime +from random import choice from typing import List import unittest +from string import ascii_letters + import pystac from pystac.extensions import sar from pystac.extensions.sar import SarExtension @@ -180,6 +183,16 @@ def test_asset_ext_add_to(self) -> None: self.assertIn(SarExtension.get_schema_uri(), item.stac_extensions) + def test_should_return_none_when_observation_direction_is_not_set(self) -> None: + extension = SarExtension.ext(self.item) + extension.apply( + choice(ascii_letters), + choice(list(sar.FrequencyBand)), + [], + choice(ascii_letters), + ) + self.assertIsNone(extension.observation_direction) + if __name__ == "__main__": unittest.main() From 431b5a74647222ea48da27e7dfb6f4f87cc3711d Mon Sep 17 00:00:00 2001 From: Victor Engmark Date: Sat, 19 Jun 2021 15:41:39 +1200 Subject: [PATCH 2/2] feat: Print human readable type name in error messages Based on . Also add tests to verify the exception being thrown. --- pystac/extensions/sar.py | 2 +- pystac/extensions/view.py | 2 +- tests/extensions/test_sar.py | 11 +++++++++++ tests/extensions/test_view.py | 12 ++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pystac/extensions/sar.py b/pystac/extensions/sar.py index 0f07213ea..9fd18ba28 100644 --- a/pystac/extensions/sar.py +++ b/pystac/extensions/sar.py @@ -318,7 +318,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "SarExtension[T]": return cast(SarExtension[T], AssetSarExtension(obj)) else: raise pystac.ExtensionTypeError( - f"SAR extension does not apply to type {type(obj)}" + f"SAR extension does not apply to type '{type(obj).__name__}'" ) diff --git a/pystac/extensions/view.py b/pystac/extensions/view.py index 6438a2cb0..47a6333d3 100644 --- a/pystac/extensions/view.py +++ b/pystac/extensions/view.py @@ -167,7 +167,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "ViewExtension[T]": return cast(ViewExtension[T], AssetViewExtension(obj)) else: raise pystac.ExtensionTypeError( - f"View extension does not apply to type {type(obj)}" + f"View extension does not apply to type '{type(obj).__name__}'" ) @staticmethod diff --git a/tests/extensions/test_sar.py b/tests/extensions/test_sar.py index d2f939e04..38ae253eb 100644 --- a/tests/extensions/test_sar.py +++ b/tests/extensions/test_sar.py @@ -8,6 +8,7 @@ from string import ascii_letters import pystac +from pystac import ExtensionTypeError from pystac.extensions import sar from pystac.extensions.sar import SarExtension from tests.utils import TestCases @@ -193,6 +194,16 @@ def test_should_return_none_when_observation_direction_is_not_set(self) -> None: ) self.assertIsNone(extension.observation_direction) + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^SAR extension does not apply to type 'object'$", + SarExtension.ext, + object(), + ) + if __name__ == "__main__": unittest.main() diff --git a/tests/extensions/test_view.py b/tests/extensions/test_view.py index ac6f10a5d..c24ef8222 100644 --- a/tests/extensions/test_view.py +++ b/tests/extensions/test_view.py @@ -1,4 +1,6 @@ import json + +from pystac import ExtensionTypeError from pystac.collection import Collection import unittest @@ -257,6 +259,16 @@ def test_asset_ext_add_to(self) -> None: self.assertIn(ViewExtension.get_schema_uri(), item.stac_extensions) + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^View extension does not apply to type 'object'$", + ViewExtension.ext, + object(), + ) + class ViewSummariestest(unittest.TestCase): def setUp(self) -> None: