-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleared code test coverage hurdle for auto-delete date mismatch iclou…
- Loading branch information
Steffen Martin
committed
Jun 24, 2023
1 parent
c8bab80
commit 4d797ea
Showing
2 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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): | ||
|