Exportdb refactor (#638)
* Working on export_db refactor * Added exportdb command, removed logic for missing export_db, #630 * Updated tests * updated docs * Added --config-only, #606 * Added validation for --exportdb * Added --info to exportdb command * Fixed exportdb --touch-file to migrate database if needed * Added exportdb --migrate
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import os
|
||||
import sqlite3
|
||||
import tempfile
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
@@ -675,6 +676,8 @@ CLI_EXIFTOOL_IGNORE_DATE_MODIFIED = {
|
||||
|
||||
CLI_EXIFTOOL_ERROR = ["E2078879-A29C-4D6F-BACB-E3BBE6C3EB91"]
|
||||
|
||||
CLI_NOT_REALLY_A_JPEG = "E2078879-A29C-4D6F-BACB-E3BBE6C3EB91"
|
||||
|
||||
CLI_EXIFTOOL_DUPLICATE_KEYWORDS = {
|
||||
"E9BC5C36-7CD1-40A1-A72B-8B8FAC227D51": "wedding.jpg"
|
||||
}
|
||||
@@ -4936,12 +4939,126 @@ def test_export_force_update():
|
||||
export, [os.path.join(cwd, photos_db_path), ".", "--force-update"]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
print(result.output)
|
||||
assert (
|
||||
f"Processed: {PHOTOS_NOT_IN_TRASH_LEN_15_7} photos, exported: 0, updated: 0, skipped: {PHOTOS_NOT_IN_TRASH_LEN_15_7+PHOTOS_EDITED_15_7}, updated EXIF data: 0, missing: 3, error: 0"
|
||||
in result.output
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skipif(exiftool is None, reason="exiftool not installed")
|
||||
def test_export_update_complex():
|
||||
"""test complex --update scenario, #630"""
|
||||
import glob
|
||||
import os
|
||||
import os.path
|
||||
|
||||
import osxphotos
|
||||
from osxphotos.cli import OSXPHOTOS_EXPORT_DB, export
|
||||
|
||||
runner = CliRunner()
|
||||
cwd = os.getcwd()
|
||||
# pylint: disable=not-context-manager
|
||||
with runner.isolated_filesystem():
|
||||
# basic export
|
||||
result = runner.invoke(export, [os.path.join(cwd, CLI_PHOTOS_DB), ".", "-V"])
|
||||
assert result.exit_code == 0
|
||||
files = glob.glob("*")
|
||||
assert sorted(files) == sorted(CLI_EXPORT_FILENAMES)
|
||||
assert os.path.isfile(OSXPHOTOS_EXPORT_DB)
|
||||
|
||||
src = os.path.join(cwd, CLI_PHOTOS_DB)
|
||||
dest = os.path.join(os.getcwd(), "export_complex_update.photoslibrary")
|
||||
photos_db_path = copy_photos_library_to_path(src, dest)
|
||||
|
||||
tempdir = TemporaryDirectory()
|
||||
|
||||
options = [
|
||||
"--verbose",
|
||||
"--update",
|
||||
"--cleanup",
|
||||
"--directory",
|
||||
"{created.year}/{created.month}",
|
||||
"--description-template",
|
||||
"Album:{album,}{newline}Description:{descr,}",
|
||||
"--exiftool",
|
||||
"--exiftool-merge-keywords",
|
||||
"--exiftool-merge-persons",
|
||||
"--keyword-template",
|
||||
"{keyword}",
|
||||
"--not-hidden",
|
||||
"--retry",
|
||||
"2",
|
||||
"--skip-original-if-edited",
|
||||
"--timestamp",
|
||||
"--strip",
|
||||
"--skip-uuid",
|
||||
CLI_NOT_REALLY_A_JPEG,
|
||||
]
|
||||
# update
|
||||
result = runner.invoke(
|
||||
export, [os.path.join(cwd, photos_db_path), tempdir.name, *options]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert (
|
||||
f"exported: {PHOTOS_NOT_IN_TRASH_LEN_15_7-1}, updated: 0, skipped: 0, updated EXIF data: {PHOTOS_NOT_IN_TRASH_LEN_15_7-1}"
|
||||
in result.output
|
||||
)
|
||||
|
||||
result = runner.invoke(
|
||||
export, [os.path.join(cwd, photos_db_path), tempdir.name, *options]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert "exported: 0" in result.output
|
||||
|
||||
# update a file
|
||||
dbpath = os.path.join(photos_db_path, "database/Photos.sqlite")
|
||||
try:
|
||||
conn = sqlite3.connect(dbpath)
|
||||
c = conn.cursor()
|
||||
except sqlite3.Error as e:
|
||||
pytest.exit(f"An error occurred opening sqlite file")
|
||||
|
||||
# photo is IMG_4547.jpg
|
||||
c.execute(
|
||||
"UPDATE ZADDITIONALASSETATTRIBUTES SET Z_OPT=9, ZTITLE='My Updated Title' WHERE Z_PK=8;"
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
# run --update to see if updated metadata forced update
|
||||
result = runner.invoke(
|
||||
export, [os.path.join(cwd, photos_db_path), tempdir.name, *options]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert (
|
||||
f"exported: 0, updated: 1, skipped: {PHOTOS_NOT_IN_TRASH_LEN_15_7-2}, updated EXIF data: 1"
|
||||
in result.output
|
||||
)
|
||||
|
||||
# update, nothing should export
|
||||
result = runner.invoke(
|
||||
export, [os.path.join(cwd, photos_db_path), tempdir.name, *options]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert (
|
||||
f"exported: 0, updated: 0, skipped: {PHOTOS_NOT_IN_TRASH_LEN_15_7-1}, updated EXIF data: 0"
|
||||
in result.output
|
||||
)
|
||||
|
||||
# change the template and run again
|
||||
options.extend(["--keyword-template", "FOO"])
|
||||
|
||||
# run update and all photos should be updated
|
||||
result = runner.invoke(
|
||||
export, [os.path.join(cwd, photos_db_path), tempdir.name, *options]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert (
|
||||
f"exported: 0, updated: {PHOTOS_NOT_IN_TRASH_LEN_15_7-1}, skipped: 0, updated EXIF data: {PHOTOS_NOT_IN_TRASH_LEN_15_7-1}"
|
||||
in result.output
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
"OSXPHOTOS_TEST_EXPORT" not in os.environ,
|
||||
reason="Skip if not running on author's personal library.",
|
||||
@@ -5266,17 +5383,14 @@ def test_export_update_no_db():
|
||||
assert os.path.isfile(OSXPHOTOS_EXPORT_DB)
|
||||
os.unlink(OSXPHOTOS_EXPORT_DB)
|
||||
|
||||
# update
|
||||
# update, will re-export all files with different names
|
||||
result = runner.invoke(
|
||||
export, [os.path.join(cwd, CLI_PHOTOS_DB), ".", "--update"]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
|
||||
# unedited files will be skipped because their signatures will compare but
|
||||
# edited files will be re-exported because there won't be an edited signature
|
||||
# in the database
|
||||
assert (
|
||||
f"Processed: {PHOTOS_NOT_IN_TRASH_LEN_15_7} photos, exported: 0, updated: {PHOTOS_EDITED_15_7}, skipped: {PHOTOS_NOT_IN_TRASH_LEN_15_7}, updated EXIF data: 0, missing: 3, error: 0"
|
||||
f"Processed: {PHOTOS_NOT_IN_TRASH_LEN_15_7} photos, exported: {PHOTOS_NOT_IN_TRASH_LEN_15_7+PHOTOS_EDITED_15_7}, updated: 0"
|
||||
in result.output
|
||||
)
|
||||
assert os.path.isfile(OSXPHOTOS_EXPORT_DB)
|
||||
@@ -5590,7 +5704,7 @@ def test_export_touch_files_update():
|
||||
|
||||
# touch one file and run update again
|
||||
ts = time.time()
|
||||
os.utime(CLI_EXPORT_BY_DATE[0], (ts, ts))
|
||||
os.utime(CLI_EXPORT_BY_DATE_NEED_TOUCH[1], (ts, ts))
|
||||
|
||||
result = runner.invoke(
|
||||
export,
|
||||
@@ -5922,7 +6036,12 @@ def test_export_ignore_signature_sidecar():
|
||||
# should result in a new sidecar being exported but not the image itself
|
||||
exportdb = osxphotos.export_db.ExportDB("./.osxphotos_export.db", ".")
|
||||
for filename in CLI_EXPORT_IGNORE_SIGNATURE_FILENAMES:
|
||||
exportdb.set_sidecar_for_file(f"{filename}.xmp", "FOO", (0, 1, 2))
|
||||
record = exportdb.get_file_record(filename)
|
||||
sidecar_record = exportdb.create_or_get_file_record(
|
||||
f"{filename}.xmp", record.uuid
|
||||
)
|
||||
sidecar_record.dest_sig = (0, 1, 2)
|
||||
sidecar_record.digest = "FOO"
|
||||
|
||||
result = runner.invoke(
|
||||
export,
|
||||
@@ -6426,7 +6545,7 @@ def test_save_load_config():
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert "Saving options to file" in result.output
|
||||
assert "Saving options to config file" in result.output
|
||||
files = glob.glob("*")
|
||||
assert "config.toml" in files
|
||||
|
||||
@@ -6462,7 +6581,7 @@ def test_save_load_config():
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert "Saving options to file" in result.output
|
||||
assert "Saving options to config file" in result.output
|
||||
files = glob.glob("*")
|
||||
assert "config.toml" in files
|
||||
|
||||
@@ -6499,6 +6618,41 @@ def test_save_load_config():
|
||||
assert "Writing XMP sidecar" not in result.output
|
||||
|
||||
|
||||
def test_config_only():
|
||||
"""test --save-config, --config-only"""
|
||||
import glob
|
||||
import os
|
||||
import os.path
|
||||
|
||||
from osxphotos.cli import export
|
||||
|
||||
runner = CliRunner()
|
||||
cwd = os.getcwd()
|
||||
# pylint: disable=not-context-manager
|
||||
with runner.isolated_filesystem():
|
||||
# test save config file
|
||||
result = runner.invoke(
|
||||
export,
|
||||
[
|
||||
os.path.join(cwd, CLI_PHOTOS_DB),
|
||||
".",
|
||||
"-V",
|
||||
"--sidecar",
|
||||
"XMP",
|
||||
"--touch-file",
|
||||
"--update",
|
||||
"--save-config",
|
||||
"config.toml",
|
||||
"--config-only",
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert "Saved config file" in result.output
|
||||
assert "Processed:" not in result.output
|
||||
files = glob.glob("*")
|
||||
assert "config.toml" in files
|
||||
|
||||
|
||||
def test_export_exportdb():
|
||||
"""test --exportdb"""
|
||||
import glob
|
||||
|
||||
@@ -1,26 +1,36 @@
|
||||
""" Test ExportDB """
|
||||
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import sqlite3
|
||||
import tempfile
|
||||
|
||||
import pytest
|
||||
|
||||
from osxphotos._version import __version__
|
||||
from osxphotos.export_db import (
|
||||
OSXPHOTOS_EXPORTDB_VERSION,
|
||||
ExportDB,
|
||||
ExportDBInMemory,
|
||||
ExportDBTemp,
|
||||
ExportRecord,
|
||||
)
|
||||
from osxphotos.export_db_utils import export_db_get_version
|
||||
|
||||
EXIF_DATA = """[{"_CreatedBy": "osxphotos, https://github.com/RhetTbull/osxphotos", "EXIF:ImageDescription": "\u2068Elder Park\u2069, \u2068Adelaide\u2069, \u2068Australia\u2069", "XMP:Description": "\u2068Elder Park\u2069, \u2068Adelaide\u2069, \u2068Australia\u2069", "XMP:Title": "Elder Park", "EXIF:GPSLatitude": "34 deg 55' 8.01\" S", "EXIF:GPSLongitude": "138 deg 35' 48.70\" E", "Composite:GPSPosition": "34 deg 55' 8.01\" S, 138 deg 35' 48.70\" E", "EXIF:GPSLatitudeRef": "South", "EXIF:GPSLongitudeRef": "East", "EXIF:DateTimeOriginal": "2017:06:20 17:18:56", "EXIF:OffsetTimeOriginal": "+09:30", "EXIF:ModifyDate": "2020:05:18 14:42:04"}]"""
|
||||
INFO_DATA = """{"uuid": "3DD2C897-F19E-4CA6-8C22-B027D5A71907", "filename": "3DD2C897-F19E-4CA6-8C22-B027D5A71907.jpeg", "original_filename": "IMG_4547.jpg", "date": "2017-06-20T17:18:56.518000+09:30", "description": "\u2068Elder Park\u2069, \u2068Adelaide\u2069, \u2068Australia\u2069", "title": "Elder Park", "keywords": [], "labels": ["Statue", "Art"], "albums": ["AlbumInFolder"], "folders": {"AlbumInFolder": ["Folder1", "SubFolder2"]}, "persons": [], "path": "/Users/rhet/Pictures/Test-10.15.4.photoslibrary/originals/3/3DD2C897-F19E-4CA6-8C22-B027D5A71907.jpeg", "ismissing": false, "hasadjustments": true, "external_edit": false, "favorite": false, "hidden": false, "latitude": -34.91889167000001, "longitude": 138.59686167, "path_edited": "/Users/rhet/Pictures/Test-10.15.4.photoslibrary/resources/renders/3/3DD2C897-F19E-4CA6-8C22-B027D5A71907_1_201_a.jpeg", "shared": false, "isphoto": true, "ismovie": false, "uti": "public.jpeg", "burst": false, "live_photo": false, "path_live_photo": null, "iscloudasset": false, "incloud": null, "date_modified": "2020-05-18T14:42:04.608664+09:30", "portrait": false, "screenshot": false, "slow_mo": false, "time_lapse": false, "hdr": false, "selfie": false, "panorama": false, "has_raw": false, "uti_raw": null, "path_raw": null, "place": {"name": "Elder Park, Adelaide, South Australia, Australia, River Torrens", "names": {"field0": [], "country": ["Australia"], "state_province": ["South Australia"], "sub_administrative_area": ["Adelaide"], "city": ["Adelaide", "Adelaide"], "field5": [], "additional_city_info": ["Adelaide CBD", "Tarndanya"], "ocean": [], "area_of_interest": ["Elder Park", ""], "inland_water": ["River Torrens", "River Torrens"], "field10": [], "region": [], "sub_throughfare": [], "field13": [], "postal_code": [], "field15": [], "field16": [], "street_address": [], "body_of_water": ["River Torrens", "River Torrens"]}, "country_code": "AU", "ishome": false, "address_str": "River Torrens, Adelaide SA, Australia", "address": {"street": null, "sub_locality": "Tarndanya", "city": "Adelaide", "sub_administrative_area": "Adelaide", "state_province": "SA", "postal_code": null, "country": "Australia", "iso_country_code": "AU"}}, "exif": {"flash_fired": false, "iso": 320, "metering_mode": 3, "sample_rate": null, "track_format": null, "white_balance": 0, "aperture": 2.2, "bit_rate": null, "duration": null, "exposure_bias": 0.0, "focal_length": 4.15, "fps": null, "latitude": null, "longitude": null, "shutter_speed": 0.058823529411764705, "camera_make": "Apple", "camera_model": "iPhone 6s", "codec": null, "lens_model": "iPhone 6s back camera 4.15mm f/2.2"}}"""
|
||||
SIDECAR_DATA = """FOO_BAR"""
|
||||
METADATA_DATA = "FIZZ"
|
||||
DIGEST_DATA = "FIZZ"
|
||||
|
||||
EXIF_DATA2 = """[{"_CreatedBy": "osxphotos, https://github.com/RhetTbull/osxphotos", "XMP:Title": "St. James's Park", "XMP:TagsList": ["London 2018", "St. James's Park", "England", "United Kingdom", "UK", "London"], "IPTC:Keywords": ["London 2018", "St. James's Park", "England", "United Kingdom", "UK", "London"], "XMP:Subject": ["London 2018", "St. James's Park", "England", "United Kingdom", "UK", "London"], "EXIF:GPSLatitude": "51 deg 30' 12.86\" N", "EXIF:GPSLongitude": "0 deg 7' 54.50\" W", "Composite:GPSPosition": "51 deg 30' 12.86\" N, 0 deg 7' 54.50\" W", "EXIF:GPSLatitudeRef": "North", "EXIF:GPSLongitudeRef": "West", "EXIF:DateTimeOriginal": "2018:10:13 09:18:12", "EXIF:OffsetTimeOriginal": "-04:00", "EXIF:ModifyDate": "2019:12:08 14:06:44"}]"""
|
||||
INFO_DATA2 = """{"uuid": "F2BB3F98-90F0-4E4C-A09B-25C6822A4529", "filename": "F2BB3F98-90F0-4E4C-A09B-25C6822A4529.jpeg", "original_filename": "IMG_8440.JPG", "date": "2019-06-11T11:42:06.711805-07:00", "description": null, "title": null, "keywords": [], "labels": ["Sky", "Cloudy", "Fence", "Land", "Outdoor", "Park", "Amusement Park", "Roller Coaster"], "albums": [], "folders": {}, "persons": [], "path": "/Volumes/MacBook Catalina - Data/Users/rhet/Pictures/Photos Library.photoslibrary/originals/F/F2BB3F98-90F0-4E4C-A09B-25C6822A4529.jpeg", "ismissing": false, "hasadjustments": false, "external_edit": false, "favorite": false, "hidden": false, "latitude": 33.81558666666667, "longitude": -117.99298, "path_edited": null, "shared": false, "isphoto": true, "ismovie": false, "uti": "public.jpeg", "burst": false, "live_photo": false, "path_live_photo": null, "iscloudasset": true, "incloud": true, "date_modified": "2019-10-14T00:51:47.141950-07:00", "portrait": false, "screenshot": false, "slow_mo": false, "time_lapse": false, "hdr": false, "selfie": false, "panorama": false, "has_raw": false, "uti_raw": null, "path_raw": null, "place": {"name": "Adventure City, Stanton, California, United States", "names": {"field0": [], "country": ["United States"], "state_province": ["California"], "sub_administrative_area": ["Orange"], "city": ["Stanton", "Anaheim", "Anaheim"], "field5": [], "additional_city_info": ["West Anaheim"], "ocean": [], "area_of_interest": ["Adventure City", "Adventure City"], "inland_water": [], "field10": [], "region": [], "sub_throughfare": [], "field13": [], "postal_code": [], "field15": [], "field16": [], "street_address": [], "body_of_water": []}, "country_code": "US", "ishome": false, "address_str": "Adventure City, 1240 S Beach Blvd, Anaheim, CA 92804, United States", "address": {"street": "1240 S Beach Blvd", "sub_locality": "West Anaheim", "city": "Stanton", "sub_administrative_area": "Orange", "state_province": "CA", "postal_code": "92804", "country": "United States", "iso_country_code": "US"}}, "exif": {"flash_fired": false, "iso": 25, "metering_mode": 5, "sample_rate": null, "track_format": null, "white_balance": 0, "aperture": 2.2, "bit_rate": null, "duration": null, "exposure_bias": 0.0, "focal_length": 4.15, "fps": null, "latitude": null, "longitude": null, "shutter_speed": 0.0004940711462450593, "camera_make": "Apple", "camera_model": "iPhone 6s", "codec": null, "lens_model": "iPhone 6s back camera 4.15mm f/2.2"}}"""
|
||||
DIGEST_DATA2 = "BUZZ"
|
||||
DATABASE_VERSION1 = "tests/export_db_version1.db"
|
||||
|
||||
|
||||
def test_export_db():
|
||||
"""test ExportDB"""
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from osxphotos.export_db import OSXPHOTOS_EXPORTDB_VERSION, ExportDB
|
||||
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
dbname = os.path.join(tempdir.name, ".osxphotos_export.db")
|
||||
db = ExportDB(dbname, tempdir.name)
|
||||
@@ -32,104 +42,51 @@ def test_export_db():
|
||||
filepath = os.path.join(tempdir.name, "test.JPG")
|
||||
filepath_lower = os.path.join(tempdir.name, "test.jpg")
|
||||
|
||||
db.set_uuid_for_file(filepath, "FOO-BAR")
|
||||
# filename should be case-insensitive
|
||||
assert db.get_uuid_for_file(filepath_lower) == "FOO-BAR"
|
||||
db.set_info_for_uuid("FOO-BAR", INFO_DATA)
|
||||
assert db.get_info_for_uuid("FOO-BAR") == INFO_DATA
|
||||
db.set_exifdata_for_file(filepath, EXIF_DATA)
|
||||
assert db.get_exifdata_for_file(filepath) == EXIF_DATA
|
||||
db.set_stat_orig_for_file(filepath, (1, 2, 3))
|
||||
assert db.get_stat_orig_for_file(filepath) == (1, 2, 3)
|
||||
db.set_stat_exif_for_file(filepath, (4, 5, 6))
|
||||
assert db.get_stat_exif_for_file(filepath) == (4, 5, 6)
|
||||
db.set_stat_edited_for_file(filepath, (10, 11, 12))
|
||||
assert db.get_stat_edited_for_file(filepath) == (10, 11, 12)
|
||||
db.set_stat_converted_for_file(filepath, (7, 8, 9))
|
||||
assert db.get_stat_converted_for_file(filepath) == (7, 8, 9)
|
||||
db.set_sidecar_for_file(filepath, SIDECAR_DATA, (13, 14, 15))
|
||||
assert db.get_sidecar_for_file(filepath) == (SIDECAR_DATA, (13, 14, 15))
|
||||
assert db.get_previous_uuids() == ["FOO-BAR"]
|
||||
uuid = "FOOBAR"
|
||||
assert db.get_photoinfo_for_uuid(uuid) is None
|
||||
db.set_photoinfo_for_uuid(uuid, INFO_DATA)
|
||||
assert db.get_photoinfo_for_uuid(uuid) == INFO_DATA
|
||||
|
||||
db.set_detected_text_for_uuid("FOO-BAR", json.dumps([["foo", 0.5]]))
|
||||
assert json.loads(db.get_detected_text_for_uuid("FOO-BAR")) == [["foo", 0.5]]
|
||||
assert db.get_uuid_for_file(filepath) is None
|
||||
db.create_file_record(filepath, uuid)
|
||||
assert db.get_uuid_for_file(filepath) == uuid
|
||||
|
||||
# test set_data which sets all at the same time
|
||||
filepath2 = os.path.join(tempdir.name, "test2.jpg")
|
||||
db.set_data(
|
||||
filepath2,
|
||||
"BAR-FOO",
|
||||
(1, 2, 3),
|
||||
(4, 5, 6),
|
||||
(7, 8, 9),
|
||||
(10, 11, 12),
|
||||
INFO_DATA,
|
||||
EXIF_DATA,
|
||||
METADATA_DATA,
|
||||
)
|
||||
assert db.get_uuid_for_file(filepath2) == "BAR-FOO"
|
||||
assert db.get_info_for_uuid("BAR-FOO") == INFO_DATA
|
||||
assert db.get_exifdata_for_file(filepath2) == EXIF_DATA
|
||||
assert db.get_stat_orig_for_file(filepath2) == (1, 2, 3)
|
||||
assert db.get_stat_exif_for_file(filepath2) == (4, 5, 6)
|
||||
assert db.get_stat_converted_for_file(filepath2) == (7, 8, 9)
|
||||
assert db.get_stat_edited_for_file(filepath2) == (10, 11, 12)
|
||||
assert sorted(db.get_previous_uuids()) == (["BAR-FOO", "FOO-BAR"])
|
||||
assert db.get_metadata_for_file(filepath2) == METADATA_DATA
|
||||
record = db.get_file_record(filepath)
|
||||
assert record.uuid == uuid
|
||||
assert record.photoinfo == INFO_DATA
|
||||
assert record.filepath == pathlib.Path(filepath).name
|
||||
assert record.filepath_normalized == pathlib.Path(filepath).name.lower()
|
||||
assert record.src_sig == (None, None, None)
|
||||
assert record.dest_sig == (None, None, None)
|
||||
assert record.digest is None
|
||||
assert record.exifdata is None
|
||||
record.digest = DIGEST_DATA # for next assert
|
||||
|
||||
# test set_data value=None doesn't overwrite existing data
|
||||
db.set_data(
|
||||
filepath2,
|
||||
"BAR-FOO",
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
assert db.get_uuid_for_file(filepath2) == "BAR-FOO"
|
||||
assert db.get_info_for_uuid("BAR-FOO") == INFO_DATA
|
||||
assert db.get_exifdata_for_file(filepath2) == EXIF_DATA
|
||||
assert db.get_stat_orig_for_file(filepath2) == (1, 2, 3)
|
||||
assert db.get_stat_exif_for_file(filepath2) == (4, 5, 6)
|
||||
assert db.get_stat_converted_for_file(filepath2) == (7, 8, 9)
|
||||
assert db.get_stat_edited_for_file(filepath2) == (10, 11, 12)
|
||||
assert sorted(db.get_previous_uuids()) == (["BAR-FOO", "FOO-BAR"])
|
||||
assert db.get_metadata_for_file(filepath2) == METADATA_DATA
|
||||
# test create_or_get_file_record
|
||||
# existing record
|
||||
record2 = db.create_or_get_file_record(filepath, uuid)
|
||||
assert record2.uuid == uuid
|
||||
assert record.photoinfo == INFO_DATA
|
||||
assert record.digest == DIGEST_DATA
|
||||
|
||||
# close and re-open
|
||||
db.close()
|
||||
db = ExportDB(dbname, tempdir.name)
|
||||
assert not db.was_created
|
||||
assert db.get_uuid_for_file(filepath2) == "BAR-FOO"
|
||||
assert db.get_info_for_uuid("BAR-FOO") == INFO_DATA
|
||||
assert db.get_exifdata_for_file(filepath2) == EXIF_DATA
|
||||
assert db.get_stat_orig_for_file(filepath2) == (1, 2, 3)
|
||||
assert db.get_stat_exif_for_file(filepath2) == (4, 5, 6)
|
||||
assert db.get_stat_converted_for_file(filepath2) == (7, 8, 9)
|
||||
assert db.get_stat_edited_for_file(filepath2) == (10, 11, 12)
|
||||
assert sorted(db.get_previous_uuids()) == (["BAR-FOO", "FOO-BAR"])
|
||||
assert json.loads(db.get_detected_text_for_uuid("FOO-BAR")) == [["foo", 0.5]]
|
||||
assert db.get_metadata_for_file(filepath2) == METADATA_DATA
|
||||
# new record
|
||||
filepath3 = os.path.join(tempdir.name, "test3.JPG")
|
||||
record3 = db.create_or_get_file_record(filepath3, "new_uuid")
|
||||
assert record3.uuid == "new_uuid"
|
||||
assert record3.photoinfo is None
|
||||
assert record3.digest is None
|
||||
|
||||
# all uuids
|
||||
uuids = db.get_previous_uuids()
|
||||
assert sorted(uuids) == sorted(["new_uuid", uuid])
|
||||
|
||||
|
||||
# update data
|
||||
db.set_uuid_for_file(filepath, "FUBAR")
|
||||
assert db.get_uuid_for_file(filepath) == "FUBAR"
|
||||
assert sorted(db.get_previous_uuids()) == (["BAR-FOO", "FUBAR"])
|
||||
|
||||
|
||||
def test_export_db_no_op():
|
||||
"""test ExportDBNoOp"""
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from osxphotos.export_db import OSXPHOTOS_EXPORTDB_VERSION, ExportDBNoOp
|
||||
|
||||
def test_export_db_constraints():
|
||||
"""test ExportDB constraints"""
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
db = ExportDBNoOp()
|
||||
dbname = os.path.join(tempdir.name, ".osxphotos_export.db")
|
||||
db = ExportDB(dbname, tempdir.name)
|
||||
assert os.path.isfile(dbname)
|
||||
assert db.was_created
|
||||
assert not db.was_upgraded
|
||||
assert db.version == OSXPHOTOS_EXPORTDB_VERSION
|
||||
@@ -137,68 +94,28 @@ def test_export_db_no_op():
|
||||
filepath = os.path.join(tempdir.name, "test.JPG")
|
||||
filepath_lower = os.path.join(tempdir.name, "test.jpg")
|
||||
|
||||
db.set_uuid_for_file(filepath, "FOO-BAR")
|
||||
# filename should be case-insensitive
|
||||
assert db.get_uuid_for_file(filepath_lower) is None
|
||||
db.set_info_for_uuid("FOO-BAR", INFO_DATA)
|
||||
assert db.get_info_for_uuid("FOO-BAR") is None
|
||||
db.set_exifdata_for_file(filepath, EXIF_DATA)
|
||||
assert db.get_exifdata_for_file(filepath) is None
|
||||
db.set_stat_orig_for_file(filepath, (1, 2, 3))
|
||||
assert db.get_stat_orig_for_file(filepath) is None
|
||||
db.set_stat_exif_for_file(filepath, (4, 5, 6))
|
||||
assert db.get_stat_exif_for_file(filepath) is None
|
||||
db.set_stat_converted_for_file(filepath, (7, 8, 9))
|
||||
assert db.get_stat_converted_for_file(filepath) is None
|
||||
db.set_stat_edited_for_file(filepath, (10, 11, 12))
|
||||
assert db.get_stat_edited_for_file(filepath) is None
|
||||
db.set_sidecar_for_file(filepath, SIDECAR_DATA, (13, 14, 15))
|
||||
assert db.get_sidecar_for_file(filepath) == (None, (None, None, None))
|
||||
assert db.get_previous_uuids() == []
|
||||
db.set_detected_text_for_uuid("FOO-BAR", json.dumps([["foo", 0.5]]))
|
||||
assert db.get_detected_text_for_uuid("FOO-BAR") is None
|
||||
db.set_metadata_for_file(filepath, METADATA_DATA)
|
||||
assert db.get_metadata_for_file(filepath) is None
|
||||
uuid = "FOOBAR"
|
||||
db.set_photoinfo_for_uuid(uuid, INFO_DATA)
|
||||
record = db.create_file_record(filepath, uuid)
|
||||
record.photoinfo = INFO_DATA
|
||||
record.exifdata = EXIF_DATA
|
||||
record.digest = DIGEST_DATA
|
||||
record.src_sig = (7, 8, 9)
|
||||
record.dest_sig = (10, 11, 12)
|
||||
|
||||
# test set_data which sets all at the same time
|
||||
filepath2 = os.path.join(tempdir.name, "test2.jpg")
|
||||
db.set_data(
|
||||
filepath2,
|
||||
"BAR-FOO",
|
||||
(1, 2, 3),
|
||||
(4, 5, 6),
|
||||
(7, 8, 9),
|
||||
(10, 11, 12),
|
||||
INFO_DATA,
|
||||
EXIF_DATA,
|
||||
METADATA_DATA,
|
||||
)
|
||||
assert db.get_uuid_for_file(filepath2) is None
|
||||
assert db.get_info_for_uuid("BAR-FOO") is None
|
||||
assert db.get_exifdata_for_file(filepath2) is None
|
||||
assert db.get_stat_orig_for_file(filepath2) is None
|
||||
assert db.get_stat_exif_for_file(filepath2) is None
|
||||
assert db.get_stat_converted_for_file(filepath) is None
|
||||
assert db.get_stat_edited_for_file(filepath) is None
|
||||
assert db.get_previous_uuids() == []
|
||||
assert db.get_metadata_for_file(filepath) is None
|
||||
with pytest.raises(AttributeError):
|
||||
record.uuid = "BARFOO"
|
||||
|
||||
# update data
|
||||
db.set_uuid_for_file(filepath, "FUBAR")
|
||||
assert db.get_uuid_for_file(filepath) is None
|
||||
with pytest.raises(sqlite3.IntegrityError):
|
||||
record2 = db.create_file_record(filepath, "NEW_UUID")
|
||||
|
||||
with pytest.raises(AttributeError):
|
||||
# verify we can't add new attributes
|
||||
record.src_stats = (7, 8, 9)
|
||||
|
||||
|
||||
def test_export_db_in_memory():
|
||||
"""test ExportDBInMemory"""
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from osxphotos.export_db import (
|
||||
OSXPHOTOS_EXPORTDB_VERSION,
|
||||
ExportDB,
|
||||
ExportDBInMemory,
|
||||
)
|
||||
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
dbname = os.path.join(tempdir.name, ".osxphotos_export.db")
|
||||
db = ExportDB(dbname, tempdir.name)
|
||||
@@ -207,87 +124,61 @@ def test_export_db_in_memory():
|
||||
filepath = os.path.join(tempdir.name, "test.JPG")
|
||||
filepath_lower = os.path.join(tempdir.name, "test.jpg")
|
||||
|
||||
db.set_uuid_for_file(filepath, "FOO-BAR")
|
||||
db.set_info_for_uuid("FOO-BAR", INFO_DATA)
|
||||
db.set_exifdata_for_file(filepath, EXIF_DATA)
|
||||
db.set_stat_orig_for_file(filepath, (1, 2, 3))
|
||||
db.set_stat_exif_for_file(filepath, (4, 5, 6))
|
||||
db.set_stat_converted_for_file(filepath, (7, 8, 9))
|
||||
db.set_stat_edited_for_file(filepath, (10, 11, 12))
|
||||
db.set_sidecar_for_file(filepath, SIDECAR_DATA, (13, 14, 15))
|
||||
assert db.get_previous_uuids() == ["FOO-BAR"]
|
||||
db.set_detected_text_for_uuid("FOO-BAR", json.dumps([["foo", 0.5]]))
|
||||
db.set_metadata_for_file(filepath, METADATA_DATA)
|
||||
uuid = "FOOBAR"
|
||||
record = db.create_file_record(filepath, uuid)
|
||||
record.photoinfo = INFO_DATA
|
||||
record.exifdata = EXIF_DATA
|
||||
record.digest = DIGEST_DATA
|
||||
record.src_sig = (7, 8, 9)
|
||||
record.dest_sig = (10, 11, 12)
|
||||
db.close()
|
||||
|
||||
# create in memory version
|
||||
dbram = ExportDBInMemory(dbname, tempdir.name)
|
||||
assert not dbram.was_created
|
||||
assert not dbram.was_upgraded
|
||||
assert dbram.version == OSXPHOTOS_EXPORTDB_VERSION
|
||||
record2 = dbram.get_file_record(filepath)
|
||||
assert record2.uuid == uuid
|
||||
assert record2.photoinfo == INFO_DATA
|
||||
assert record2.exifdata == EXIF_DATA
|
||||
assert record2.digest == DIGEST_DATA
|
||||
assert record2.src_sig == (7, 8, 9)
|
||||
assert record2.dest_sig == (10, 11, 12)
|
||||
|
||||
# verify values as expected
|
||||
assert dbram.get_uuid_for_file(filepath_lower) == "FOO-BAR"
|
||||
assert dbram.get_info_for_uuid("FOO-BAR") == INFO_DATA
|
||||
assert dbram.get_exifdata_for_file(filepath) == EXIF_DATA
|
||||
assert dbram.get_stat_orig_for_file(filepath) == (1, 2, 3)
|
||||
assert dbram.get_stat_exif_for_file(filepath) == (4, 5, 6)
|
||||
assert dbram.get_stat_converted_for_file(filepath) == (7, 8, 9)
|
||||
assert dbram.get_stat_edited_for_file(filepath) == (10, 11, 12)
|
||||
assert dbram.get_sidecar_for_file(filepath) == (SIDECAR_DATA, (13, 14, 15))
|
||||
assert dbram.get_previous_uuids() == ["FOO-BAR"]
|
||||
assert json.loads(dbram.get_detected_text_for_uuid("FOO-BAR")) == [["foo", 0.5]]
|
||||
assert dbram.get_metadata_for_file(filepath) == METADATA_DATA
|
||||
# change some values
|
||||
record2.photoinfo = INFO_DATA2
|
||||
record2.exifdata = EXIF_DATA2
|
||||
record2.digest = DIGEST_DATA2
|
||||
record2.src_sig = (13, 14, 15)
|
||||
record2.dest_sig = (16, 17, 18)
|
||||
|
||||
# change a value
|
||||
dbram.set_uuid_for_file(filepath, "FUBAR")
|
||||
dbram.set_info_for_uuid("FUBAR", INFO_DATA2)
|
||||
dbram.set_exifdata_for_file(filepath, EXIF_DATA2)
|
||||
dbram.set_stat_orig_for_file(filepath, (7, 8, 9))
|
||||
dbram.set_stat_exif_for_file(filepath, (10, 11, 12))
|
||||
dbram.set_stat_converted_for_file(filepath, (1, 2, 3))
|
||||
dbram.set_stat_edited_for_file(filepath, (4, 5, 6))
|
||||
dbram.set_sidecar_for_file(filepath, "FUBAR", (20, 21, 22))
|
||||
dbram.set_detected_text_for_uuid("FUBAR", json.dumps([["bar", 0.5]]))
|
||||
dbram.set_metadata_for_file(filepath, "FUBAR")
|
||||
assert record2.photoinfo == INFO_DATA2
|
||||
assert record2.exifdata == EXIF_DATA2
|
||||
assert record2.digest == DIGEST_DATA2
|
||||
assert record2.src_sig == (13, 14, 15)
|
||||
assert record2.dest_sig == (16, 17, 18)
|
||||
|
||||
assert dbram.get_uuid_for_file(filepath_lower) == "FUBAR"
|
||||
assert dbram.get_info_for_uuid("FUBAR") == INFO_DATA2
|
||||
assert dbram.get_exifdata_for_file(filepath) == EXIF_DATA2
|
||||
assert dbram.get_stat_orig_for_file(filepath) == (7, 8, 9)
|
||||
assert dbram.get_stat_exif_for_file(filepath) == (10, 11, 12)
|
||||
assert dbram.get_stat_converted_for_file(filepath) == (1, 2, 3)
|
||||
assert dbram.get_stat_edited_for_file(filepath) == (4, 5, 6)
|
||||
assert dbram.get_sidecar_for_file(filepath) == ("FUBAR", (20, 21, 22))
|
||||
assert dbram.get_previous_uuids() == ["FUBAR"]
|
||||
assert json.loads(dbram.get_detected_text_for_uuid("FUBAR")) == [["bar", 0.5]]
|
||||
assert dbram.get_metadata_for_file(filepath) == "FUBAR"
|
||||
# all uuids
|
||||
uuids = dbram.get_previous_uuids()
|
||||
assert uuids == [uuid]
|
||||
|
||||
dbram.close()
|
||||
|
||||
# re-open on disk and verify no changes
|
||||
# re-open original, assert no changes
|
||||
db = ExportDB(dbname, tempdir.name)
|
||||
assert db.get_uuid_for_file(filepath_lower) == "FOO-BAR"
|
||||
assert db.get_info_for_uuid("FOO-BAR") == INFO_DATA
|
||||
assert db.get_exifdata_for_file(filepath) == EXIF_DATA
|
||||
assert db.get_stat_orig_for_file(filepath) == (1, 2, 3)
|
||||
assert db.get_stat_exif_for_file(filepath) == (4, 5, 6)
|
||||
assert db.get_stat_converted_for_file(filepath) == (7, 8, 9)
|
||||
assert db.get_stat_edited_for_file(filepath) == (10, 11, 12)
|
||||
assert db.get_sidecar_for_file(filepath) == (SIDECAR_DATA, (13, 14, 15))
|
||||
assert db.get_previous_uuids() == ["FOO-BAR"]
|
||||
record = db.get_file_record(filepath)
|
||||
assert record.uuid == uuid
|
||||
assert record.photoinfo == INFO_DATA
|
||||
assert record.exifdata == EXIF_DATA
|
||||
assert record.digest == DIGEST_DATA
|
||||
assert record.src_sig == (7, 8, 9)
|
||||
assert record.dest_sig == (10, 11, 12)
|
||||
|
||||
assert db.get_info_for_uuid("FUBAR") is None
|
||||
assert db.get_detected_text_for_uuid("FUBAR") is None
|
||||
assert db.get_metadata_for_file(filepath) == METADATA_DATA
|
||||
# all uuids
|
||||
uuids = db.get_previous_uuids()
|
||||
assert uuids == [uuid]
|
||||
|
||||
|
||||
def test_export_db_in_memory_nofile():
|
||||
"""test ExportDBInMemory with no dbfile"""
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from osxphotos.export_db import OSXPHOTOS_EXPORTDB_VERSION, ExportDBInMemory
|
||||
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
filepath = os.path.join(tempdir.name, "test.JPG")
|
||||
filepath_lower = os.path.join(tempdir.name, "test.jpg")
|
||||
@@ -299,28 +190,187 @@ def test_export_db_in_memory_nofile():
|
||||
assert not dbram.was_upgraded
|
||||
assert dbram.version == OSXPHOTOS_EXPORTDB_VERSION
|
||||
|
||||
# change a value
|
||||
dbram.set_uuid_for_file(filepath, "FUBAR")
|
||||
dbram.set_info_for_uuid("FUBAR", INFO_DATA2)
|
||||
dbram.set_exifdata_for_file(filepath, EXIF_DATA2)
|
||||
dbram.set_stat_orig_for_file(filepath, (7, 8, 9))
|
||||
dbram.set_stat_exif_for_file(filepath, (10, 11, 12))
|
||||
dbram.set_stat_converted_for_file(filepath, (1, 2, 3))
|
||||
dbram.set_stat_edited_for_file(filepath, (4, 5, 6))
|
||||
dbram.set_sidecar_for_file(filepath, "FUBAR", (20, 21, 22))
|
||||
dbram.set_detected_text_for_uuid("FUBAR", json.dumps([["bar", 0.5]]))
|
||||
dbram.set_metadata_for_file(filepath, METADATA_DATA)
|
||||
# set values
|
||||
uuid = "FOOBAR"
|
||||
record = dbram.create_file_record(filepath, uuid)
|
||||
record.photoinfo = INFO_DATA
|
||||
record.exifdata = EXIF_DATA
|
||||
record.digest = DIGEST_DATA
|
||||
record.src_sig = (7, 8, 9)
|
||||
record.dest_sig = (10, 11, 12)
|
||||
|
||||
assert dbram.get_uuid_for_file(filepath_lower) == "FUBAR"
|
||||
assert dbram.get_info_for_uuid("FUBAR") == INFO_DATA2
|
||||
assert dbram.get_exifdata_for_file(filepath) == EXIF_DATA2
|
||||
assert dbram.get_stat_orig_for_file(filepath) == (7, 8, 9)
|
||||
assert dbram.get_stat_exif_for_file(filepath) == (10, 11, 12)
|
||||
assert dbram.get_stat_converted_for_file(filepath) == (1, 2, 3)
|
||||
assert dbram.get_stat_edited_for_file(filepath) == (4, 5, 6)
|
||||
assert dbram.get_sidecar_for_file(filepath) == ("FUBAR", (20, 21, 22))
|
||||
assert dbram.get_previous_uuids() == ["FUBAR"]
|
||||
assert json.loads(dbram.get_detected_text_for_uuid("FUBAR")) == [["bar", 0.5]]
|
||||
assert dbram.get_metadata_for_file(filepath) == METADATA_DATA
|
||||
assert record.photoinfo == INFO_DATA
|
||||
assert record.exifdata == EXIF_DATA
|
||||
assert record.digest == DIGEST_DATA
|
||||
assert record.src_sig == (7, 8, 9)
|
||||
assert record.dest_sig == (10, 11, 12)
|
||||
assert record.uuid == uuid
|
||||
|
||||
# change some values
|
||||
record.photoinfo = INFO_DATA2
|
||||
record.digest = DIGEST_DATA2
|
||||
assert record.photoinfo == INFO_DATA2
|
||||
assert record.digest == DIGEST_DATA2
|
||||
assert record.exifdata == EXIF_DATA
|
||||
|
||||
|
||||
def test_export_db_temp():
|
||||
"""test ExportDBTemp"""
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
filepath = os.path.join(tempdir.name, "test.JPG")
|
||||
filepath_lower = os.path.join(tempdir.name, "test.jpg")
|
||||
|
||||
dbram = ExportDBTemp()
|
||||
assert dbram.was_created
|
||||
assert not dbram.was_upgraded
|
||||
assert dbram.version == OSXPHOTOS_EXPORTDB_VERSION
|
||||
|
||||
# set values
|
||||
uuid = "FOOBAR"
|
||||
record = dbram.create_file_record(filepath, uuid)
|
||||
record.photoinfo = INFO_DATA
|
||||
record.exifdata = EXIF_DATA
|
||||
record.digest = DIGEST_DATA
|
||||
record.src_sig = (7, 8, 9)
|
||||
record.dest_sig = (10, 11, 12)
|
||||
|
||||
assert record.photoinfo == INFO_DATA
|
||||
assert record.exifdata == EXIF_DATA
|
||||
assert record.digest == DIGEST_DATA
|
||||
assert record.src_sig == (7, 8, 9)
|
||||
assert record.dest_sig == (10, 11, 12)
|
||||
assert record.uuid == uuid
|
||||
|
||||
# change some values
|
||||
record.photoinfo = INFO_DATA2
|
||||
record.digest = DIGEST_DATA2
|
||||
assert record.photoinfo == INFO_DATA2
|
||||
assert record.digest == DIGEST_DATA2
|
||||
assert record.exifdata == EXIF_DATA
|
||||
|
||||
dbram.close()
|
||||
|
||||
|
||||
def test_export_record():
|
||||
"""Test ExportRecord"""
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
filepath = os.path.join(tempdir.name, "test.JPG")
|
||||
uuid = "FOOBAR"
|
||||
dbname = os.path.join(tempdir.name, ".osxphotos_export.db")
|
||||
db = ExportDB(dbname, tempdir.name)
|
||||
|
||||
assert db.get_file_record(filepath) is None
|
||||
record = db.create_file_record(filepath, uuid)
|
||||
assert record.uuid == uuid
|
||||
assert record.filepath == pathlib.Path(filepath).name
|
||||
assert record.filepath_normalized == pathlib.Path(filepath).name.lower()
|
||||
record.src_sig = (1, 2, 3.0)
|
||||
assert record.src_sig == (1, 2, 3)
|
||||
record.dest_sig = (4, 5, 6.0)
|
||||
assert record.dest_sig == (4, 5, 6)
|
||||
record.digest = DIGEST_DATA
|
||||
assert record.digest == DIGEST_DATA
|
||||
record.exifdata = EXIF_DATA
|
||||
assert record.exifdata == EXIF_DATA
|
||||
record.photoinfo = INFO_DATA
|
||||
assert record.photoinfo == INFO_DATA
|
||||
record.export_options = 1
|
||||
assert record.export_options == 1
|
||||
|
||||
# close and re-open
|
||||
db.close()
|
||||
db2 = ExportDB(dbname, tempdir.name)
|
||||
record = db2.get_file_record(filepath)
|
||||
assert record.uuid == uuid
|
||||
assert record.filepath == pathlib.Path(filepath).name
|
||||
assert record.filepath_normalized == pathlib.Path(filepath).name.lower()
|
||||
assert record.src_sig == (1, 2, 3)
|
||||
assert record.dest_sig == (4, 5, 6)
|
||||
assert record.digest == "FIZZ"
|
||||
assert record.exifdata == EXIF_DATA
|
||||
assert record.photoinfo == INFO_DATA
|
||||
assert record.export_options == 1
|
||||
|
||||
|
||||
def test_export_record_context_manager():
|
||||
"""Test ExportRecord as context manager"""
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
filepath = os.path.join(tempdir.name, "test.JPG")
|
||||
uuid = "FOOBAR_CONTEXT"
|
||||
dbname = os.path.join(tempdir.name, ".osxphotos_export.db")
|
||||
db = ExportDB(dbname, tempdir.name)
|
||||
|
||||
assert db.get_file_record(filepath) is None
|
||||
|
||||
with db.create_file_record(filepath, uuid) as record:
|
||||
record.src_sig = (1, 2, 3.0)
|
||||
record.dest_sig = (4, 5, 6.0)
|
||||
record.digest = DIGEST_DATA
|
||||
record.exifdata = EXIF_DATA
|
||||
record.photoinfo = INFO_DATA
|
||||
record.export_options = 1
|
||||
|
||||
assert record.uuid == uuid
|
||||
assert record.filepath == pathlib.Path(filepath).name
|
||||
assert record.filepath_normalized == pathlib.Path(filepath).name.lower()
|
||||
assert record.src_sig == (1, 2, 3)
|
||||
assert record.dest_sig == (4, 5, 6)
|
||||
assert record.digest == "FIZZ"
|
||||
assert record.exifdata == EXIF_DATA
|
||||
assert record.photoinfo == INFO_DATA
|
||||
assert record.export_options == 1
|
||||
|
||||
# close and re-open
|
||||
db.close()
|
||||
db2 = ExportDB(dbname, tempdir.name)
|
||||
record = db2.get_file_record(filepath)
|
||||
assert record.uuid == uuid
|
||||
assert record.filepath == pathlib.Path(filepath).name
|
||||
assert record.filepath_normalized == pathlib.Path(filepath).name.lower()
|
||||
assert record.src_sig == (1, 2, 3)
|
||||
assert record.dest_sig == (4, 5, 6)
|
||||
assert record.digest == "FIZZ"
|
||||
assert record.exifdata == EXIF_DATA
|
||||
assert record.photoinfo == INFO_DATA
|
||||
assert record.export_options == 1
|
||||
|
||||
|
||||
def test_export_record_context_manager_error():
|
||||
"""Test ExportRecord as context manager doesn't commit data on error"""
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
filepath = os.path.join(tempdir.name, "test_boom.JPG")
|
||||
uuid = "FOOBAR_CONTEXT_BOOM"
|
||||
dbname = os.path.join(tempdir.name, ".osxphotos_export.db")
|
||||
db = ExportDB(dbname, tempdir.name)
|
||||
|
||||
try:
|
||||
with db.create_file_record(filepath, uuid) as record:
|
||||
record.src_sig = (1, 2, 3.0)
|
||||
record.dest_sig = (4, 5, 6.0)
|
||||
record.digest = DIGEST_DATA
|
||||
record.exifdata = EXIF_DATA
|
||||
record.photoinfo = INFO_DATA
|
||||
raise Exception("Boom")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
record = db.get_file_record(filepath)
|
||||
assert record.uuid == uuid
|
||||
assert record.filepath == pathlib.Path(filepath).name
|
||||
assert record.filepath_normalized == pathlib.Path(filepath).name.lower()
|
||||
assert record.src_sig == (None, None, None)
|
||||
assert record.dest_sig == (None, None, None)
|
||||
assert record.digest is None
|
||||
assert record.exifdata is None
|
||||
assert record.photoinfo is None
|
||||
|
||||
|
||||
def test_get_export_db_version():
|
||||
"""Test export_db_get_version"""
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
|
||||
dbname = os.path.join(tempdir.name, ".osxphotos_export.db")
|
||||
db = ExportDB(dbname, tempdir.name)
|
||||
|
||||
osxphotos_ver, export_db_ver = export_db_get_version(dbname)
|
||||
assert osxphotos_ver == __version__
|
||||
assert export_db_ver == OSXPHOTOS_EXPORTDB_VERSION
|
||||
|
||||
Reference in New Issue
Block a user