Commit dcfe7455 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Update changelog, unittests for upload_from_string

Refs: #3608
parent 6fa30b1b
......@@ -33,4 +33,5 @@ Features:
- Add option to cache container info for upload_object [#3707]
- Add enumeration to all listing commands, make it optional [#3739]
- Add a download_to_string method in pithos client [#3608]
- Add an upload_from_string method in pithos client [#3608]
......@@ -826,8 +826,133 @@ class PithosClient(TestCase):
r = self.client.upload_object(obj, tmpFile)
self.assert_dicts_are_equal(r, exp_headers)
self.assertEqual(GCI.mock_calls[-1], call())
[call1, call2] = OP.mock_calls
(args1, kwargs1) = call1[1:3]
(args2, kwargs2) = call2[1:3]
self.assertEqual(args1, (obj,))
expected1 = dict(
hashmap=True,
success=(201, 409),
format='json',
json=dict(
hashes=['s0m3h@5h'] * num_of_blocks,
bytes=num_of_blocks * 4 * 1024 * 1024),
content_encoding=None,
content_type='application/octet-stream',
content_disposition=None,
public=None,
permissions=None)
for k, v in expected1.items():
if k == 'json':
self.assertEqual(len(v['hashes']), len(kwargs1[k]['hashes']))
self.assertEqual(v['bytes'], kwargs1[k]['bytes'])
else:
self.assertEqual(v, kwargs1[k])
(args2, kwargs2) = call2[1:3]
self.assertEqual(args2, (obj,))
expected2 = dict(
json=dict(
hashes=['s0m3h@5h'] * num_of_blocks,
bytes=num_of_blocks * 4 * 1024 * 1024),
content_type='application/octet-stream',
hashmap=True,
success=201,
format='json')
for k, v in expected2.items():
if k == 'json':
self.assertEqual(len(v['hashes']), len(kwargs2[k]['hashes']))
self.assertEqual(v['bytes'], kwargs2[k]['bytes'])
else:
self.assertEqual(v, kwargs2[k])
mock_offset = 2
# With progress bars
try:
from progress.bar import ShadyBar
blck_bar = ShadyBar('Mock blck calc.')
upld_bar = ShadyBar('Mock uplds')
except ImportError:
blck_bar = None
upld_bar = None
if blck_bar and upld_bar:
def blck_gen(n):
for i in blck_bar.iter(range(n)):
yield
yield
def upld_gen(n):
for i in upld_bar.iter(range(n)):
yield
yield
tmpFile.seek(0)
r = self.client.upload_object(
obj, tmpFile,
hash_cb=blck_gen, upload_cb=upld_gen)
self.assert_dicts_are_equal(r, exp_headers)
for i, c in enumerate(OP.mock_calls[-mock_offset:]):
self.assertEqual(OP.mock_calls[i], c)
# With content-type
tmpFile.seek(0)
ctype = 'video/mpeg'
sharing = dict(read=['u1', 'g1', 'u2'], write=['u1'])
r = self.client.upload_object(obj, tmpFile,
content_type=ctype, sharing=sharing)
self.assert_dicts_are_equal(r, exp_headers)
self.assertEqual(OP.mock_calls[-1][2]['content_type'], ctype)
self.assert_dicts_are_equal(
OP.mock_calls[-2][2]['permissions'],
sharing)
# With other args
tmpFile.seek(0)
kwargs = dict(
etag='s0m3E74g',
if_etag_match='if etag match',
if_not_exist=True,
content_type=ctype,
content_disposition=ctype + 'd15p051710n',
public=True,
content_encoding='802.11',
container_info_cache={})
r = self.client.upload_object(obj, tmpFile, **kwargs)
self.assert_dicts_are_equal(r, exp_headers)
kwargs.pop('if_not_exist')
ematch = kwargs.pop('if_etag_match')
etag = kwargs.pop('etag')
self.assert_dicts_are_equal(
kwargs.pop('container_info_cache'),
{self.client.container: container_info})
for arg, val in kwargs.items():
self.assertEqual(OP.mock_calls[-2][2][arg], val)
self.assertEqual(OP.mock_calls[-1][2]['if_etag_match'], ematch)
self.assertEqual(OP.mock_calls[-1][2]['if_etag_not_match'], '*')
self.assertEqual(OP.mock_calls[-1][2]['etag'], etag)
@patch('%s.get_container_info' % pithos_pkg, return_value=container_info)
@patch('%s.container_post' % pithos_pkg, return_value=FR())
@patch('%s.object_put' % pithos_pkg, return_value=FR())
def test_upload_from_string(self, OP, CP, GCI):
num_of_blocks = 2
tmpFile = self._create_temp_file(num_of_blocks)
tmpFile.seek(0)
src_str = tmpFile.read()
exp_headers = dict(id='container id', name='container name')
FR.headers = dict(exp_headers)
r = self.client.upload_from_string(obj, src_str)
self.assert_dicts_are_equal(r, exp_headers)
self.assertEqual(GCI.mock_calls[-1], call())
[call1, call2] = OP.mock_calls
(args1, kwargs1) = call1[1:3]
(args2, kwargs2) = call2[1:3]
self.assertEqual(args1, (obj,))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment