On Dec 6, 9:00 pm, Alex Willmer <[email protected]> wrote:
> 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:
>
> https://github.com/moreati/trac-gitsvn/tree/0.12-versionedattachments
>
> <https://github.com/moreati/trac-gitsvn/tree/0.12-versionedattachments>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:
> a...@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 
> <[email protected]>http://moreati.org.uk/bloghttp://twitter.com/moreati

You need to instantiate the test plugin to load it - as you do for the
listener but not for the permission policy plugin. Plugins that are
part of trac.* namespace should get enabled automatically when loaded,
but anything in external plugins or test plugins saved to project
plugins directory or otherwise outside regular namespace needs to be
specifically enabled. Here is an example of how that is done:

https://www.coderesort.com/p/open/browser/trac-talkplugin/trunk/tractalk/tests/plugins.py

The magic lines being:

self.env = EnvironmentStub(
   enable=['trac.*', 'tractalk.core.*', 'tractalk.plugins.*'])

...and...

TalkActionPlugin(self.env)

Hope it helps.


:::simon

https://www.coderesort.com
http://trac-hacks.org/wiki/osimons

-- 
You received this message because you are subscribed to the Google Groups "Trac 
Development" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/trac-dev?hl=en.

Reply via email to