Debian Codegraph @

tests/test_swaggerdict.py

raw
1 from collections import OrderedDict
2 from random import shuffle
3
4 from drf_yasg import openapi
5
6
7 def test_vendor_extensions():
8 """Any attribute starting with x_ should map to a vendor property of the form
9 x-camelCase"""
10 sd = openapi.SwaggerDict(x_vendor_ext_1="test")
11 sd.x_vendor_ext_2 = "test"
12 assert "x-vendorExt1" in sd
13 assert sd.x_vendor_ext_1 == "test"
14 assert sd["x-vendorExt2"] == "test"
15
16 del sd.x_vendor_ext_1
17 assert "x-vendorExt1" not in sd
18
19
20 def test_ref():
21 """The attribute 'ref' maps to the swagger key '$ref'"""
22 sd = openapi.SwaggerDict(ref="reftest")
23 assert "$ref" in sd
24 assert sd["$ref"] == sd.ref == "reftest"
25
26 del sd["$ref"]
27 assert not hasattr(sd, "ref")
28
29
30 def test_leading_underscore_ignored():
31 """Attributes with a leading underscore are set on the object as-is and are not
32 added to its dict form"""
33 sd = openapi.SwaggerDict(_private_attr_1="not_camelized")
34 initial_len = len(sd)
35 sd._nope = "not camelized either"
36 assert len(sd) == initial_len
37 assert "privateAttr1" not in sd
38 assert sd._private_attr_1 == "not_camelized"
39 assert "_private_attr_1" not in sd
40 assert hasattr(sd, "_nope")
41
42 del sd._nope
43 assert not hasattr(sd, "_nope")
44
45
46 def test_trailing_underscore_stripped():
47 """Trailing underscores are stripped when converting attribute names.
48 This allows, for example, python keywords to function as SwaggerDict attributes."""
49 sd = openapi.SwaggerDict(trailing_underscore_="trailing")
50 sd.in_ = "trailing"
51 assert "in" in sd
52 assert "trailingUnderscore" in sd
53 assert sd.trailing_underscore == sd["in"]
54 assert hasattr(sd, "in___")
55
56 del sd.in_
57 assert "in" not in sd
58 assert not hasattr(sd, "in__")
59
60
61 def test_extra_ordering():
62 """Insertion order should also be consistent when setting undeclared parameters
63 (kwargs) in SwaggerDict"""
64 extras = [("beta", 1), ("alpha", 2), ("omega", 3), ("gamma", 4)]
65 shuffled_extras = list(extras)
66 shuffle(shuffled_extras)
67
68 s1 = openapi.SwaggerDict(**OrderedDict(extras))
69 s2 = openapi.SwaggerDict(**OrderedDict(shuffled_extras))
70
71 assert list(s1.items()) == list(s2.items())