Template system now supports default values
This commit is contained in:
@@ -109,11 +109,7 @@ def test_PlaceInfo5():
|
||||
assert place.name == "Washington, District of Columbia, United States"
|
||||
assert place.names.street_address == ["2038 18th St NW"]
|
||||
assert place.names.additional_city_info == ["Adams Morgan"]
|
||||
assert place.names.city == [
|
||||
"Washington",
|
||||
"Washington",
|
||||
"Washington",
|
||||
]
|
||||
assert place.names.city == ["Washington", "Washington", "Washington"]
|
||||
assert place.names.state_province == ["District of Columbia"]
|
||||
assert place.names.country == ["United States"]
|
||||
assert place.country_code == "US"
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
""" Test PlaceInfo """
|
||||
import pytest
|
||||
|
||||
from osxphotos._constants import _UNKNOWN_PERSON
|
||||
|
||||
|
||||
PHOTOS_DB = "./tests/Test-Places-Catalina-10_15_1.photoslibrary/database/photos.db"
|
||||
|
||||
@@ -62,23 +60,11 @@ def test_place_place_info_2():
|
||||
assert not photo.place.ishome
|
||||
assert photo.place.name == "Maui, Wailea, Hawai'i, United States"
|
||||
assert photo.place.names.street_address == ["3700 Wailea Alanui Dr"]
|
||||
assert photo.place.names.city == [
|
||||
"Wailea",
|
||||
"Kihei",
|
||||
"Kihei",
|
||||
]
|
||||
assert photo.place.names.region == [
|
||||
"Maui",
|
||||
]
|
||||
assert photo.place.names.sub_administrative_area == [
|
||||
"Maui",
|
||||
]
|
||||
assert photo.place.names.state_province == [
|
||||
"Hawai'i",
|
||||
]
|
||||
assert photo.place.names.country == [
|
||||
"United States",
|
||||
]
|
||||
assert photo.place.names.city == ["Wailea", "Kihei", "Kihei"]
|
||||
assert photo.place.names.region == ["Maui"]
|
||||
assert photo.place.names.sub_administrative_area == ["Maui"]
|
||||
assert photo.place.names.state_province == ["Hawai'i"]
|
||||
assert photo.place.names.country == ["United States"]
|
||||
|
||||
assert photo.place.country_code == "US"
|
||||
assert (
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
""" Test PlaceInfo """
|
||||
import pytest
|
||||
|
||||
from osxphotos._constants import _UNKNOWN_PERSON
|
||||
|
||||
PHOTOS_DB = "./tests/Test-Places-High-Sierra-10.13.6.photoslibrary/database/photos.db"
|
||||
|
||||
UUID_DICT = {
|
||||
@@ -144,6 +142,7 @@ def test_place_place_info_4():
|
||||
assert photo.place.names.sub_throughfare == []
|
||||
assert photo.place.names.body_of_water == ["River Torrens"]
|
||||
|
||||
|
||||
def test_place_no_place_info():
|
||||
# test valid place info
|
||||
import osxphotos
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
""" Test PlaceInfo """
|
||||
import pytest
|
||||
|
||||
from osxphotos._constants import _UNKNOWN_PERSON
|
||||
|
||||
PHOTOS_DB = "./tests/Test-10.14.6.photoslibrary/database/photos.db"
|
||||
|
||||
UUID_DICT = {"place_uk": "3Jn73XpSQQCluzRBMWRsMA", "no_place": "15uNd7%8RguTEgNPKHfTWw"}
|
||||
@@ -58,7 +56,7 @@ def test_place_str():
|
||||
"names='PlaceNames(field0=[], country=['United Kingdom'], "
|
||||
"state_province=['England'], sub_administrative_area=['London'], "
|
||||
"city=['Westminster'], field5=[], additional_city_info=[], ocean=[], "
|
||||
"area_of_interest=[\"St James's Park\"], inland_water=[], field10=[], "
|
||||
'area_of_interest=["St James\'s Park"], inland_water=[], field10=[], '
|
||||
"region=[], sub_throughfare=[], field13=[], postal_code=[], field15=[], "
|
||||
"field16=[], street_address=[], body_of_water=[])', country_code='GB')"
|
||||
)
|
||||
|
||||
120
tests/test_template.py
Normal file
120
tests/test_template.py
Normal file
@@ -0,0 +1,120 @@
|
||||
""" Test template.py """
|
||||
import pytest
|
||||
|
||||
PHOTOS_DB = "./tests/Test-Places-Catalina-10_15_1.photoslibrary/database/photos.db"
|
||||
|
||||
UUID_DICT = {"place_dc": "128FB4C6-0B16-4E7D-9108-FB2E90DA1546"}
|
||||
|
||||
|
||||
TEMPLATE_VALUES = {
|
||||
"{name}": "128FB4C6-0B16-4E7D-9108-FB2E90DA1546",
|
||||
"{original_name}": "IMG_1064",
|
||||
"{title}": "Glen Ord",
|
||||
"{descr}": "Jack Rose Dining Saloon",
|
||||
"{created.date}": "2020-02-04",
|
||||
"{created.year}": "2020",
|
||||
"{created.yy}": "20",
|
||||
"{created.mm}": "02",
|
||||
"{created.month}": "February",
|
||||
"{created.mon}": "Feb",
|
||||
"{created.doy}": "035",
|
||||
"{modified.date}": "2020-03-21",
|
||||
"{modified.year}": "2020",
|
||||
"{modified.yy}": "20",
|
||||
"{modified.mm}": "03",
|
||||
"{modified.month}": "March",
|
||||
"{modified.mon}": "Mar",
|
||||
"{modified.doy}": "081",
|
||||
"{place.name}": "Washington, District of Columbia, United States",
|
||||
"{place.name.country}": "United States",
|
||||
"{place.name.state_province}": "District of Columbia",
|
||||
"{place.name.city}": "Washington",
|
||||
"{place.name.area_of_interest}": "_",
|
||||
"{place.address}": "2038 18th St NW, Washington, DC 20009, United States",
|
||||
"{place.address.street}": "2038 18th St NW",
|
||||
"{place.address.city}": "Washington",
|
||||
"{place.address.state_province}": "DC",
|
||||
"{place.address.postal_code}": "20009",
|
||||
"{place.address.country}": "United States",
|
||||
"{place.address.country_code}": "US",
|
||||
}
|
||||
|
||||
|
||||
def test_lookup():
|
||||
""" Test that a lookup is returned for every possible value """
|
||||
import re
|
||||
import osxphotos
|
||||
from osxphotos.template import (
|
||||
get_template_value,
|
||||
render_filepath_template,
|
||||
TEMPLATE_SUBSTITUTIONS,
|
||||
)
|
||||
|
||||
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
|
||||
photo = photosdb.photos(uuid=[UUID_DICT["place_dc"]])[0]
|
||||
|
||||
for subst in TEMPLATE_SUBSTITUTIONS:
|
||||
lookup_str = re.match(r"\{([^\\,}]+)\}", subst).group(1)
|
||||
lookup = get_template_value(lookup_str, photo)
|
||||
assert lookup or lookup is None
|
||||
|
||||
|
||||
def test_subst():
|
||||
""" Test that substitutions are correct """
|
||||
import locale
|
||||
import osxphotos
|
||||
from osxphotos.template import render_filepath_template
|
||||
|
||||
locale.setlocale(locale.LC_ALL, "en_US")
|
||||
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
|
||||
photo = photosdb.photos(uuid=[UUID_DICT["place_dc"]])[0]
|
||||
|
||||
for template in TEMPLATE_VALUES:
|
||||
rendered, _ = render_filepath_template(template, photo)
|
||||
assert rendered == TEMPLATE_VALUES[template]
|
||||
|
||||
|
||||
def test_subst_default_val():
|
||||
""" Test substitution with default value specified """
|
||||
import locale
|
||||
import osxphotos
|
||||
from osxphotos.template import render_filepath_template
|
||||
|
||||
locale.setlocale(locale.LC_ALL, "en_US")
|
||||
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
|
||||
photo = photosdb.photos(uuid=[UUID_DICT["place_dc"]])[0]
|
||||
|
||||
template = "{place.name.area_of_interest,UNKNOWN}"
|
||||
rendered, _ = render_filepath_template(template, photo)
|
||||
assert rendered == "UNKNOWN"
|
||||
|
||||
|
||||
def test_subst_default_val_2():
|
||||
""" Test substitution with ',' but no default value """
|
||||
import locale
|
||||
import osxphotos
|
||||
from osxphotos.template import render_filepath_template
|
||||
|
||||
locale.setlocale(locale.LC_ALL, "en_US")
|
||||
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
|
||||
photo = photosdb.photos(uuid=[UUID_DICT["place_dc"]])[0]
|
||||
|
||||
template = "{place.name.area_of_interest,}"
|
||||
rendered, _ = render_filepath_template(template, photo)
|
||||
assert rendered == "_"
|
||||
|
||||
|
||||
def test_subst_unknown_val():
|
||||
""" Test substitution with unknown value specified """
|
||||
import locale
|
||||
import osxphotos
|
||||
from osxphotos.template import render_filepath_template
|
||||
|
||||
locale.setlocale(locale.LC_ALL, "en_US")
|
||||
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
|
||||
photo = photosdb.photos(uuid=[UUID_DICT["place_dc"]])[0]
|
||||
|
||||
template = "{created.year}/{foo}"
|
||||
rendered, unknown = render_filepath_template(template, photo)
|
||||
assert rendered == "2020/{foo}"
|
||||
assert unknown == ["{foo}"]
|
||||
Reference in New Issue
Block a user