6 Dec 2010 21:00
[Trac-dev] Unit testing an extension point
Alex Willmer <alex <at> moreati.org.uk>
2010-12-06 20:00:06 GMT
2010-12-06 20:00:06 GMT
Hello again,
In the last week I've been working on adding versioning to attachments in Trac, on behalf of Logica.
The code so far, based on Trac 0.12 and modelled after the WikiPage class is here:
I've tried to maintain unit test coverage as I've gone along. Today I tried to add tests for IAttachmentChangeListener, but my TestAttachmentChangeListener is not being registered with the existing AttachmentModule.change_listeners. Is there another step I need to add, or can you spot anything I've done wrong?
from datetime import datetime
import os.path
import shutil
from StringIO import StringIO
import tempfile
import unittest
from trac.attachment import IAttachmentChangeListener, \
Attachment, AttachmentModule
from trac.core import Component, implements
from trac.perm import IPermissionPolicy, PermissionCache
from trac.resource import Resource, resource_exists
from trac.test import EnvironmentStub
from trac.util.datefmt import utc, to_utimestamp
class TicketOnlyViewsTicket(Component):
implements(IPermissionPolicy)
def check_permission(self, action, username, resource, perm):
if action.startswith('TICKET_'):
return resource.realm == 'ticket'
else:
return None
...
class AttachmentTestCase(unittest.TestCase):
def setUp(self):
self.env = EnvironmentStub()
self.env.path = os.path.join(tempfile.gettempdir(), 'trac-tempenv')
os.mkdir(self.env.path)
self.attachments_dir = os.path.join(self.env.path, 'attachments')
self.archive_dir = os.path.join(self.env.path,
AttachmentModule.ARCHIVE_DIR)
self.env.config.set('trac', 'permission_policies',
'TicketOnlyViewsTicket, LegacyAttachmentPolicy')
self.env.config.set('attachment', 'max_size', 512)
self.perm = PermissionCache(self.env)
...
def test_insert(self):
attachment = Attachment(self.env, 'ticket', 42)
attachment.insert('foo.txt', StringIO(''), 0, 1)
attachment = Attachment(self.env, 'ticket', 42)
attachment.insert('bar.jpg', StringIO(''), 0, 2)
attachments = Attachment.select(self.env, 'ticket', 42)
self.assertEqual('foo.txt', attachments.next().filename)
self.assertEqual('bar.jpg', attachments.next().filename)
self.assertRaises(StopIteration, attachments.next)
listener = TestAttachmentChangeListener(self.env)
module = AttachmentModule(self.env)
self.assertEquals(1, len(module.change_listeners))
self.assertEquals('foo.txt', listener.added[0].filename)
self.assertEquals('bar.jpg', listener.added[1].filename)
The output of this is:
alex <at> martha:~/src/trac-gitsvn$ python trac/tests/attachment.py
............F.......F.......
======================================================================
FAIL: test_insert (__main__.AttachmentTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "trac/tests/attachment.py", line 174, in test_insert
self.assertEquals(1, len(module.change_listeners))
AssertionError: 1 != 0
======================================================================
FAIL: Ensure that legacy action tests are done on parent. As
----------------------------------------------------------------------
Traceback (most recent call last):
File "trac/tests/attachment.py", line 425, in test_legacy_permission_on_parent
self.assert_('ATTACHMENT_VIEW' in self.perm(attachment.resource))
AssertionError
----------------------------------------------------------------------
Ran 28 tests in 1.669s
FAILED (failures=2)
I'm not too concerned about the second (ATTACHMENT_VIEW) failure, as it doesn't occur during a full unit-test run. The first however I cannot correct - if I remove the check on change_listeners then it fails on the next line - added has no members. Any ideas?
Thanks, Alex
-- Alex Willmer <alex <at> moreati.org.uk>
http://moreati.org.uk/blog http://twitter.com/moreati
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To post to this group, send email to trac-dev <at> googlegroups.com.
To unsubscribe from this group, send email to trac-dev+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/trac-dev?hl=en.
RSS Feed