Skip to content

Commit

Permalink
Merge pull request #1 from bryanchriswhite/mrosa
Browse files Browse the repository at this point in the history
add delete_file tests
  • Loading branch information
marcorosa authored Mar 16, 2018
2 parents f404eef + 2a727b3 commit b609662
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 31 deletions.
Binary file added lib/.storj_env.py.swp
Binary file not shown.
142 changes: 111 additions & 31 deletions tests/test_storj_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ def get_bucket_id(self, name):
buckets = self.env.list_buckets()
return next(bucket['id'] for bucket in buckets if bucket['name'] == name)

def get_file_iterator_match(self, bucket_id, file_name):
files = self.env.list_files(bucket_id)
return (file for file in files if re.search(r'%s$' % file_name, file['filename']) is not None)

def get_file_id(self, bucket_id, file_name):
return next(file['id'] for file in self.get_file_iterator_match(bucket_id, file_name))

@staticmethod
def upload_test_data(bucket_id):
file_path = path.join(path.dirname(path.realpath(__file__)), 'upload.data')
upload_file_command = 'storj upload-file %s %s' % (bucket_id, file_path)
# NB: `stdout=subprocess.PIPE` prevents subprocess
# stdout from printing during unittest
subprocess.call(upload_file_command.split(), stdout=subprocess.PIPE)


class TestStorjEnvBadHost(UtilityTestCase):
def setUp(self):
Expand Down Expand Up @@ -88,7 +103,7 @@ def callback():
self.assertRaisesWithBadHost(self.env.get_info, callback)


class TestCreateDeleteSuccess(TestStorjEnv):
class TestCreateDeleteBucketSuccess(TestStorjEnv):
def test_create_bucket_without_callback_success(self):
bucket_name = 'python_libstorj-test2'

Expand All @@ -112,6 +127,9 @@ def test_delete_bucket_without_callback_success(self):
bucket_name = self.get_bucket_id('python_libstorj-test2')

self.env.delete_bucket(bucket_name)
# NB: asserts no bucket with name: `bucket_name` exists
bucket_iterator = (b for b in self.env.list_buckets() if b['name'] == bucket_name)
self.assertRaises(StopIteration, next, bucket_iterator)

def test_delete_bucket_with_callback_success(self):
bucket_name = self.get_bucket_id('python_libstorj-test')
Expand Down Expand Up @@ -233,41 +251,103 @@ def callback(error, buckets):
self.fail('Callback not called!')


class TestListFilesSuccess(TestStorjEnv):
def setUp(self):
self.file_name = 'test.data'
super(TestListFilesSuccess, self).setUp()
file_path = path.join(path.dirname(path.realpath(__file__)), 'upload.data')
self.bucket = self.env.create_bucket('python_libstorj-test4')
upload_file_command = 'storj upload-file %s %s' % (self.bucket['id'], file_path)
# NB: `stdout=subprocess.PIPE` prevents subprocess
# stdout from printing during unittest
subprocess.call(upload_file_command.split(), stdout=subprocess.PIPE)
# class TestDeleteFileSuccess(TestStorjEnv):
# def setUp(self):
# super(TestDeleteFileSuccess, self).setUp()
# bucket_name = 'python_libstorj-test_bucket7'
# # self.file_name = 'test.data'
# self.file_name = 'upload.data'
# self.bucket = self.env.create_bucket(bucket_name)
# self.upload_test_data(self.bucket['id'])
# self.file_id = self.get_file_id(self.bucket['id'], 'upload.data')
#
# def tearDown(self):
# self.env.delete_bucket(self.bucket['id'])
# super(TestDeleteFileSuccess, self).tearDown()
#
# def test_delete_file_without_callback_success(self):
# self.env.delete_file(self.bucket['id'], self.file_id)
# # NB: asserts no file with name: `bucket_name` exists
# file_iterator = self.get_file_iterator_match(self.bucket['id'], self.file_name)
# self.assertRaises(StopIteration, next, file_iterator)
#
# def test_delete_file_with_callback_success(self):
# results = []
#
# def callback(error):
# results.append(error)
#
# self.env.delete_file(self.bucket['id'], self.file_id, callback)
# try:
# error = results[0]
# self.assertIsNone(error)
#
# # NB: asserts no bucket with name: `bucket_name` exists
# # file_iterator = (f for f in self.env.list_files(self.bucket['id']) if re.search(r'%s' % self.file_name, f['filename']) is not None)
# file_iterator = self.get_file_iterator_match(self.bucket['id'], self.file_name)
# self.assertRaises(StopIteration, next, file_iterator)
# except IndexError:
# self.fail('Callback not called!')


class TestDeleteFileFailure(TestStorjEnvBadHost):
def test_delete_file_without_callback_failure(self):
bucket_name = 'python_libstorj-test_bucket2'
file_name = 'python_libstorj-test_file2'
self.assertRaisesWithBadHost(self.env.delete_file, bucket_name, file_name)

def test_delete_file_with_callback_failure(self):
bucket_name = 'python_libstorj-test_bucket'
file_name = 'python_libstorj-test_file'
results = []

def tearDown(self):
# TODO: remove file as soon as it's implemented
self.env.delete_bucket(self.bucket['id'])
super(TestListFilesSuccess, self).tearDown()
def callback(error):
results.append(error)

def test_list_files_without_callback_success(self):
files = self.env.list_files(self.bucket['id'])
self.assertEqual(len(files), 1)
# TODO: uncomment when file naming works
# self.assertEqual(files[0]['filename'], self.file_name)
self.assertRaisesWithBadHost(self.env.delete_file, bucket_name, file_name, callback)
try:
error = results[0]

def test_list_files_with_callback_success(self):
results = []
self.assertBadHostError(error)
except KeyError:
self.fail('Callback not called!')

def callback(error_, files_):
results.append(error_)
results.append(files_)

self.env.list_files(self.bucket['id'], callback)
error, files = results
self.assertEqual(error, None)
self.assertEqual(len(files), 1)
# TODO: uncomment when file naming works
# self.assertEqual(files[0]['filename'], self.file_name)
# class TestListFilesSuccess(TestStorjEnv):
# def setUp(self):
# self.file_name = 'test.data'
# super(TestListFilesSuccess, self).setUp()
# file_path = path.join(path.dirname(path.realpath(__file__)), 'upload.data')
# self.bucket = self.env.create_bucket('python_libstorj-test4')
# upload_file_command = 'storj upload-file %s %s' % (self.bucket['id'], file_path)
# # NB: `stdout=subprocess.PIPE` prevents subprocess
# # stdout from printing during unittest
# subprocess.call(upload_file_command.split(), stdout=subprocess.PIPE)
#
# def tearDown(self):
# # TODO: remove file as soon as it's implemented
# self.env.delete_bucket(self.bucket['id'])
# super(TestListFilesSuccess, self).tearDown()
#
# def test_list_files_without_callback_success(self):
# files = self.env.list_files(self.bucket['id'])
# self.assertEqual(len(files), 1)
# # TODO: uncomment when file naming works
# # self.assertEqual(files[0]['filename'], self.file_name)
#
# def test_list_files_with_callback_success(self):
# results = []
#
# def callback(error_, files_):
# results.append(error_)
# results.append(files_)
#
# self.env.list_files(self.bucket['id'], callback)
# error, files = results
# self.assertEqual(error, None)
# self.assertEqual(len(files), 1)
# # TODO: uncomment when file naming works
# # self.assertEqual(files[0]['filename'], self.file_name)


class TestListFilesFailure(TestStorjEnvBadHost):
Expand Down

0 comments on commit b609662

Please sign in to comment.