Skip to content

Commit

Permalink
Initial tests for Folder
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Sep 14, 2020
1 parent fe1d7ff commit c3dc56d
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 4 deletions.
17 changes: 13 additions & 4 deletions photoscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def __init__(self, uuid):
else:
uuid = uuid.split("/")[0]

valuuidalbum = run_script("_album_exists", uuid)
valuuidalbum = run_script("_album_exists", id_)
if valuuidalbum:
self.id = id_
self._uuid = uuid
Expand Down Expand Up @@ -657,10 +657,19 @@ def __len__(self):

class Folder:
def __init__(self, uuid):
valid_folder = run_script("_folder_exists", uuid)
id_ = uuid
# check to see if we need to add UUID suffix
if float(PhotosLibrary().version) >= 5.0:
if len(uuid.split("/")) == 1:
# osxphotos style UUID without the suffix
id_ = f"{uuid}{UUID_SUFFIX_FOLDER}"
else:
uuid = uuid.split("/")[0]

valid_folder = run_script("_folder_exists", id_)
if valid_folder:
self.id = uuid
self._uuid = uuid.split("/")[0]
self.id = id_
self._uuid = uuid
else:
raise ValueError(f"Invalid folder id: {uuid}")

Expand Down
15 changes: 15 additions & 0 deletions tests/photoscript_config_catalina.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@
FOLDER_NAMES_ALL = ["Travel", "Folder1", "SubFolder1"]
FOLDER_NAMES_TOP = ["Travel", "Folder1"]

FOLDER_1_UUID = "1FB9BF4B-3CF5-45DE-B4E7-C92047341196/L0/020"
FOLDER_1_UUID_OSXPHOTOS = "1FB9BF4B-3CF5-45DE-B4E7-C92047341196"
FOLDER_1_NAME = "Folder1"
FOLDER_1_LEN = 1
FOLDER_2_UUID = "9BC83BEA-7598-4846-BBD7-AB14BE771825/L0/020"
FOLDER_2_NAME = "SubFolder1"
FOLDER_2_PATH_STR = "Folder1/SubFolder1"
FOLDER_2_PATH_STR_COLON = "Folder1:SubFolder1"
FOLDER_2_PATH = ["Folder1"]
FOLDER_2_LEN = 0
FOLDER_3_UUID = "3205FEEF-B22D-43D6-8D31-9A4D112B67E3/L0/020"
FOLDER_3_NAME = "Travel"
FOLDER_3_LEN = 1
FOLDER_3_ALBUMS = ["San Juan Capistrano"]

PHOTO_ADD_UUID = "3A71DE26-EDEF-41D3-86C1-E8328DFC9FA0/L0/001"
PHOTOS_FAVORITES = ["IMG_2510.JPG", "IMG_2768.JPG"]
PHOTOS_FAVORITES_SET = ["IMG_2242.JPG", "IMG_2510.JPG"]
Expand Down
176 changes: 176 additions & 0 deletions tests/test_3_folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
""" Test Folder class """

import pytest
from applescript import AppleScript
from tests.conftest import photoslib, suspend_capture, get_os_version


OS_VER = get_os_version()[1]
if OS_VER == "15":
from tests.photoscript_config_catalina import (
FOLDER_NAMES_ALL,
FOLDER_NAMES_TOP,
FOLDER_1_LEN,
FOLDER_1_NAME,
FOLDER_1_UUID,
FOLDER_1_UUID_OSXPHOTOS,
FOLDER_2_LEN,
FOLDER_2_NAME,
FOLDER_2_UUID,
FOLDER_2_PATH,
FOLDER_2_PATH_STR,
FOLDER_2_PATH_STR_COLON,
FOLDER_3_LEN,
FOLDER_3_NAME,
FOLDER_3_ALBUMS,
FOLDER_3_UUID,
FOLDER_NAME,
FOLDER_NAMES_ALL,
FOLDER_NAMES_TOP,
FOLDER_UUID,
IMPORT_PATHS,
IMPORT_PHOTOS,
NUM_PHOTOS,
PHOTO_ADD_UUID,
PHOTO_FAVORITES_SET_UUID,
PHOTO_FAVORITES_UNSET_UUID,
PHOTOS_FAVORITES,
PHOTOS_FAVORITES_SET,
PHOTOS_FILENAMES,
PHOTOS_PLANTS,
PHOTOS_UUID,
PHOTOS_UUID_FILENAMES,
SELECTION_UUIDS,
)
else:
pytest.exit("This test suite currently only runs on MacOS Catalina ")

