Skip to content

Commit

Permalink
Updating some tests to accomodate file:// in self links
Browse files Browse the repository at this point in the history
  • Loading branch information
jsignell committed Nov 5, 2024
1 parent 2af7e74 commit b73ffbc
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 22 deletions.
18 changes: 9 additions & 9 deletions tests/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def test_alter_asset_absolute_path(
assert asset.get_absolute_href() == new_href
assert os.path.exists(new_href)
if action == "move":
assert not os.path.exists(old_href)
assert not os.path.exists(old_href.replace("file://", ""))
elif action == "copy":
assert os.path.exists(old_href)
assert os.path.exists(old_href.replace("file://", ""))


@pytest.mark.parametrize("action", ["copy", "move"])
Expand All @@ -38,11 +38,11 @@ def test_alter_asset_relative_path(action: str, tmp_asset: pystac.Asset) -> None
assert asset.href == new_href
href = asset.get_absolute_href()
assert href is not None
assert os.path.exists(href)
assert os.path.exists(href.replace("file://", ""))
if action == "move":
assert not os.path.exists(old_href)
assert not os.path.exists(old_href.replace("file://", ""))
elif action == "copy":
assert os.path.exists(old_href)
assert os.path.exists(old_href.replace("file://", ""))


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

asset.delete()

assert not os.path.exists(href)
assert not os.path.exists(href.replace("file://", ""))


def test_delete_asset_relative_no_owner_fails(tmp_asset: pystac.Asset) -> None:
asset = tmp_asset
href = asset.get_absolute_href()
assert href is not None
assert os.path.exists(href)
assert os.path.exists(href.replace("file://", ""))

asset.owner = None

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

assert asset.href in str(e.value)
assert os.path.exists(href)
assert os.path.exists(href.replace("file://", ""))
2 changes: 1 addition & 1 deletion tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def test_save_with_different_stac_io(self) -> None:

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

def test_subcatalogs_saved_to_correct_path(self) -> None:
with tempfile.TemporaryDirectory() as tmp_dir:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ def test_delete_asset_relative_no_self_link_fails(

assert asset.href in str(e.value)
assert name in collection.assets
assert os.path.exists(href)
assert os.path.exists(href.replace("file://", ""))


def test_permissive_temporal_extent_deserialization(collection: Collection) -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ def test_asset_absolute_href(self) -> None:
item.set_self_href(item_path)
rel_asset = Asset("./data.geojson")
rel_asset.set_owner(item)
expected_href = make_posix_style(
expected_filepath = make_posix_style(
os.path.abspath(os.path.join(os.path.dirname(item_path), "./data.geojson"))
)
actual_href = rel_asset.get_absolute_href()
self.assertEqual(expected_href, actual_href)
self.assertEqual(f"file://{expected_filepath}", actual_href)

def test_asset_absolute_href_no_item_self(self) -> None:
item_dict = self.get_example_item_dict()
Expand Down Expand Up @@ -612,7 +612,7 @@ def test_delete_asset_relative_no_self_link_fails(tmp_asset: pystac.Asset) -> No

assert asset.href in str(e.value)
assert name in item.assets
assert os.path.exists(href)
assert os.path.exists(href.replace("file://", ""))


def test_resolve_collection_with_root(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def test_catalog(self) -> None:
href = self.strategy.get_href(
cat, parent_dir="https://example.com", is_root=True
)
self.assertEqual(href, "/an/href")
self.assertEqual(href, "file:///an/href")

def test_collection(self) -> None:
collection = TestCases.case_8()
Expand All @@ -434,7 +434,7 @@ def test_collection(self) -> None:
href = self.strategy.get_href(
collection, parent_dir="https://example.com", is_root=True
)
self.assertEqual(href, "/an/href")
self.assertEqual(href, "file:///an/href")

def test_item(self) -> None:
collection = TestCases.case_8()
Expand All @@ -444,7 +444,7 @@ def test_item(self) -> None:
self.strategy.get_href(item, parent_dir="http://example.com")
item.set_self_href("/an/href")
href = self.strategy.get_href(item, parent_dir="http://example.com")
self.assertEqual(href, "/an/href")
self.assertEqual(href, "file:///an/href")


class APILayoutStrategyTest(unittest.TestCase):
Expand Down
16 changes: 12 additions & 4 deletions tests/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def test_resolved_self_href(self) -> None:
link = catalog.get_single_link(pystac.RelType.SELF)
assert link
link.resolve_stac_object()
self.assertEqual(link.get_absolute_href(), make_posix_style(path))
self.assertEqual(
link.get_absolute_href(), f"file://{make_posix_style(path)}"
)

def test_target_getter_setter(self) -> None:
link = pystac.Link("my rel", target="./foo/bar.json")
Expand Down Expand Up @@ -140,7 +142,10 @@ def test_relative_self_href(self) -> None:
item = pystac.read_file("item.json")
href = item.get_self_href()
assert href
self.assertTrue(os.path.isabs(href), f"Not an absolute path: {href}")
self.assertTrue(
os.path.isabs(href.replace("file://", "")),
f"Not an absolute path: {href}",
)
finally:
os.chdir(previous)

Expand Down Expand Up @@ -237,7 +242,10 @@ def test_from_dict_round_trip(self) -> None:
d2 = pystac.Link.from_dict(d).to_dict()
self.assertEqual(d, d2)
d = {"rel": "self", "href": "t"}
d2 = {"rel": "self", "href": make_posix_style(os.path.join(os.getcwd(), "t"))}
d2 = {
"rel": "self",
"href": f"file://{make_posix_style(os.path.join(os.getcwd(), 't'))}",
}
self.assertEqual(pystac.Link.from_dict(d).to_dict(), d2)

def test_from_dict_failures(self) -> None:
Expand Down Expand Up @@ -333,7 +341,7 @@ def test_relative_self_link(tmp_path: Path) -> None:
assert read_item
asset_href = read_item.assets["data"].get_absolute_href()
assert asset_href
assert Path(asset_href).exists()
assert Path(asset_href.replace("file://", "")).exists()


@pytest.mark.parametrize("rel", HIERARCHICAL_LINKS)
Expand Down
4 changes: 3 additions & 1 deletion tests/validation/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def test_validate_all_dict(self, test_case: pystac.Catalog) -> None:
dst_dir = os.path.join(tmp_dir, "catalog")
# Copy test case 7 to the temporary directory
catalog_href = get_opt(TestCases.case_7().get_self_href())
shutil.copytree(os.path.dirname(catalog_href), dst_dir)
shutil.copytree(
os.path.dirname(catalog_href.replace("file://", "")), dst_dir
)

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

Expand Down

0 comments on commit b73ffbc

Please sign in to comment.