~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/testing/__init__.py

Merge db-devel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
    'ZopeTestInSubProcess',
52
52
    ]
53
53
 
 
54
from cStringIO import StringIO
54
55
from contextlib import contextmanager
55
56
from datetime import (
56
57
    datetime,
91
92
import testtools
92
93
from testtools.content import Content
93
94
from testtools.content_type import UTF8_TEXT
94
 
from testtools.matchers import Matcher, Mismatch
 
95
from testtools.matchers import MatchesRegex
95
96
import transaction
96
97
from windmill.authoring import WindmillTestClient
97
98
from zope.component import (
530
531
        expected_vector, observed_vector = zip(*args)
531
532
        return self.assertEqual(expected_vector, observed_vector)
532
533
 
 
534
    @contextmanager
 
535
    def expectedLog(self, regex):
 
536
        """Expect a log to be written that matches the regex."""
 
537
        output = StringIO()
 
538
        handler = logging.StreamHandler(output)
 
539
        logger = logging.getLogger()
 
540
        logger.addHandler(handler)
 
541
        try:
 
542
            yield
 
543
        finally:
 
544
            logger.removeHandler(handler)
 
545
        self.assertThat(output.getvalue(), MatchesRegex(regex))
 
546
 
533
547
    def pushConfig(self, section, **kwargs):
534
548
        """Push some key-value pairs into a section of the config.
535
549
 
1323
1337
            source_package.productseries,
1324
1338
            source_package.sourcepackagename,
1325
1339
            source_package.distroseries)
1326
 
 
1327
 
 
1328
 
class RegexMatcher(Matcher):
1329
 
    """A matcher that matches a regular expression."""
1330
 
 
1331
 
    def __init__(self, pattern, flags=0):
1332
 
        """Constructor.
1333
 
 
1334
 
        :param pattern: The pattern to match.
1335
 
        :param flags: The flags to use when performing the match.
1336
 
        """
1337
 
        self.pattern = pattern
1338
 
        self.flags = flags
1339
 
 
1340
 
    def match(self, something):
1341
 
        """See `Matcher`."""
1342
 
        if re.match(self.pattern, something, self.flags):
1343
 
            return None
1344
 
        return Mismatch('Pattern "%s" not in "%s".' % (self.pattern, something))
1345
 
 
1346
 
    def __str__(self):
1347
 
        """See `Matcher`."""
1348
 
        return 'RegexMatcher(%r)' % self.pattern