Skip to content

Commit

Permalink
Merge pull request stac-utils#503 from l0b0/increase-test-coverage
Browse files Browse the repository at this point in the history
Increase test coverage
  • Loading branch information
Jon Duckworth authored Jul 1, 2021
2 parents 8b4737e + de29122 commit 0426bd1
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[report]
fail_under = 95
fail_under = 96
exclude_lines =
if TYPE_CHECKING:

Expand Down
4 changes: 0 additions & 4 deletions tests/extensions/test_sar.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,3 @@ def test_should_raise_exception_when_passing_invalid_extension_object(
SarExtension.ext,
object(),
)


if __name__ == "__main__":
unittest.main()
4 changes: 0 additions & 4 deletions tests/extensions/test_scientific.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,3 @@ def test_set_doi_summaries(self) -> None:

assert new_dois is not None
self.assertListEqual([PUB2_DOI], new_dois)


if __name__ == "__main__":
unittest.main()
4 changes: 0 additions & 4 deletions tests/extensions/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,3 @@ def test_ext_add_to(self) -> None:
_ = VersionExtension.ext(collection, add_if_missing=True)

self.assertIn(VersionExtension.get_schema_uri(), collection.stac_extensions)


if __name__ == "__main__":
unittest.main()
10 changes: 3 additions & 7 deletions tests/serialization/test_identify.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
from urllib.error import HTTPError

import pystac
from pystac.cache import CollectionCache
Expand All @@ -24,12 +23,9 @@ def test_identify(self) -> None:
path = example.path
d = pystac.StacIO.default().read_json(path)
if identify_stac_object_type(d) == pystac.STACObjectType.ITEM:
try:
merge_common_properties(
d, json_href=path, collection_cache=collection_cache
)
except HTTPError:
pass
merge_common_properties(
d, json_href=path, collection_cache=collection_cache
)

actual = identify_stac_object(d)
# Explicitly cover __repr__ functions in tests
Expand Down
18 changes: 5 additions & 13 deletions tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import os
import unittest
from unittest.mock import patch

import pystac
from tests.utils import TestCases


class VersionTest(unittest.TestCase):
def setUp(self) -> None:
self._prev_env_version = os.environ.get("PYSTAC_STAC_VERSION_OVERRIDE")
self._prev_version = pystac.get_stac_version()

def tearDown(self) -> None:
if self._prev_env_version is None:
os.environ.pop("PYSTAC_STAC_VERSION_OVERRIDE", None)
else:
os.environ["PYSTAC_STAC_VERSION_OVERRIDE"] = self._prev_env_version
pystac.set_stac_version(None)
pystac.version.STACVersion._override_version = None

def test_override_stac_version_with_environ(self) -> None:

override_version = "1.0.0-gamma.2"
os.environ["PYSTAC_STAC_VERSION_OVERRIDE"] = override_version
cat = TestCases.test_case_1()
d = cat.to_dict()
with patch.dict(os.environ, {"PYSTAC_STAC_VERSION_OVERRIDE": override_version}):
cat = TestCases.test_case_1()
d = cat.to_dict()
self.assertEqual(d["stac_version"], override_version)

def test_override_stac_version_with_call(self) -> None:
Expand Down
21 changes: 2 additions & 19 deletions tests/test_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pystac
from pystac import Collection, CatalogType, HIERARCHICAL_LINKS
from pystac.utils import is_absolute_href, make_absolute_href, make_relative_href
from pystac.utils import is_absolute_href, make_absolute_href
from pystac.validation import validate_dict

from tests.utils import TestCases
Expand Down Expand Up @@ -35,18 +35,6 @@ def validate_file(self, path: str, object_type: str) -> List[Any]:
def validate_link_types(
self, root_href: str, catalog_type: pystac.CatalogType
) -> None:
def validate_asset_href_type(item: pystac.Item, item_href: str) -> None:
for asset in item.assets.values():
if not is_absolute_href(asset.href):
is_valid = not is_absolute_href(asset.href)
if not is_valid:
# If the item href and asset href don't share
# the same root, the asset href must be absolute
rel_href = make_relative_href(asset.href, item_href)
self.assertEqual(asset.href, rel_href)
else:
self.assertTrue(is_valid)

def validate_item_link_type(
href: str, link_type: str, should_include_self: bool
) -> None:
Expand All @@ -63,8 +51,6 @@ def validate_item_link_type(
else:
self.assertTrue(is_absolute_href(link.href))

validate_asset_href_type(item, href)

rels = set([link["rel"] for link in item_dict["links"]])
self.assertEqual("self" in rels, should_include_self)

Expand All @@ -73,10 +59,7 @@ def validate_catalog_link_type(
) -> None:
cat_dict = pystac.StacIO.default().read_json(href)
cat = pystac.read_file(href)
if not isinstance(cat, pystac.Catalog):
raise pystac.STACTypeError(
f"File at {href} is a {cat.STAC_OBJECT_TYPE} not a Catalog."
)
assert isinstance(cat, pystac.Catalog)

rels = set([link["rel"] for link in cat_dict["links"]])
self.assertEqual("self" in rels, should_include_self)
Expand Down
42 changes: 17 additions & 25 deletions tests/validation/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,27 @@ def test_validate_examples(self) -> None:
path = example.path
valid = example.valid

if stac_version < "0.8":
with self.subTest(path):
with open(path, encoding="utf-8") as f:
stac_json = json.load(f)

self.assertEqual(len(pystac.validation.validate_dict(stac_json)), 0)
else:
with self.subTest(path):
with open(path, encoding="utf-8") as f:
stac_json = json.load(f)

# Check if common properties need to be merged
if stac_version < "1.0":
if example.object_type == pystac.STACObjectType.ITEM:
collection_cache = CollectionCache()
merge_common_properties(
stac_json, collection_cache, path
# Check if common properties need to be merged
if stac_version < "1.0":
if example.object_type == pystac.STACObjectType.ITEM:
collection_cache = CollectionCache()
merge_common_properties(stac_json, collection_cache, path)

if valid:
pystac.validation.validate_dict(stac_json)
else:
with self.assertRaises(pystac.STACValidationError):
try:
pystac.validation.validate_dict(stac_json)
except pystac.STACValidationError as e:
self.assertIsInstance(
e.source, jsonschema.ValidationError
)

if valid:
pystac.validation.validate_dict(stac_json)
else:
with self.assertRaises(pystac.STACValidationError):
try:
pystac.validation.validate_dict(stac_json)
except pystac.STACValidationError as e:
self.assertIsInstance(
e.source, jsonschema.ValidationError
)
raise e
raise e

def test_validate_error_contains_href(self) -> None:
# Test that the exception message contains the HREF of the object if available.
Expand Down

0 comments on commit 0426bd1

Please sign in to comment.