def test_basic_actions(self): content = b'content' storage = CloudStorage() name = u'tmp.ąćęłńóśźż.马铃薯.zip' f = ContentFile(content, name='my_file') filename = storage.save(name, f) self.assertIsInstance(filename, six.string_types) self.assertTrue(filename.endswith(name)) self.assertTrue(storage.exists(filename)) self.assertEqual(storage.size(filename), len(content)) url = storage.url(filename) self.assertIsInstance(url, six.string_types) self.assertNotEqual(url, '') response = requests.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, content) f = storage.open(filename) self.assertIsInstance(f, File) self.assertEqual(f.read(), content) # Delete it storage.delete(filename) self.assertFalse(storage.exists(filename))
def test_basic_actions(self): storage = CloudStorage() name = u'tmp.ąćęłńóśźż.马铃薯.zip' f = ContentFile('content', name='my_file') filename = storage.save(name, f) self.assertIsInstance(filename, basestring) self.assertTrue(filename.endswith(name)) self.assertTrue(storage.exists(filename)) self.assertEqual(storage.size(filename), len('content')) url = storage.url(filename) self.assertIsInstance(url, basestring) self.assertNotEqual(url, '') abs_url = urlparse.urlunparse( ('http', os.environ['HTTP_HOST'], url, None, None, None)) response = urlfetch.fetch(abs_url) self.assertEqual(response.status_code, httplib.OK) self.assertEqual(response.content, 'content') f = storage.open(filename) self.assertIsInstance(f, File) self.assertEqual(f.read(), 'content') # Delete it storage.delete(filename) self.assertFalse(storage.exists(filename))
def test_different_bucket(self): from google.cloud.exceptions import NotFound storage = CloudStorage(bucket_name='different_test_bucket') name = './my_file' f = ContentFile(b'content') with self.assertRaises(NotFound) as cm: storage.save(name, f) self.assertIn('different_test_bucket', cm.exception.message)
def test_new_objects_with_an_explicit_acl(self): storage = CloudStorage(google_acl="public-read") filename = "example.txt" fileobj = ContentFile("content", name=filename) with sleuth.watch("cloudstorage.open") as open_func: storage.save(filename, fileobj) self.assertTrue(storage.exists(filename)) self.assertEqual(open_func.calls[0].kwargs["options"], {"x-goog-acl": "public-read"})
def test_basic_actions(self): storage = CloudStorage() f = ContentFile('content', name='my_file') filename = storage.save('tmp', f) self.assertIsInstance(filename, basestring) self.assertTrue(filename.endswith('tmp')) self.assertTrue(storage.exists(filename)) self.assertEqual(storage.size(filename), len('content')) url = storage.url(filename) self.assertIsInstance(url, basestring) self.assertNotEqual(url, '') abs_url = urlparse.urlunparse( ('http', os.environ['HTTP_HOST'], url, None, None, None) ) response = urlfetch.fetch(abs_url) self.assertEqual(response.status_code, httplib.OK) self.assertEqual(response.content, 'content') f = storage.open(filename) self.assertIsInstance(f, File) self.assertEqual(f.read(), 'content') # Delete it storage.delete(filename) self.assertFalse(storage.exists(filename))
def test_basic_actions(self): storage = CloudStorage() name = u"tmp.ąćęłńóśźż.马铃薯.zip" f = ContentFile("content", name="my_file") filename = storage.save(name, f) self.assertIsInstance(filename, basestring) self.assertTrue(filename.endswith(name)) self.assertTrue(storage.exists(filename)) self.assertEqual(storage.size(filename), len("content")) url = storage.url(filename) self.assertIsInstance(url, basestring) self.assertNotEqual(url, "") abs_url = urlparse.urlunparse(("http", os.environ["HTTP_HOST"], url, None, None, None)) response = urlfetch.fetch(abs_url) self.assertEqual(response.status_code, httplib.OK) self.assertEqual(response.content, "content") f = storage.open(filename) self.assertIsInstance(f, File) self.assertEqual(f.read(), "content") # Delete it storage.delete(filename) self.assertFalse(storage.exists(filename))
def test_basic_actions(self): storage = CloudStorage() name = u'tmp.ąćęłńóśźż.马铃薯.zip' f = ContentFile('content', name='my_file') filename = storage.save(name, f) self.assertIsInstance(filename, six.string_types) self.assertTrue(filename.endswith(name)) self.assertTrue(storage.exists(filename)) self.assertEqual(storage.size(filename), len('content')) url = storage.url(filename) self.assertIsInstance(url, six.string_types) self.assertNotEqual(url, '') abs_url = six.moves.urllib.parse.urlunparse( ('http', os.environ['HTTP_HOST'], url, None, None, None) ) response = urlfetch.fetch(abs_url) self.assertEqual(response.status_code, six.moves.http_client.OK) self.assertEqual(response.content, 'content') f = storage.open(filename) self.assertIsInstance(f, File) self.assertEqual(f.read(), 'content') # Delete it storage.delete(filename) self.assertFalse(storage.exists(filename))
def test_new_objects_get_the_default_acl(self): storage = CloudStorage() filename = 'example.txt' fileobj = ContentFile(b'content') with sleuth.watch('google.cloud.storage.blob.Blob.upload_from_file' ) as upload_func: storage.save(filename, fileobj) self.assertTrue(storage.exists(filename)) self.assertIsNone(upload_func.calls[0].kwargs['predefined_acl'])
def test_new_objects_get_the_default_acl(self): storage = CloudStorage() filename = "example.txt" fileobj = ContentFile("content", name=filename) with sleuth.watch("cloudstorage.open") as open_func: storage.save(filename, fileobj) self.assertTrue(storage.exists(filename)) # There's no x-goog-acl argument, so default perms are applied. self.assertEqual(open_func.calls[0].kwargs["options"], {})
def test_new_objects_get_the_default_acl(self): storage = CloudStorage() filename = 'example.txt' fileobj = ContentFile('content', name=filename) with sleuth.watch('cloudstorage.open') as open_func: storage.save(filename, fileobj) self.assertTrue(storage.exists(filename)) # There's no x-goog-acl argument, so default perms are applied. self.assertEqual(open_func.calls[0].kwargs['options'], {})
def test_new_objects_with_an_explicit_acl(self): storage = CloudStorage(google_acl='public-read') filename = 'example.txt' fileobj = ContentFile('content', name=filename) with sleuth.watch('cloudstorage.open') as open_func: storage.save(filename, fileobj) self.assertTrue(storage.exists(filename)) self.assertEqual( open_func.calls[0].kwargs['options'], {'x-goog-acl': 'public-read'}, )
def test_delete_uses_correct_bucket(self): storage = CloudStorage() filename = storage.save('file1', ContentFile(b'content', name='file1')) storage = CloudStorage() # new instance storage.delete(filename) self.assertFalse(storage.exists(filename))
def test_new_objects_with_an_explicit_acl(self): storage = CloudStorage(google_acl='publicRead') filename = 'example.txt' fileobj = ContentFile(b'content', name=filename) with sleuth.watch('google.cloud.storage.blob.Blob.upload_from_file' ) as upload_func: storage.save(filename, fileobj) self.assertTrue(storage.exists(filename)) self.assertEqual( upload_func.calls[0].kwargs['predefined_acl'], 'publicRead', )
def test_map_over_files(self): storage = CloudStorage() storage.save('a/b/c/tmp1', ContentFile('abcdefghi')) storage.save('c/tmp2', ContentFile('not matching pattern')) storage.save('a/d/tmp3', ContentFile('xxx')) counter = Counter.objects.create() pipeline = map_files('test_bucket', count_contents, filenames=['a/*'], counter_id=counter.pk) self.process_task_queues() pipeline = get_pipeline_by_id(pipeline.pipeline_id) self.assertTrue(pipeline.has_finalized) counter.refresh_from_db() self.assertEqual(12, counter.count)
def test_map_over_files(self): storage = CloudStorage() storage.save('a/b/c/tmp1', ContentFile('abcdefghi')) storage.save('c/tmp2', ContentFile('not matching pattern')) storage.save('a/d/tmp3', ContentFile('xxx')) counter = Counter.objects.create() pipeline = map_files( 'test_bucket', count_contents, filenames=['a/*'], counter_id=counter.pk ) self.process_task_queues() pipeline = get_pipeline_by_id(pipeline.pipeline_id) self.assertTrue(pipeline.has_finalized) counter.refresh_from_db() self.assertEqual(12, counter.count)
def test_dotslash_prefix(self): storage = CloudStorage() name = './my_file' f = ContentFile('content') filename = storage.save(name, f) self.assertEqual(filename, name.lstrip("./"))
def test_supports_nameless_files(self): storage = CloudStorage() f2 = ContentFile("nameless-content") storage.save("tmp2", f2)
def test_dotslash_prefix(self): storage = CloudStorage() name = "./my_file" f = ContentFile("content") filename = storage.save(name, f) self.assertEqual(filename, name.lstrip("./"))
def test_open_uses_correct_bucket(self): storage = CloudStorage() filename = storage.save('file1', ContentFile(b'content', name='file1')) storage = CloudStorage() # new instance storage._open(filename)
def test_supports_nameless_files(self): storage = CloudStorage() f2 = ContentFile('nameless-content') storage.save('tmp2', f2)
def test_no_config_raises(self): from django.core.exceptions import ImproperlyConfigured with sleuth.fake("djangae.storage.project_id", return_value=None): with self.assertRaises(ImproperlyConfigured): CloudStorage()