2015-12-08 06:33:59 +01:00
|
|
|
# Project imports
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2015-12-09 08:40:01 +01:00
|
|
|
import re
|
2015-12-08 06:33:59 +01:00
|
|
|
import shutil
|
2016-01-27 15:28:18 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from datetime import timedelta
|
|
|
|
import time
|
2015-12-08 06:33:59 +01:00
|
|
|
|
|
|
|
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
|
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
import helper
|
|
|
|
from elodie.filesystem import FileSystem
|
2015-12-09 08:40:01 +01:00
|
|
|
from elodie.media.media import Media
|
|
|
|
from elodie.media.photo import Photo
|
|
|
|
from elodie.media.video import Video
|
2016-01-27 15:28:18 +01:00
|
|
|
from nose.plugins.skip import SkipTest
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
os.environ['TZ'] = 'GMT'
|
2015-12-08 06:33:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_directory_success():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
2016-01-26 20:01:05 +01:00
|
|
|
folder = os.path.join(helper.temp_dir(), helper.random_string(10))
|
2015-12-08 06:33:59 +01:00
|
|
|
status = filesystem.create_directory(folder)
|
|
|
|
|
|
|
|
# Needs to be a subdirectory
|
2015-12-11 08:02:43 +01:00
|
|
|
assert helper.temp_dir() != folder
|
2015-12-08 06:33:59 +01:00
|
|
|
|
|
|
|
assert status == True
|
|
|
|
assert os.path.isdir(folder) == True
|
|
|
|
assert os.path.exists(folder) == True
|
|
|
|
|
|
|
|
# Clean up
|
|
|
|
shutil.rmtree(folder)
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_directory_recursive_success():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
2016-01-26 20:01:05 +01:00
|
|
|
folder = os.path.join(helper.temp_dir(), helper.random_string(10), helper.random_string(10))
|
2015-12-08 06:33:59 +01:00
|
|
|
status = filesystem.create_directory(folder)
|
|
|
|
|
|
|
|
# Needs to be a subdirectory
|
2015-12-11 08:02:43 +01:00
|
|
|
assert helper.temp_dir() != folder
|
2015-12-08 06:33:59 +01:00
|
|
|
|
|
|
|
assert status == True
|
|
|
|
assert os.path.isdir(folder) == True
|
|
|
|
assert os.path.exists(folder) == True
|
|
|
|
|
|
|
|
shutil.rmtree(folder)
|
|
|
|
|
|
|
|
def test_create_directory_invalid_permissions():
|
2016-01-26 20:01:05 +01:00
|
|
|
if os.name == 'nt':
|
|
|
|
raise SkipTest("It isn't implemented on Windows")
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
2016-01-27 13:54:56 +01:00
|
|
|
status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist')
|
2015-12-08 06:33:59 +01:00
|
|
|
|
|
|
|
assert status == False
|
|
|
|
|
2015-12-09 08:40:01 +01:00
|
|
|
def test_delete_directory_if_empty():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
2016-01-26 20:01:05 +01:00
|
|
|
folder = os.path.join(helper.temp_dir(), helper.random_string(10))
|
2015-12-09 08:40:01 +01:00
|
|
|
os.makedirs(folder)
|
|
|
|
|
|
|
|
assert os.path.isdir(folder) == True
|
|
|
|
assert os.path.exists(folder) == True
|
|
|
|
|
|
|
|
filesystem.delete_directory_if_empty(folder)
|
|
|
|
|
|
|
|
assert os.path.isdir(folder) == False
|
|
|
|
assert os.path.exists(folder) == False
|
|
|
|
|
|
|
|
def test_delete_directory_if_empty_when_not_empty():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
2016-01-26 20:01:05 +01:00
|
|
|
folder = os.path.join(helper.temp_dir(), helper.random_string(10), helper.random_string(10))
|
2015-12-09 08:40:01 +01:00
|
|
|
os.makedirs(folder)
|
|
|
|
parent_folder = os.path.dirname(folder)
|
|
|
|
|
|
|
|
assert os.path.isdir(folder) == True
|
|
|
|
assert os.path.exists(folder) == True
|
|
|
|
assert os.path.isdir(parent_folder) == True
|
|
|
|
assert os.path.exists(parent_folder) == True
|
|
|
|
|
|
|
|
filesystem.delete_directory_if_empty(parent_folder)
|
|
|
|
|
|
|
|
assert os.path.isdir(folder) == True
|
|
|
|
assert os.path.exists(folder) == True
|
|
|
|
assert os.path.isdir(parent_folder) == True
|
|
|
|
assert os.path.exists(parent_folder) == True
|
|
|
|
|
|
|
|
shutil.rmtree(parent_folder)
|
|
|
|
|
|
|
|
def test_get_all_files_success():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
folder = helper.populate_folder(5)
|
2015-12-09 08:40:01 +01:00
|
|
|
files = filesystem.get_all_files(folder)
|
|
|
|
shutil.rmtree(folder)
|
|
|
|
|
2015-12-12 10:42:22 +01:00
|
|
|
length = len(files)
|
|
|
|
assert length == 5, length
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_all_files_by_extension():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
folder = helper.populate_folder(5)
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
files = filesystem.get_all_files(folder)
|
2015-12-12 10:42:22 +01:00
|
|
|
length = len(files)
|
|
|
|
assert length == 5, length
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
files = filesystem.get_all_files(folder, 'jpg')
|
2015-12-12 10:42:22 +01:00
|
|
|
length = len(files)
|
|
|
|
assert length == 3, length
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
files = filesystem.get_all_files(folder, 'txt')
|
2015-12-12 10:42:22 +01:00
|
|
|
length = len(files)
|
|
|
|
assert length == 2, length
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
files = filesystem.get_all_files(folder, 'gif')
|
2015-12-12 10:42:22 +01:00
|
|
|
length = len(files)
|
|
|
|
assert length == 0, length
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
shutil.rmtree(folder)
|
|
|
|
|
|
|
|
def test_get_current_directory():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
2015-12-09 08:40:01 +01:00
|
|
|
assert os.getcwd() == filesystem.get_current_directory()
|
|
|
|
|
|
|
|
def test_get_file_name_plain():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
media = Photo(helper.get_file('plain.jpg'))
|
2015-12-09 08:40:01 +01:00
|
|
|
file_name = filesystem.get_file_name(media)
|
|
|
|
|
2016-01-28 19:30:13 +01:00
|
|
|
assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain.jpg'), file_name
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
def test_get_file_name_with_title():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
media = Photo(helper.get_file('with-title.jpg'))
|
2015-12-09 08:40:01 +01:00
|
|
|
file_name = filesystem.get_file_name(media)
|
|
|
|
|
2016-01-28 19:30:13 +01:00
|
|
|
assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-with-title-some-title.jpg'), file_name
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
def test_get_folder_name_by_date():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
2015-12-09 08:40:01 +01:00
|
|
|
time_tuple = (2010, 4, 15, 1, 2, 3, 0, 0, 0)
|
|
|
|
folder_name = filesystem.get_folder_name_by_date(time_tuple)
|
|
|
|
|
2015-12-12 10:42:22 +01:00
|
|
|
assert folder_name == '2010-04-Apr', folder_name
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
time_tuple = (2010, 9, 15, 1, 2, 3, 0, 0, 0)
|
|
|
|
folder_name = filesystem.get_folder_name_by_date(time_tuple)
|
|
|
|
|
2015-12-12 10:42:22 +01:00
|
|
|
assert folder_name == '2010-09-Sep', folder_name
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
def test_get_folder_path_plain():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
media = Photo(helper.get_file('plain.jpg'))
|
2015-12-09 08:40:01 +01:00
|
|
|
path = filesystem.get_folder_path(media.get_metadata())
|
|
|
|
|
2016-01-26 20:01:05 +01:00
|
|
|
assert path == os.path.join('2015-12-Dec','Unknown Location'), path
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
def test_get_folder_path_with_title():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
media = Photo(helper.get_file('with-title.jpg'))
|
2015-12-09 08:40:01 +01:00
|
|
|
path = filesystem.get_folder_path(media.get_metadata())
|
|
|
|
|
2016-01-26 20:01:05 +01:00
|
|
|
assert path == os.path.join('2015-12-Dec','Unknown Location'), path
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
def test_get_folder_path_with_location():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
media = Photo(helper.get_file('with-location.jpg'))
|
2015-12-09 08:40:01 +01:00
|
|
|
path = filesystem.get_folder_path(media.get_metadata())
|
|
|
|
|
2016-01-26 20:01:05 +01:00
|
|
|
assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
def test_get_folder_path_with_location_and_title():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
media = Photo(helper.get_file('with-location-and-title.jpg'))
|
2015-12-09 08:40:01 +01:00
|
|
|
path = filesystem.get_folder_path(media.get_metadata())
|
|
|
|
|
2016-01-26 20:01:05 +01:00
|
|
|
assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
|
2015-12-09 08:40:01 +01:00
|
|
|
|
2016-02-14 09:55:39 +01:00
|
|
|
def test_process_file_invalid():
|
|
|
|
filesystem = FileSystem()
|
|
|
|
temporary_folder, folder = helper.create_working_folder()
|
|
|
|
|
|
|
|
origin = os.path.join(folder,'photo.jpg')
|
|
|
|
shutil.copyfile(helper.get_file('invalid.jpg'), origin)
|
|
|
|
|
|
|
|
media = Photo(origin)
|
|
|
|
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
|
|
|
|
|
|
|
|
assert destination is None
|
|
|
|
|
2015-12-09 08:40:01 +01:00
|
|
|
def test_process_file_plain():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
temporary_folder, folder = helper.create_working_folder()
|
2015-12-09 08:40:01 +01:00
|
|
|
|
2016-01-26 20:01:05 +01:00
|
|
|
origin = os.path.join(folder,'photo.jpg')
|
2015-12-11 08:02:43 +01:00
|
|
|
shutil.copyfile(helper.get_file('plain.jpg'), origin)
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
media = Photo(origin)
|
|
|
|
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
|
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
origin_checksum = helper.checksum(origin)
|
|
|
|
destination_checksum = helper.checksum(destination)
|
2015-12-10 05:22:56 +01:00
|
|
|
|
2015-12-09 08:40:01 +01:00
|
|
|
shutil.rmtree(folder)
|
|
|
|
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
|
|
|
|
|
2015-12-12 10:42:22 +01:00
|
|
|
assert origin_checksum is not None, origin_checksum
|
|
|
|
assert origin_checksum == destination_checksum, destination_checksum
|
2016-01-28 19:30:13 +01:00
|
|
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo.jpg')) in destination, destination
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
def test_process_file_with_title():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
temporary_folder, folder = helper.create_working_folder()
|
2015-12-09 08:40:01 +01:00
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
origin = '%s/photo.jpg' % folder
|
|
|
|
shutil.copyfile(helper.get_file('with-title.jpg'), origin)
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
media = Photo(origin)
|
|
|
|
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
|
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
origin_checksum = helper.checksum(origin)
|
|
|
|
destination_checksum = helper.checksum(destination)
|
2015-12-10 05:22:56 +01:00
|
|
|
|
2015-12-09 08:40:01 +01:00
|
|
|
shutil.rmtree(folder)
|
|
|
|
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
|
|
|
|
|
2015-12-12 10:42:22 +01:00
|
|
|
assert origin_checksum is not None, origin_checksum
|
|
|
|
assert origin_checksum == destination_checksum, destination_checksum
|
2016-01-28 19:30:13 +01:00
|
|
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
def test_process_file_with_location():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
temporary_folder, folder = helper.create_working_folder()
|
2015-12-09 08:40:01 +01:00
|
|
|
|
2016-01-26 20:01:05 +01:00
|
|
|
origin = os.path.join(folder,'photo.jpg')
|
2015-12-11 08:02:43 +01:00
|
|
|
shutil.copyfile(helper.get_file('with-location.jpg'), origin)
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
media = Photo(origin)
|
|
|
|
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
|
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
origin_checksum = helper.checksum(origin)
|
|
|
|
destination_checksum = helper.checksum(destination)
|
2015-12-10 05:22:56 +01:00
|
|
|
|
2015-12-09 08:40:01 +01:00
|
|
|
shutil.rmtree(folder)
|
|
|
|
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
|
|
|
|
|
2015-12-12 10:42:22 +01:00
|
|
|
assert origin_checksum is not None, origin_checksum
|
|
|
|
assert origin_checksum == destination_checksum, destination_checksum
|
2016-01-28 19:30:13 +01:00
|
|
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo.jpg')) in destination, destination
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
def test_process_file_with_location_and_title():
|
2015-12-11 08:02:43 +01:00
|
|
|
filesystem = FileSystem()
|
|
|
|
temporary_folder, folder = helper.create_working_folder()
|
2015-12-09 08:40:01 +01:00
|
|
|
|
2016-01-26 20:01:05 +01:00
|
|
|
origin = os.path.join(folder,'photo.jpg')
|
2015-12-11 08:02:43 +01:00
|
|
|
shutil.copyfile(helper.get_file('with-location-and-title.jpg'), origin)
|
2015-12-09 08:40:01 +01:00
|
|
|
|
|
|
|
media = Photo(origin)
|
|
|
|
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
|
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
origin_checksum = helper.checksum(origin)
|
|
|
|
destination_checksum = helper.checksum(destination)
|
2015-12-10 05:22:56 +01:00
|
|
|
|
2015-12-09 08:40:01 +01:00
|
|
|
shutil.rmtree(folder)
|
|
|
|
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
|
|
|
|
|
2015-12-12 10:42:22 +01:00
|
|
|
assert origin_checksum is not None, origin_checksum
|
|
|
|
assert origin_checksum == destination_checksum, destination_checksum
|
2016-01-28 19:30:13 +01:00
|
|
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
2015-12-09 08:40:01 +01:00
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
def test_process_file_with_album():
|
|
|
|
filesystem = FileSystem()
|
|
|
|
temporary_folder, folder = helper.create_working_folder()
|
2015-12-10 05:22:56 +01:00
|
|
|
|
2016-01-26 20:01:05 +01:00
|
|
|
origin = os.path.join(folder,'photo.jpg')
|
2015-12-11 08:02:43 +01:00
|
|
|
shutil.copyfile(helper.get_file('with-album.jpg'), origin)
|
2015-12-10 05:22:56 +01:00
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
media = Photo(origin)
|
|
|
|
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
|
2015-12-10 05:22:56 +01:00
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
origin_checksum = helper.checksum(origin)
|
|
|
|
destination_checksum = helper.checksum(destination)
|
|
|
|
|
|
|
|
shutil.rmtree(folder)
|
|
|
|
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
|
|
|
|
|
2015-12-12 10:42:22 +01:00
|
|
|
assert origin_checksum is not None, origin_checksum
|
|
|
|
assert origin_checksum == destination_checksum, destination_checksum
|
2016-01-28 19:30:13 +01:00
|
|
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo.jpg')) in destination, destination
|
2015-12-11 08:02:43 +01:00
|
|
|
|
|
|
|
def test_process_file_with_album_and_title():
|
|
|
|
filesystem = FileSystem()
|
|
|
|
temporary_folder, folder = helper.create_working_folder()
|
|
|
|
|
2016-01-26 20:01:05 +01:00
|
|
|
origin = os.path.join(folder,'photo.jpg')
|
2015-12-11 08:02:43 +01:00
|
|
|
shutil.copyfile(helper.get_file('with-album-and-title.jpg'), origin)
|
|
|
|
|
|
|
|
media = Photo(origin)
|
|
|
|
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
|
2015-12-09 08:40:01 +01:00
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
origin_checksum = helper.checksum(origin)
|
|
|
|
destination_checksum = helper.checksum(destination)
|
2015-12-09 08:40:01 +01:00
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
shutil.rmtree(folder)
|
|
|
|
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
|
2015-12-09 08:40:01 +01:00
|
|
|
|
2015-12-12 10:42:22 +01:00
|
|
|
assert origin_checksum is not None, origin_checksum
|
|
|
|
assert origin_checksum == destination_checksum, destination_checksum
|
2016-01-28 19:30:13 +01:00
|
|
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|
2015-12-11 08:02:43 +01:00
|
|
|
|
|
|
|
def test_process_file_with_album_and_title_and_location():
|
|
|
|
filesystem = FileSystem()
|
|
|
|
temporary_folder, folder = helper.create_working_folder()
|
|
|
|
|
2016-01-26 20:01:05 +01:00
|
|
|
origin = os.path.join(folder,'photo.jpg')
|
2015-12-11 08:02:43 +01:00
|
|
|
shutil.copyfile(helper.get_file('with-album-and-title-and-location.jpg'), origin)
|
|
|
|
|
|
|
|
media = Photo(origin)
|
|
|
|
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
|
|
|
|
|
|
|
|
origin_checksum = helper.checksum(origin)
|
|
|
|
destination_checksum = helper.checksum(destination)
|
|
|
|
|
|
|
|
shutil.rmtree(folder)
|
|
|
|
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
|
|
|
|
|
2015-12-12 10:42:22 +01:00
|
|
|
assert origin_checksum is not None, origin_checksum
|
|
|
|
assert origin_checksum == destination_checksum, destination_checksum
|
2016-01-28 19:30:13 +01:00
|
|
|
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
|