def test_stream(self): with self.new_document_from_file() as d: d.author_attachments.add( AA(u'document.pdf', self.test_doc_contents)) d = self.api.update_document(d) raa = list(d.author_attachments)[0] with contextlib.closing(raa.stream()) as f: self.assertPDFsEqual(f.read(), self.test_doc_contents)
def test_get_bytes(self): with self.new_document_from_file() as d: d.author_attachments.add( AA(u'document.pdf', self.test_doc_contents)) d = self.api.update_document(d) raa = list(d.author_attachments)[0] result = raa.get_bytes() self.assertTrue(isinstance(result, bytes)) self.assertPDFsEqual(result, self.test_doc_contents)
def test_save_to(self): with self.new_document_from_file() as d: d.author_attachments.add( AA(u'document.pdf', self.test_doc_contents)) d = self.api.update_document(d) raa = list(d.author_attachments)[0] with utils.temporary_dir() as dir_path: raa.save_to(dir_path) with open(os.path.join(dir_path, raa.name), 'rb') as f: self.assertPDFsEqual(f.read(), self.test_doc_contents)
def test_name(self): err_msg = u'name must be unicode or str, not None' with self.assertRaises(TypeError, err_msg): AA(name=None, content=b'') with self.assertRaises(ValueError, u'name must be non-empty string'): AA(name=u'', content=b'') f = AA(name=u'document.pdf', content=b'') self.assertEqual(f.name, u'document.pdf') with self.assertRaises(AttributeError): f.name = u'2' f._set_read_only() self.assertEqual(f.name, u'document.pdf') f._set_invalid() with self.assertRaises(InvalidScriveObject): f.name
def test_merge(self): err_msg = u'merge must be bool, not 2' with self.assertRaises(TypeError, err_msg): AA(name=u'x', content=b'', merge=2) aa = AA(name=u'document.pdf', content=b'') self.assertTrue(aa.merge) aa.merge = False self.assertFalse(aa.merge) err_msg = u'merge must be bool, not []' with self.assertRaises(TypeError, err_msg): aa.merge = [] aa._set_read_only() self.assertFalse(aa.merge) aa._set_invalid() with self.assertRaises(InvalidScriveObject): aa.merge
def test_author_attachments(self): json = self.json.copy() json[u'status'] = u'Preparation' # so it's not read only d = self.O._from_json_obj(json) self.assertEqual(d.author_attachments, ScriveSet()) json[u'authorattachments'] = [{ u'id': u'1', u'name': u'document1.pdf', u'required': True, u'add_to_sealed_file': False }, { u'id': u'2', u'name': u'document2.pdf', u'required': False, u'add_to_sealed_file': True }] d2 = self.O._from_json_obj(json) self.assertEqual(2, len(d2.author_attachments)) for file_ in d2.author_attachments: if file_.id == u'1': self.assertEqual(file_.name, u'document1.pdf') else: self.assertEqual(file_.name, u'document2.pdf') file2 = filter(lambda f: f.id == u'2', d2.author_attachments)[0] d2.author_attachments.remove(file2) file1 = list(d2.author_attachments)[0] self.assertEqual([file1], d2._to_json_obj()[u'authorattachments']) type_err_msg = (u'elem must be AuthorAttachment, not ' u'<scrivepy._document.RemoteAuthorAttachment ' u'object at .*') with self.assertRaisesRegexp(TypeError, type_err_msg): d2.author_attachments.add(file1) d2._set_read_only() # set() is because the 2nd one is read only and not really equal self.assertEqual(set(ScriveSet([file1])), set(d2.author_attachments)) with self.assertRaises(ReadOnlyScriveObject, None): d2.author_attachments.clear() d2.author_attachments.add(_file.LocalFile(u'x', b'')) atts = d2.author_attachments d2._set_invalid() with self.assertRaises(InvalidScriveObject, None): d2.author_attachments with self.assertRaises(InvalidScriveObject, None): atts.add(AA(u'x', b''))
def file_(n): return AA(u'document' + unicode(n) + u'.pdf', contents)
def setUpClass(class_): super(AuthorAttachmentTest, class_).setUpClass() class_.file_ = AA(u'document.pdf', class_.test_doc_contents)