From 10b45cba78ae8e4b1d8919ef548e8478852ae6c1 Mon Sep 17 00:00:00 2001 From: Jaisen Mathai Date: Tue, 13 Sep 2016 20:10:29 -0700 Subject: [PATCH] gh-40 Increase test coverage for media and geolocation modules --- elodie/geolocation.py | 9 +++- elodie/tests/geolocation_test.py | 10 +++++ elodie/tests/media/base_test.py | 72 +++++++++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 6 deletions(-) diff --git a/elodie/geolocation.py b/elodie/geolocation.py index e0f4780..478de08 100644 --- a/elodie/geolocation.py +++ b/elodie/geolocation.py @@ -17,6 +17,8 @@ import urllib.error from elodie import constants from elodie.localstorage import Db +__KEY__ = None + def coordinates_by_name(name): # Try to get cached location first @@ -95,6 +97,10 @@ def dms_string(decimal, type='latitude'): def get_key(): + global __KEY__ + if __KEY__ is not None: + return __KEY__ + config_file = '%s/config.ini' % constants.application_directory if not path.exists(config_file): return None @@ -104,7 +110,8 @@ def get_key(): if('MapQuest' not in config.sections()): return None - return config.get('MapQuest', 'key') + __KEY__ = config.get('MapQuest', 'key') + return __KEY__ def place_name(lat, lon): diff --git a/elodie/tests/geolocation_test.py b/elodie/tests/geolocation_test.py index 7642ba8..22c64a9 100644 --- a/elodie/tests/geolocation_test.py +++ b/elodie/tests/geolocation_test.py @@ -75,3 +75,13 @@ def test_dms_string_longitude(): assert check_value in dms_string, '%s not in %s' % (check_value, dms_string) assert str(dms[0]) in dms_string, '%s not in %s' % (dms[0], dms_string) + +def test_reverse_lookup_with_invalid_key(): + geolocation.__KEY__ = 'invalid_key' + res = geolocation.reverse_lookup(123.45, 123.45) + assert res is None, res + +def test_lookup_with_invalid_key(): + geolocation.__KEY__ = 'invalid_key' + res = geolocation.lookup('foo') + assert res is None, res diff --git a/elodie/tests/media/base_test.py b/elodie/tests/media/base_test.py index 955d76b..365fbb0 100644 --- a/elodie/tests/media/base_test.py +++ b/elodie/tests/media/base_test.py @@ -24,6 +24,14 @@ from elodie.media.video import Video os.environ['TZ'] = 'GMT' + +def test_get_class_by_file_without_extension(): + base_file = helper.get_file('withoutextension') + + cls = Base.get_class_by_file(base_file, [Audio, Text, Photo, Video]) + + assert cls is None, cls + def test_set_album_from_folder_invalid_file(): temporary_folder, folder = helper.create_working_folder() @@ -38,9 +46,63 @@ def test_set_album_from_folder_invalid_file(): assert status == False, status -def test_get_class_by_file_without_extension(): - base_file = helper.get_file('withoutextension') +def test_set_album_from_folder(): + temporary_folder, folder = helper.create_working_folder() - cls = Base.get_class_by_file(base_file, [Audio, Text, Photo, Video]) - - assert cls is None, cls + origin = '%s/photo.jpg' % folder + shutil.copyfile(helper.get_file('plain.jpg'), origin) + + photo = Photo(origin) + metadata = photo.get_metadata() + + assert metadata['album'] is None, metadata['album'] + + new_album_name = os.path.split(folder)[1] + status = photo.set_album_from_folder() + + assert status == True, status + + photo_new = Photo(origin) + metadata_new = photo_new.get_metadata() + + shutil.rmtree(folder) + + assert metadata_new['album'] == new_album_name, metadata_new['album'] + +def test_set_metadata(): + temporary_folder, folder = helper.create_working_folder() + + origin = '%s/photo.jpg' % folder + shutil.copyfile(helper.get_file('plain.jpg'), origin) + + photo = Photo(origin) + + metadata = photo.get_metadata() + + assert metadata['title'] == None, metadata['title'] + + new_title = 'Some Title' + photo.set_metadata(title = new_title) + + new_metadata = photo.get_metadata() + + assert new_metadata['title'] == new_title, new_metadata['title'] + +def test_set_metadata_basename(): + temporary_folder, folder = helper.create_working_folder() + + origin = '%s/photo.jpg' % folder + shutil.copyfile(helper.get_file('plain.jpg'), origin) + + photo = Photo(origin) + + metadata = photo.get_metadata() + + assert metadata['base_name'] == 'photo', metadata['base_name'] + + new_basename = 'Some Base Name' + photo.set_metadata_basename(new_basename) + + new_metadata = photo.get_metadata() + + assert new_metadata['base_name'] == new_basename, new_metadata['base_name']