Skip to content

Commit c2c40a1

Browse files
authored
Move to pytest and deprecate CliTestCase (#447)
1 parent 8100450 commit c2c40a1

24 files changed

+845
-949
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222

2323
### Deprecated
2424

25+
- Many functions in `stactools.testing.CliTestCase` ([#447](https://github.com/stac-utils/stactools/pull/447)).
2526
- `raster_footprint.reproject_polygon` and `projection.reproject_geom`. Use `projection.reproject_shape` instead with `shapely.Geometry` objects as the input and output ([#441](https://github.com/stac-utils/stactools/pull/441))
2627

2728
### Removed

src/stactools/testing/cli_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import unittest
3+
import warnings
34
from abc import ABC, abstractmethod
45
from typing import Callable, List, Optional, Sequence, Union
56

@@ -29,6 +30,11 @@ def setUp(self) -> None:
2930
def cli() -> None:
3031
pass
3132

33+
warnings.warn(
34+
"CliTestCase is deprecated in v0.5.0 and will be removed in v0.6.0. "
35+
"Please use `click.testing.CliRunner` instead",
36+
DeprecationWarning,
37+
)
3238
for create_subcommand in self.create_subcommand_functions():
3339
create_subcommand(cli)
3440
self.cli = cli

tests/cli/commands/cli_test_utils.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/cli/commands/test_add.py

Lines changed: 79 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,80 @@
1-
from tempfile import TemporaryDirectory
2-
31
import pystac
4-
from stactools.cli.commands.add import create_add_command
5-
from stactools.testing import CliTestCase
6-
7-
from tests.utils import create_planet_disaster_clone
8-
9-
from .test_cases import TestCases
10-
11-
12-
class AddTest(CliTestCase):
13-
def create_subcommand_functions(self):
14-
return [create_add_command]
15-
16-
def test_add_item(self):
17-
catalog = TestCases.basic_catalog()
18-
subcatalog = list(list(catalog.get_children())[0].get_children())[0]
19-
item = list(subcatalog.get_all_items())[0]
20-
item_path = item.get_self_href()
21-
with TemporaryDirectory() as tmp_dir:
22-
target_catalog = create_planet_disaster_clone(tmp_dir)
23-
24-
items = list(target_catalog.get_all_items())
25-
self.assertEqual(len(items), 5)
26-
27-
cmd = ["add", item_path, target_catalog.get_self_href()]
28-
29-
self.run_command(cmd)
30-
31-
target_col = pystac.read_file(target_catalog.get_self_href())
32-
items = list(target_col.get_all_items())
33-
self.assertEqual(len(items), 6)
34-
35-
def test_add_item_to_specific_collection(self):
36-
catalog = TestCases.basic_catalog()
37-
subcatalog = list(list(catalog.get_children())[0].get_children())[0]
38-
item = list(subcatalog.get_all_items())[0]
39-
item_path = item.get_self_href()
40-
with TemporaryDirectory() as tmp_dir:
41-
target_catalog = create_planet_disaster_clone(tmp_dir)
42-
items = list(target_catalog.get_all_items())
43-
self.assertEqual(len(items), 5)
44-
45-
cmd = [
46-
"add",
47-
item_path,
48-
target_catalog.get_self_href(),
49-
"--collection",
50-
"hurricane-harvey",
51-
]
52-
53-
res = self.run_command(cmd)
54-
self.assertEqual(res.exit_code, 0)
55-
56-
target_col = pystac.read_file(target_catalog.get_self_href())
57-
child_col = target_col.get_child("hurricane-harvey")
58-
target_item = child_col.get_item(item.id)
59-
self.assertIsNotNone(target_item)
60-
61-
def test_add_item_to_missing_collection(self):
62-
catalog = TestCases.basic_catalog()
63-
subcatalog = list(list(catalog.get_children())[0].get_children())[0]
64-
item = list(subcatalog.get_all_items())[0]
65-
item_path = item.get_self_href()
66-
with TemporaryDirectory() as tmp_dir:
67-
target_catalog = create_planet_disaster_clone(tmp_dir)
68-
69-
items = list(target_catalog.get_all_items())
70-
self.assertEqual(len(items), 5)
71-
72-
cmd = [
73-
"add",
74-
item_path,
75-
target_catalog.get_self_href(),
76-
"--collection",
77-
"WRONG",
78-
]
79-
80-
res = self.run_command(cmd)
81-
self.assertEqual(res.exit_code, 2)
82-
self.assertTrue(" A collection with ID WRONG does not exist" in res.output)
2+
import pystac.utils
3+
import pytest
4+
from click.testing import CliRunner
5+
from stactools.cli.cli import cli
6+
7+
from tests import test_data
8+
9+
10+
@pytest.fixture(scope="module")
11+
def item_path() -> str:
12+
return test_data.get_path(
13+
"data-files/basic/country-1/area-1-1"
14+
"/area-1-1-imagery/area-1-1-imagery-invalid.json"
15+
)
16+
17+
18+
def test_add_item(item_path: str, tmp_planet_disaster: pystac.Collection):
19+
collection = tmp_planet_disaster
20+
collection_path = collection.get_self_href()
21+
items = list(collection.get_all_items())
22+
assert len(items) == 5
23+
24+
runner = CliRunner()
25+
result = runner.invoke(cli, ["add", item_path, collection_path])
26+
assert result.exit_code == 0
27+
28+
collection_after = pystac.read_file(collection_path)
29+
items = list(collection_after.get_all_items())
30+
assert len(items) == 6
31+
32+
33+
def test_add_item_to_specific_collection(
34+
item_path: str, tmp_planet_disaster: pystac.Collection
35+
):
36+
collection = tmp_planet_disaster
37+
collection_path = collection.get_self_href()
38+
items = list(collection.get_all_items())
39+
assert len(items) == 5
40+
item_before = pystac.read_file(item_path)
41+
42+
runner = CliRunner()
43+
result = runner.invoke(
44+
cli,
45+
[
46+
"add",
47+
item_path,
48+
collection_path,
49+
"--collection",
50+
"hurricane-harvey",
51+
],
52+
)
53+
assert result.exit_code == 0
54+
55+
collection_after = pystac.read_file(collection_path)
56+
items_after = collection_after.get_child("hurricane-harvey").get_items()
57+
assert any(item.id == item_before.id for item in items_after)
58+
59+
60+
def test_add_item_to_missing_collection(
61+
item_path: str, tmp_planet_disaster: pystac.Collection
62+
):
63+
collection = tmp_planet_disaster
64+
collection_path = collection.get_self_href()
65+
items = list(collection.get_all_items())
66+
assert len(items) == 5
67+
68+
runner = CliRunner()
69+
result = runner.invoke(
70+
cli,
71+
[
72+
"add",
73+
item_path,
74+
collection_path,
75+
"--collection",
76+
"WRONG",
77+
],
78+
)
79+
assert result.exit_code == 2
80+
assert " A collection with ID WRONG does not exist" in result.output
Lines changed: 52 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,60 @@
11
import os
2-
from tempfile import TemporaryDirectory
3-
from typing import Callable, List
42

53
import pystac
64
import pystac.utils
7-
from click import Command, Group
8-
from pystac import Item
9-
from stactools.cli.commands.add_asset import create_add_asset_command
10-
from stactools.testing.cli_test import CliTestCase
5+
from click.testing import CliRunner
6+
from stactools.cli.cli import cli
117

128
from tests import test_data
13-
from tests.utils import create_temp_copy
149

1510

16-
class AddAssetTest(CliTestCase):
17-
def create_subcommand_functions(self) -> List[Callable[[Group], Command]]:
18-
return [create_add_asset_command]
19-
20-
def test_add_asset_to_item(self) -> None:
21-
with TemporaryDirectory() as tmp_dir:
22-
item_path = create_temp_copy(
23-
test_data.get_path("data-files/core/simple-item.json"),
24-
tmp_dir,
25-
"item.json",
26-
)
27-
item = Item.from_file(item_path)
28-
assert "test-asset" not in item.assets
29-
30-
asset_path = test_data.get_path("data-files/core/byte.tif")
31-
cmd = [
32-
"add-asset",
33-
item_path,
34-
"test-asset",
35-
asset_path,
36-
"--title",
37-
"test",
38-
"--description",
39-
"placeholder asset",
40-
"--role",
41-
"thumbnail",
42-
"--role",
43-
"overview",
44-
]
45-
res = self.run_command(cmd)
46-
self.assertEqual(res.exit_code, 0)
47-
48-
item = Item.from_file(item_path)
49-
asset = item.assets["test-asset"]
50-
assert isinstance(asset, pystac.Asset), asset
51-
assert asset.href is not None, asset.to_dict()
52-
assert os.path.isfile(asset.href), asset.to_dict()
53-
assert asset.title == "test", asset.to_dict()
54-
assert asset.description == "placeholder asset", asset.to_dict()
55-
assert asset.roles
56-
self.assertListEqual(asset.roles, ["thumbnail", "overview"])
57-
58-
def test_add_asset_to_item_with_relative_paths(self) -> None:
59-
with TemporaryDirectory() as tmp_dir:
60-
item_path = create_temp_copy(
61-
test_data.get_path("data-files/core/simple-item.json"),
62-
tmp_dir,
63-
"item.json",
64-
)
65-
asset_path = test_data.get_path("data-files/core/byte.tif")
66-
cmd = [
67-
"add-asset",
68-
pystac.utils.make_relative_href(
69-
item_path, os.getcwd(), start_is_dir=True
70-
),
71-
"test-asset",
72-
pystac.utils.make_relative_href(
73-
asset_path, os.getcwd(), start_is_dir=True
74-
),
75-
]
76-
result = self.run_command(cmd)
77-
self.assertEqual(result.exit_code, 0)
11+
def test_add_asset_to_item(tmp_item_path: str) -> None:
12+
asset_path = test_data.get_path("data-files/core/byte.tif")
13+
item_path = tmp_item_path
14+
item = pystac.Item.from_file(item_path)
15+
assert "test-asset" not in item.assets
16+
17+
runner = CliRunner()
18+
result = runner.invoke(
19+
cli,
20+
[
21+
"add-asset",
22+
item_path,
23+
"test-asset",
24+
asset_path,
25+
"--title",
26+
"test",
27+
"--description",
28+
"placeholder asset",
29+
"--role",
30+
"thumbnail",
31+
"--role",
32+
"overview",
33+
],
34+
)
35+
assert result.exit_code == 0
36+
37+
item = pystac.Item.from_file(item_path)
38+
asset = item.assets["test-asset"]
39+
assert isinstance(asset, pystac.Asset), asset
40+
assert asset.href is not None, asset.to_dict()
41+
assert os.path.isfile(asset.href), asset.to_dict()
42+
assert asset.title == "test", asset.to_dict()
43+
assert asset.description == "placeholder asset", asset.to_dict()
44+
assert asset.roles == ["thumbnail", "overview"]
45+
46+
47+
def test_add_asset_to_item_with_relative_paths(tmp_item_path: str) -> None:
48+
asset_path = test_data.get_path("data-files/core/byte.tif")
49+
item_path = tmp_item_path
50+
runner = CliRunner()
51+
result = runner.invoke(
52+
cli,
53+
[
54+
"add-asset",
55+
pystac.utils.make_relative_href(item_path, os.getcwd(), start_is_dir=True),
56+
"test-asset",
57+
pystac.utils.make_relative_href(asset_path, os.getcwd(), start_is_dir=True),
58+
],
59+
)
60+
assert result.exit_code == 0
Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
1-
from tempfile import TemporaryDirectory
2-
31
import pystac
4-
from pystac.utils import make_absolute_href
5-
from stactools.cli.commands.add_raster import create_add_raster_command
6-
from stactools.testing import CliTestCase
7-
8-
from tests.utils import create_planet_disaster_clone
9-
10-
from .cli_test_utils import expected_json
11-
12-
13-
class AddRasterTest(CliTestCase):
14-
def create_subcommand_functions(self):
15-
return [create_add_raster_command]
16-
17-
def test_add_raster_to_item(self):
18-
with TemporaryDirectory() as tmp_dir:
19-
catalog = create_planet_disaster_clone(tmp_dir)
20-
items = list(catalog.get_all_items())
21-
item_path = make_absolute_href(
22-
items[0].get_self_href(), catalog.get_self_href()
23-
)
24-
25-
cmd = ["add-raster", item_path]
26-
self.run_command(cmd)
27-
28-
updated = pystac.read_file(catalog.get_self_href())
29-
item = list(updated.get_all_items())[0]
30-
asset = item.get_assets().get("analytic")
31-
assert asset is not None
32-
expected = expected_json("rasterbands.json")
33-
self.maxDiff = None
34-
for a, b in zip(expected, asset.to_dict().get("raster:bands")):
35-
self.assertDictEqual(a, b)
2+
import pystac.utils
3+
from click.testing import CliRunner
4+
from stactools.cli.cli import cli
5+
6+
from tests.conftest import expected_json
7+
8+
9+
def test_add_raster_to_items(tmp_planet_disaster: pystac.Collection):
10+
collection = tmp_planet_disaster
11+
collection_path = collection.get_self_href()
12+
items = list(collection.get_all_items())
13+
item_path = pystac.utils.make_absolute_href(
14+
items[0].get_self_href(), collection_path
15+
)
16+
17+
runner = CliRunner()
18+
result = runner.invoke(cli, ["add-raster", item_path])
19+
assert result.exit_code == 0
20+
21+
updated = pystac.read_file(collection_path)
22+
item = list(updated.get_all_items())[0]
23+
asset = item.get_assets().get("analytic")
24+
assert asset is not None
25+
expected = expected_json("rasterbands.json")
26+
for a, b in zip(expected, asset.to_dict().get("raster:bands")):
27+
assert a == b

0 commit comments

Comments
 (0)