Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Added test coverage for the flickr_cli file, as well as test images t…
Browse files Browse the repository at this point in the history
…o be used.
  • Loading branch information
Jawaad Mahmood committed Dec 6, 2015
1 parent 5521e0c commit 7e40849
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
40 changes: 30 additions & 10 deletions flickr_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def valid_img(f):
return True
except AttributeError as e:
# You probably passed something that is not a path.
logging.exception(e)
logging.warning(e)
except IOError as e:
# You passed a path that does not exist, or you do not have access to it.
logging.exception(e)
logging.warning(e)
return False


Expand All @@ -28,17 +28,38 @@ def get_upload_size(files):
class UploadStatus(object):
"""Used to maintain state while performing uploads."""
# TODO: Is this actually being used?

def __init__(self, file_list):
if isinstance(file_list, basestring):
# The file list is a directory and should be converted into a directory list.
file_list = [os.path.join(file_list, f)
for f in os.listdir(file_list)
if not os.path.isdir(os.path.join(file_list, f))]

self.file_list = file_list
self.total_upload_size = get_upload_size(self.file_list)
self._file_no = 0
self.file = self.get_current_file()

def increment(self):
try:
self._file_no += 1
self.file = self.get_current_file()
return self.file
except IndexError:
return None

def get_current_file(self):
return self.file_list[self._file_no]

def uploaded_thus_far(self):
return float(get_upload_size(
self.file_list[0:self.file_list.index(self.file)]))
self.file_list[0:self._file_no]))

def status(self, progress, done):
# which file?
# What % of it is done?
def status(self, progress):
"""
Progress: Float: How much of the currently uploading file has been uploaded.
"""
total = self.uploaded_thus_far() + float(progress * os.path.getsize(self.file)) / 100
return round(total / self.total_upload_size * 100, 2)

Expand All @@ -56,7 +77,7 @@ def __init__(self, flickr):

def exists(self, title):
"""Returns Photoset ID that matches title, if such a set exists. Otherwise false."""
# TODO: What happens if there is no photoset in someone's account?
# TODO: What happens if there is no photoset in your account?
photosets = self.flickr.photosets_getList().find("photosets").findall("photoset")

for p in photosets:
Expand Down Expand Up @@ -104,9 +125,8 @@ def filter_directory_contents(self, d, f):
return os.path.isdir(os.path.join(d, f))

def get_directory_contents(self, d):
dir_list = os.listdir(d)
self.files = [os.path.join(d, f)
for f in dir_list
for f in os.listdir(d)
if not self.filter_directory_contents(d, f)]

def prehook(self, **kwargs):
Expand Down Expand Up @@ -161,7 +181,7 @@ def flickr_upload(self, f, **kwargs):
is_family=kwargs.get('is_family', 0))

def upload(self):
self.responses = [(self.flickr_upload(f), f) for f in self.files]
self.responses = [(self.flickr_upload(f, is_public=0, is_family=0), f) for f in self.files]

def parse_response(self):
self.ids = [r.find("photoid").text for (r, f) in self.responses if r.attrib['stat'] == "ok"]
Expand Down
Binary file added test_img/actual_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test_img/fake_img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 31 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import flickrapi
import flickr_cli
import unittest
import os.path

__author__ = 'jawaad'

Expand All @@ -11,6 +12,11 @@


class TestSuccessfulUploads(unittest.TestCase):
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

def path(self, relative_path):
return os.path.join(self.BASE_DIR, relative_path)

def setUp(self):
config = ConfigParser()
config.read('flickr.config')
Expand All @@ -34,14 +40,37 @@ def setUp(self):
def test_upload(self):
upload = flickr_cli.DirectoryFlickrUpload(self.flickr)
upload(directory='./test_img/', pset=['test-0001'], tags=['tests'])
# print repr(upload.successful_uploads_count)
self.assertEqual(upload.successful_uploads_count, 1)

def test_upload_public(self):
public_upload = flickr_cli.PublicDirectoryUpload(self.flickr)
public_upload(directory='./test_img/', pset=['test-0001'], tags=['tests'])
self.assertEqual(public_upload.successful_uploads_count, 1)

def test_upload_family(self):
family_upload = flickr_cli.FamilyDirectoryUpload(self.flickr)
family_upload(directory='./test_img/', pset=['test-0001'], tags=['tests'])
# print repr(family_upload.successful_uploads_count)
self.assertEqual(family_upload.successful_uploads_count, 1)

def test_filter_bad_images(self):
self.assertFalse(flickr_cli.valid_img({}))
self.assertFalse(flickr_cli.valid_img(self.path('./non_existant_file.png')))
self.assertTrue(flickr_cli.valid_img(self.path('./test_img/actual_image.jpg')))

def test_upload_status(self):
"""
TODO: What about OSes where the files are not sorted in alphabetical order? Does this matter really?
:return:
"""
a = flickr_cli.UploadStatus(self.path('./test_img/'))
self.assertEqual(a.uploaded_thus_far(), 0.0) # actual_image.jpg
a.increment() # first file done uploading
self.assertEqual(a.uploaded_thus_far(), 198167.0)
self.assertAlmostEqual(a.status(0), 100 * 198167.0 / 198241.0, 2)
a.increment() # second file done uploading
self.assertEqual(a.uploaded_thus_far(), 198241.0)
self.assertEqual(a.status(0), 100.00)

if __name__ == '__main__':
unittest.main()

0 comments on commit 7e40849

Please sign in to comment.