File: test_message.py

package info (click to toggle)
python-eliot 1.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: python: 8,641; makefile: 151
file content (334 lines) | stat: -rw-r--r-- 11,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
Tests for L{eliot._message}.
"""

from unittest import TestCase
from uuid import UUID
import time

from pyrsistent import pmap

try:
    from twisted.python.failure import Failure
except ImportError:
    Failure = None

from .._message import WrittenMessage, Message, log_message
from .._output import MemoryLogger
from .._action import Action, start_action, TaskLevel
from .. import add_destinations, remove_destination


class DeprecatedMessageTests(TestCase):
    """
    Test for L{Message}.
    """

    def test_new(self):
        """
        L{Message.new} returns a new L{Message} that is initialized with the
        given keyword arguments as its contents, and a default message type.
        """
        msg = Message.new(key="value", another=2)
        self.assertEqual(msg.contents(), {"key": "value", "another": 2})

    def test_contentsCopies(self):
        """
        L{Message.contents} returns a copy of the L{Message} contents.
        """
        msg = Message.new(key="value")
        del msg.contents()["key"]
        self.assertEqual(msg.contents(), {"key": "value"})

    def test_bindOverwrites(self):
        """
        L{Message.bind} returns a new L{Message} whose contents include the
        additional given fields.
        """
        msg = Message.new(key="value", another=2)
        another = msg.bind(another=3, more=4)
        self.assertIsInstance(another, Message)
        self.assertEqual(another.contents(), {"key": "value", "another": 3, "more": 4})

    def test_bindPreservesOriginal(self):
        """
        L{Message.bind} does not mutate the instance it is called on.
        """
        msg = Message.new(key=4)
        msg.bind(key=6)
        self.assertEqual(msg.contents(), {"key": 4})

    def test_writeCallsLoggerWrite(self):
        """
        L{Message.write} calls the given logger's C{write} method with a
        dictionary that is superset of the L{Message} contents.
        """
        logger = MemoryLogger()
        msg = Message.new(key=4)
        msg.write(logger)
        self.assertEqual(len(logger.messages), 1)
        self.assertEqual(logger.messages[0]["key"], 4)

    def test_writeDefaultLogger(self):
        """
        L{Message.write} writes to the default logger if none is given.
        """
        messages = []
        add_destinations(messages.append)
        self.addCleanup(remove_destination, messages.append)
        Message.new(some_key=1234).write()
        self.assertEqual(messages[0]["some_key"], 1234)

    def test_writeCreatesNewDictionary(self):
        """
        L{Message.write} creates a new dictionary on each call.

        This is important because we mutate the dictionary in
        ``Logger.write``, so we want to make sure the ``Message`` is unchanged
        in that case. In general we want ``Message`` objects to be effectively
        immutable.
        """

        class Logger(list):
            def write(self, d, serializer):
                self.append(d)

        logger = Logger()
        msg = Message.new(key=4)
        msg.write(logger)
        logger[0]["key"] = 5
        msg.write(logger)
        self.assertEqual(logger[1]["key"], 4)

    def test_logCallsDefaultLoggerWrite(self):
        """
        L{Message.log} calls the default logger's C{write} method with a
        dictionary that is superset of the L{Message} contents.
        """
        messages = []
        add_destinations(messages.append)
        self.addCleanup(remove_destination, messages.append)
        Message.log(some_key=1234)
        self.assertEqual(messages[0]["some_key"], 1234)

    def test_defaultTime(self):
        """
        L{Message._time} returns L{time.time} by default.
        """
        msg = Message({})
        self.assertIs(msg._time, time.time)

    def test_writeAddsTimestamp(self):
        """
        L{Message.write} adds a C{"timestamp"} field to the dictionary written
        to the logger, with the current time in seconds since the epoch.
        """
        logger = MemoryLogger()
        msg = Message.new(key=4)
        msg.write(logger)
        self.assertTrue(time.time() - logger.messages[0]["timestamp"] < 0.1)

    def test_write_preserves_message_type(self):
        """
        L{Message.write} doesn't add a C{message_type} if one is already set.
        """
        logger = MemoryLogger()
        msg = Message.new(key=4, message_type="isetit")
        msg.write(logger)
        self.assertEqual(logger.messages[0]["message_type"], "isetit")
        self.assertNotIn("action_type", logger.messages[0])

    def test_explicitAction(self):
        """
        L{Message.write} adds the identification fields from the given
        L{Action} to the dictionary written to the logger, as well as a
        message_type if none is set.
        """
        logger = MemoryLogger()
        action = Action(logger, "unique", TaskLevel(level=[]), "sys:thename")
        msg = Message.new(key=2)
        msg.write(logger, action)
        written = logger.messages[0]
        del written["timestamp"]
        self.assertEqual(
            written,
            {"task_uuid": "unique", "task_level": [1], "key": 2, "message_type": ""},
        )

    def test_implicitAction(self):
        """
        If no L{Action} is specified, L{Message.write} adds the identification
        fields from the current execution context's L{Action} to the
        dictionary written to the logger.
        """
        logger = MemoryLogger()
        action = Action(logger, "unique", TaskLevel(level=[]), "sys:thename")
        msg = Message.new(key=2)
        with action:
            msg.write(logger)
        written = logger.messages[0]
        del written["timestamp"]
        self.assertEqual(
            written,
            {"task_uuid": "unique", "task_level": [1], "key": 2, "message_type": ""},
        )

    def test_missingAction(self):
        """
        If no L{Action} is specified, and the current execution context has no
        L{Action}, a new task_uuid is generated.

        This ensures all messages have a unique identity, as specified by
        task_uuid/task_level.
        """
        logger = MemoryLogger()
        Message.new(key=2).write(logger)
        Message.new(key=3).write(logger)

        message1, message2 = logger.messages
        self.assertEqual(
            (
                UUID(message1["task_uuid"]) != UUID(message2["task_uuid"]),
                message1["task_level"],
                message2["task_level"],
            ),
            (True, [1], [1]),
        )

    def test_actionCounter(self):
        """
        Each message written within the context of an L{Action} gets its
        C{task_level} field incremented.
        """
        logger = MemoryLogger()
        msg = Message.new(key=2)
        with start_action(logger, "sys:thename"):
            for i in range(4):
                msg.write(logger)
        # We expect 6 messages: start action, 4 standalone messages, finish
        # action:
        self.assertEqual(
            [m["task_level"] for m in logger.messages], [[1], [2], [3], [4], [5], [6]]
        )

    def test_writePassesSerializer(self):
        """
        If a L{Message} is created with a serializer, it is passed as a second
        argument to the logger when C{write} is called.
        """

        class ListLogger(list):
            def write(self, dictionary, serializer):
                self.append(serializer)

        logger = ListLogger()
        serializer = object()
        msg = Message({}, serializer)
        msg.write(logger)
        self.assertIs(logger[0], serializer)

    def test_serializerPassedInBind(self):
        """
        The L{Message} returned by L{Message.bind} includes the serializer
        passed to the parent.
        """
        serializer = object()
        msg = Message({}, serializer)
        msg2 = msg.bind(x=1)
        self.assertIs(msg2._serializer, serializer)

    def test_newWithSerializer(self):
        """
        L{Message.new} can accept a serializer.
        """
        serializer = object()
        msg = Message.new(serializer, x=1)
        self.assertIs(msg._serializer, serializer)


class WrittenMessageTests(TestCase):
    """
    Tests for L{WrittenMessage}.
    """

    def test_as_dict(self):
        """
        L{WrittenMessage.as_dict} returns the dictionary that will be serialized
        to the log.
        """
        log_entry = pmap(
            {"timestamp": 1, "task_uuid": "unique", "task_level": [1], "foo": "bar"}
        )
        self.assertEqual(WrittenMessage.from_dict(log_entry).as_dict(), log_entry)

    def test_from_dict(self):
        """
        L{WrittenMessage.from_dict} converts a dictionary that has been
        deserialized from a log into a L{WrittenMessage} object.
        """
        log_entry = pmap(
            {"timestamp": 1, "task_uuid": "unique", "task_level": [1], "foo": "bar"}
        )
        parsed = WrittenMessage.from_dict(log_entry)
        self.assertEqual(parsed.timestamp, 1)
        self.assertEqual(parsed.task_uuid, "unique")
        self.assertEqual(parsed.task_level, TaskLevel(level=[1]))
        self.assertEqual(parsed.contents, pmap({"foo": "bar"}))


class LogMessageTests(TestCase):
    """Tests for L{log_message}."""

    def test_writes_message(self):
        """
        L{log_message} writes to the default logger.
        """
        messages = []
        add_destinations(messages.append)
        self.addCleanup(remove_destination, messages.append)
        log_message(message_type="hello", some_key=1234)
        self.assertEqual(messages[0]["some_key"], 1234)
        self.assertEqual(messages[0]["message_type"], "hello")
        self.assertTrue(time.time() - messages[0]["timestamp"] < 0.1)

    def test_implicitAction(self):
        """
        If no L{Action} is specified, L{log_message} adds the identification
        fields from the current execution context's L{Action} to the
        dictionary written to the logger.
        """
        logger = MemoryLogger()
        action = Action(logger, "unique", TaskLevel(level=[]), "sys:thename")
        with action:
            log_message(key=2, message_type="a")
        written = logger.messages[0]
        del written["timestamp"]
        self.assertEqual(
            written,
            {"task_uuid": "unique", "task_level": [1], "key": 2, "message_type": "a"},
        )

    def test_missingAction(self):
        """
        If no L{Action} is specified, and the current execution context has no
        L{Action}, a new task_uuid is generated.

        This ensures all messages have a unique identity, as specified by
        task_uuid/task_level.
        """
        messages = []
        add_destinations(messages.append)
        self.addCleanup(remove_destination, messages.append)

        log_message(key=2, message_type="")
        log_message(key=3, message_type="")

        message1, message2 = messages
        self.assertEqual(
            (
                UUID(message1["task_uuid"]) != UUID(message2["task_uuid"]),
                message1["task_level"],
                message2["task_level"],
            ),
            (True, [1], [1]),
        )