Skip to content

Commit

Permalink
Move to pytest and deprecate CliTestCase (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsignell authored Jun 29, 2023
1 parent 8100450 commit c2c40a1
Show file tree
Hide file tree
Showing 24 changed files with 845 additions and 949 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Deprecated

- Many functions in `stactools.testing.CliTestCase` ([#447](https://github.com/stac-utils/stactools/pull/447)).
- `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))

### Removed
Expand Down
6 changes: 6 additions & 0 deletions src/stactools/testing/cli_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import unittest
import warnings
from abc import ABC, abstractmethod
from typing import Callable, List, Optional, Sequence, Union

Expand Down Expand Up @@ -29,6 +30,11 @@ def setUp(self) -> None:
def cli() -> None:
pass

warnings.warn(
"CliTestCase is deprecated in v0.5.0 and will be removed in v0.6.0. "
"Please use `click.testing.CliRunner` instead",
DeprecationWarning,
)
for create_subcommand in self.create_subcommand_functions():
create_subcommand(cli)
self.cli = cli
Expand Down
8 changes: 0 additions & 8 deletions tests/cli/commands/cli_test_utils.py

This file was deleted.

160 changes: 79 additions & 81 deletions tests/cli/commands/test_add.py
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
121 changes: 52 additions & 69 deletions tests/cli/commands/test_add_asset.py
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
60 changes: 26 additions & 34 deletions tests/cli/commands/test_add_raster.py
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
Loading

0 comments on commit c2c40a1

Please sign in to comment.