gh-21 Refactor filesystem test by moving helper functions into a module

This commit is contained in:
Jaisen Mathai 2015-12-10 23:02:43 -08:00
parent 41c9446a9a
commit 1713206368
2 changed files with 158 additions and 76 deletions

View File

@ -2,31 +2,27 @@
import os import os
import sys import sys
import hashlib
import random
import re import re
import shutil import shutil
import string
import tempfile
import time
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.realpath(__file__))))))
from elodie import filesystem import helper
from elodie.filesystem import FileSystem
from elodie.media.media import Media from elodie.media.media import Media
from elodie.media.photo import Photo from elodie.media.photo import Photo
from elodie.media.video import Video from elodie.media.video import Video
os.environ['TZ'] = 'GMT' os.environ['TZ'] = 'GMT'
filesystem = filesystem.FileSystem()
def test_create_directory_success(): def test_create_directory_success():
folder = '%s/%s' % (tempfile.gettempdir(), random_string(10)) filesystem = FileSystem()
folder = '%s/%s' % (helper.temp_dir(), helper.random_string(10))
status = filesystem.create_directory(folder) status = filesystem.create_directory(folder)
# Needs to be a subdirectory # Needs to be a subdirectory
assert tempfile.gettempdir() != folder assert helper.temp_dir() != folder
assert status == True assert status == True
assert os.path.isdir(folder) == True assert os.path.isdir(folder) == True
@ -37,11 +33,12 @@ def test_create_directory_success():
def test_create_directory_recursive_success(): def test_create_directory_recursive_success():
folder = '%s/%s/%s' % (tempfile.gettempdir(), random_string(10), random_string(10)) filesystem = FileSystem()
folder = '%s/%s/%s' % (helper.temp_dir(), helper.random_string(10), helper.random_string(10))
status = filesystem.create_directory(folder) status = filesystem.create_directory(folder)
# Needs to be a subdirectory # Needs to be a subdirectory
assert tempfile.gettempdir() != folder assert helper.temp_dir() != folder
assert status == True assert status == True
assert os.path.isdir(folder) == True assert os.path.isdir(folder) == True
@ -50,12 +47,14 @@ def test_create_directory_recursive_success():
shutil.rmtree(folder) shutil.rmtree(folder)
def test_create_directory_invalid_permissions(): def test_create_directory_invalid_permissions():
filesystem = FileSystem()
status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist') status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist')
assert status == False assert status == False
def test_delete_directory_if_empty(): def test_delete_directory_if_empty():
folder = '%s/%s' % (tempfile.gettempdir(), random_string(10)) filesystem = FileSystem()
folder = '%s/%s' % (helper.temp_dir(), helper.random_string(10))
os.makedirs(folder) os.makedirs(folder)
assert os.path.isdir(folder) == True assert os.path.isdir(folder) == True
@ -67,7 +66,8 @@ def test_delete_directory_if_empty():
assert os.path.exists(folder) == False assert os.path.exists(folder) == False
def test_delete_directory_if_empty_when_not_empty(): def test_delete_directory_if_empty_when_not_empty():
folder = '%s/%s/%s' % (tempfile.gettempdir(), random_string(10), random_string(10)) filesystem = FileSystem()
folder = '%s/%s/%s' % (helper.temp_dir(), helper.random_string(10), helper.random_string(10))
os.makedirs(folder) os.makedirs(folder)
parent_folder = os.path.dirname(folder) parent_folder = os.path.dirname(folder)
@ -86,7 +86,8 @@ def test_delete_directory_if_empty_when_not_empty():
shutil.rmtree(parent_folder) shutil.rmtree(parent_folder)
def test_get_all_files_success(): def test_get_all_files_success():
folder = populate_folder(5) filesystem = FileSystem()
folder = helper.populate_folder(5)
files = filesystem.get_all_files(folder) files = filesystem.get_all_files(folder)
shutil.rmtree(folder) shutil.rmtree(folder)
@ -94,7 +95,8 @@ def test_get_all_files_success():
def test_get_all_files_by_extension(): def test_get_all_files_by_extension():
folder = populate_folder(5) filesystem = FileSystem()
folder = helper.populate_folder(5)
files = filesystem.get_all_files(folder) files = filesystem.get_all_files(folder)
assert len(files) == 5 assert len(files) == 5
@ -111,21 +113,25 @@ def test_get_all_files_by_extension():
shutil.rmtree(folder) shutil.rmtree(folder)
def test_get_current_directory(): def test_get_current_directory():
filesystem = FileSystem()
assert os.getcwd() == filesystem.get_current_directory() assert os.getcwd() == filesystem.get_current_directory()
def test_get_file_name_plain(): def test_get_file_name_plain():
media = Photo(get_file('plain.jpg')) filesystem = FileSystem()
media = Photo(helper.get_file('plain.jpg'))
file_name = filesystem.get_file_name(media) file_name = filesystem.get_file_name(media)
assert file_name == '2015-12-05_00-59-26-plain.jpg' assert file_name == '2015-12-05_00-59-26-plain.jpg'
def test_get_file_name_with_title(): def test_get_file_name_with_title():
media = Photo(get_file('with-title.jpg')) filesystem = FileSystem()
media = Photo(helper.get_file('with-title.jpg'))
file_name = filesystem.get_file_name(media) file_name = filesystem.get_file_name(media)
assert file_name == '2015-12-05_00-59-26-with-title-some-title.jpg' assert file_name == '2015-12-05_00-59-26-with-title-some-title.jpg'
def test_get_folder_name_by_date(): def test_get_folder_name_by_date():
filesystem = FileSystem()
time_tuple = (2010, 4, 15, 1, 2, 3, 0, 0, 0) time_tuple = (2010, 4, 15, 1, 2, 3, 0, 0, 0)
folder_name = filesystem.get_folder_name_by_date(time_tuple) folder_name = filesystem.get_folder_name_by_date(time_tuple)
@ -137,139 +143,169 @@ def test_get_folder_name_by_date():
assert folder_name == '2010-09-Sep' assert folder_name == '2010-09-Sep'
def test_get_folder_path_plain(): def test_get_folder_path_plain():
media = Photo(get_file('plain.jpg')) filesystem = FileSystem()
media = Photo(helper.get_file('plain.jpg'))
path = filesystem.get_folder_path(media.get_metadata()) path = filesystem.get_folder_path(media.get_metadata())
assert path == '2015-12-Dec/Unknown Location' assert path == '2015-12-Dec/Unknown Location'
def test_get_folder_path_with_title(): def test_get_folder_path_with_title():
media = Photo(get_file('with-title.jpg')) filesystem = FileSystem()
media = Photo(helper.get_file('with-title.jpg'))
path = filesystem.get_folder_path(media.get_metadata()) path = filesystem.get_folder_path(media.get_metadata())
assert path == '2015-12-Dec/Unknown Location' assert path == '2015-12-Dec/Unknown Location'
def test_get_folder_path_with_location(): def test_get_folder_path_with_location():
media = Photo(get_file('with-location.jpg')) filesystem = FileSystem()
media = Photo(helper.get_file('with-location.jpg'))
path = filesystem.get_folder_path(media.get_metadata()) path = filesystem.get_folder_path(media.get_metadata())
assert path == '2015-12-Dec/Sunnyvale' assert path == '2015-12-Dec/Sunnyvale'
def test_get_folder_path_with_location_and_title(): def test_get_folder_path_with_location_and_title():
media = Photo(get_file('with-location-and-title.jpg')) filesystem = FileSystem()
media = Photo(helper.get_file('with-location-and-title.jpg'))
path = filesystem.get_folder_path(media.get_metadata()) path = filesystem.get_folder_path(media.get_metadata())
assert path == '2015-12-Dec/Sunnyvale' assert path == '2015-12-Dec/Sunnyvale'
def test_process_file_plain(): def test_process_file_plain():
temporary_folder = tempfile.gettempdir() filesystem = FileSystem()
folder = '%s/%s/%s' % (temporary_folder, random_string(10), random_string(10)) temporary_folder, folder = helper.create_working_folder()
os.makedirs(folder)
origin = '%s/plain.jpg' % folder origin = '%s/photo.jpg' % folder
shutil.copyfile(get_file('plain.jpg'), origin) shutil.copyfile(helper.get_file('plain.jpg'), origin)
media = Photo(origin) media = Photo(origin)
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
origin_checksum = checksum(origin) origin_checksum = helper.checksum(origin)
destination_checksum = checksum(destination) destination_checksum = helper.checksum(destination)
shutil.rmtree(folder) shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination))) shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
assert origin_checksum is not None assert origin_checksum is not None
assert origin_checksum == destination_checksum assert origin_checksum == destination_checksum
assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain.jpg' in destination assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-photo.jpg' in destination
def test_process_file_with_title(): def test_process_file_with_title():
temporary_folder = tempfile.gettempdir() filesystem = FileSystem()
folder = '%s/%s/%s' % (temporary_folder, random_string(10), random_string(10)) temporary_folder, folder = helper.create_working_folder()
os.makedirs(folder)
origin = '%s/plain.jpg' % folder origin = '%s/photo.jpg' % folder
shutil.copyfile(get_file('with-title.jpg'), origin) shutil.copyfile(helper.get_file('with-title.jpg'), origin)
media = Photo(origin) media = Photo(origin)
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
origin_checksum = checksum(origin) origin_checksum = helper.checksum(origin)
destination_checksum = checksum(destination) destination_checksum = helper.checksum(destination)
shutil.rmtree(folder) shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination))) shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
assert origin_checksum is not None assert origin_checksum is not None
assert origin_checksum == destination_checksum assert origin_checksum == destination_checksum
assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain-some-title.jpg' in destination assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-photo-some-title.jpg' in destination
def test_process_file_with_location(): def test_process_file_with_location():
temporary_folder = tempfile.gettempdir() filesystem = FileSystem()
folder = '%s/%s/%s' % (temporary_folder, random_string(10), random_string(10)) temporary_folder, folder = helper.create_working_folder()
os.makedirs(folder)
origin = '%s/plain.jpg' % folder origin = '%s/photo.jpg' % folder
shutil.copyfile(get_file('with-location.jpg'), origin) shutil.copyfile(helper.get_file('with-location.jpg'), origin)
media = Photo(origin) media = Photo(origin)
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
origin_checksum = checksum(origin) origin_checksum = helper.checksum(origin)
destination_checksum = checksum(destination) destination_checksum = helper.checksum(destination)
shutil.rmtree(folder) shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination))) shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
assert origin_checksum is not None assert origin_checksum is not None
assert origin_checksum == destination_checksum assert origin_checksum == destination_checksum
assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-plain.jpg' in destination assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-photo.jpg' in destination
def test_process_file_with_location_and_title(): def test_process_file_with_location_and_title():
temporary_folder = tempfile.gettempdir() filesystem = FileSystem()
folder = '%s/%s/%s' % (temporary_folder, random_string(10), random_string(10)) temporary_folder, folder = helper.create_working_folder()
os.makedirs(folder)
origin = '%s/plain.jpg' % folder origin = '%s/photo.jpg' % folder
shutil.copyfile(get_file('with-location-and-title.jpg'), origin) shutil.copyfile(helper.get_file('with-location-and-title.jpg'), origin)
media = Photo(origin) media = Photo(origin)
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
origin_checksum = checksum(origin) origin_checksum = helper.checksum(origin)
destination_checksum = checksum(destination) destination_checksum = helper.checksum(destination)
shutil.rmtree(folder) shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination))) shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
assert origin_checksum is not None assert origin_checksum is not None
assert origin_checksum == destination_checksum assert origin_checksum == destination_checksum
assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-plain-some-title.jpg' in destination assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-photo-some-title.jpg' in destination
def checksum(file_path, blocksize=65536): def test_process_file_with_album():
hasher = hashlib.sha256() filesystem = FileSystem()
with open(file_path, 'r') as f: temporary_folder, folder = helper.create_working_folder()
buf = f.read(blocksize)
while len(buf) > 0: origin = '%s/photo.jpg' % folder
hasher.update(buf) shutil.copyfile(helper.get_file('with-album.jpg'), origin)
buf = f.read(blocksize)
return hasher.hexdigest()
return None
def get_file(name): media = Photo(origin)
current_folder = os.path.dirname(os.path.realpath(__file__)) destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
return '%s/files/%s' % (current_folder, name)
def populate_folder(number_of_files): origin_checksum = helper.checksum(origin)
folder = '%s/%s' % (tempfile.gettempdir(), random_string(10)) destination_checksum = helper.checksum(destination)
os.makedirs(folder)
for x in range(0, number_of_files): shutil.rmtree(folder)
ext = 'jpg' if x % 2 == 0 else 'txt' shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
fname = '%s/%s.%s' % (folder, x, ext)
with open(fname, 'a'):
os.utime(fname, None)
return folder assert origin_checksum is not None
assert origin_checksum == destination_checksum
assert '2015-12-Dec/Test Album/2015-12-05_00-59-26-photo.jpg' in destination
def random_string(length): def test_process_file_with_album_and_title():
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(length)) filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder()
origin = '%s/photo.jpg' % folder
shutil.copyfile(helper.get_file('with-album-and-title.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)))
assert origin_checksum is not None
assert origin_checksum == destination_checksum
assert '2015-12-Dec/Test Album/2015-12-05_00-59-26-photo-some-title.jpg' in destination
def test_process_file_with_album_and_title_and_location():
filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder()
origin = '%s/photo.jpg' % folder
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)))
assert origin_checksum is not None
assert origin_checksum == destination_checksum
assert '2015-12-Dec/Test Album/2015-12-05_00-59-26-photo-some-title.jpg' in destination

46
elodie/tests/helper.py Normal file
View File

@ -0,0 +1,46 @@
import hashlib
import os
import random
import shutil
import string
import tempfile
def checksum(file_path, blocksize=65536):
hasher = hashlib.sha256()
with open(file_path, 'r') as f:
buf = f.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = f.read(blocksize)
return hasher.hexdigest()
return None
def create_working_folder():
temporary_folder = tempfile.gettempdir()
folder = '%s/%s/%s' % (temporary_folder, random_string(10), random_string(10))
os.makedirs(folder)
return (temporary_folder, folder)
def get_file(name):
current_folder = os.path.dirname(os.path.realpath(__file__))
return '%s/files/%s' % (current_folder, name)
def populate_folder(number_of_files):
folder = '%s/%s' % (tempfile.gettempdir(), random_string(10))
os.makedirs(folder)
for x in range(0, number_of_files):
ext = 'jpg' if x % 2 == 0 else 'txt'
fname = '%s/%s.%s' % (folder, x, ext)
with open(fname, 'a'):
os.utime(fname, None)
return folder
def random_string(length):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(length))
def temp_dir():
return tempfile.gettempdir()