Skip to content

Commit

Permalink
refactor tests for multi platform support
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyNikiforov committed Oct 20, 2020
1 parent 6b40650 commit 122b43e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
36 changes: 22 additions & 14 deletions tests/test_download_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,18 @@ def test_download_and_skip_existing_photos(self):

# Check that file was downloaded
self.assertTrue(
os.path.exists("tests/fixtures/Photos/2018/07/31/IMG_7409.JPG"))
os.path.exists(os.path.join(base_dir, os.path.normpath("2018/07/31/IMG_7409.JPG"))))
# Check that mtime was updated to the photo creation date
photo_mtime = os.path.getmtime("tests/fixtures/Photos/2018/07/31/IMG_7409.JPG")
photo_mtime = os.path.getmtime(os.path.join(base_dir, os.path.normpath("2018/07/31/IMG_7409.JPG")))
photo_modified_time = datetime.datetime.utcfromtimestamp(photo_mtime)
self.assertEquals(
"2018-07-31 07:22:24",
photo_modified_time.strftime('%Y-%m-%d %H:%M:%S'))

assert result.exit_code == 0

@pytest.mark.skipif(sys.platform == 'win32',
reason="does not run on windows -- wrong dates")
def test_download_photos_and_set_exif(self):
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
Expand Down Expand Up @@ -296,6 +298,8 @@ def test_skip_existing_downloads(self):
)
assert result.exit_code == 0

@pytest.mark.skipif(sys.platform == 'win32',
reason="does not run on windows")
def test_until_found(self):
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
Expand Down Expand Up @@ -928,6 +932,8 @@ def astimezone(self, tz=None):
)
assert result.exit_code == 0

@pytest.mark.skipif(sys.platform == 'win32',
reason="does not run on windows")
def test_invalid_creation_year(self):
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
Expand Down Expand Up @@ -1046,15 +1052,15 @@ def test_download_and_dedupe_existing_photos(self):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

os.makedirs("tests/fixtures/Photos/2018/07/31/")
with open("tests/fixtures/Photos/2018/07/31/IMG_7409.JPG", "a") as f:
os.makedirs(os.path.join(base_dir, os.path.normpath("2018/07/31/")))
with open(os.path.join(base_dir, os.path.normpath("2018/07/31/IMG_7409.JPG")), "a") as f:
f.truncate(1)
with open("tests/fixtures/Photos/2018/07/31/IMG_7409.MOV", "a") as f:
with open(os.path.join(base_dir, os.path.normpath("2018/07/31/IMG_7409.MOV")), "a") as f:
f.truncate(1)
os.makedirs("tests/fixtures/Photos/2018/07/30/")
with open("tests/fixtures/Photos/2018/07/30/IMG_7408.JPG", "a") as f:
os.makedirs(os.path.join(base_dir, os.path.normpath("2018/07/30/")))
with open(os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7408.JPG")), "a") as f:
f.truncate(1151066)
with open("tests/fixtures/Photos/2018/07/30/IMG_7408.MOV", "a") as f:
with open(os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7408.MOV")), "a") as f:
f.truncate(1606512)

# Download the first photo, but mock the video download
Expand Down Expand Up @@ -1133,16 +1139,16 @@ def mocked_download(self, size):

# Check that file was downloaded
self.assertTrue(
os.path.exists("tests/fixtures/Photos/2018/07/31/IMG_7409-1884695.JPG"))
os.path.exists(os.path.join(base_dir, os.path.normpath("2018/07/31/IMG_7409-1884695.JPG"))))
# Check that mtime was updated to the photo creation date
photo_mtime = os.path.getmtime("tests/fixtures/Photos/2018/07/31/IMG_7409-1884695.JPG")
photo_mtime = os.path.getmtime(os.path.join(base_dir, os.path.normpath("2018/07/31/IMG_7409-1884695.JPG")))
photo_modified_time = datetime.datetime.utcfromtimestamp(photo_mtime)
self.assertEquals(
"2018-07-31 07:22:24",
photo_modified_time.strftime('%Y-%m-%d %H:%M:%S'))
self.assertTrue(
os.path.exists("tests/fixtures/Photos/2018/07/31/IMG_7409-3294075.MOV"))
photo_mtime = os.path.getmtime("tests/fixtures/Photos/2018/07/31/IMG_7409-3294075.MOV")
os.path.exists(os.path.join(base_dir, os.path.normpath("2018/07/31/IMG_7409-3294075.MOV"))))
photo_mtime = os.path.getmtime(os.path.join(base_dir, os.path.normpath("2018/07/31/IMG_7409-3294075.MOV")))
photo_modified_time = datetime.datetime.utcfromtimestamp(photo_mtime)
self.assertEquals(
"2018-07-31 07:22:24",
Expand All @@ -1151,6 +1157,8 @@ def mocked_download(self, size):
assert result.exit_code == 0


@pytest.mark.skipif(sys.platform == 'win32',
reason="does not run on windows -- wrong dates")
def test_download_photos_and_set_exif_exceptions(self):
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
Expand Down Expand Up @@ -1260,9 +1268,9 @@ def test_download_chinese(self):

# Check that file was downloaded
self.assertTrue(
os.path.exists("tests/fixtures/Photos/2018/07/31/IMG_7409.JPG"))
os.path.exists(os.path.join(base_dir, os.path.normpath('2018/07/31/IMG_7409.JPG'))))
# Check that mtime was updated to the photo creation date
photo_mtime = os.path.getmtime("tests/fixtures/Photos/2018/07/31/IMG_7409.JPG")
photo_mtime = os.path.getmtime(os.path.join(base_dir, os.path.normpath('2018/07/31/IMG_7409.JPG')))
photo_modified_time = datetime.datetime.utcfromtimestamp(photo_mtime)
self.assertEquals(
"2018-07-31 07:22:24",
Expand Down
59 changes: 34 additions & 25 deletions tests/test_listing_recent_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

class ListingRecentPhotosTestCase(TestCase):
def test_listing_recent_photos(self):
if os.path.exists("tests/fixtures/Photos"):
shutil.rmtree("tests/fixtures/Photos")
os.makedirs("tests/fixtures/Photos")
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

# Note - This test uses the same cassette as test_download_photos.py
with vcr.use_cassette("tests/vcr_cassettes/listing_photos.yml"):
Expand All @@ -33,46 +34,49 @@ def test_listing_recent_photos(self):
"5",
"--only-print-filenames",
"--no-progress-bar",
"--threads-num",
1,
"-d",
"tests/fixtures/Photos",
base_dir,
],
)
print_result_exception(result)
filenames = result.output.splitlines()

