def test_get_success(self): http_session = MagicMock() http_session.get = MagicMock() response = MagicMock() http_session.get.return_value = response response.status_code = 200 response.content = b'dummy blob' blobs = BlobClient( http_session, 'http://example.com/blerbs/', ) result = blobs.get('sha1-7928f34bd3263b86e67d11efff30d67fe7f3d176') http_session.get.assert_called_with( "http://example.com/blerbs/camli/" "sha1-7928f34bd3263b86e67d11efff30d67fe7f3d176") self.assertEqual( type(result), Blob, ) self.assertEqual( result.data, b'dummy blob', )
def test_url_building(self): http_session = MagicMock() blobs = BlobClient( http_session=http_session, base_url="http://example.com/dummy-blobs/", ) self.assertEqual( blobs._make_blob_url('dummy-blobref'), 'http://example.com/dummy-blobs/camli/dummy-blobref', )
def test_unavailable(self): http_session = MagicMock() blobs = BlobClient( http_session=http_session, base_url=None, ) from camlistore.exceptions import ServerFeatureUnavailableError self.assertRaises( ServerFeatureUnavailableError, lambda: blobs._make_blob_url('dummy-blobref'), )
def test_get_not_found(self): http_session = MagicMock() http_session.get = MagicMock() response = MagicMock() http_session.get.return_value = response response.status_code = 404 response.content = 'not found' blobs = BlobClient( http_session, 'http://example.com/blerbs/', ) from camlistore.exceptions import NotFoundError self.assertRaises( NotFoundError, lambda: blobs.get('dummy-blobref'), ) http_session.get.assert_called_with( "http://example.com/blerbs/camli/dummy-blobref")
def test_get_hash_mismatch(self): from camlistore.exceptions import HashMismatchError http_session = MagicMock() http_session.get = MagicMock() response = MagicMock() http_session.get.return_value = response response.status_code = 200 response.content = b'dummy blob' blobs = BlobClient( http_session, 'http://example.com/blerbs/', ) self.assertRaises( HashMismatchError, lambda: blobs.get('sha1-dummyblobref'), ) http_session.get.assert_called_with( "http://example.com/blerbs/camli/sha1-dummyblobref")
def test_get_size_success(self): http_session = MagicMock() http_session.request = MagicMock() response = MagicMock() http_session.request.return_value = response response.status_code = 200 response.headers = {'content-length': 5} blobs = BlobClient( http_session, 'http://example.com/blerbs/', ) result = blobs.get_size('dummy-blobref') http_session.request.assert_called_with( "HEAD", "http://example.com/blerbs/camli/dummy-blobref") self.assertEqual( result, 5, )
def test_get_size_multi(self): http_session = MagicMock() http_session.post = MagicMock() response = MagicMock() http_session.post.return_value = response response.status_code = 200 response.content = """ { "stat": [ { "blobRef": "dummy1", "size": 5 }, { "blobRef": "dummy2", "size": 9 } ] } """ blobs = BlobClient(http_session, 'http://example.com/') result = blobs.get_size_multi("dummy1", "dummy2") http_session.post.assert_called_with( "http://example.com/camli/stat", data={ "camliversion": "1", "blob1": "dummy1", "blob2": "dummy2", }, ) self.assertEqual(result, { "dummy1": 5, "dummy2": 9, })
def __init__( self, http_session=None, blob_root=None, search_root=None, sign_root=None, ): self.http_session = http_session self.blob_root = blob_root self.search_root = search_root self.sign_root = sign_root from camlistore.blobclient import BlobClient self.blobs = BlobClient( http_session=http_session, base_url=blob_root, ) from camlistore.searchclient import SearchClient self.searcher = SearchClient( http_session=http_session, base_url=search_root, )
def __init__(self, http_session=None, blob_root=None, search_root=None, sign_root=None, uploadhelper_root=None): self.http_session = http_session self.blob_root = blob_root self.search_root = search_root self.sign_root = sign_root self.uploadhelper_root = uploadhelper_root from camlistore.blobclient import BlobClient self.blobs = BlobClient( http_session=http_session, base_url=blob_root, ) from camlistore.searchclient import SearchClient self.searcher = SearchClient( http_session=http_session, base_url=search_root, ) if sign_root: from camlistore.signing import Signer self.signer = Signer(http_session=http_session, base_url=sign_root) else: self.signer = None if uploadhelper_root: from camlistore.uploadhelper import UploadHelper self.uploadhelper = UploadHelper(http_session=http_session, base_url=uploadhelper_root) else: self.uploadhelper_root = None
def test_enumerate(self): http_session = MagicMock() http_session.get = MagicMock() response = MagicMock() http_session.get.return_value = response response.status_code = 200 response.content = """ { "blobs": [ { "blobRef": "dummy1", "size": 5 }, { "blobRef": "dummy2", "size": 9 } ], "continueAfter": "dummy2" } """ blobs = BlobClient(http_session, 'http://example.com/') iterable = blobs.enumerate() iterator = iterable.__iter__() blob_metas = [] blob_metas.append(next(iterator)) blob_metas.append(next(iterator)) http_session.get.assert_called_with( 'http://example.com/camli/enumerate-blobs') # now set up for the second request http_session.get = MagicMock() response = MagicMock() http_session.get.return_value = response response.status_code = 200 response.content = """ { "blobs": [ { "blobRef": "dummy3", "size": 17 } ] } """ blob_metas.append(next(iterator)) self.assertRaises( StopIteration, lambda: next(iterator), ) self.assertEqual( [type(x) for x in blob_metas], [BlobMeta, BlobMeta, BlobMeta], ) self.assertEqual( [x.blobref for x in blob_metas], ["dummy1", "dummy2", "dummy3"], ) self.assertEqual( [x.size for x in blob_metas], [5, 9, 17], )