def test_edit_existing_rollback(self): doc = Document(name=u_('Foo3')) doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() DBSession.remove() d = DBSession.query(Document).filter_by(name=u_('Foo3')).first() old_file = d.content.path d.content = b'HELLO' new_file = d.content.path DBSession.flush() DBSession.rollback() DBSession.remove() assert get_file(old_file).read() == self.file_content try: fold = get_file(new_file) assert False, 'Should have raised IOError here' except IOError: pass
def test_edit_existing(self): doc = Document(name=u_("Foo2")) doc.content = open(self.fake_file.name, "rb") DBSession.add(doc) DBSession.flush() DBSession.commit() DBSession.remove() d = DBSession.query(Document).filter_by(name=u_("Foo2")).first() old_file = d.content.path d.content = b"HELLO" new_file = d.content.path DBSession.flush() DBSession.commit() DBSession.remove() assert get_file(new_file).read() == b"HELLO" try: fold = get_file(old_file) assert False, "Should have raised IOError here" except IOError: pass
def test_edit_existing(self): doc = Document(name=u_('Foo2')) doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) self._session_flush() DBSession.commit() DBSession.remove() d = DBSession.query(Document).filter_by(name=u_('Foo2')).first() old_file = d.content.path d.content = b'HELLO' new_file = d.content.path self._session_flush() DBSession.commit() DBSession.remove() assert get_file(new_file).read() == b'HELLO' try: fold = get_file(old_file) assert False, 'Should have raised IOError here' except IOError: pass
def test_create_empty(self): doc = Document(name=u_('Foo'), content=None) DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.content is None
def test_create_empty(self): doc = Document(name=u_('Foo'), content=None) DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.content is None
def test_column_cannot_delete_after_save(self): doc = SimpleDocument(name=u_('Foo')) doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(SimpleDocument).filter_by(name=u_('Foo')).first() del d.content['hello']
def test_column_cannot_edit_attr_after_save(self): doc = SimpleDocument(name=u_('Foo')) doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(SimpleDocument).filter_by(name=u_('Foo')).first() d.content.hello = 'Everybody'
def create_file(self, lang='en'): fname = {'en': 'hello.txt', 'ru': u_('Крупный'), 'it': u_('àèìòù')}.get(lang, 'unknown') self.UPLOADED_FILES += [DepotManager.get().create(FILE_CONTENT, filename=fname)] return dict(files=self.UPLOADED_FILES, uploaded_to=DepotManager.get_default(), last=self.UPLOADED_FILES[-1])
def test_create_fromfile(self): doc = Document(name=u_("Foo")) doc.second_photo = open(self.fake_file.name, "rb") DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_("Foo")).first() assert d.second_photo.file.read() == self.file_content assert d.second_photo.filename == os.path.basename(self.fake_file.name)
def test_create_from_bytes(self): doc = Document(name=u_('Foo')) doc.second_photo = self.file_content DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.second_photo.file.read() == self.file_content assert d.second_photo.filename == 'unknown'
def test_create_fromfile(self): doc = Document(name=u_('Foo')) doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.content.file.read() == self.file_content assert d.content.file.filename == os.path.basename(self.fake_file.name)
def test_create_fromfile(self): doc = Document(name=u_('Foo')) doc.second_photo = open(self.fake_file.name, 'rb') DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.second_photo.file.read() == self.file_content assert d.second_photo.filename == os.path.basename(self.fake_file.name)
def test_create_from_bytes(self): doc = Document(name=u_('Foo')) doc.second_photo = self.file_content DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.second_photo.file.read() == self.file_content assert d.second_photo.filename == 'unnamed'
def test_create_polymorphic_from_file(self): doc = Confidential(name=u_('Secret')) doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Confidential).filter_by(name=u_('Secret')).first() assert d.content.file.read() == self.file_content assert d.content.file.filename == os.path.basename(self.fake_file.name)
def test_create_with_alias(self): doc = Document(name=u_('Foo')) doc.targeted_content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.targeted_content.file.read() == self.file_content assert d.targeted_content.file.filename == os.path.basename(self.fake_file.name) assert d.targeted_content.depot_name == 'another'
def test_create_with_alias(self): doc = Document(name=u_('Foo')) doc.targeted_content = open(self.fake_file.name, 'rb') DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.targeted_content.file.read() == self.file_content assert d.targeted_content.file.filename == os.path.basename(self.fake_file.name) assert d.targeted_content.depot_name == 'another'
def test_column_is_dictlike(self): doc = SimpleDocument(name=u_('Foo')) doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(SimpleDocument).filter_by(name=u_('Foo')).first() assert doc.content.hello == 'World', doc.content assert 'deleted' not in doc.content, doc.content assert doc.content.changed == 'NEW', doc.content assert 'deleted_attr' not in doc.content, doc.content assert doc.content.missing_attr_trapped == True, doc.content
def test_create_fromfield(self): field = create_cgifs("image/jpeg", self.fake_file, "test.jpg") doc = Document(name=u_("Foo"), content=field) DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_("Foo")).first() assert d.content.file.read() == self.file_content assert d.content.filename == "test.jpg" assert d.content.content_type == "image/jpeg", d.content.content_type assert d.content.url == "/depot/%s" % d.content.path
def test_create_fromfield(self): field = create_cgifs('image/jpeg', self.fake_file, 'test.jpg') doc = Document(name=u_('Foo'), content=field) DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.content.file.read() == self.file_content assert d.content.filename == 'test.jpg' assert d.content.content_type == 'image/jpeg', d.content.content_type assert d.content.url == '/depot/%s' % d.content.path
def test_create_fromfield(self): field = create_cgifs('image/jpeg', self.fake_file, 'test.jpg') doc = Document(name=u_('Foo'), content=field) DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.content.file.read() == self.file_content assert d.content.filename == 'test.jpg' assert d.content.content_type == 'image/jpeg', d.content.content_type assert d.content.url == '/depot/%s' % d.content.path
def test_serving_files_content_disposition(self): app = self.make_app() new_file = app.post('/create_file', params={'lang': 'ru'}).json uploaded_file = app.get(DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file)) content_disposition = uploaded_file.headers['Content-Disposition'] assert content_disposition == "inline;filename=Krupnyi;filename*=utf-8''%D0%9A%D1%80%D1%83%D0%BF%D0%BD%D1%8B%D0%B9", content_disposition new_file = app.post('/create_file', params={'lang': 'it'}).json uploaded_file = app.get(DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file)) content_disposition = uploaded_file.headers['Content-Disposition'] _, asciiname, uniname = content_disposition.split(';') assert asciiname == 'filename=aeiou', asciiname assert u_(unquote(uniname[17:])) == u_('àèìòù'), unquote(uniname[17:])
def test_thumbnail(self): field = create_cgifs('image/gif', self.fake_file, 'test.gif') doc = Document(name=u_('Foo'), photo=field) DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert os.path.exists(d.photo.thumb_file._file_path) thumb = Image.open(d.photo.thumb_file._file_path) thumb.verify() assert thumb.format.upper() == d.photo.thumbnail_format.upper()
def test_thumbnail(self): field = create_cgifs('image/gif', self.fake_file, 'test.gif') doc = Document(name=u_('Foo'), photo=field) DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert os.path.exists(d.photo.thumb_file._file_path) thumb = Image.open(d.photo.thumb_file._file_path) thumb.verify() assert thumb.format.upper() == d.photo.thumbnail_format.upper()
def test_serving_files_content_disposition(self): app = self.make_app() new_file = app.post('/create_file', params={'lang': 'ru'}).json uploaded_file = app.get(DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file)) content_disposition = uploaded_file.headers['Content-Disposition'] assert content_disposition == "inline;filename=\"Krupnyi\";filename*=utf-8''%D0%9A%D1%80%D1%83%D0%BF%D0%BD%D1%8B%D0%B9", content_disposition new_file = app.post('/create_file', params={'lang': 'it'}).json uploaded_file = app.get(DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file)) content_disposition = uploaded_file.headers['Content-Disposition'] _, asciiname, uniname = content_disposition.split(';') assert asciiname == 'filename="aeiou"', asciiname assert u_(unquote(uniname[17:])) == u_('àèìòù'), unquote(uniname[17:])
def test_create_fileintent(self): field = FileIntent(open(self.fake_file.name, 'rb'), u_('àèìòù.gif'), 'image/gif') doc = Document(name=u_('Foo'), second_photo=field) DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.second_photo.file.read() == self.file_content assert d.second_photo.filename == field._filename assert d.second_photo.url == '/depot/%s' % d.second_photo.path assert d.second_photo.thumb_12x12_url == '/depot/%s' % d.second_photo.thumb_12x12_path assert d.second_photo.url != d.second_photo.thumb_12x12_url assert d.second_photo.content_type == field._content_type
def test_create_fileintent(self): field = FileIntent(open(self.fake_file.name, 'rb'), u_('àèìòù.gif'), 'image/gif') doc = Document(name=u_('Foo'), second_photo=field) DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.second_photo.file.read() == self.file_content assert d.second_photo.filename == field._filename assert d.second_photo.url == '/depot/%s' % d.second_photo.path assert d.second_photo.thumb_url == '/depot/%s' % d.second_photo.thumb_path assert d.second_photo.url != d.second_photo.thumb_url assert d.second_photo.content_type == field._content_type
def test_create_fromfield(self): field = cgi.FieldStorage() field.filename = u_('àèìòù.gif') field.file = open(self.fake_file.name, 'rb') doc = Document(name=u_('Foo'), second_photo=field) DBSession.flush() DBSession.clear() d = Document.query.find(dict(name='Foo')).first() assert d.second_photo.file.read() == self.file_content assert d.second_photo.filename == field.filename assert d.second_photo.url == '/depot/%s' % d.second_photo.path assert d.second_photo.thumb_12x12_url == '/depot/%s' % d.second_photo.thumb_12x12_path assert d.second_photo.url != d.second_photo.thumb_12x12_url
def test_create_fromfield(self): field = cgi.FieldStorage() field.filename = u_('àèìòù.gif') field.file = open(self.fake_file.name, 'rb') doc = Document(name=u_('Foo'), photo=field) DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.photo.file.read() == self.file_content assert d.photo.filename == field.filename assert d.photo.url == '/depot/%s' % d.photo.path assert d.photo.thumb_url == '/depot/%s' % d.photo.thumb_path assert d.photo.url != d.photo.thumb_url
def test_create_empty(self): doc = Document(name=u_('Foo'), content=None) DBSession.flush() DBSession.clear() d = Document.query.find(dict(name='Foo')).first() assert d.content is None
def test_create_fromfield(self): field = cgi.FieldStorage() field.filename = u_('àèìòù.gif') field.file = open(self.fake_file.name, 'rb') doc = Document(name=u_('Foo'), photo=field) DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.photo.file.read() == self.file_content assert d.photo.filename == field.filename assert d.photo.url == '/depot/%s' % d.photo.path assert d.photo.thumb_url == '/depot/%s' % d.photo.thumb_path assert d.photo.url != d.photo.thumb_url
def test_maximum_size(self): field = create_cgifs('image/png', self.bigimage, 'test.png') doc = Document(name=u_('Foo'), photo=field) DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() img = Image.open(d.photo.file._file_path) img.verify() assert img.format.upper() == 'PNG' assert max(img.size) == 1024 assert d.photo.filename == field.filename assert d.photo.content_type == 'image/png'
def test_storage_does_not_exists(self): doc = SimpleDocument(name=u_('Foo')) doc.content = UploadedFile(open(self.fake_file.name, 'rb'), 'missing_storage') DBSession.add(doc) DBSession.flush() DBSession.commit()
def test_maximum_size(self): field = create_cgifs('image/png', self.bigimage, 'test.png') doc = Document(name=u_('Foo'), photo=field) DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() img = Image.open(d.photo.file._file_path) img.verify() assert img.format.upper() == 'PNG' assert max(img.size) == 1024 assert d.photo.filename == field.filename assert d.photo.content_type == 'image/png'
def test_create_fromfield(self): field = cgi.FieldStorage() field.filename = u_("àèìòù.gif") field.file = open(self.fake_file.name, "rb") doc = Document(name=u_("Foo"), second_photo=field) DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_("Foo")).first() assert d.second_photo.file.read() == self.file_content assert d.second_photo.filename == field.filename assert d.second_photo.url == "/depot/%s" % d.second_photo.path assert d.second_photo.thumb_12x12_url == "/depot/%s" % d.second_photo.thumb_12x12_path assert d.second_photo.url != d.second_photo.thumb_12x12_url
def test_delete_existing_rollback(self): doc = Document(name=u_('Foo3')) doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() DBSession.remove() d = DBSession.query(Document).filter_by(name=u_('Foo3')).first() old_file = d.content.path DBSession.delete(d) DBSession.flush() DBSession.rollback() DBSession.remove() assert get_file(old_file).read() == self.file_content
def test_public_url(self): doc = Document(name=u_('Foo')) doc.second_photo = open(self.fake_file.name, 'rb') doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() # Hack to force object to have a public url d.second_photo._thaw() d.second_photo['_public_url'] = 'PUBLIC_URL' d.second_photo._freeze() assert d.second_photo.url == 'PUBLIC_URL' assert d.second_photo.thumb_url.startswith('/depot/default/')
def test_public_url(self): doc = Document(name=u_("Foo")) doc.second_photo = open(self.fake_file.name, "rb") doc.content = open(self.fake_file.name, "rb") DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_("Foo")).first() # Hack to force object to have a public url d.second_photo._thaw() d.second_photo["_public_url"] = "PUBLIC_URL" d.second_photo._freeze() assert d.second_photo.url == "PUBLIC_URL" assert d.second_photo.thumb_12x12_url.startswith("/depot/default/")
def test_thumbnail(self): field = create_cgifs('image/gif', self.fake_file, 'test.gif') doc = Document(name=u_('Foo'), second_photo=field) DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() thumbnail_local_path = DepotManager.get_file(d.second_photo.thumb_path)._file_path assert os.path.exists(thumbnail_local_path) thumb = Image.open(thumbnail_local_path) thumb.verify() assert thumb.format.upper() == 'PNG' assert max(thumb.size) == 12
def test_thumbnail(self): field = create_cgifs('image/gif', self.fake_file, 'test.gif') doc = Document(name=u_('Foo'), second_photo=field) DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() thumbnail_local_path = DepotManager.get_file(d.second_photo.thumb_12x12_path)._file_path assert os.path.exists(thumbnail_local_path) thumb = Image.open(thumbnail_local_path) thumb.verify() assert thumb.format.upper() == 'PNG' assert max(thumb.size) == 12
def test_public_url(self): doc = Document(name=u_('Foo')) doc.second_photo = open(self.fake_file.name, 'rb') doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() # Hack to force object to have a public url d.second_photo._thaw() d.second_photo['_public_url'] = 'PUBLIC_URL' d.second_photo._freeze() assert d.second_photo.url == 'PUBLIC_URL' assert d.second_photo.thumb_12x12_url.startswith('/depot/default/')
def test_delete_existing_rollback(self): doc = Document(name=u_('Foo3')) doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) self._session_flush() DBSession.commit() DBSession.remove() d = DBSession.query(Document).filter_by(name=u_('Foo3')).first() old_file = d.content.path DBSession.delete(d) self._session_flush() DBSession.rollback() DBSession.remove() assert get_file(old_file).read() == self.file_content
def test_relationship_cascade_delete_rollback(self): directory = Directory(name='Parent') DBSession.add(directory) directory.documents.append( Document(name=u_('Foo'), content=open(self.fake_file.name, 'rb'))) self._session_flush() DBSession.commit() d = DBSession.query(Directory).filter_by(name=u_('Parent')).first() doc = d.documents[0] old_file = doc.content.path assert self.file_exists(old_file) DBSession.delete(d) self._session_flush() DBSession.rollback() assert self.file_exists(old_file)
def test_create_fromfile(self): doc = Document(name=u_('Foo')) doc.second_photo = open(self.fake_file.name, 'rb') DBSession.flush() DBSession.clear() d = Document.query.find(dict(name='Foo')).first() assert d.second_photo.file.read() == self.file_content assert d.second_photo.filename == os.path.basename(self.fake_file.name)
def test_public_url(self): doc = Document(name=u_('Foo')) doc.photo = open(self.fake_file.name, 'rb') doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() # Hack to force object to have a public url d.photo._thaw() d.photo['_public_url'] = 'PUBLIC_URL' d.photo['_thumb_public_url'] = 'THUMB_PUBLIC_URL' d.photo._freeze() assert d.photo.url == 'PUBLIC_URL' assert d.photo.thumb_url == 'THUMB_PUBLIC_URL'
def test_delete_existing_rollback(self): doc = Document(name=u_('Foo3')) doc.content = open(self.fake_file.name, 'rb') DBSession.flush() DBSession.clear() d = Document.query.find(dict(name='Foo3')).first() old_file = d.content.path d.delete() DBSession.clear() assert get_file(old_file).read() == self.file_content
def test_create_fromfield(self): field = create_cgifs('image/jpeg', self.fake_file, 'test.jpg') doc = Document(name=u_('Foo'), content=field) DBSession.flush() DBSession.clear() d = Document.query.find(dict(name='Foo')).first() assert d.content.file.read() == self.file_content assert d.content.filename == 'test.jpg' assert d.content.content_type == 'image/jpeg', d.content.content_type assert d.content.url == '/depot/%s' % d.content.path
def test_delete_existing(self): doc = Document(name=u_('Foo2')) doc.content = open(self.fake_file.name, 'rb') DBSession.add(doc) DBSession.flush() DBSession.commit() DBSession.remove() d = DBSession.query(Document).filter_by(name=u_('Foo2')).first() old_file = d.content.path DBSession.delete(d) DBSession.flush() DBSession.commit() DBSession.remove() try: fold = get_file(old_file) assert False, 'Should have raised IOError here' except IOError: pass
def test_check_assigned_type(self): doc = Document(name=u_('Foo')) doc.photo = UploadedFile(open(self.fake_file.name, 'rb')) DBSession.add(doc) try: DBSession.flush() except StatementError as e: assert 'ValueError' in str(e) return assert False, 'FLUSH did not raise exception'
def test_rollback(self): doc = Document(name=u_('Foo3')) doc.second_photo = open(self.fake_file.name, 'rb') DBSession.add(doc) self._session_flush() uploaded_file = doc.second_photo.path uploaded_thumb = doc.second_photo.thumb_12x12_path DBSession.rollback() DBSession.remove() assert not self.file_exists(uploaded_file) assert not self.file_exists(uploaded_thumb)