diff --git a/osxphotos/__main__.py b/osxphotos/__main__.py index f5d989a7..dd8025d7 100644 --- a/osxphotos/__main__.py +++ b/osxphotos/__main__.py @@ -565,7 +565,9 @@ def query( if only_photos: ismovie = False - db = get_photos_db(*photos_library, db, cli_obj.db) + # below needed for to make CliRunner work for testing + cli_db = cli_obj.db if cli_obj is not None else None + db = get_photos_db(*photos_library, db, cli_db) if db is None: click.echo(cli.commands["query"].get_help(ctx), err=True) click.echo("\n\nLocated the following Photos library databases: ", err=True) @@ -605,7 +607,10 @@ def query( incloud=incloud, not_incloud=not_incloud, ) - print_photo_info(photos, cli_obj.json or json_) + + # below needed for to make CliRunner work for testing + cli_json = cli_obj.json if cli_obj is not None else None + print_photo_info(photos, cli_json or json_) @cli.command() diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 00000000..9fb0cb74 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,70 @@ +import pytest +from click.testing import CliRunner + +CLI_OUTPUT_NO_SUBCOMMAND = [ + "Options:", + "--db Specify Photos database path. Path to Photos", + "library/database can be specified using either", + "--db or directly as PHOTOS_LIBRARY positional", + "argument.", + "--json Print output in JSON format.", + "-v, --version Show the version and exit.", + "-h, --help Show this message and exit.", + "Commands:", + " albums Print out albums found in the Photos library.", + " dump Print list of all photos & associated info from the Photos", + " export Export photos from the Photos database.", + " help Print help; for help on commands: help .", + " info Print out descriptive info of the Photos library database.", + " keywords Print out keywords found in the Photos library.", + " list Print list of Photos libraries found on the system.", + " persons Print out persons (faces) found in the Photos library.", + " query Query the Photos database using 1 or more search options; if", +] + +CLI_OUTPUT_QUERY_UUID = '[{"uuid": "D79B8D77-BFFC-460B-9312-034F2877D35B", "filename": "D79B8D77-BFFC-460B-9312-034F2877D35B.jpeg", "original_filename": "Pumkins2.jpg", "date": "2018-09-28T16:07:07-04:00", "description": "Girl holding pumpkin", "title": "I found one!", "keywords": ["Kids"], "albums": ["Pumpkin Farm", "Test Album"], "persons": ["Katie"], "path": "/tests/Test-10.15.1.photoslibrary/originals/D/D79B8D77-BFFC-460B-9312-034F2877D35B.jpeg", "ismissing": false, "hasadjustments": false, "external_edit": false, "favorite": false, "hidden": false, "latitude": null, "longitude": null, "path_edited": null, "shared": false, "isphoto": true, "ismovie": false, "uti": "public.jpeg", "burst": false, "live_photo": false, "path_live_photo": null, "iscloudasset": false, "incloud": null}]' + + +def test_osxphotos(): + import osxphotos + from osxphotos.__main__ import cli + + runner = CliRunner() + result = runner.invoke(cli, []) + output = result.output + assert result.exit_code == 0 + for line in CLI_OUTPUT_NO_SUBCOMMAND: + assert line in output + + +def test_query_uuid(): + import json + import osxphotos + from osxphotos.__main__ import query + + runner = CliRunner() + result = runner.invoke( + query, + [ + "--json", + "--db", + "./tests/Test-10.15.1.photoslibrary", + "--uuid", + "D79B8D77-BFFC-460B-9312-034F2877D35B", + ], + ) + assert result.exit_code == 0 + + json_expected = json.loads(CLI_OUTPUT_QUERY_UUID)[0] + json_got = json.loads(result.output)[0] + + assert list(json_expected.keys()).sort() == list(json_got.keys()).sort() + + # check values expected vs got + # path needs special handling as path is set to full path which will differ system to system + for key_ in json_expected: + assert key_ in json_got + if key_ != "path": + assert json_expected[key_] == json_got[key_] + else: + assert json_expected[key_] in json_got[key_]