Skip to content

Commit 8446839

Browse files
committed
Updating some tests to accomodate file:// in self links
1 parent 77d610e commit 8446839

File tree

7 files changed

+32
-22
lines changed

7 files changed

+32
-22
lines changed

tests/test_asset.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def test_alter_asset_absolute_path(
2121
assert asset.get_absolute_href() == new_href
2222
assert os.path.exists(new_href)
2323
if action == "move":
24-
assert not os.path.exists(old_href)
24+
assert not os.path.exists(old_href.replace("file://", ""))
2525
elif action == "copy":
26-
assert os.path.exists(old_href)
26+
assert os.path.exists(old_href.replace("file://", ""))
2727

2828

2929
@pytest.mark.parametrize("action", ["copy", "move"])
@@ -38,11 +38,11 @@ def test_alter_asset_relative_path(action: str, tmp_asset: pystac.Asset) -> None
3838
assert asset.href == new_href
3939
href = asset.get_absolute_href()
4040
assert href is not None
41-
assert os.path.exists(href)
41+
assert os.path.exists(href.replace("file://", ""))
4242
if action == "move":
43-
assert not os.path.exists(old_href)
43+
assert not os.path.exists(old_href.replace("file://", ""))
4444
elif action == "copy":
45-
assert os.path.exists(old_href)
45+
assert os.path.exists(old_href.replace("file://", ""))
4646

4747

4848
@pytest.mark.parametrize("action", ["copy", "move"])
@@ -82,23 +82,23 @@ def test_delete_asset(tmp_asset: pystac.Asset) -> None:
8282
asset = tmp_asset
8383
href = asset.get_absolute_href()
8484
assert href is not None
85-
assert os.path.exists(href)
85+
assert os.path.exists(href.replace("file://", ""))
8686

8787
asset.delete()
8888

89-
assert not os.path.exists(href)
89+
assert not os.path.exists(href.replace("file://", ""))
9090

9191

9292
def test_delete_asset_relative_no_owner_fails(tmp_asset: pystac.Asset) -> None:
9393
asset = tmp_asset
9494
href = asset.get_absolute_href()
9595
assert href is not None
96-
assert os.path.exists(href)
96+
assert os.path.exists(href.replace("file://", ""))
9797

9898
asset.owner = None
9999

100100
with pytest.raises(ValueError, match="Cannot delete file") as e:
101101
asset.delete()
102102

103103
assert asset.href in str(e.value)
104-
assert os.path.exists(href)
104+
assert os.path.exists(href.replace("file://", ""))

tests/test_catalog.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def test_save_with_different_stac_io(self) -> None:
541541

542542
assert len(hrefs) == stac_io.mock.write_text.call_count
543543
for call_args_list in stac_io.mock.write_text.call_args_list:
544-
assert call_args_list[0][0] in hrefs
544+
assert f"file://{call_args_list[0][0]}" in hrefs
545545

546546
def test_subcatalogs_saved_to_correct_path(self) -> None:
547547
with tempfile.TemporaryDirectory() as tmp_dir:

tests/test_collection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def test_delete_asset_relative_no_self_link_fails(
691691

692692
assert asset.href in str(e.value)
693693
assert name in collection.assets
694-
assert os.path.exists(href)
694+
assert os.path.exists(href.replace("file://", ""))
695695

696696

697697
def test_permissive_temporal_extent_deserialization(collection: Collection) -> None:

tests/test_item.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ def test_asset_absolute_href(self) -> None:
9292
item.set_self_href(item_path)
9393
rel_asset = Asset("./data.geojson")
9494
rel_asset.set_owner(item)
95-
expected_href = make_posix_style(
95+
expected_filepath = make_posix_style(
9696
os.path.abspath(os.path.join(os.path.dirname(item_path), "./data.geojson"))
9797
)
9898
actual_href = rel_asset.get_absolute_href()
99-
self.assertEqual(expected_href, actual_href)
99+
self.assertEqual(f"file://{expected_filepath}", actual_href)
100100

101101
def test_asset_absolute_href_no_item_self(self) -> None:
102102
item_dict = self.get_example_item_dict()
@@ -612,7 +612,7 @@ def test_delete_asset_relative_no_self_link_fails(tmp_asset: pystac.Asset) -> No
612612

613613
assert asset.href in str(e.value)
614614
assert name in item.assets
615-
assert os.path.exists(href)
615+
assert os.path.exists(href.replace("file://", ""))
616616

617617

618618
def test_resolve_collection_with_root(

tests/test_layout.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def test_catalog(self) -> None:
421421
href = self.strategy.get_href(
422422
cat, parent_dir="https://example.com", is_root=True
423423
)
424-
self.assertEqual(href, "/an/href")
424+
self.assertEqual(href, "file:///an/href")
425425

426426
def test_collection(self) -> None:
427427
collection = TestCases.case_8()
@@ -434,7 +434,7 @@ def test_collection(self) -> None:
434434
href = self.strategy.get_href(
435435
collection, parent_dir="https://example.com", is_root=True
436436
)
437-
self.assertEqual(href, "/an/href")
437+
self.assertEqual(href, "file:///an/href")
438438

439439
def test_item(self) -> None:
440440
collection = TestCases.case_8()
@@ -444,7 +444,7 @@ def test_item(self) -> None:
444444
self.strategy.get_href(item, parent_dir="http://example.com")
445445
item.set_self_href("/an/href")
446446
href = self.strategy.get_href(item, parent_dir="http://example.com")
447-
self.assertEqual(href, "/an/href")
447+
self.assertEqual(href, "file:///an/href")
448448

449449

450450
class APILayoutStrategyTest(unittest.TestCase):

tests/test_link.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ def test_resolved_self_href(self) -> None:
107107
link = catalog.get_single_link(pystac.RelType.SELF)
108108
assert link
109109
link.resolve_stac_object()
110-
self.assertEqual(link.get_absolute_href(), make_posix_style(path))
110+
self.assertEqual(
111+
link.get_absolute_href(), f"file://{make_posix_style(path)}"
112+
)
111113

112114
def test_target_getter_setter(self) -> None:
113115
link = pystac.Link("my rel", target="./foo/bar.json")
@@ -140,7 +142,10 @@ def test_relative_self_href(self) -> None:
140142
item = pystac.read_file("item.json")
141143
href = item.get_self_href()
142144
assert href
143-
self.assertTrue(os.path.isabs(href), f"Not an absolute path: {href}")
145+
self.assertTrue(
146+
os.path.isabs(href.replace("file://", "")),
147+
f"Not an absolute path: {href}",
148+
)
144149
finally:
145150
os.chdir(previous)
146151

@@ -237,7 +242,10 @@ def test_from_dict_round_trip(self) -> None:
237242
d2 = pystac.Link.from_dict(d).to_dict()
238243
self.assertEqual(d, d2)
239244
d = {"rel": "self", "href": "t"}
240-
d2 = {"rel": "self", "href": make_posix_style(os.path.join(os.getcwd(), "t"))}
245+
d2 = {
246+
"rel": "self",
247+
"href": f"file://{make_posix_style(os.path.join(os.getcwd(), 't'))}",
248+
}
241249
self.assertEqual(pystac.Link.from_dict(d).to_dict(), d2)
242250

243251
def test_from_dict_failures(self) -> None:
@@ -333,7 +341,7 @@ def test_relative_self_link(tmp_path: Path) -> None:
333341
assert read_item
334342
asset_href = read_item.assets["data"].get_absolute_href()
335343
assert asset_href
336-
assert Path(asset_href).exists()
344+
assert Path(asset_href.replace("file://", "")).exists()
337345

338346

339347
@pytest.mark.parametrize("rel", HIERARCHICAL_LINKS)

tests/validation/test_validate.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def test_validate_all_dict(self, test_case: pystac.Catalog) -> None:
125125
dst_dir = os.path.join(tmp_dir, "catalog")
126126
# Copy test case 7 to the temporary directory
127127
catalog_href = get_opt(TestCases.case_7().get_self_href())
128-
shutil.copytree(os.path.dirname(catalog_href), dst_dir)
128+
shutil.copytree(
129+
os.path.dirname(catalog_href.replace("file://", "")), dst_dir
130+
)
129131

130132
new_cat_href = os.path.join(dst_dir, "catalog.json")
131133

0 commit comments

Comments
 (0)