def test_locate(self): with mockup_os2_server(self.temp_path, TEST_BUCKET) as (server, url): StoreManager.register('os2', functools.partial(create_os2_store, server, base_url=url), default=True) class Person(self.Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) file = Column(File.as_mutable(Json)) session = self.create_all_and_get_session() person1 = Person() self.assertIsNone(person1.file) sample_content = b'Simple text.' with StoreManager(session): person1 = Person() person1.file = File.create_from(io.BytesIO(sample_content), content_type='text/plain', extension='.txt') self.assertIsInstance(person1.file, File) self.assertEqual( person1.file.locate(), '%s/%s?_ts=%s' % ( url, person1.file.path, person1.file.timestamp ) )
def test_put_error(self): with mockup_os2_server(self.temp_path, TEST_BUCKET) as (server, url): store = create_os2_store(server, bucket=TEST_BUCKET[:-2], base_url=url) target_filename = 'test_put_from_stream/file_from_stream1.txt' content = b'Lorem ipsum dolor sit amet' stream = io.BytesIO(content) with self.assertRaises(OS2Error): store.put(target_filename, stream)
def test_put_from_stream(self): with mockup_os2_server(self.temp_path, TEST_BUCKET) as (server, url): store = create_os2_store(server, base_url=url) target_filename = 'test_put_from_stream/file_from_stream1.txt' content = b'Lorem ipsum dolor sit amet' stream = io.BytesIO(content) length = store.put(target_filename, stream) self.assertEqual(length, len(content)) self.assertIsInstance(store.open(target_filename), io.BytesIO)
def test_delete_error(self): with mockup_os2_server(self.temp_path, TEST_BUCKET) as (server, url): store = create_os2_store(server, base_url=url) wrong_store = create_os2_store(server, base_url=url, bucket=TEST_BUCKET[:-2]) target_filename = 'test_delete/sample_text_file1.txt' with open(self.sample_text_file1, 'rb') as f: length = store.put(target_filename, f) self.assertEqual(length, getsize(self.sample_text_file1)) self.assertIsInstance(store.open(target_filename), io.BytesIO) with self.assertRaises(OS2Error): wrong_store.delete(target_filename)
def test_open(self): with mockup_os2_server(self.temp_path, TEST_BUCKET) as (server, url): store = create_os2_store(server, base_url=url) target_filename = 'test_delete/sample_text_file1.txt' with open(self.sample_text_file1, 'rb') as f: length = store.put(target_filename, f) self.assertEqual(length, getsize(self.sample_text_file1)) self.assertIsInstance(store.open(target_filename), io.BytesIO) # Reading with store.open(target_filename, mode='rb') as stored_file, \ open(self.sample_text_file1, mode='rb') as original_file: self.assertEqual(stored_file.read(), original_file.read())