File: test_migrate.py

package info (click to toggle)
stactools 0.5.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,796 kB
  • sloc: python: 4,498; xml: 554; sh: 395; makefile: 34
file content (164 lines) | stat: -rw-r--r-- 4,954 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import shutil
from pathlib import Path

import pystac
import pytest
from click.testing import CliRunner
from stactools.cli.cli import cli

from tests import test_data


@pytest.fixture(scope="function")
def tmp_planet_disaster_path(tmp_path: Path) -> str:
    src = test_data.get_path("data-files/planet-disaster-v1.0.0-beta.2")
    dst = tmp_path / "planet-disaster"
    shutil.copytree(src, str(dst))
    return str(dst / "collection.json")


def test_migrate_no_save_by_default(tmp_planet_disaster_path: str):
    path = tmp_planet_disaster_path
    with open(path) as f:
        before = f.readlines()

    runner = CliRunner()
    result = runner.invoke(cli, ["migrate", path])
    assert result.exit_code == 0

    with open(path) as f:
        after = f.readlines()

    assert before == after


def test_migrate_with_save_no_recursive(tmp_planet_disaster_path: str):
    path = tmp_planet_disaster_path
    root = pystac.Collection.from_file(path)
    child_path = next(root.get_children()).get_self_href()
    item_path = next(root.get_items(recursive=True)).get_self_href()

    with open(path) as f:
        root_before = f.readlines()

    with open(child_path) as f:
        child_before = f.readlines()

    with open(item_path) as f:
        item_before = f.readlines()

    runner = CliRunner()
    result = runner.invoke(cli, ["migrate", path, "--save"])
    assert result.exit_code == 0

    with open(path) as f:
        root_after = f.readlines()

    with open(child_path) as f:
        child_after = f.readlines()

    with open(item_path) as f:
        item_after = f.readlines()

    assert root_before != root_after, path
    assert child_before == child_after, child_path
    assert item_before == item_after, item_path


def test_migrate_with_save_and_recursive(tmp_planet_disaster_path: str):
    path = tmp_planet_disaster_path
    root = pystac.Collection.from_file(path)
    child_path = next(root.get_children()).get_self_href()
    item_path = next(root.get_items(recursive=True)).get_self_href()

    with open(path) as f:
        root_before = f.readlines()

    with open(child_path) as f:
        child_before = f.readlines()

    with open(item_path) as f:
        item_before = f.readlines()

    runner = CliRunner()
    result = runner.invoke(cli, ["migrate", path, "-r", "-s"])
    assert result.exit_code == 0

    with open(path) as f:
        root_after = f.readlines()

    with open(child_path) as f:
        child_after = f.readlines()

    with open(item_path) as f:
        item_after = f.readlines()

    assert root_before != root_after, path
    assert child_before != child_after, child_path
    assert item_before != item_after, item_path


def test_migrate_show_diff(tmp_planet_disaster_path: str):
    path = tmp_planet_disaster_path
    root = pystac.Collection.from_file(path)
    child_path = next(root.get_children()).get_self_href()
    item_path = next(root.get_items(recursive=True)).get_self_href()

    runner = CliRunner()
    result = runner.invoke(cli, ["migrate", path, "--show-diff"])
    assert result.exit_code == 0

    assert result.output.startswith("--- a")
    assert path in result.output
    assert child_path not in result.output
    assert item_path not in result.output


def test_migrate_show_diff_and_recursive(tmp_planet_disaster_path: str):
    path = tmp_planet_disaster_path
    root = pystac.Collection.from_file(path)
    child_path = next(root.get_children()).get_self_href()
    item_path = next(root.get_items(recursive=True)).get_self_href()

    runner = CliRunner()
    result = runner.invoke(cli, ["migrate", path, "--show-diff", "--recursive"])
    assert result.exit_code == 0

    assert result.output.startswith("--- a")
    assert path in result.output
    assert child_path in result.output
    assert item_path in result.output


def test_migrate_hide_diff_with_no_save_raises(tmp_planet_disaster_path: str):
    path = tmp_planet_disaster_path

    runner = CliRunner()
    result = runner.invoke(cli, ["migrate", path, "--hide-diff"])
    assert result.exit_code == 2
    assert (
        "Error: It is only valid to use 'hide-diff' when 'save' is enabled "
        "otherwise there would be no output." in result.output
    )


def test_migrate_hide_diff(tmp_planet_disaster_path: str):
    path = tmp_planet_disaster_path

    runner = CliRunner()
    result = runner.invoke(cli, ["migrate", path, "--hide-diff", "--save"])
    assert not result.output


def test_migrate_recursive_invalid_for_items(tmp_planet_disaster_path: str):
    path = tmp_planet_disaster_path
    root = pystac.Collection.from_file(path)
    item_path = next(root.get_items(recursive=True)).get_self_href()

    runner = CliRunner()
    result = runner.invoke(cli, ["migrate", item_path, "-r"])
    assert result.exit_code == 2
    assert (
        "Error: 'recursive' is only a valid option for "
        "pystac.Catalogs and pystac.Collections" in result.output
    )