Skip to content

Commit 0426bd1

Browse files
author
Jon Duckworth
authored
Merge pull request #503 from l0b0/increase-test-coverage
Increase test coverage
2 parents 8b4737e + de29122 commit 0426bd1

File tree

8 files changed

+28
-77
lines changed

8 files changed

+28
-77
lines changed

.coveragerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[report]
2-
fail_under = 95
2+
fail_under = 96
33
exclude_lines =
44
if TYPE_CHECKING:
55

tests/extensions/test_sar.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,3 @@ def test_should_raise_exception_when_passing_invalid_extension_object(
203203
SarExtension.ext,
204204
object(),
205205
)
206-
207-
208-
if __name__ == "__main__":
209-
unittest.main()

tests/extensions/test_scientific.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,3 @@ def test_set_doi_summaries(self) -> None:
456456

457457
assert new_dois is not None
458458
self.assertListEqual([PUB2_DOI], new_dois)
459-
460-
461-
if __name__ == "__main__":
462-
unittest.main()

tests/extensions/test_version.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,3 @@ def test_ext_add_to(self) -> None:
473473
_ = VersionExtension.ext(collection, add_if_missing=True)
474474

475475
self.assertIn(VersionExtension.get_schema_uri(), collection.stac_extensions)
476-
477-
478-
if __name__ == "__main__":
479-
unittest.main()

tests/serialization/test_identify.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import unittest
2-
from urllib.error import HTTPError
32

43
import pystac
54
from pystac.cache import CollectionCache
@@ -24,12 +23,9 @@ def test_identify(self) -> None:
2423
path = example.path
2524
d = pystac.StacIO.default().read_json(path)
2625
if identify_stac_object_type(d) == pystac.STACObjectType.ITEM:
27-
try:
28-
merge_common_properties(
29-
d, json_href=path, collection_cache=collection_cache
30-
)
31-
except HTTPError:
32-
pass
26+
merge_common_properties(
27+
d, json_href=path, collection_cache=collection_cache
28+
)
3329

3430
actual = identify_stac_object(d)
3531
# Explicitly cover __repr__ functions in tests

tests/test_version.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
import os
22
import unittest
3+
from unittest.mock import patch
34

45
import pystac
56
from tests.utils import TestCases
67

78

89
class VersionTest(unittest.TestCase):
910
def setUp(self) -> None:
10-
self._prev_env_version = os.environ.get("PYSTAC_STAC_VERSION_OVERRIDE")
11-
self._prev_version = pystac.get_stac_version()
12-
13-
def tearDown(self) -> None:
14-
if self._prev_env_version is None:
15-
os.environ.pop("PYSTAC_STAC_VERSION_OVERRIDE", None)
16-
else:
17-
os.environ["PYSTAC_STAC_VERSION_OVERRIDE"] = self._prev_env_version
18-
pystac.set_stac_version(None)
11+
pystac.version.STACVersion._override_version = None
1912

2013
def test_override_stac_version_with_environ(self) -> None:
21-
2214
override_version = "1.0.0-gamma.2"
23-
os.environ["PYSTAC_STAC_VERSION_OVERRIDE"] = override_version
24-
cat = TestCases.test_case_1()
25-
d = cat.to_dict()
15+
with patch.dict(os.environ, {"PYSTAC_STAC_VERSION_OVERRIDE": override_version}):
16+
cat = TestCases.test_case_1()
17+
d = cat.to_dict()
2618
self.assertEqual(d["stac_version"], override_version)
2719

2820
def test_override_stac_version_with_call(self) -> None:

tests/test_writing.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pystac
66
from pystac import Collection, CatalogType, HIERARCHICAL_LINKS
7-
from pystac.utils import is_absolute_href, make_absolute_href, make_relative_href
7+
from pystac.utils import is_absolute_href, make_absolute_href
88
from pystac.validation import validate_dict
99

1010
from tests.utils import TestCases
@@ -35,18 +35,6 @@ def validate_file(self, path: str, object_type: str) -> List[Any]:
3535
def validate_link_types(
3636
self, root_href: str, catalog_type: pystac.CatalogType
3737
) -> None:
38-
def validate_asset_href_type(item: pystac.Item, item_href: str) -> None:
39-
for asset in item.assets.values():
40-
if not is_absolute_href(asset.href):
41-
is_valid = not is_absolute_href(asset.href)
42-
if not is_valid:
43-
# If the item href and asset href don't share
44-
# the same root, the asset href must be absolute
45-
rel_href = make_relative_href(asset.href, item_href)
46-
self.assertEqual(asset.href, rel_href)
47-
else:
48-
self.assertTrue(is_valid)
49-
5038
def validate_item_link_type(
5139
href: str, link_type: str, should_include_self: bool
5240
) -> None:
@@ -63,8 +51,6 @@ def validate_item_link_type(
6351
else:
6452
self.assertTrue(is_absolute_href(link.href))
6553

66-
validate_asset_href_type(item, href)
67-
6854
rels = set([link["rel"] for link in item_dict["links"]])
6955
self.assertEqual("self" in rels, should_include_self)
7056

@@ -73,10 +59,7 @@ def validate_catalog_link_type(
7359
) -> None:
7460
cat_dict = pystac.StacIO.default().read_json(href)
7561
cat = pystac.read_file(href)
76-
if not isinstance(cat, pystac.Catalog):
77-
raise pystac.STACTypeError(
78-
f"File at {href} is a {cat.STAC_OBJECT_TYPE} not a Catalog."
79-
)
62+
assert isinstance(cat, pystac.Catalog)
8063

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

tests/validation/test_validate.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,27 @@ def test_validate_examples(self) -> None:
4242
path = example.path
4343
valid = example.valid
4444

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

49-
self.assertEqual(len(pystac.validation.validate_dict(stac_json)), 0)
50-
else:
51-
with self.subTest(path):
52-
with open(path, encoding="utf-8") as f:
53-
stac_json = json.load(f)
54-
55-
# Check if common properties need to be merged
56-
if stac_version < "1.0":
57-
if example.object_type == pystac.STACObjectType.ITEM:
58-
collection_cache = CollectionCache()
59-
merge_common_properties(
60-
stac_json, collection_cache, path
49+
# Check if common properties need to be merged
50+
if stac_version < "1.0":
51+
if example.object_type == pystac.STACObjectType.ITEM:
52+
collection_cache = CollectionCache()
53+
merge_common_properties(stac_json, collection_cache, path)
54+
55+
if valid:
56+
pystac.validation.validate_dict(stac_json)
57+
else:
58+
with self.assertRaises(pystac.STACValidationError):
59+
try:
60+
pystac.validation.validate_dict(stac_json)
61+
except pystac.STACValidationError as e:
62+
self.assertIsInstance(
63+
e.source, jsonschema.ValidationError
6164
)
62-
63-
if valid:
64-
pystac.validation.validate_dict(stac_json)
65-
else:
66-
with self.assertRaises(pystac.STACValidationError):
67-
try:
68-
pystac.validation.validate_dict(stac_json)
69-
except pystac.STACValidationError as e:
70-
self.assertIsInstance(
71-
e.source, jsonschema.ValidationError
72-
)
73-
raise e
65+
raise e
7466

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

0 commit comments

Comments
 (0)