File: _test_dumper.py

package info (click to toggle)
python-djangorestframework-yaml 3.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 268 kB
  • sloc: python: 483; makefile: 11
file content (494 lines) | stat: -rw-r--r-- 13,649 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import textwrap
from collections import OrderedDict
from datetime import datetime, time
from decimal import Decimal
from uuid import UUID

import yaml
from django.test import TestCase
from django.utils.safestring import SafeString
from django.utils.timezone import utc
from rest_framework.exceptions import ErrorDetail
from rest_framework.relations import Hyperlink
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList

from rest_framework_yaml import styles
from rest_framework_yaml.encoders import SafeDumper


class _YAMLDumperTests(TestCase):
    """Tests specific to the YAML SafeDumper."""

    def test_bytes(self) -> None:
        """
        Test that Bytes are represented as YAML strings.

        Given:
            - A Bytes object
            - The YAML representation of that Bytes object as a string
        Do:
            - Render the Bytes object to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "bytes: abcd\n"

        obj = {"bytes": b"abcd"}

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_time(self) -> None:
        """
        Test that Times are represented as YAML strings.

        Given:
            - A Time
            - The YAML representation of that Time as a string
        Do:
            - Render the Time to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "time: '12:34:56'\n"

        obj = {"time": time(12, 34, 56)}

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_timedelta(self) -> None:
        """
        Test that Timedeltas are represented as YAML strings.

        Given:
            - A Timedelta
            - The YAML representation of that Timedelta as a string
        Do:
            - Render the Timedelta to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "timedelta: 97440.0\n"

        obj = {
            "timedelta": datetime(1, 1, 2, 3, 4, tzinfo=utc)
            - datetime(1, 1, 1, tzinfo=utc),
        }

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_decimal(self) -> None:
        """
        Test that Decimals are represented as YAML strings.

        Given:
            - A Decimal
            - The YAML representation of that Decimal as a string
        Do:
            - Render the Decimal to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "'123.456'\n"

        obj = Decimal("123.456")

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_uuid(self) -> None:
        """
        Test that UUIDs are represented as YAML strings.

        Given:
            - A UUID
            - The YAML representation of that UUID as a string
        Do:
            - Render the UUID to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "uuid: 12345678-1234-5678-1234-567812345678\n"

        obj = {"uuid": UUID("12345678-1234-5678-1234-567812345678")}

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_hyperlink(self) -> None:
        """
        Test that Hyperlinks are represented as YAML strings.

        Given:
            - A Hyperlink
            - The YAML representation of that Hyperlink as a string
        Do:
            - Render the Hyperlink to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "link: http://example.com\n"

        obj = {"link": Hyperlink("http://example.com", obj=None)}

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_error_detail(self) -> None:
        """
        Test that ErrorDetails are represented as YAML strings.

        Given:
            - An ErrorDetail
            - The YAML representation of that ErrorDetail as a string
        Do:
            - Render the ErrorDetail to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "error: foo\n"

        obj = {"error": ErrorDetail("foo")}

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_safe_string(self) -> None:
        """
        Test that SafeStrings are represented as YAML strings.

        Given:
            - A SafeString
            - The YAML representation of that SafeString as a string
        Do:
            - Render the SafeString to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "foo: bar\n"

        obj = {"foo": SafeString("bar")}

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_ordered_dict(self) -> None:
        """
        Test that OrderedDicts are represented as YAML mappings.

        Given:
            - A simple ordered dict
            - The YAML representation of that dict as a string
        Do:
            - Render the dict to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "foo:\n- bar\n- baz\n"

        obj = OrderedDict({"foo": ["bar", "baz"]})

        self.assertEqual(
            yaml.dump(
                obj,
                sort_keys=False,
                Dumper=SafeDumper,
                default_flow_style=False,
            ),
            _yaml_repr,
        )

    def test_return_dict(self) -> None:
        """
        Test that ReturnDicts are represented as YAML mappings.

        Given:
            - A ReturnDict
            - The YAML representation of that ReturnDict as a string
        Do:
            - Render the ReturnDict to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "foo:\n- bar\n- baz\n"

        obj = ReturnDict({"foo": ["bar", "baz"]}, serializer=None)

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_return_list(self) -> None:
        """
        Test that ReturnLists are represented as YAML sequences.

        Given:
            - A ReturnList
            - The YAML representation of that ReturnList as a string
        Do:
            - Render the ReturnList to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "- foo\n- bar\n- baz\n"

        obj = ReturnList(["foo", "bar", "baz"], serializer=None)

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_generator_type(self) -> None:
        """
        Test that GeneratorTypes are represented as YAML sequences.

        Given:
            - A GeneratorType
            - The YAML representation of that GeneratorType as a string
        Do:
            - Render the GeneratorType to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "- foo\n- bar\n- baz\n"

        obj = (item for item in ["foo", "bar", "baz"])

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_folded_str(self) -> None:
        """
        Test that folded strings are represented as YAML folded strings.

        Given:
            - A folded string
            - The YAML representation of that string as a string
        Do:
            - Render the string to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "foo: >-\n  bar\n\n  baz\n"

        obj = {"foo": styles.FoldedStr("bar\nbaz")}

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_literal_str(self) -> None:
        """
        Test that literal strings are represented as YAML literal strings.

        Given:
            - A literal string
            - The YAML representation of that string as a string
        Do:
            - Render the string to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "foo: |-\n  bar\n  baz\n"

        obj = {"foo": styles.LiteralStr("bar\nbaz")}

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_single_quoted_str(self) -> None:
        """
        Test that single quoted strings are represented as YAML single quoted strings.

        Given:
            - A single quoted string
            - The YAML representation of that string as a string
        Do:
            - Render the string to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = "foo: 'bar'\n"

        obj = {"foo": styles.SingleQuotedStr("bar")}

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_double_quoted_str(self) -> None:
        """
        Test that double quoted strings are represented as YAML double quoted strings.

        Given:
            - A double quoted string
            - The YAML representation of that string as a string
        Do:
            - Render the string to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = 'foo: "bar"\n'

        obj = {"foo": styles.DoubleQuotedStr("bar")}

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_flow_style_sequence(self) -> None:
        """
        Test that flow style sequences are represented as YAML flow style sequences.

        Given:
            - A flow style sequence
            - The YAML representation of that sequence as a string
        Do:
            - Render the sequence to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = textwrap.dedent(
            """
            flowing: [bar, baz]
            not-flowing:
            - bar
            - baz
            """,
        ).lstrip()

        obj = {
            "flowing": styles.FlowStyleSequence(["bar", "baz"]),
            "not-flowing": ["bar", "baz"],
        }

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)

    def test_flow_style_mapping(self) -> None:
        """
        Test that flow style mappings are represented as YAML flow style mappings.

        Given:
            - A flow style mapping
            - The YAML representation of that mapping as a string
        Do:
            - Render the mapping to YAML
        Expect:
            - The YAML string to be the same as the original YAML string
        """
        _yaml_repr = textwrap.dedent(
            """
            flowing: {foo: bar, baz: qux}
            not-flowing:
              foo: bar
              baz: qux
            """,
        ).lstrip()

        obj = {
            "flowing": styles.FlowStyleMapping({"foo": "bar", "baz": "qux"}),
            "not-flowing": {"foo": "bar", "baz": "qux"},
        }

        yaml_repr = yaml.dump(
            obj,
            sort_keys=False,
            Dumper=SafeDumper,
            default_flow_style=False,
        )

        self.assertEqual(yaml_repr, _yaml_repr)