From 77266a7bd4263a004d35c240c643e4c4a7a2bf54 Mon Sep 17 00:00:00 2001 From: Jaisen Mathai Date: Wed, 20 Apr 2016 22:23:57 -0700 Subject: [PATCH] gh-108 Add tests for the import_file function in elodie.py --- elodie.py | 2 + elodie/geolocation.py | 5 ++ elodie/tests/elodie_test.py | 93 +++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 elodie/tests/elodie_test.py diff --git a/elodie.py b/elodie.py index 382c0b9..3340153 100755 --- a/elodie.py +++ b/elodie.py @@ -60,6 +60,8 @@ def import_file(_file, destination, album_from_folder, trash): if trash: send2trash(_file) + return dest_path or None + @click.command('import') @click.option('--destination', type=click.Path(file_okay=False), diff --git a/elodie/geolocation.py b/elodie/geolocation.py index 37d91e8..0e3280c 100644 --- a/elodie/geolocation.py +++ b/elodie/geolocation.py @@ -126,6 +126,11 @@ def get_key(): def place_name(lat, lon): + # Convert lat/lon to floats + if not isinstance(lat, float): + lat = float(lat) + if not isinstance(lon, float): + lon = float(lon) # Try to get cached location first db = Db() diff --git a/elodie/tests/elodie_test.py b/elodie/tests/elodie_test.py new file mode 100644 index 0000000..381f37f --- /dev/null +++ b/elodie/tests/elodie_test.py @@ -0,0 +1,93 @@ +# Project imports +from imp import load_source +import os +import sys +import shutil + +from nose.plugins.skip import SkipTest + +sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) +sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))) + +import helper +elodie = load_source('elodie', os.path.abspath('{}/../../elodie.py'.format(os.path.dirname(os.path.realpath(__file__))))) + +from elodie import constants + +os.environ['TZ'] = 'GMT' + + +def test_import_file_text(): + temporary_folder, folder = helper.create_working_folder() + temporary_folder_destination, folder_destination = helper.create_working_folder() + + origin = '%s/valid.txt' % folder + shutil.copyfile(helper.get_file('valid.txt'), origin) + + reset_hash_db() + dest_path = elodie.import_file(origin, folder_destination, False, False) + restore_hash_db() + + shutil.rmtree(folder) + shutil.rmtree(folder_destination) + + assert helper.path_tz_fix(os.path.join('2016-04-Apr','Unknown Location','2016-04-07_11-15-26-valid-sample-title.txt')) in dest_path, dest_path + +def test_import_file_audio(): + raise SkipTest('gh-61 this test fails on travisci') + temporary_folder, folder = helper.create_working_folder() + temporary_folder_destination, folder_destination = helper.create_working_folder() + + origin = '%s/audio.m4a' % folder + shutil.copyfile(helper.get_file('audio.m4a'), origin) + + reset_hash_db() + dest_path = elodie.import_file(origin, folder_destination, False, False) + restore_hash_db() + + shutil.rmtree(folder) + shutil.rmtree(folder_destination) + + assert helper.path_tz_fix(os.path.join('2016-01-Jan','Houston','2016-01-04_05-28-15-audio-test-audio.m4a')) in dest_path, dest_path + +def test_import_file_photo(): + temporary_folder, folder = helper.create_working_folder() + temporary_folder_destination, folder_destination = helper.create_working_folder() + + origin = '%s/plain.jpg' % folder + shutil.copyfile(helper.get_file('plain.jpg'), origin) + + reset_hash_db() + dest_path = elodie.import_file(origin, folder_destination, False, False) + restore_hash_db() + + shutil.rmtree(folder) + shutil.rmtree(folder_destination) + + assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-plain.jpg')) in dest_path, dest_path + +def test_import_file_video(): + temporary_folder, folder = helper.create_working_folder() + temporary_folder_destination, folder_destination = helper.create_working_folder() + + origin = '%s/video.mov' % folder + shutil.copyfile(helper.get_file('video.mov'), origin) + + reset_hash_db() + dest_path = elodie.import_file(origin, folder_destination, False, False) + restore_hash_db() + + shutil.rmtree(folder) + shutil.rmtree(folder_destination) + + assert helper.path_tz_fix(os.path.join('2015-01-Jan','California','2015-01-19_12-45-11-video.mov')) in dest_path, dest_path + +def reset_hash_db(): + hash_db = constants.hash_db + if os.path.isfile(hash_db): + os.rename(hash_db, '{}-test'.format(hash_db)) + +def restore_hash_db(): + hash_db = '{}-test'.format(constants.hash_db) + if os.path.isfile(hash_db): + os.rename(hash_db, hash_db.replace('-test', ''))