-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to pytest and deprecate
CliTestCase
(#447)
- Loading branch information
Showing
24 changed files
with
845 additions
and
949 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,80 @@ | ||
from tempfile import TemporaryDirectory | ||
|
||
import pystac | ||
from stactools.cli.commands.add import create_add_command | ||
from stactools.testing import CliTestCase | ||
|
||
from tests.utils import create_planet_disaster_clone | ||
|
||
from .test_cases import TestCases | ||
|
||
|
||
class AddTest(CliTestCase): | ||
def create_subcommand_functions(self): | ||
return [create_add_command] | ||
|
||
def test_add_item(self): | ||
catalog = TestCases.basic_catalog() | ||
subcatalog = list(list(catalog.get_children())[0].get_children())[0] | ||
item = list(subcatalog.get_all_items())[0] | ||
item_path = item.get_self_href() | ||
with TemporaryDirectory() as tmp_dir: | ||
target_catalog = create_planet_disaster_clone(tmp_dir) | ||
|
||
items = list(target_catalog.get_all_items()) | ||
self.assertEqual(len(items), 5) | ||
|
||
cmd = ["add", item_path, target_catalog.get_self_href()] | ||
|
||
self.run_command(cmd) | ||
|
||
target_col = pystac.read_file(target_catalog.get_self_href()) | ||
items = list(target_col.get_all_items()) | ||
self.assertEqual(len(items), 6) | ||
|
||
def test_add_item_to_specific_collection(self): | ||
catalog = TestCases.basic_catalog() | ||
subcatalog = list(list(catalog.get_children())[0].get_children())[0] | ||
item = list(subcatalog.get_all_items())[0] | ||
item_path = item.get_self_href() | ||
with TemporaryDirectory() as tmp_dir: | ||
target_catalog = create_planet_disaster_clone(tmp_dir) | ||
items = list(target_catalog.get_all_items()) | ||
self.assertEqual(len(items), 5) | ||
|
||
cmd = [ | ||
"add", | ||
item_path, | ||
target_catalog.get_self_href(), | ||
"--collection", | ||
"hurricane-harvey", | ||
] | ||
|
||
res = self.run_command(cmd) | ||
self.assertEqual(res.exit_code, 0) | ||
|
||
target_col = pystac.read_file(target_catalog.get_self_href()) | ||
child_col = target_col.get_child("hurricane-harvey") | ||
target_item = child_col.get_item(item.id) | ||
self.assertIsNotNone(target_item) | ||
|
||
def test_add_item_to_missing_collection(self): | ||
catalog = TestCases.basic_catalog() | ||
subcatalog = list(list(catalog.get_children())[0].get_children())[0] | ||
item = list(subcatalog.get_all_items())[0] | ||
item_path = item.get_self_href() | ||
with TemporaryDirectory() as tmp_dir: | ||
target_catalog = create_planet_disaster_clone(tmp_dir) | ||
|
||
items = list(target_catalog.get_all_items()) | ||
self.assertEqual(len(items), 5) | ||
|
||
cmd = [ | ||
"add", | ||
item_path, | ||
target_catalog.get_self_href(), | ||
"--collection", | ||
"WRONG", | ||
] | ||
|
||
res = self.run_command(cmd) | ||
self.assertEqual(res.exit_code, 2) | ||
self.assertTrue(" A collection with ID WRONG does not exist" in res.output) | ||
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_all_items()) | ||
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_all_items()) | ||
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_all_items()) | ||
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_all_items()) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,60 @@ | ||
import os | ||
from tempfile import TemporaryDirectory | ||
from typing import Callable, List | ||
|
||
import pystac | ||
import pystac.utils | ||
from click import Command, Group | ||
from pystac import Item | ||
from stactools.cli.commands.add_asset import create_add_asset_command | ||
from stactools.testing.cli_test import CliTestCase | ||
from click.testing import CliRunner | ||
from stactools.cli.cli import cli | ||
|
||
from tests import test_data | ||
from tests.utils import create_temp_copy | ||
|
||
|
||
class AddAssetTest(CliTestCase): | ||
def create_subcommand_functions(self) -> List[Callable[[Group], Command]]: | ||
return [create_add_asset_command] | ||
|
||
def test_add_asset_to_item(self) -> None: | ||
with TemporaryDirectory() as tmp_dir: | ||
item_path = create_temp_copy( | ||
test_data.get_path("data-files/core/simple-item.json"), | ||
tmp_dir, | ||
"item.json", | ||
) | ||
item = Item.from_file(item_path) | ||
assert "test-asset" not in item.assets | ||
|
||
asset_path = test_data.get_path("data-files/core/byte.tif") | ||
cmd = [ | ||
"add-asset", | ||
item_path, | ||
"test-asset", | ||
asset_path, | ||
"--title", | ||
"test", | ||
"--description", | ||
"placeholder asset", | ||
"--role", | ||
"thumbnail", | ||
"--role", | ||
"overview", | ||
] | ||
res = self.run_command(cmd) | ||
self.assertEqual(res.exit_code, 0) | ||
|
||
item = Item.from_file(item_path) | ||
asset = item.assets["test-asset"] | ||
assert isinstance(asset, pystac.Asset), asset | ||
assert asset.href is not None, asset.to_dict() | ||
assert os.path.isfile(asset.href), asset.to_dict() | ||
assert asset.title == "test", asset.to_dict() | ||
assert asset.description == "placeholder asset", asset.to_dict() | ||
assert asset.roles | ||
self.assertListEqual(asset.roles, ["thumbnail", "overview"]) | ||
|
||
def test_add_asset_to_item_with_relative_paths(self) -> None: | ||
with TemporaryDirectory() as tmp_dir: | ||
item_path = create_temp_copy( | ||
test_data.get_path("data-files/core/simple-item.json"), | ||
tmp_dir, | ||
"item.json", | ||
) | ||
asset_path = test_data.get_path("data-files/core/byte.tif") | ||
cmd = [ | ||
"add-asset", | ||
pystac.utils.make_relative_href( | ||
item_path, os.getcwd(), start_is_dir=True | ||
), | ||
"test-asset", | ||
pystac.utils.make_relative_href( | ||
asset_path, os.getcwd(), start_is_dir=True | ||
), | ||
] | ||
result = self.run_command(cmd) | ||
self.assertEqual(result.exit_code, 0) | ||
def test_add_asset_to_item(tmp_item_path: str) -> None: | ||
asset_path = test_data.get_path("data-files/core/byte.tif") | ||
item_path = tmp_item_path | ||
item = pystac.Item.from_file(item_path) | ||
assert "test-asset" not in item.assets | ||
|
||
runner = CliRunner() | ||
result = runner.invoke( | ||
cli, | ||
[ | ||
"add-asset", | ||
item_path, | ||
"test-asset", | ||
asset_path, | ||
"--title", | ||
"test", | ||
"--description", | ||
"placeholder asset", | ||
"--role", | ||
"thumbnail", | ||
"--role", | ||
"overview", | ||
], | ||
) | ||
assert result.exit_code == 0 | ||
|
||
item = pystac.Item.from_file(item_path) | ||
asset = item.assets["test-asset"] | ||
assert isinstance(asset, pystac.Asset), asset | ||
assert asset.href is not None, asset.to_dict() | ||
assert os.path.isfile(asset.href), asset.to_dict() | ||
assert asset.title == "test", asset.to_dict() | ||
assert asset.description == "placeholder asset", asset.to_dict() | ||
assert asset.roles == ["thumbnail", "overview"] | ||
|
||
|
||
def test_add_asset_to_item_with_relative_paths(tmp_item_path: str) -> None: | ||
asset_path = test_data.get_path("data-files/core/byte.tif") | ||
item_path = tmp_item_path | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
cli, | ||
[ | ||
"add-asset", | ||
pystac.utils.make_relative_href(item_path, os.getcwd(), start_is_dir=True), | ||
"test-asset", | ||
pystac.utils.make_relative_href(asset_path, os.getcwd(), start_is_dir=True), | ||
], | ||
) | ||
assert result.exit_code == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,27 @@ | ||
from tempfile import TemporaryDirectory | ||
|
||
import pystac | ||
from pystac.utils import make_absolute_href | ||
from stactools.cli.commands.add_raster import create_add_raster_command | ||
from stactools.testing import CliTestCase | ||
|
||
from tests.utils import create_planet_disaster_clone | ||
|
||
from .cli_test_utils import expected_json | ||
|
||
|
||
class AddRasterTest(CliTestCase): | ||
def create_subcommand_functions(self): | ||
return [create_add_raster_command] | ||
|
||
def test_add_raster_to_item(self): | ||
with TemporaryDirectory() as tmp_dir: | ||
catalog = create_planet_disaster_clone(tmp_dir) | ||
items = list(catalog.get_all_items()) | ||
item_path = make_absolute_href( | ||
items[0].get_self_href(), catalog.get_self_href() | ||
) | ||
|
||
cmd = ["add-raster", item_path] | ||
self.run_command(cmd) | ||
|
||
updated = pystac.read_file(catalog.get_self_href()) | ||
item = list(updated.get_all_items())[0] | ||
asset = item.get_assets().get("analytic") | ||
assert asset is not None | ||
expected = expected_json("rasterbands.json") | ||
self.maxDiff = None | ||
for a, b in zip(expected, asset.to_dict().get("raster:bands")): | ||
self.assertDictEqual(a, b) | ||
import pystac.utils | ||
from click.testing import CliRunner | ||
from stactools.cli.cli import cli | ||
|
||
from tests.conftest import expected_json | ||
|
||
|
||
def test_add_raster_to_items(tmp_planet_disaster: pystac.Collection): | ||
collection = tmp_planet_disaster | ||
collection_path = collection.get_self_href() | ||
items = list(collection.get_all_items()) | ||
item_path = pystac.utils.make_absolute_href( | ||
items[0].get_self_href(), collection_path | ||
) | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(cli, ["add-raster", item_path]) | ||
assert result.exit_code == 0 | ||
|
||
updated = pystac.read_file(collection_path) | ||
item = list(updated.get_all_items())[0] | ||
asset = item.get_assets().get("analytic") | ||
assert asset is not None | ||
expected = expected_json("rasterbands.json") | ||
for a, b in zip(expected, asset.to_dict().get("raster:bands")): | ||
assert a == b |
Oops, something went wrong.