|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | 3 | import logging |
| 4 | +import pytest |
| 5 | +from ucloud.core import exc |
4 | 6 | from ucloud.core.typesystem import fields, schema |
5 | 7 |
|
6 | 8 | logger = logging.getLogger(__name__) |
7 | 9 |
|
8 | 10 |
|
9 | | -def test_array(): |
10 | | - class TestSchema(schema.RequestSchema): |
| 11 | +def test_request_array(): |
| 12 | + class Schema(schema.RequestSchema): |
11 | 13 | fields = {"IP": fields.List(fields.Str())} |
12 | 14 |
|
13 | | - ret = TestSchema().dumps({"IP": ["127.0.0.1"]}) |
14 | | - assert ret.get("IP.0") == "127.0.0.1" |
15 | | - assert ret.get("IP") is None |
16 | | - assert ret.get("IP.1") is None |
| 15 | + d = Schema().dumps({"IP": ["127.0.0.1"]}) |
| 16 | + assert d == {"IP.0": "127.0.0.1"} |
| 17 | + d = Schema().dumps({}) |
| 18 | + assert d == {} |
17 | 19 |
|
18 | 20 |
|
19 | | -def test_array_model(): |
20 | | - class TestSchema(schema.RequestSchema): |
| 21 | +def test_request_array_with_default(): |
| 22 | + class Schema(schema.RequestSchema): |
21 | 23 | fields = {"IP": fields.List(fields.Str(), default=["127.0.0.1"])} |
22 | 24 |
|
23 | | - class TestArrayModel(schema.RequestSchema): |
24 | | - fields = {"Interface": TestSchema()} |
| 25 | + d = Schema().dumps({"IP": ["192.168.0.1"]}) |
| 26 | + assert d == {"IP.0": "192.168.0.1"} |
| 27 | + d = Schema().dumps({}) |
| 28 | + assert d == {"IP.0": "127.0.0.1"} |
25 | 29 |
|
26 | | - class TestArrayModelArray(schema.RequestSchema): |
27 | | - fields = {"Interface": fields.List(TestSchema(), default=list)} |
28 | 30 |
|
29 | | - d = {"Interface": {"IP": ["127.0.0.1", "192.168.0.1"]}} |
30 | | - ret = TestArrayModel().dumps(d) |
31 | | - assert ret.get("Interface.IP.0") == "127.0.0.1" |
32 | | - assert ret.get("Interface.IP.1") == "192.168.0.1" |
| 31 | +def test_request_object_model(): |
| 32 | + class Schema(schema.RequestSchema): |
| 33 | + fields = {"IP": fields.List(fields.Str())} |
| 34 | + |
| 35 | + class NestedObjectSchema(schema.RequestSchema): |
| 36 | + fields = {"Interface": Schema()} |
| 37 | + |
| 38 | + d = NestedObjectSchema().dumps({"Interface": {"IP": ["127.0.0.1"]}}) |
| 39 | + assert d == {"Interface.IP.0": "127.0.0.1"} |
| 40 | + with pytest.raises(exc.ValidationException): |
| 41 | + NestedObjectSchema().dumps({"Interface": 1}) |
| 42 | + |
| 43 | + |
| 44 | +def test_request_array_model_with_default(): |
| 45 | + class Schema(schema.RequestSchema): |
| 46 | + fields = {"IP": fields.List(fields.Str())} |
| 47 | + |
| 48 | + class NestedArraySchema(schema.RequestSchema): |
| 49 | + fields = { |
| 50 | + "Interface": fields.List( |
| 51 | + Schema(default=lambda: "127.0.0.1"), |
| 52 | + default=lambda: [{"IP": ["192.168.1.1"]}], |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + d = NestedArraySchema().dumps({}) |
| 57 | + assert d == {"Interface.0.IP.0": "192.168.1.1"} |
| 58 | + d = {"Interface": [{"IP": ["127.0.0.1", "192.168.0.1"]}, {"IP": ["172.16.0.1"]}]} |
| 59 | + d = NestedArraySchema().dumps(d) |
| 60 | + assert d == { |
| 61 | + "Interface.0.IP.0": "127.0.0.1", |
| 62 | + "Interface.0.IP.1": "192.168.0.1", |
| 63 | + "Interface.1.IP.0": "172.16.0.1", |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +def test_response_array(): |
| 68 | + class Schema(schema.ResponseSchema): |
| 69 | + fields = {"IP": fields.List(fields.Str())} |
| 70 | + |
| 71 | + d = Schema().loads({}) |
| 72 | + assert d == {"IP": []} |
| 73 | + d = Schema().loads({"IP": ["127.0.0.1"]}) |
| 74 | + assert d == {"IP": ["127.0.0.1"]} |
| 75 | + with pytest.raises(exc.ValidationException): |
| 76 | + Schema().loads({"IP": 1}) |
| 77 | + |
| 78 | + |
| 79 | +def test_response_array_with_default(): |
| 80 | + class Schema(schema.ResponseSchema): |
| 81 | + fields = {"IP": fields.List(fields.Str(), default=["127.0.0.1"])} |
| 82 | + |
| 83 | + d = Schema().dumps({"IP": ["192.168.0.1"]}) |
| 84 | + assert d == {"IP": ["192.168.0.1"]} |
| 85 | + d = Schema().dumps({}) |
| 86 | + assert d == {"IP": ["127.0.0.1"]} |
| 87 | + |
| 88 | + |
| 89 | +def test_response_object_model(): |
| 90 | + class Schema(schema.ResponseSchema): |
| 91 | + fields = {"IP": fields.List(fields.Str())} |
| 92 | + |
| 93 | + class NestedObjectSchema(schema.ResponseSchema): |
| 94 | + fields = {"EIP": Schema()} |
| 95 | + |
| 96 | + d = NestedObjectSchema().loads({"EIP": {"IP": ["127.0.0.1"]}}) |
| 97 | + assert d == {"EIP": {"IP": ["127.0.0.1"]}} |
| 98 | + d = NestedObjectSchema().loads({}) |
| 99 | + assert d == {"EIP": {"IP": []}} |
| 100 | + |
| 101 | + |
| 102 | +def test_response_object_model_case_insensitive(): |
| 103 | + class Schema(schema.ResponseSchema): |
| 104 | + fields = {"IP": fields.List(fields.Str())} |
| 105 | + |
| 106 | + class NestedObjectSchema(schema.ResponseSchema): |
| 107 | + fields = {"EIP": Schema()} |
| 108 | + |
| 109 | + d = NestedObjectSchema().loads({"eip": {"Ip": ["127.0.0.1"]}}) |
| 110 | + assert d == {"EIP": {"IP": ["127.0.0.1"]}} |
| 111 | + |
| 112 | + |
| 113 | +def test_response_array_model_with_default(): |
| 114 | + class Schema(schema.ResponseSchema): |
| 115 | + fields = {"IP": fields.List(fields.Str())} |
| 116 | + |
| 117 | + class NestedArraySchema(schema.ResponseSchema): |
| 118 | + fields = { |
| 119 | + "Interface": fields.List( |
| 120 | + Schema(default=lambda: {"IP": ["127.0.0.1"]}), |
| 121 | + default=lambda: [{"IP": ["192.168.1.1"]}], |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + d = NestedArraySchema().dumps({}) |
| 126 | + assert d == {"Interface": [{"IP": ["192.168.1.1"]}]} |
33 | 127 | d = {"Interface": [{"IP": ["127.0.0.1", "192.168.0.1"]}, {"IP": ["172.16.0.1"]}]} |
34 | | - ret = TestArrayModelArray().dumps(d) |
35 | | - assert ret.get("Interface.0.IP.0") == "127.0.0.1" |
36 | | - assert ret.get("Interface.0.IP.1") == "192.168.0.1" |
37 | | - assert ret.get("Interface.1.IP.0") == "172.16.0.1" |
38 | | - d = {} |
39 | | - ret = TestArrayModelArray().dumps(d) |
40 | | - assert ret.get("Interface") is None |
41 | | - d = {"Interface": [{"IP": None}]} |
42 | | - ret = TestArrayModelArray().dumps(d) |
43 | | - assert ret.get("Interface.0.IP.0") == "127.0.0.1" |
44 | | - d = {"Interface": [{}]} |
45 | | - ret = TestArrayModelArray().dumps(d) |
46 | | - assert ret.get("Interface.0.IP.0") == "127.0.0.1" |
| 128 | + d = NestedArraySchema().dumps(d) |
| 129 | + assert d == { |
| 130 | + "Interface": [{"IP": ["127.0.0.1", "192.168.0.1"]}, {"IP": ["172.16.0.1"]}] |
| 131 | + } |
0 commit comments