|
| 1 | +import shutil |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pystac |
| 5 | +import pytest |
| 6 | +from click.testing import CliRunner |
| 7 | +from stactools.cli.cli import cli |
| 8 | + |
| 9 | +from tests import test_data |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="function") |
| 13 | +def tmp_planet_disaster_path(tmp_path: Path) -> str: |
| 14 | + src = test_data.get_path("data-files/planet-disaster-v1.0.0-beta.2") |
| 15 | + dst = tmp_path / "planet-disaster" |
| 16 | + shutil.copytree(src, str(dst)) |
| 17 | + return str(dst / "collection.json") |
| 18 | + |
| 19 | + |
| 20 | +def test_migrate_no_save_by_default(tmp_planet_disaster_path: str): |
| 21 | + path = tmp_planet_disaster_path |
| 22 | + with open(path) as f: |
| 23 | + before = f.readlines() |
| 24 | + |
| 25 | + runner = CliRunner() |
| 26 | + result = runner.invoke(cli, ["migrate", path]) |
| 27 | + assert result.exit_code == 0 |
| 28 | + |
| 29 | + with open(path) as f: |
| 30 | + after = f.readlines() |
| 31 | + |
| 32 | + assert before == after |
| 33 | + |
| 34 | + |
| 35 | +def test_migrate_with_save_no_recursive(tmp_planet_disaster_path: str): |
| 36 | + path = tmp_planet_disaster_path |
| 37 | + root = pystac.Collection.from_file(path) |
| 38 | + child_path = next(root.get_children()).get_self_href() |
| 39 | + item_path = next(root.get_all_items()).get_self_href() |
| 40 | + |
| 41 | + with open(path) as f: |
| 42 | + root_before = f.readlines() |
| 43 | + |
| 44 | + with open(child_path) as f: |
| 45 | + child_before = f.readlines() |
| 46 | + |
| 47 | + with open(item_path) as f: |
| 48 | + item_before = f.readlines() |
| 49 | + |
| 50 | + runner = CliRunner() |
| 51 | + result = runner.invoke(cli, ["migrate", path, "--save"]) |
| 52 | + assert result.exit_code == 0 |
| 53 | + |
| 54 | + with open(path) as f: |
| 55 | + root_after = f.readlines() |
| 56 | + |
| 57 | + with open(child_path) as f: |
| 58 | + child_after = f.readlines() |
| 59 | + |
| 60 | + with open(item_path) as f: |
| 61 | + item_after = f.readlines() |
| 62 | + |
| 63 | + assert root_before != root_after, path |
| 64 | + assert child_before == child_after, child_path |
| 65 | + assert item_before == item_after, item_path |
| 66 | + |
| 67 | + |
| 68 | +def test_migrate_with_save_and_recursive(tmp_planet_disaster_path: str): |
| 69 | + path = tmp_planet_disaster_path |
| 70 | + root = pystac.Collection.from_file(path) |
| 71 | + child_path = next(root.get_children()).get_self_href() |
| 72 | + item_path = next(root.get_all_items()).get_self_href() |
| 73 | + |
| 74 | + with open(path) as f: |
| 75 | + root_before = f.readlines() |
| 76 | + |
| 77 | + with open(child_path) as f: |
| 78 | + child_before = f.readlines() |
| 79 | + |
| 80 | + with open(item_path) as f: |
| 81 | + item_before = f.readlines() |
| 82 | + |
| 83 | + runner = CliRunner() |
| 84 | + result = runner.invoke(cli, ["migrate", path, "-r", "-s"]) |
| 85 | + assert result.exit_code == 0 |
| 86 | + |
| 87 | + with open(path) as f: |
| 88 | + root_after = f.readlines() |
| 89 | + |
| 90 | + with open(child_path) as f: |
| 91 | + child_after = f.readlines() |
| 92 | + |
| 93 | + with open(item_path) as f: |
| 94 | + item_after = f.readlines() |
| 95 | + |
| 96 | + assert root_before != root_after, path |
| 97 | + assert child_before != child_after, child_path |
| 98 | + assert item_before != item_after, item_path |
| 99 | + |
| 100 | + |
| 101 | +def test_migrate_show_diff(tmp_planet_disaster_path: str): |
| 102 | + path = tmp_planet_disaster_path |
| 103 | + root = pystac.Collection.from_file(path) |
| 104 | + child_path = next(root.get_children()).get_self_href() |
| 105 | + item_path = next(root.get_all_items()).get_self_href() |
| 106 | + |
| 107 | + runner = CliRunner() |
| 108 | + result = runner.invoke(cli, ["migrate", path, "--show-diff"]) |
| 109 | + assert result.exit_code == 0 |
| 110 | + |
| 111 | + assert result.output.startswith("--- a") |
| 112 | + assert path in result.output |
| 113 | + assert child_path not in result.output |
| 114 | + assert item_path not in result.output |
| 115 | + |
| 116 | + |
| 117 | +def test_migrate_show_diff_and_recursive(tmp_planet_disaster_path: str): |
| 118 | + path = tmp_planet_disaster_path |
| 119 | + root = pystac.Collection.from_file(path) |
| 120 | + child_path = next(root.get_children()).get_self_href() |
| 121 | + item_path = next(root.get_all_items()).get_self_href() |
| 122 | + |
| 123 | + runner = CliRunner() |
| 124 | + result = runner.invoke(cli, ["migrate", path, "--show-diff", "--recursive"]) |
| 125 | + assert result.exit_code == 0 |
| 126 | + |
| 127 | + assert result.output.startswith("--- a") |
| 128 | + assert path in result.output |
| 129 | + assert child_path in result.output |
| 130 | + assert item_path in result.output |
| 131 | + |
| 132 | + |
| 133 | +def test_migrate_hide_diff_with_no_save_raises(tmp_planet_disaster_path: str): |
| 134 | + path = tmp_planet_disaster_path |
| 135 | + |
| 136 | + runner = CliRunner() |
| 137 | + result = runner.invoke(cli, ["migrate", path, "--hide-diff"]) |
| 138 | + assert result.exit_code == 2 |
| 139 | + assert ( |
| 140 | + "Error: It is only valid to use 'hide-diff' when 'save' is enabled " |
| 141 | + "otherwise there would be no output." in result.output |
| 142 | + ) |
| 143 | + |
| 144 | + |
| 145 | +def test_migrate_hide_diff(tmp_planet_disaster_path: str): |
| 146 | + path = tmp_planet_disaster_path |
| 147 | + |
| 148 | + runner = CliRunner() |
| 149 | + result = runner.invoke(cli, ["migrate", path, "--hide-diff", "--save"]) |
| 150 | + assert not result.output |
| 151 | + |
| 152 | + |
| 153 | +def test_migrate_recursive_invalid_for_items(tmp_planet_disaster_path: str): |
| 154 | + path = tmp_planet_disaster_path |
| 155 | + root = pystac.Collection.from_file(path) |
| 156 | + item_path = next(root.get_all_items()).get_self_href() |
| 157 | + |
| 158 | + runner = CliRunner() |
| 159 | + result = runner.invoke(cli, ["migrate", item_path, "-r"]) |
| 160 | + assert result.exit_code == 2 |
| 161 | + assert ( |
| 162 | + "Error: 'recursive' is only a valid option for " |
| 163 | + "pystac.Catalogs and pystac.Collections" in result.output |
| 164 | + ) |
0 commit comments