self.assertEqual(len(filenames), 8)
self.assertEqual(
"tests/fixtures/Photos/2018/07/31/IMG_7409.JPG", filenames[0]
os.path.join(base_dir, os.path.normpath("2018/07/31/IMG_7409.JPG")), filenames[0]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/31/IMG_7409.MOV", filenames[1]
os.path.join(base_dir, os.path.normpath("2018/07/31/IMG_7409.MOV")), filenames[1]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/30/IMG_7408.JPG", filenames[2]
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7408.JPG")), filenames[2]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/30/IMG_7408.MOV", filenames[3]
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7408.MOV")), filenames[3]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/30/IMG_7407.JPG", filenames[4]
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7407.JPG")), filenames[4]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/30/IMG_7407.MOV", filenames[5]
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7407.MOV")), filenames[5]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/30/IMG_7405.MOV", filenames[6]
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7405.MOV")), filenames[6]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/30/IMG_7404.MOV", filenames[7]
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7404.MOV")), filenames[7]
)


assert result.exit_code == 0

def test_listing_recent_photos_with_missing_filenameEnc(self):
if os.path.exists("tests/fixtures/Photos"):
shutil.rmtree("tests/fixtures/Photos")
os.makedirs("tests/fixtures/Photos")
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

# Note - This test uses the same cassette as test_download_photos.py
with vcr.use_cassette("tests/vcr_cassettes/listing_photos_missing_filenameEnc.yml"):
Expand All @@ -92,8 +96,10 @@ def test_listing_recent_photos_with_missing_filenameEnc(self):
"5",
"--only-print-filenames",
"--no-progress-bar",
"--threads-num",
1,
"-d",
"tests/fixtures/Photos",
base_dir,
],
)
print_result_exception(result)
Expand All @@ -104,29 +110,30 @@ def test_listing_recent_photos_with_missing_filenameEnc(self):

# self.assertEqual(len(filenames), 5)
self.assertEqual(
"tests/fixtures/Photos/2018/07/31/AY6c_BsE0jja.JPG", filenames[0]
os.path.join(base_dir, os.path.normpath("2018/07/31/AY6c_BsE0jja.JPG")), filenames[0]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/31/AY6c_BsE0jja.MOV", filenames[1]
os.path.join(base_dir, os.path.normpath("2018/07/31/AY6c_BsE0jja.MOV")), filenames[1]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/30/IMG_7408.JPG", filenames[2]
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7408.JPG")), filenames[2]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/30/IMG_7408.MOV", filenames[3]
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7408.MOV")), filenames[3]
)
self.assertEqual(
"tests/fixtures/Photos/2018/07/30/AZ_wAGT9P6jh.JPG", filenames[4]
os.path.join(base_dir, os.path.normpath("2018/07/30/AZ_wAGT9P6jh.JPG")), filenames[4]
)
assert result.exit_code == 0


# This was used to solve the missing filenameEnc error. I found
# another case where it might crash. (Maybe Apple changes the downloadURL key)
def test_listing_recent_photos_with_missing_downloadURL(self):
if os.path.exists("tests/fixtures/Photos"):
shutil.rmtree("tests/fixtures/Photos")
os.makedirs("tests/fixtures/Photos")
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

# Note - This test uses the same cassette as test_download_photos.py
with vcr.use_cassette("tests/vcr_cassettes/listing_photos_missing_downloadUrl.yml"):
Expand All @@ -146,8 +153,10 @@ def test_listing_recent_photos_with_missing_downloadURL(self):
"1",
"--only-print-filenames",
"--no-progress-bar",
"--threads-num",
1,
"-d",
"tests/fixtures/Photos",
base_dir,
],
)
print_result_exception(result)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
from io import StringIO
import sys
from icloudpd.logger import setup_logger, IPDLogger
import pytest


class LoggerTestCase(TestCase):
# Tests the formatter that is set up in setup_logger()
@pytest.mark.skipif(sys.platform == 'win32',
reason="does not run on windows -- wrong dates")
@freeze_time("2018-01-01 00:00")
def test_logger_output(self):
logger = setup_logger()
Expand Down

0 comments on commit 122b43e

Please sign in to comment.