def test_put_multi(self): http_session = MagicMock() class MockBlobClient(BlobClient): get_size_multi = MagicMock() http_session.post = MagicMock() response = MagicMock() http_session.post.return_value = response response.status_code = 200 MockBlobClient.get_size_multi.return_value = { ("sha224-" "5390db278c2643999c8aa539d9cc8b587e89bdbb289c4aff97971ccf"): 6, ("sha224-" "5d0307b0baad2ce34aab7cac92a3d72c57ca962ffad1dd365002d45e"): 6, ("sha224-" "f8d2cbd0991675d4e30fe52fff2b3bd00bdbfccc189c8c2e0fad4718"): None, } blobs = MockBlobClient(http_session, 'http://example.com/') result = blobs.put_multi( Blob(b"dummy1"), Blob(b"dummy2"), Blob(b"dummy3"), ) MockBlobClient.get_size_multi.assert_called_with( 'sha224-5390db278c2643999c8aa539d9cc8b587e89bdbb289c4aff97971ccf', 'sha224-5d0307b0baad2ce34aab7cac92a3d72c57ca962ffad1dd365002d45e', 'sha224-f8d2cbd0991675d4e30fe52fff2b3bd00bdbfccc189c8c2e0fad4718', ) http_session.post.assert_called_with( "http://example.com/camli/upload", files={ ('sha224-' 'f8d2cbd0991675d4e30fe52fff2b3bd00bdbfccc189c8c2e0fad4718'): ( ('sha224-f8d2cbd0991675d4e30fe5' '2fff2b3bd00bdbfccc189c8c2e0fad4718'), b'dummy3', 'application/octet-stream', ) }) self.assertEqual(result, [ ('sha224-' '5390db278c2643999c8aa539d9cc8b587e89bdbb289c4aff97971ccf'), ('sha224-' '5d0307b0baad2ce34aab7cac92a3d72c57ca962ffad1dd365002d45e'), ('sha224-' 'f8d2cbd0991675d4e30fe52fff2b3bd00bdbfccc189c8c2e0fad4718'), ])
def test_change_hash_func(self): blob = Blob(b'hello') self.assertEqual( blob.blobref, 'sha224-ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193', ) blob.hash_func_name = 'sha1' self.assertEqual(blob.blobref, 'sha1-aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
def test_change_data(self): blob = Blob(b'hello') self.assertEqual( blob.blobref, 'sha224-ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193', ) blob.data = b'world' self.assertEqual( blob.blobref, 'sha224-06d2dbdb71973e31e4f1df3d7001fa7de268aa72fcb1f6f9ea37e0e5', )
def test_change_data(self): blob = Blob('hello') self.assertEqual( blob.blobref, 'sha1-aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d', ) blob.data = 'world' self.assertEqual( blob.blobref, 'sha1-7c211433f02071597741e6ff5a8ea34789abbf43', )
def test_change_hash_func(self): blob = Blob('hello') self.assertEqual( blob.blobref, 'sha1-aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d', ) blob.hash_func_name = 'sha256' self.assertEqual( blob.blobref, 'sha256-' '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824', )
def test_string_data(self): self.assertRaises( TypeError, lambda: Blob('hello'), ) blob = Blob(b'hello') def change_data(): blob.data = 'hello' self.assertRaises( TypeError, change_data, )
def test_unicode_data(self): self.assertRaises( TypeError, lambda: Blob(u'hello'), ) blob = Blob('hello') def change_data(): blob.data = u'hello' self.assertRaises( TypeError, change_data, )
def test_put_multi(self): http_session = MagicMock() class MockBlobClient(BlobClient): get_size_multi = MagicMock() http_session.post = MagicMock() response = MagicMock() http_session.post.return_value = response response.status_code = 200 MockBlobClient.get_size_multi.return_value = { "sha1-c9a291475b1bcaa4aa0c4cf459c29c2c52078949": 6, "sha1-403c716ea737afeb54f40549cdf5727f10ba6f18": 6, "sha1-1a434c0daa0b17e48abd4b59c632cf13501c7d24": None, } blobs = MockBlobClient(http_session, 'http://example.com/') result = blobs.put_multi( Blob("dummy1"), Blob("dummy2"), Blob("dummy3"), ) MockBlobClient.get_size_multi.assert_called_with( 'sha1-c9a291475b1bcaa4aa0c4cf459c29c2c52078949', 'sha1-403c716ea737afeb54f40549cdf5727f10ba6f18', 'sha1-1a434c0daa0b17e48abd4b59c632cf13501c7d24', ) http_session.post.assert_called_with( "http://example.com/camli/upload", files={ 'sha1-1a434c0daa0b17e48abd4b59c632cf13501c7d24': ( 'sha1-1a434c0daa0b17e48abd4b59c632cf13501c7d24', 'dummy3', 'application/octet-stream', ) }) self.assertEqual(result, [ 'sha1-c9a291475b1bcaa4aa0c4cf459c29c2c52078949', 'sha1-403c716ea737afeb54f40549cdf5727f10ba6f18', 'sha1-1a434c0daa0b17e48abd4b59c632cf13501c7d24', ])
def test_func_name_as_func(self): import hashlib # must pass a function name, not a function # (we need the name so we can make the blobref prefix) self.assertRaises( TypeError, lambda: Blob('hello', hashlib.sha1), )
def to_blob(self, signer=None): """ Return a blob with object's JSON data. If :attr:`needs_signing` is set, a :class:`Signer` should be passed as ``signer``. If sigining is required, and this function is unable to sign the JSON, it will raise a :class:`SigningError` """ # TODO: Keep a Blob in this object, and invalidate it when needed, so # that we can return Blobs from other things. if not self.needs_signing: # Perkeep generally uses tabs for indentation for stored blobs. # This should not matter, and our serialization is likely # different, but we use tabs for consistency anyway return Blob(json.dumps(self.data, indent='\t').encode('utf8')) if not signer: raise SigningError( 'A signer was needed, and none was passed to to_blob()') return Blob(signer.sign_dict(self.data))
def test_instantiate(self): blob = Blob('hello') self.assertEqual( blob.hash_func_name, 'sha1', ) self.assertEqual( blob.data, 'hello', ) self.assertEqual( blob.blobref, 'sha1-aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d', )
def test_instantiate_different_hash(self): blob = Blob(b'hello', hash_func_name='sha256') self.assertEqual( blob.hash_func_name, 'sha256', ) self.assertEqual( blob.data, b'hello', ) self.assertEqual( blob.blobref, 'sha256-' '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824', )
def test_instantiate(self): # TODO: Check for sha224 agreeing, too blob = Blob(b'hello', ) self.assertEqual( blob.hash_func_name, 'sha224', ) self.assertEqual( blob.data, b'hello', ) self.assertEqual( blob.blobref, 'sha224-ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193', )