~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/test/test_log.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
#
5
5
 
6
 
import sys
 
6
import sys, time
7
7
 
8
8
from twisted.trial import unittest
9
9
 
50
50
            self.assertEquals(i['isError'], 1)
51
51
            log.flushErrors(ig)
52
52
 
 
53
    def testErrorsWithWhy(self):
 
54
        for e, ig in [("hello world","hello world"),
 
55
                      (KeyError(), KeyError),
 
56
                      (failure.Failure(RuntimeError()), RuntimeError)]:
 
57
            log.err(e, 'foobar')
 
58
            i = self.catcher.pop()
 
59
            self.assertEquals(i['isError'], 1)
 
60
            self.assertEquals(i['why'], 'foobar')
 
61
            log.flushErrors(ig)
 
62
 
 
63
 
53
64
    def testErroneousErrors(self):
54
65
        L1 = []
55
66
        L2 = []
56
 
        log.addObserver(lambda events: events['isError'] or L1.append(events))
 
67
        log.addObserver(lambda events: L1.append(events))
57
68
        log.addObserver(lambda events: 1/0)
58
 
        log.addObserver(lambda events: events['isError'] or L2.append(events))
 
69
        log.addObserver(lambda events: L2.append(events))
59
70
        log.msg("Howdy, y'all.")
60
71
 
61
 
        excs = [f.type for f in log.flushErrors(ZeroDivisionError)]
 
72
        # XXX - use private _flushErrors so we don't also catch
 
73
        # the deprecation warnings
 
74
        excs = [f.type for f in log._flushErrors(ZeroDivisionError)]
62
75
        self.assertEquals([ZeroDivisionError], excs)
63
76
 
64
77
        self.assertEquals(len(L1), 2)
115
128
        sys.setdefaultencoding(self._origEncoding)
116
129
        del sys.setdefaultencoding
117
130
 
 
131
 
 
132
 
118
133
class LogPublisherTestCase(LogPublisherTestCaseMixin, unittest.TestCase):
119
 
 
120
134
    def testSingleString(self):
121
135
        self.lp.msg("Hello, world.")
122
136
        self.assertEquals(len(self.out), 1)
123
137
 
 
138
 
124
139
    def testMultipleString(self):
125
140
        # Test some stupid behavior that will be deprecated real soon.
126
141
        # If you are reading this and trying to learn how the logging
128
143
        self.lp.msg("Hello, ", "world.")
129
144
        self.assertEquals(len(self.out), 1)
130
145
 
 
146
 
131
147
    def testSingleUnicode(self):
132
148
        self.lp.msg(u"Hello, \N{VULGAR FRACTION ONE HALF} world.")
133
149
        self.assertEquals(len(self.out), 1)
134
150
        self.assertIn('with str error Traceback', self.out[0])
135
151
        self.assertIn('UnicodeEncodeError', self.out[0])
136
152
 
 
153
 
 
154
 
137
155
class FileObserverTestCase(LogPublisherTestCaseMixin, unittest.TestCase):
 
156
    def test_timeFormatting(self):
 
157
        """
 
158
        Test the method of L{FileLogObserver} which turns a timestamp into a
 
159
        human-readable string.
 
160
        """
 
161
        # There is no function in the time module which converts a UTC time
 
162
        # tuple to a timestamp.
 
163
        when = time.mktime((2001, 2, 3, 4, 5, 6, 7, 8, 0)) - time.timezone
 
164
 
 
165
        # Pretend to be in US/Eastern for a moment
 
166
        self.flo.getTimezoneOffset = lambda: 18000
 
167
        self.assertEquals(self.flo.formatTime(when), '2001/02/02 23:05 -0500')
 
168
 
 
169
        # Okay now we're in Eastern Europe somewhere
 
170
        self.flo.getTimezoneOffset = lambda: -3600
 
171
        self.assertEquals(self.flo.formatTime(when), '2001/02/03 05:05 +0100')
 
172
 
 
173
        # And off in the Pacific or someplace like that
 
174
        self.flo.getTimezoneOffset = lambda: -39600
 
175
        self.assertEquals(self.flo.formatTime(when), '2001/02/03 15:05 +1100')
 
176
 
 
177
        # One of those weird places with a half-hour offset timezone
 
178
        self.flo.getTimezoneOffset = lambda: 5400
 
179
        self.assertEquals(self.flo.formatTime(when), '2001/02/03 02:35 -0130')
 
180
 
 
181
        # Half-hour offset in the other direction
 
182
        self.flo.getTimezoneOffset = lambda: -5400
 
183
        self.assertEquals(self.flo.formatTime(when), '2001/02/03 05:35 +0130')
 
184
 
 
185
        # If a strftime-format string is present on the logger, it should
 
186
        # use that instead.  Note we don't assert anything about day, hour
 
187
        # or minute because we cannot easily control what time.strftime()
 
188
        # thinks the local timezone is.
 
189
        self.flo.timeFormat = '%Y %m'
 
190
        self.assertEquals(self.flo.formatTime(when), '2001 02')
 
191
 
 
192
 
138
193
    def testLoggingAnObjectWithBroken__str__(self):
139
194
        #HELLO, MCFLY
140
195
        self.lp.msg(EvilStr())
142
197
        # Logging system shouldn't need to crap itself for this trivial case
143
198
        self.assertNotIn('UNFORMATTABLE', self.out[0])
144
199
 
 
200
 
145
201
    def testFormattingAnObjectWithBroken__str__(self):
146
202
        self.lp.msg(format='%(blat)s', blat=EvilStr())
147
203
        self.assertEquals(len(self.out), 1)
148
204
        self.assertIn('Invalid format string or unformattable object', self.out[0])
149
205
 
 
206
 
150
207
    def testBrokenSystem__str__(self):
151
208
        self.lp.msg('huh', system=EvilStr())
152
209
        self.assertEquals(len(self.out), 1)
153
210
        self.assertIn('Invalid format string or unformattable object', self.out[0])
154
211
 
 
212
 
155
213
    def testFormattingAnObjectWithBroken__repr__Indirect(self):
156
214
        self.lp.msg(format='%(blat)s', blat=[EvilRepr()])
157
215
        self.assertEquals(len(self.out), 1)
158
216
        self.assertIn('UNFORMATTABLE OBJECT', self.out[0])
159
217
 
 
218
 
160
219
    def testSystemWithBroker__repr__Indirect(self):
161
220
        self.lp.msg('huh', system=[EvilRepr()])
162
221
        self.assertEquals(len(self.out), 1)
163
222
        self.assertIn('UNFORMATTABLE OBJECT', self.out[0])
164
223
 
 
224
 
165
225
    def testSimpleBrokenFormat(self):
166
226
        self.lp.msg(format='hooj %s %s', blat=1)
167
227
        self.assertEquals(len(self.out), 1)
168
228
        self.assertIn('Invalid format string or unformattable object', self.out[0])
169
229
 
 
230
 
170
231
    def testRidiculousFormat(self):
171
232
        self.lp.msg(format=42, blat=1)
172
233
        self.assertEquals(len(self.out), 1)
173
234
        self.assertIn('Invalid format string or unformattable object', self.out[0])
174
235
 
 
236
 
175
237
    def testEvilFormat__repr__And__str__(self):
176
238
        self.lp.msg(format=EvilReprStr(), blat=1)
177
239
        self.assertEquals(len(self.out), 1)