File: test_swaggerdict.py

package info (click to toggle)
drf-yasg-nonfree 1.21.10-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid, trixie
  • size: 15,784 kB
  • sloc: javascript: 6,247; python: 5,073; makefile: 31; sh: 13
file content (68 lines) | stat: -rw-r--r-- 2,188 bytes parent folder | download | duplicates (2)
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
from collections import OrderedDict
from random import shuffle

from drf_yasg import openapi


def test_vendor_extensions():
    """Any attribute starting with x_ should map to a vendor property of the form x-camelCase"""
    sd = openapi.SwaggerDict(x_vendor_ext_1='test')
    sd.x_vendor_ext_2 = 'test'
    assert 'x-vendorExt1' in sd
    assert sd.x_vendor_ext_1 == 'test'
    assert sd['x-vendorExt2'] == 'test'

    del sd.x_vendor_ext_1
    assert 'x-vendorExt1' not in sd


def test_ref():
    """The attribute 'ref' maps to the swagger key '$ref'"""
    sd = openapi.SwaggerDict(ref='reftest')
    assert '$ref' in sd
    assert sd['$ref'] == sd.ref == 'reftest'

    del sd['$ref']
    assert not hasattr(sd, 'ref')


def test_leading_underscore_ignored():
    """Attributes with a leading underscore are set on the object as-is and are not added to its dict form"""
    sd = openapi.SwaggerDict(_private_attr_1='not_camelized')
    initial_len = len(sd)
    sd._nope = 'not camelized either'
    assert len(sd) == initial_len
    assert 'privateAttr1' not in sd
    assert sd._private_attr_1 == 'not_camelized'
    assert '_private_attr_1' not in sd
    assert hasattr(sd, '_nope')

    del sd._nope
    assert not hasattr(sd, '_nope')


def test_trailing_underscore_stripped():
    """Trailing underscores are stripped when converting attribute names.
    This allows, for example, python keywords to function as SwaggerDict attributes."""
    sd = openapi.SwaggerDict(trailing_underscore_='trailing')
    sd.in_ = 'trailing'
    assert 'in' in sd
    assert 'trailingUnderscore' in sd
    assert sd.trailing_underscore == sd['in']
    assert hasattr(sd, 'in___')

    del sd.in_
    assert 'in' not in sd
    assert not hasattr(sd, 'in__')


def test_extra_ordering():
    """Insertion order should also be consistent when setting undeclared parameters (kwargs) in SwaggerDict"""
    extras = [('beta', 1), ('alpha', 2), ('omega', 3), ('gamma', 4)]
    shuffled_extras = list(extras)
    shuffle(shuffled_extras)

    s1 = openapi.SwaggerDict(**OrderedDict(extras))
    s2 = openapi.SwaggerDict(**OrderedDict(shuffled_extras))

    assert list(s1.items()) == list(s2.items())