osxphotos/tests/test_cli_sync.py
Rhet Turnbull c383212822
Feature add sync command 887 (#921)
* Starting work on sync command, #887

* Added parsing for --set, --merge

* Added query options

* Added --incloud, --not-incloud, --not-missing, --cloudasset, --not-cloudasset to query options, #800 (#902)

* Got basic import logic working

* Got basic set/merge logic working

* add to album now working

* Resolve paths for --import

* Refactored report writer to reuse code from export report

* Removed report_writer_sync.py

* add oPromessa as a contributor for code (#914)

* update README.md [skip ci]

* update .all-contributorsrc [skip ci]

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>

* Added --profile, --watch, --breakpoint, --debug as global options (#917)

* Removed unnecessary import

* Release 0.56.0 (#918)

* Release 0 56 0 (#919)

* Release 0.56.0

* Release 0.56.0

* Updated CHANGELOG.md [skip ci]

* Got CSV reporting, summary results done

* Added json report for sync results

* Added sqlite report for sync

* Basic set/merge working for sync

* sync mvp working

* Added help text for sync

* Added test for sync

* Updated tests for sync

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2023-01-14 22:06:20 -08:00

109 lines
3.3 KiB
Python

"""Test osxphotos sync command"""
import os
import json
import photoscript
import pytest
from click.testing import CliRunner
from osxphotos.cli.sync import sync
UUID_TEST_PHOTO_1 = "D79B8D77-BFFC-460B-9312-034F2877D35B" # Pumkins2.jpg
UUID_TEST_PHOTO_2 = "E9BC5C36-7CD1-40A1-A72B-8B8FAC227D51" # wedding.jpg
TEST_ALBUM_NAME = "SyncTestAlbum"
@pytest.mark.test_sync
def test_sync_export():
"""Test --export"""
with CliRunner().isolated_filesystem():
result = CliRunner().invoke(
sync,
[
"--export",
"test.db",
],
)
assert result.exit_code == 0
assert os.path.exists("test.db")
@pytest.mark.test_sync
def test_sync_export_import():
"""Test --export and --import"""
photoslib = photoscript.PhotosLibrary()
# create a new album and initialize metadata
test_album = photoslib.create_album(TEST_ALBUM_NAME)
for uuid in [UUID_TEST_PHOTO_1, UUID_TEST_PHOTO_2]:
photo = photoscript.Photo(uuid)
photo.favorite = True
test_album.add([photo])
# export data
with CliRunner().isolated_filesystem():
result = CliRunner().invoke(
sync,
[
"--export",
"test.db",
],
)
assert result.exit_code == 0
# preserve metadata for comparison and clear metadata
metadata_before = {}
for uuid in [UUID_TEST_PHOTO_1, UUID_TEST_PHOTO_2]:
photo = photoscript.Photo(uuid)
metadata_before[uuid] = {
"title": photo.title,
"description": photo.description,
"keywords": photo.keywords,
"favorites": photo.favorite,
}
photo.title = ""
photo.description = ""
photo.keywords = ["NewKeyword"]
photo.favorite = False
# delete the test album
photoslib.delete_album(test_album)
# import metadata
result = CliRunner().invoke(
sync,
[
"--import",
"test.db",
"--set",
"title,description,favorite,albums",
"--merge",
"keywords",
"--report",
"test_report.json",
],
)
assert result.exit_code == 0
assert os.path.exists("test_report.json")
# check metadata
for uuid in [UUID_TEST_PHOTO_1, UUID_TEST_PHOTO_2]:
photo = photoscript.Photo(uuid)
assert photo.title == metadata_before[uuid]["title"]
assert photo.description == metadata_before[uuid]["description"]
assert sorted(photo.keywords) == sorted(
["NewKeyword", *metadata_before[uuid]["keywords"]]
)
assert photo.favorite == metadata_before[uuid]["favorites"]
assert TEST_ALBUM_NAME in [album.title for album in photo.albums]
# check report
with open("test_report.json", "r") as f:
report = json.load(f)
report_data = {record["uuid"]: record for record in report}
for uuid in [UUID_TEST_PHOTO_1, UUID_TEST_PHOTO_2]:
assert report_data[uuid]["updated"]
assert report_data[uuid]["albums"]["updated"]