Skip to content

Commit

Permalink
Cleared code test coverage hurdle for auto-delete date mismatch iclou…
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen Martin committed Jun 24, 2023
1 parent c8bab80 commit 4d797ea
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion icloudpd/autodelete.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def autodelete_photos(icloud, folder_structure, directory):
created_date = media.created.astimezone(get_localzone())
except (ValueError, OSError):
logger.set_tqdm_description(
f"Could not convert media created date to local timezone {created_date}",
f"Could not convert media created date to local timezone {media.created}",
logging.ERROR)
created_date = media.created

Expand Down
105 changes: 105 additions & 0 deletions tests/test_autodelete_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import os
import shutil
import pytest
import mock
import datetime
from click.testing import CliRunner
from pyicloud_ipd.services.photos import PhotoAsset
from icloudpd.base import main
from tests.helpers.print_result_exception import print_result_exception
import inspect
import glob

Expand All @@ -16,6 +20,107 @@ class AutodeletePhotosTestCase(TestCase):
def inject_fixtures(self, caplog):
self._caplog = caplog

def test_autodelete_invalid_creation_date(self):
base_dir = os.path.normpath(f"tests/fixtures/Photos/{inspect.stack()[0][3]}")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

files = [
"2018/01/01/IMG_3589.JPG"
]

with mock.patch.object(PhotoAsset, "created", new_callable=mock.PropertyMock) as dt_mock:
# Can't mock `astimezone` because it's a readonly property, so have to
# create a new class that inherits from datetime.datetime
class NewDateTime(datetime.datetime):
def astimezone(self, tz=None):
raise ValueError('Invalid date')
dt_mock.return_value = NewDateTime(2018,1,1,0,0,0)

with vcr.use_cassette("tests/vcr_cassettes/download_autodelete_photos.yml"):
# Pass fixed client ID via environment variable
runner = CliRunner(env={
"CLIENT_ID": "DE309E26-942E-11E8-92F5-14109FE0B321"
})
result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--password",
"password1",
"--recent",
"1",
"--delete-after-download",
"-d",
base_dir,
],
)

self.assertIn("DEBUG Looking up all photos and videos from album All Photos...", self._caplog.text)
self.assertIn(
f"INFO Downloading the first original photo or video to {base_dir} ...",
self._caplog.text,
)
self.assertIn(
f"ERROR Could not convert photo created date to local timezone (2018-01-01 00:00:00)",
self._caplog.text,
)
self.assertIn(
f"INFO Downloading {os.path.join(base_dir, os.path.normpath('2018/01/01/IMG_3589.JPG'))}",
self._caplog.text,
)
self.assertIn(
f"INFO Deleting IMG_3589.JPG",
self._caplog.text,
)
self.assertIn(
"INFO All photos have been downloaded!", self._caplog.text
)

# check files
for file_name in files:
assert os.path.exists(os.path.join(base_dir, file_name)), f"{file_name} expected, but missing"

result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--password",
"password1",
"--recent",
"0",
"--auto-delete",
"-d",
base_dir,
],
)
print_result_exception(result)

self.assertIn("DEBUG Looking up all photos and videos from album All Photos...", self._caplog.text)
self.assertIn(
f"INFO Downloading 0 original photos and videos to {base_dir} ...",
self._caplog.text,
)
self.assertIn(
f"INFO All photos have been downloaded!", self._caplog.text
)
self.assertIn(
f"INFO Deleting any files found in 'Recently Deleted'...",
self._caplog.text,
)

self.assertIn(
f"INFO Deleting {os.path.join(base_dir, os.path.normpath('2018/01/01/IMG_3589.JPG'))}",
self._caplog.text,
)

for file_name in files:
assert not os.path.exists(
os.path.join(base_dir, file_name)), f"{file_name} not expected, but present"

def test_download_autodelete_photos(self):
base_dir = os.path.normpath(f"tests/fixtures/Photos/{inspect.stack()[0][3]}")
if os.path.exists(base_dir):
Expand Down

0 comments on commit 4d797ea

Please sign in to comment.