From 2190628e82b28cb793859a1942910b03d879790e Mon Sep 17 00:00:00 2001 From: Rhet Turnbull Date: Sat, 24 Jun 2023 10:50:34 -0700 Subject: [PATCH] Implemented --count, #1098 (#1102) --- osxphotos/cli/query.py | 6 ++++++ tests/test_cli.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/osxphotos/cli/query.py b/osxphotos/cli/query.py index 6c3f1cef..b5b2ea12 100644 --- a/osxphotos/cli/query.py +++ b/osxphotos/cli/query.py @@ -52,6 +52,7 @@ MACOS_OPTIONS = make_click_option_decorator( @click.command() @DB_OPTION @JSON_OPTION +@click.option("--count", is_flag=True, help="Print count of photos matching query and exit.") @QUERY_OPTIONS @DELETED_OPTIONS @MACOS_OPTIONS @@ -82,6 +83,7 @@ def query( db, field, json_, + count, print_template, quiet, photos_library, @@ -137,6 +139,10 @@ def query( # below needed for to make CliRunner work for testing cli_json = cli_obj.json if cli_obj is not None else None + if count: + click.echo(len(photos)) + return + if add_to_album and photos: assert_macos() diff --git a/tests/test_cli.py b/tests/test_cli.py index a124470a..941f6623 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -8176,6 +8176,46 @@ def test_query_added_in_last(): assert results.exit_code == 0 +def test_query_count(): + """Test query --count""" + + runner = CliRunner() + cwd = os.getcwd() + # pylint: disable=not-context-manager + results = runner.invoke( + query, + [ + "--library", + os.path.join(cwd, PHOTOS_DB_15_7), + "--added-before", + "2019-07-28", + "--count", + ], + ) + assert results.exit_code == 0 + assert results.output.strip() == "7" + + +def test_query_count_0(): + """Test query --count with zero results""" + + runner = CliRunner() + cwd = os.getcwd() + # pylint: disable=not-context-manager + results = runner.invoke( + query, + [ + "--library", + os.path.join(cwd, PHOTOS_DB_15_7), + "--added-before", + "1900-01-01", + "--count", + ], + ) + assert results.exit_code == 0 + assert results.output.strip() == "0" + + def test_export_export_dir_template(): """Test {export_dir} template"""