def test_reparent(self): """Change the parent realm and parent id of an attachment """ attachment1 = Attachment(self.env, 'wiki', 'SomePage') attachment1.insert('foo.txt', io.BytesIO(), 0) path1 = attachment1.path attachment2 = Attachment(self.env, 'wiki', 'SomePage') attachment2.insert('bar.jpg', io.BytesIO(), 0) attachments = Attachment.select(self.env, 'wiki', 'SomePage') self.assertEqual(2, len(list(attachments))) attachments = Attachment.select(self.env, 'ticket', 42) self.assertEqual(0, len(list(attachments))) self.assertTrue(os.path.exists(path1) and os.path.exists(attachment2.path)) attachment1.move('ticket', 42) self.assertEqual('ticket', attachment1.parent_realm) self.assertEqual('ticket', attachment1.resource.parent.realm) self.assertEqual('42', attachment1.parent_id) self.assertEqual('42', attachment1.resource.parent.id) attachments = Attachment.select(self.env, 'wiki', 'SomePage') self.assertEqual(1, len(list(attachments))) attachments = Attachment.select(self.env, 'ticket', 42) self.assertEqual(1, len(list(attachments))) self.assertFalse(os.path.exists(path1) and os.path.exists(attachment1.path)) self.assertTrue(os.path.exists(attachment2.path))
def test_attachment_reparented_not_called_on_rename(self): attachment = Attachment(self.env, 'wiki', 'SomePage') attachment.insert('foo.txt', io.BytesIO(), 0) attachment.move(new_filename='bar.txt') modern_listener = self.attachment_change_listeners[0](self.env) self.assertEqual(1, modern_listener.moved_call_count) self.assertEqual(0, modern_listener.reparented_call_count)
def test_move_attachment_not_modified_raises(self): """TracError is raised when attachment not modified on move.""" attachment = Attachment(self.env, 'wiki', 'SomePage') attachment.insert('foo.txt', io.BytesIO(), 0) with self.assertRaises(TracError) as cm: attachment.move(attachment.parent_realm, attachment.parent_id, attachment.filename) self.assertEqual("Attachment not modified", unicode(cm.exception))
def test_move_nonexistent_attachment_raises(self): """TracError is raised when moving a non-existent attachment.""" attachment = Attachment(self.env, 'wiki', 'SomePage') with self.assertRaises(TracError) as cm: attachment.move(attachment.parent_realm, attachment.parent_id, attachment.filename) self.assertEqual("Cannot rename non-existent attachment", unicode(cm.exception))
def test_move_attachment_to_nonexistent_resource_raises(self): """TracError is raised moving an attachment to nonexistent resource """ attachment = Attachment(self.env, 'wiki', 'SomePage') attachment.insert('foo.txt', io.BytesIO(), 0) with self.assertRaises(TracError) as cm: attachment.move('wiki', 'NonExistentPage') self.assertEqual("NonExistentPage doesn't exist, can't move attachment", unicode(cm.exception))
def test_move_attachment_to_existing_path_raises(self): """TracError is raised if target already exists""" attachment1 = Attachment(self.env, 'wiki', 'SomePage') attachment1.insert('foo.txt', io.BytesIO(), 0) attachment2 = Attachment(self.env, 'wiki', 'SomePage') attachment2.insert('bar.txt', io.BytesIO(), 0) with self.assertRaises(TracError) as cm: attachment1.move(new_filename=attachment2.filename) self.assertEqual('Cannot move attachment "foo.txt" to "wiki:SomePage: ' 'bar.txt" as it already exists', unicode(cm.exception))
def test_attachment_change_listeners_called(self): """The move method calls attachment change listeners""" attachment = Attachment(self.env, 'wiki', 'SomePage') attachment.insert('foo.txt', io.BytesIO(), 0) attachment.move(new_realm='ticket', new_id=42) attachment.delete() modern_listener = self.attachment_change_listeners[0](self.env) self.assertEqual(1, modern_listener.added_call_count) self.assertEqual(1, modern_listener.deleted_call_count) self.assertEqual(1, modern_listener.moved_call_count) self.assertEqual('wiki', modern_listener.moved_old_parent_realm) self.assertEqual('SomePage', modern_listener.moved_old_parent_id) self.assertEqual('foo.txt', modern_listener.moved_old_filename) legacy_listener = self.attachment_change_listeners[0](self.env) self.assertEqual(1, legacy_listener.added_call_count) self.assertEqual(1, legacy_listener.deleted_call_count)
def test_rename(self): """Rename an attachment.""" attachment = Attachment(self.env, 'wiki', 'SomePage') attachment.insert('foo.txt', io.BytesIO(), 0) original_path = attachment.path self.assertTrue(os.path.exists(original_path)) attachments = Attachment.select(self.env, 'wiki', 'SomePage') self.assertEqual(1, len(list(attachments))) attachment.move(new_filename='bar.txt') attachments = Attachment.select(self.env, 'wiki', 'SomePage') self.assertEqual(1, len(list(attachments))) self.assertEqual('wiki', attachment.parent_realm) self.assertEqual('SomePage', attachment.parent_id) self.assertEqual('bar.txt', attachment.filename) self.assertFalse(os.path.exists(original_path)) self.assertTrue(os.path.exists(attachment.path))