File: test_copy.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 (153 lines) | stat: -rw-r--r-- 4,789 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
import os
from pathlib import Path

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

from tests import test_data


def test_copy(tmp_path: Path, planet_disaster: pystac.Collection) -> None:
    collection_path = planet_disaster.get_self_href()
    item_ids = set([i.id for i in planet_disaster.get_items(recursive=True)])

    runner = CliRunner()
    result = runner.invoke(cli, ["copy", collection_path, str(tmp_path)])
    assert result.exit_code == 0

    copy_cat = pystac.read_file(tmp_path / "collection.json")
    copy_cat_ids = set([i.id for i in copy_cat.get_items(recursive=True)])

    assert copy_cat_ids == item_ids


def test_copy_to_relative(tmp_path: Path, planet_disaster: pystac.Collection) -> None:
    collection_path = planet_disaster.get_self_href()
    dst_dir = tmp_path / "second"

    runner = CliRunner()
    result = runner.invoke(
        cli, ["copy", "-t", "SELF_CONTAINED", "-a", collection_path, str(dst_dir)]
    )
    assert result.exit_code == 0

    dst_cat = pystac.read_file(dst_dir / "collection.json")
    for item in dst_cat.get_items(recursive=True):
        item_href = item.get_self_href()
        for asset in item.assets.values():
            href = asset.href
            assert pystac.utils.is_absolute_href(href) is False
            common_path = os.path.commonpath(
                [
                    os.path.dirname(item_href),
                    pystac.utils.make_absolute_href(href, item_href),
                ]
            )
            assert common_path == os.path.dirname(item_href)


def test_copy_using_publish_location(
    tmp_path: Path, planet_disaster: pystac.Collection
) -> None:
    collection_path = planet_disaster.get_self_href()
    href = "http://test.com"
    dst_dir = tmp_path / "second"

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "copy",
            "-t",
            "ABSOLUTE_PUBLISHED",
            "-a",
            collection_path,
            str(dst_dir),
            "-l",
            href,
        ],
    )
    assert result.exit_code == 0

    dst_cat = pystac.read_file(dst_dir / "collection.json")

    for link in dst_cat.get_child_links():
        assert link.target.startswith(href)

    assert (
        dst_dir
        / "hurricane-harvey"
        / "hurricane-harvey-0831"
        / "Houston-East-20170831-103f-100d-0f4f-RGB"
        / "Houston-East-20170831-103f-100d-0f4f-3-band.tif"
    ).exists()


def test_move_assets(tmp_path: Path, planet_disaster: pystac.Collection) -> None:
    cat = planet_disaster
    cat.normalize_hrefs(str(tmp_path))
    cat.save(catalog_type=pystac.CatalogType.RELATIVE_PUBLISHED)
    cat_href = cat.get_self_href()

    runner = CliRunner()
    result = runner.invoke(cli, ["move-assets", "-c", cat_href])
    assert result.exit_code == 0

    cat2 = pystac.read_file(cat_href)
    for item in cat2.get_items(recursive=True):
        item_href = item.get_self_href()
        for asset in item.assets.values():
            href = asset.href

            assert pystac.utils.is_absolute_href(href) is False
            common_path = os.path.commonpath(
                [
                    os.path.dirname(item_href),
                    pystac.utils.make_absolute_href(href, item_href),
                ]
            )

            assert common_path == os.path.dirname(item_href)


def test_copy_assets(tmp_path: Path, planet_disaster: pystac.Collection) -> None:
    cat = planet_disaster
    cat_href = cat.get_self_href()

    runner = CliRunner()
    result = runner.invoke(cli, ["copy", cat_href, str(tmp_path), "-a"])
    assert result.exit_code == 0

    cat2 = pystac.read_file(tmp_path / "collection.json")
    for item in cat2.get_items(recursive=True):
        assert all(v.href.startswith("./") for v in item.assets.values())

    assert (
        tmp_path
        / "hurricane-harvey"
        / "hurricane-harvey-0831"
        / "Houston-East-20170831-103f-100d-0f4f-RGB"
        / "Houston-East-20170831-103f-100d-0f4f-3-band.tif"
    ).exists()


def test_copy_using_no_resolve_links(tmp_path: Path) -> None:
    cat_path = test_data.get_path("data-files/external-child/catalog.json")

    runner = CliRunner()
    result = runner.invoke(cli, ["copy", cat_path, str(tmp_path), "--no-resolve-links"])
    assert result.exit_code == 0

    assert os.listdir(tmp_path) == ["catalog.json"]


def test_copy_collection_with_assets(tmp_path: Path) -> None:
    cat_path = test_data.get_path("data-files/catalogs/collection-assets/catalog.json")

    runner = CliRunner()
    result = runner.invoke(cli, ["copy", cat_path, str(tmp_path), "-a"])
    assert result.exit_code == 0

    assert (tmp_path / "sentinel-2" / "metadata.xml").exists()