File: test_add.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 (80 lines) | stat: -rw-r--r-- 2,237 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
import pystac
import pystac.utils
import pytest
from click.testing import CliRunner
from stactools.cli.cli import cli

from tests import test_data


@pytest.fixture(scope="module")
def item_path() -> str:
    return test_data.get_path(
        "data-files/basic/country-1/area-1-1"
        "/area-1-1-imagery/area-1-1-imagery-invalid.json"
    )


def test_add_item(item_path: str, tmp_planet_disaster: pystac.Collection):
    collection = tmp_planet_disaster
    collection_path = collection.get_self_href()
    items = list(collection.get_items(recursive=True))
    assert len(items) == 5

    runner = CliRunner()
    result = runner.invoke(cli, ["add", item_path, collection_path])
    assert result.exit_code == 0

    collection_after = pystac.read_file(collection_path)
    items = list(collection_after.get_items(recursive=True))
    assert len(items) == 6


def test_add_item_to_specific_collection(
    item_path: str, tmp_planet_disaster: pystac.Collection
):
    collection = tmp_planet_disaster
    collection_path = collection.get_self_href()
    items = list(collection.get_items(recursive=True))
    assert len(items) == 5
    item_before = pystac.read_file(item_path)

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "add",
            item_path,
            collection_path,
            "--collection",
            "hurricane-harvey",
        ],
    )
    assert result.exit_code == 0

    collection_after = pystac.read_file(collection_path)
    items_after = collection_after.get_child("hurricane-harvey").get_items()
    assert any(item.id == item_before.id for item in items_after)


def test_add_item_to_missing_collection(
    item_path: str, tmp_planet_disaster: pystac.Collection
):
    collection = tmp_planet_disaster
    collection_path = collection.get_self_href()
    items = list(collection.get_items(recursive=True))
    assert len(items) == 5

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "add",
            item_path,
            collection_path,
            "--collection",
            "WRONG",
        ],
    )
    assert result.exit_code == 2
    assert " A collection with ID WRONG does not exist" in result.output