From 7501911c4f211af5faf97308bd4f5b6f3718793c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= <remi.cresson@inrae.fr> Date: Thu, 6 Feb 2025 14:50:00 +0100 Subject: [PATCH 1/2] fix cli bug + add cli tests --- tests/test_cli.py | 43 ++++++++++++++++++++++++++++++++++++++++ theia_dumper/__init__.py | 2 +- theia_dumper/cli.py | 16 +++++++-------- 3 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 tests/test_cli.py diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..ae5c27c --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,43 @@ +"""Test CLI.""" + +from click.testing import CliRunner +from theia_dumper.cli import list_cols, list_col_items, grab, collection_diff + + +def _test_cli(command, args=None): + """Test a CLI command.""" + print(f"Testing {command}") + runner = CliRunner() + result = runner.invoke(command, args) + print(result) + assert result.exit_code == 0 + + +_test_cli(list_cols) +_test_cli(list_col_items, ["--col_id", "spot-6-7-drs"]) +_test_cli(grab, ["--col_id", "spot-6-7-drs", "--out_json", "/tmp/col.json"]) +_test_cli(grab, ["--col_id", "spot-6-7-drs", "--out_json", "/tmp/col.json", "--pretty"]) +_test_cli( + grab, + [ + "--col_id", + "spot-6-7-drs", + "--item_id", + "SPOT7_MS_201805051026429_SPOT7_P_201805051026429_1", + "--out_json", + "/tmp/item.json", + ], +) +_test_cli( + grab, + [ + "--col_id", + "spot-6-7-drs", + "--item_id", + "SPOT7_MS_201805051026429_SPOT7_P_201805051026429_1", + "--out_json", + "/tmp/item.json", + "--pretty", + ], +) +_test_cli(collection_diff, ["--remote_id", "spot-6-7-drs", "--col_path", "/tmp/col.json"]) diff --git a/theia_dumper/__init__.py b/theia_dumper/__init__.py index 4ffdd60..dcd6905 100644 --- a/theia_dumper/__init__.py +++ b/theia_dumper/__init__.py @@ -1,3 +1,3 @@ """Theia dumper package.""" -__version__ = "0.1.2" +__version__ = "0.2.0" diff --git a/theia_dumper/cli.py b/theia_dumper/cli.py index e1a0351..334880c 100644 --- a/theia_dumper/cli.py +++ b/theia_dumper/cli.py @@ -106,9 +106,9 @@ def edit(stac_endpoint: str, col_id: str, item_id: str): ) editor = os.environ.get("EDITOR") or "vi" subprocess.run([editor, tf.name], check=False) - StacTransactionsHandler( - stac_endpoint=stac_endpoint, sign=False - ).load_and_publish(obj_pth=tf.name) + StacTransactionsHandler(stac_endpoint=stac_endpoint, sign=False).load_and_publish( + obj_pth=tf.name + ) @theia_dumper.command(context_settings={"show_default": True}) @@ -142,9 +142,9 @@ def list_cols( stac_endpoint: str, ): """List collections.""" - cols = StacTransactionsHandler( - stac_endpoint=stac_endpoint, sign=False - ).client.get_collections() + cols = list( + StacTransactionsHandler(stac_endpoint=stac_endpoint, sign=False).client.get_collections() + ) print(f"Found {len(cols)} collection(s):") for col in sorted(cols, key=lambda x: x.id): print(f"\t{col.id}") @@ -158,9 +158,7 @@ def list_cols( default=DEFAULT_STAC_EP, ) @click.option("-c", "--col_id", type=str, help="STAC collection ID", required=True) -@click.option( - "-m", "--max_items", type=int, help="Max number of items to display", default=20 -) +@click.option("-m", "--max_items", type=int, help="Max number of items to display", default=20) @click.option("-s", "--sign", is_flag=True, default=False, help="Sign assets HREFs") def list_col_items(stac_endpoint: str, col_id: str, max_items: int, sign: bool): """List collection items.""" -- GitLab From 336ce57791610795b72f390bb8cb11d1e248b07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= <remi.cresson@inrae.fr> Date: Thu, 6 Feb 2025 14:51:31 +0100 Subject: [PATCH 2/2] fix cli bug + add cli tests --- tests/test_cli.py | 4 +++- theia_dumper/cli.py | 14 +++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index ae5c27c..97c707d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -40,4 +40,6 @@ _test_cli( "--pretty", ], ) -_test_cli(collection_diff, ["--remote_id", "spot-6-7-drs", "--col_path", "/tmp/col.json"]) +_test_cli( + collection_diff, ["--remote_id", "spot-6-7-drs", "--col_path", "/tmp/col.json"] +) diff --git a/theia_dumper/cli.py b/theia_dumper/cli.py index 334880c..6902de8 100644 --- a/theia_dumper/cli.py +++ b/theia_dumper/cli.py @@ -106,9 +106,9 @@ def edit(stac_endpoint: str, col_id: str, item_id: str): ) editor = os.environ.get("EDITOR") or "vi" subprocess.run([editor, tf.name], check=False) - StacTransactionsHandler(stac_endpoint=stac_endpoint, sign=False).load_and_publish( - obj_pth=tf.name - ) + StacTransactionsHandler( + stac_endpoint=stac_endpoint, sign=False + ).load_and_publish(obj_pth=tf.name) @theia_dumper.command(context_settings={"show_default": True}) @@ -143,7 +143,9 @@ def list_cols( ): """List collections.""" cols = list( - StacTransactionsHandler(stac_endpoint=stac_endpoint, sign=False).client.get_collections() + StacTransactionsHandler( + stac_endpoint=stac_endpoint, sign=False + ).client.get_collections() ) print(f"Found {len(cols)} collection(s):") for col in sorted(cols, key=lambda x: x.id): @@ -158,7 +160,9 @@ def list_cols( default=DEFAULT_STAC_EP, ) @click.option("-c", "--col_id", type=str, help="STAC collection ID", required=True) -@click.option("-m", "--max_items", type=int, help="Max number of items to display", default=20) +@click.option( + "-m", "--max_items", type=int, help="Max number of items to display", default=20 +) @click.option("-s", "--sign", is_flag=True, default=False, help="Sign assets HREFs") def list_col_items(stac_endpoint: str, col_id: str, max_items: int, sign: bool): """List collection items.""" -- GitLab