~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/model/tests/test_bounce.py

  • Committer: Barry Warsaw
  • Date: 2012-04-22 21:33:33 UTC
  • mfrom: (7150.1.11 transactions)
  • Revision ID: barry@list.org-20120422213333-3skjqsjktooesgsl
Several non-functional improvements to the code base.

Reduce the explicit use of the config.db global by introducing two new
helpers:
 - A new transaction() context manager which will commit the transaction on
   successful exit, otherwise it will abort the transaction
 - A new dbconnection decorator which calls the decorated method with the
   Storm store object as the first argument (after self).  This can be used
   instead of config.db.store.

By reducing the explicit use of this global, we have a better chance of
refactoring it away in the future.  Still TODO: runner.py and lmtp.py.

Be explicit about the `store` attribute on the IDatabase interface.

More consistent use of __future__ imports.

Remove an obsolete command line script.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Test bounce model objects."""
19
19
 
20
 
from __future__ import absolute_import, unicode_literals
 
20
from __future__ import absolute_import, print_function, unicode_literals
21
21
 
22
22
__metaclass__ = type
23
23
__all__ = [
30
30
from zope.component import getUtility
31
31
 
32
32
from mailman.app.lifecycle import create_list
33
 
from mailman.config import config
 
33
from mailman.database.transaction import transaction
34
34
from mailman.interfaces.bounce import BounceContext, IBounceProcessor
35
35
from mailman.testing.helpers import (
36
36
    specialized_message_from_string as message_from_string)
52
52
""")
53
53
 
54
54
    def test_events_iterator(self):
55
 
        self._processor.register(self._mlist, 'anne@example.com', self._msg)
56
 
        config.db.commit()
 
55
        with transaction():
 
56
            self._processor.register(
 
57
                self._mlist, 'anne@example.com', self._msg)
57
58
        events = list(self._processor.events)
58
59
        self.assertEqual(len(events), 1)
59
60
        event = events[0]
75
76
        self.assertEqual(event.processed, False)
76
77
 
77
78
    def test_unprocessed_events_iterator(self):
78
 
        self._processor.register(self._mlist, 'anne@example.com', self._msg)
79
 
        self._processor.register(self._mlist, 'bart@example.com', self._msg)
80
 
        config.db.commit()
 
79
        with transaction():
 
80
            self._processor.register(
 
81
                self._mlist, 'anne@example.com', self._msg)
 
82
            self._processor.register(
 
83
                self._mlist, 'bart@example.com', self._msg)
81
84
        events = list(self._processor.events)
82
85
        self.assertEqual(len(events), 2)
83
86
        unprocessed = list(self._processor.unprocessed)
84
87
        # The unprocessed list will be exactly the same right now.
85
88
        self.assertEqual(len(unprocessed), 2)
86
89
        # Process one of the events.
87
 
        events[0].processed = True
88
 
        config.db.commit()
 
90
        with transaction():
 
91
            events[0].processed = True
89
92
        # Now there will be only one unprocessed event.
90
93
        unprocessed = list(self._processor.unprocessed)
91
94
        self.assertEqual(len(unprocessed), 1)
92
95
        # Process the other event.
93
 
        events[1].processed = True
94
 
        config.db.commit()
 
96
        with transaction():
 
97
            events[1].processed = True
95
98
        # Now there will be no unprocessed events.
96
99
        unprocessed = list(self._processor.unprocessed)
97
100
        self.assertEqual(len(unprocessed), 0)