########## Interactive tests run first ##########


########## Non-interactive tests ##########


def test_folder_init():
import photoscript

folder = photoscript.Folder(FOLDER_1_UUID)
assert isinstance(folder, photoscript.Folder)


def test_folder_init_osxphotos_uuid():
""" test Folder() with osxphotos style UUID """
import photoscript

folder = photoscript.Folder(FOLDER_1_UUID_OSXPHOTOS)
assert isinstance(folder, photoscript.Folder)
assert folder.id == FOLDER_1_UUID
assert folder.uuid == FOLDER_1_UUID_OSXPHOTOS


def test_folder_init_bad_uuid():
import photoscript

with pytest.raises(ValueError):
assert photoscript.Folder("BAD_UUID")


def test_folder_id():
import photoscript

folder = photoscript.Folder(FOLDER_1_UUID)
assert folder.id == FOLDER_1_UUID


def test_folder_uuid():
import photoscript

folder = photoscript.Folder(FOLDER_1_UUID)
assert folder.uuid == FOLDER_1_UUID_OSXPHOTOS


def test_folder_name_title(photoslib):
folder = photoslib.folder(FOLDER_1_NAME)
assert folder.name == FOLDER_1_NAME
assert folder.title == FOLDER_1_NAME


def test_folder_name_setter(photoslib):
folder = photoslib.folder(FOLDER_1_NAME)
new_name = "My New Folder Name"
folder.name = new_name
assert folder.name == new_name
assert folder.title == new_name


def test_folder_title_setter(photoslib):
folder = photoslib.folder(FOLDER_1_NAME)
new_name = "My New Folder Name"
folder.title = new_name
assert folder.name == new_name
assert folder.title == new_name


def test_folder_parent_id(photoslib):
folder = photoslib.folder(FOLDER_1_NAME)
assert folder.parent_id is None


def test_folder_parent_id(photoslib):
folder = photoslib.folder(FOLDER_2_NAME, top_level=False)
assert folder.parent_id == FOLDER_1_UUID


def test_folder_parent(photoslib):
import photoscript

folder = photoslib.folder(FOLDER_2_NAME, top_level=False)
parent = folder.parent
assert isinstance(parent, photoscript.Folder)
assert parent.name == FOLDER_1_NAME


def test_folder_parent_top_level(photoslib):
folder = photoslib.folder(FOLDER_NAMES_TOP[0])
assert folder.parent is None


def test_folder_path_str(photoslib):
folder = photoslib.folder(FOLDER_2_NAME, top_level=False)
assert folder.path_str() == FOLDER_2_PATH_STR
assert folder.path_str(":") == FOLDER_2_PATH_STR_COLON
with pytest.raises(ValueError):
assert folder.path_str("FOO")


def test_folder_path(photoslib):
import photoscript

folder = photoslib.folder(FOLDER_2_NAME, top_level=False)
path = folder.path()
assert len(path) == len(FOLDER_2_PATH)
for i, p in enumerate(path):
assert isinstance(p, photoscript.Folder)
assert p.name == FOLDER_2_PATH[i]

def test_folder_albums(photoslib):
folder = photoslib.folder(FOLDER_3_NAME)
albums = folder.albums
assert sorted(album.name for album in albums) == sorted(FOLDER_3_ALBUMS)

def test_len_1(photoslib):
""" test Album.__len__ """
folder = photoslib.folder(FOLDER_1_NAME)
assert len(folder) == FOLDER_1_LEN


def test_len_2(photoslib):
""" test Album.__len__ """
folder = photoslib.folder(FOLDER_2_NAME, top_level=False)
assert len(folder) == FOLDER_2_LEN


def test_len_3(photoslib):
""" test Album.__len__ """
folder = photoslib.folder(FOLDER_3_NAME)
assert len(folder) == FOLDER_3_LEN

0 comments on commit c3dc56d

Please sign in to comment.