|
1 | 1 | ["Mappings with defined keys (Map)"] |
2 | | -docs = "compound/mapping" |
3 | | -"based on" = "strictyaml" |
4 | | -description = "Mappings of one value to another are represented by : in YAML\nand parsed as python dicts.\n\nUsing StrictYAML's 'Map' you can validate that a mapping\ncontains the right keys and the right *type* of values.\n\nNote: for mappings where you don't know the exact names of\nthe keys in advance but you do know the type, use MapPattern.\n" |
| 2 | + docs = "compound/mapping" |
| 3 | + "based on" = "strictyaml" |
| 4 | + description = """ |
| 5 | +Mappings of one value to another are represented by : in YAML\ |
| 6 | +and parsed as python dicts. |
5 | 7 |
|
6 | | -["Mappings with defined keys (Map)".given] |
7 | | -setup = "from collections import OrderedDict\nfrom strictyaml import Map, Int, load, as_document\nfrom collections import OrderedDict\nfrom ensure import Ensure\n\nschema = Map({\"a\": Int(), \"b\": Int(), \"c\": Int()})\n\nschema_2 = Map({u\"â\": Int(), \"b\": Int(), \"c\": Int()})\n" |
8 | | -yaml_snippet = "â: 1\nb: 2\nc: 3\n" |
| 8 | +Using StrictYAML's 'Map' you can validate that a mapping\ |
| 9 | +contains the right keys and the right *type* of values. |
9 | 10 |
|
10 | | -["Mappings with defined keys (Map)".variations] |
| 11 | +Note: for mappings where you don't know the exact names of\ |
| 12 | +the keys in advance but you do know the type, use MapPattern.""" |
11 | 13 |
|
12 | | -["Mappings with defined keys (Map)".variations."one key mapping"] |
| 14 | + given.setup = """ |
| 15 | +from collections import OrderedDict |
| 16 | +from strictyaml import Map, Int, load, as_document |
| 17 | +from collections import OrderedDict |
| 18 | +from ensure import Ensure |
13 | 19 |
|
14 | | -["Mappings with defined keys (Map)".variations."one key mapping".given] |
15 | | -yaml_snippet = "x: 1" |
| 20 | +schema = Map({"a": Int(), "b": Int(), "c": Int()}) |
16 | 21 |
|
17 | | -[["Mappings with defined keys (Map)".variations."one key mapping".steps]] |
18 | | -Run = "Ensure(load(yaml_snippet, Map({\"x\": Int()})).data).equals(OrderedDict([('x', 1)]))\n" |
| 22 | +schema_2 = Map({u"â": Int(), "b": Int(), "c": Int()})""" |
| 23 | + given.yaml_snippet = "â: 1\nb: 2\nc: 3" |
19 | 24 |
|
20 | | -["Mappings with defined keys (Map)".variations."key value"] |
| 25 | +["Mappings with defined keys (Map)".variations."one key mapping"] |
| 26 | + given.yaml_snippet = "x: 1" |
| 27 | + [["Mappings with defined keys (Map)".variations."one key mapping".steps]] |
| 28 | + Run = """Ensure(load(yaml_snippet, Map({"x": Int()})).data).equals(OrderedDict([('x', 1)]))""" |
21 | 29 |
|
22 | 30 | [["Mappings with defined keys (Map)".variations."key value".steps]] |
23 | | -Run = "Ensure(load(yaml_snippet, schema_2)[u'â']).equals(1)\n" |
24 | | - |
25 | | -["Mappings with defined keys (Map)".variations."get item key not found"] |
| 31 | + Run = "Ensure(load(yaml_snippet, schema_2)[u'â']).equals(1)" |
26 | 32 |
|
27 | 33 | [["Mappings with defined keys (Map)".variations."get item key not found".steps]] |
28 | | - |
29 | | -["Mappings with defined keys (Map)".variations."get item key not found".steps.Run] |
30 | | -code = "load(yaml_snippet, schema_2)['keynotfound']" |
31 | | - |
32 | | -["Mappings with defined keys (Map)".variations."get item key not found".steps.Run.raises] |
33 | | -message = "'keynotfound'" |
34 | | - |
35 | | -["Mappings with defined keys (Map)".variations."cannot use .text"] |
| 34 | + Run.code = "load(yaml_snippet, schema_2)['keynotfound']" |
| 35 | + Run.raises.message = "'keynotfound'" |
36 | 36 |
|
37 | 37 | [["Mappings with defined keys (Map)".variations."cannot use .text".steps]] |
38 | | - |
39 | | -["Mappings with defined keys (Map)".variations."cannot use .text".steps.Run] |
40 | | -code = "load(yaml_snippet, schema_2).text" |
41 | | - |
42 | | -["Mappings with defined keys (Map)".variations."cannot use .text".steps.Run.raises] |
43 | | - |
44 | | -["Mappings with defined keys (Map)".variations."cannot use .text".steps.Run.raises.type] |
45 | | -"in python 3" = "builtins.TypeError" |
46 | | -"in python 2" = "exceptions.TypeError" |
47 | | - |
48 | | -["Mappings with defined keys (Map)".variations."cannot use .text".steps.Run.raises.message] |
49 | | -"in python 3" = "YAML(OrderedDict([('â', 1), ('b', 2), ('c', 3)])) is a mapping, has no text value." |
50 | | -"in python 2" = "YAML(OrderedDict([(u'\\xe2', 1), ('b', 2), ('c', 3)])) is a mapping, has no text value." |
| 38 | + Run.code = "load(yaml_snippet, schema_2).text" |
| 39 | + ["Mappings with defined keys (Map)".variations."cannot use .text".steps.Run.raises.type] |
| 40 | + "in python 3" = "builtins.TypeError" |
| 41 | + "in python 2" = "exceptions.TypeError" |
| 42 | + ["Mappings with defined keys (Map)".variations."cannot use .text".steps.Run.raises.message] |
| 43 | + "in python 3" = "YAML(OrderedDict([('â', 1), ('b', 2), ('c', 3)])) is a mapping, has no text value." |
| 44 | + "in python 2" = "YAML(OrderedDict([(u'\\xe2', 1), ('b', 2), ('c', 3)])) is a mapping, has no text value." |
51 | 45 |
|
52 | 46 | ["Mappings with defined keys (Map)".variations."parse snippet where key is not found in schema"] |
53 | | - |
54 | | -["Mappings with defined keys (Map)".variations."parse snippet where key is not found in schema".given] |
55 | | -yaml_snippet = "a: 1\nb: 2\nâ: 3 \n" |
56 | | - |
57 | | -[["Mappings with defined keys (Map)".variations."parse snippet where key is not found in schema".steps]] |
58 | | - |
59 | | -["Mappings with defined keys (Map)".variations."parse snippet where key is not found in schema".steps.Run] |
60 | | -code = "load(yaml_snippet, schema)" |
61 | | - |
62 | | -["Mappings with defined keys (Map)".variations."parse snippet where key is not found in schema".steps.Run.raises] |
63 | | -type = "strictyaml.exceptions.YAMLValidationError" |
64 | | -message = "while parsing a mapping\nunexpected key not in schema 'â'\n in \"<unicode string>\", line 3, column 1:\n \"\\xE2\": '3'\n ^ (line: 3)" |
| 47 | + given.yaml_snippet = "a: 1\nb: 2\nâ: 3" |
| 48 | + [["Mappings with defined keys (Map)".variations."parse snippet where key is not found in schema".steps]] |
| 49 | + Run.code = "load(yaml_snippet, schema)" |
| 50 | + Run.raises.type = "strictyaml.exceptions.YAMLValidationError" |
| 51 | + Run.raises.message = """ |
| 52 | +while parsing a mapping |
| 53 | +unexpected key not in schema 'â' |
| 54 | + in "<unicode string>", line 3, column 1: |
| 55 | + "\\xE2": '3' |
| 56 | + ^ (line: 3)""" |
65 | 57 |
|
66 | 58 | ["Mappings with defined keys (Map)".variations."sequence not expected when parsing"] |
67 | | - |
68 | | -["Mappings with defined keys (Map)".variations."sequence not expected when parsing".given] |
69 | | -yaml_snippet = "- 1\n- 2\n- 3 \n" |
70 | | - |
71 | | -[["Mappings with defined keys (Map)".variations."sequence not expected when parsing".steps]] |
72 | | - |
73 | | -["Mappings with defined keys (Map)".variations."sequence not expected when parsing".steps.Run] |
74 | | -code = "load(yaml_snippet, schema)" |
75 | | - |
76 | | -["Mappings with defined keys (Map)".variations."sequence not expected when parsing".steps.Run.raises] |
77 | | -type = "strictyaml.exceptions.YAMLValidationError" |
78 | | -message = "when expecting a mapping\n in \"<unicode string>\", line 1, column 1:\n - '1'\n ^ (line: 1)\nfound a sequence\n in \"<unicode string>\", line 3, column 1:\n - '3'\n ^ (line: 3)" |
79 | | - |
80 | | -["Mappings with defined keys (Map)".variations."List not expected when serializing"] |
| 59 | + given.yaml_snippet = "- 1\n- 2\n- 3" |
| 60 | + [["Mappings with defined keys (Map)".variations."sequence not expected when parsing".steps]] |
| 61 | + Run.code = "load(yaml_snippet, schema)" |
| 62 | + Run.raises.type = "strictyaml.exceptions.YAMLValidationError" |
| 63 | + Run.raises.message = """ |
| 64 | +when expecting a mapping |
| 65 | + in "<unicode string>", line 1, column 1: |
| 66 | + - '1' |
| 67 | + ^ (line: 1) |
| 68 | +found a sequence |
| 69 | + in "<unicode string>", line 3, column 1: |
| 70 | + - '3' |
| 71 | + ^ (line: 3)""" |
81 | 72 |
|
82 | 73 | [["Mappings with defined keys (Map)".variations."List not expected when serializing".steps]] |
83 | | - |
84 | | -["Mappings with defined keys (Map)".variations."List not expected when serializing".steps.Run] |
85 | | -code = "as_document([1, 2, 3], schema)" |
86 | | - |
87 | | -["Mappings with defined keys (Map)".variations."List not expected when serializing".steps.Run.raises] |
88 | | -type = "strictyaml.exceptions.YAMLSerializationError" |
89 | | -message = "Expected a dict, found '[1, 2, 3]'" |
90 | | - |
91 | | -["Mappings with defined keys (Map)".variations."Empty dict not valid when serializing"] |
| 74 | + Run.code = "as_document([1, 2, 3], schema)" |
| 75 | + Run.raises.type = "strictyaml.exceptions.YAMLSerializationError" |
| 76 | + Run.raises.message = "Expected a dict, found '[1, 2, 3]'" |
92 | 77 |
|
93 | 78 | [["Mappings with defined keys (Map)".variations."Empty dict not valid when serializing".steps]] |
94 | | - |
95 | | -["Mappings with defined keys (Map)".variations."Empty dict not valid when serializing".steps.Run] |
96 | | -code = "as_document({}, schema)" |
97 | | - |
98 | | -["Mappings with defined keys (Map)".variations."Empty dict not valid when serializing".steps.Run.raises] |
99 | | -type = "strictyaml.exceptions.YAMLSerializationError" |
100 | | -message = "Expected a non-empty dict, found an empty dict.\nUse EmptyDict validator to serialize empty dicts." |
| 79 | + Run.code = "as_document({}, schema)" |
| 80 | + Run.raises.type = "strictyaml.exceptions.YAMLSerializationError" |
| 81 | + Run.raises.message = """ |
| 82 | +Expected a non-empty dict, found an empty dict. |
| 83 | +Use EmptyDict validator to serialize empty dicts.""" |
101 | 84 |
|
102 | 85 | ["Mappings with defined keys (Map)".variations."Unexpected key"] |
103 | | - |
104 | | -["Mappings with defined keys (Map)".variations."Unexpected key".given] |
105 | | -yaml_snippet = "a: 1\nb: 2\nc: 3\nd: 4\n" |
106 | | - |
107 | | -[["Mappings with defined keys (Map)".variations."Unexpected key".steps]] |
108 | | - |
109 | | -["Mappings with defined keys (Map)".variations."Unexpected key".steps.Run] |
110 | | -code = "load(yaml_snippet, schema)" |
111 | | - |
112 | | -["Mappings with defined keys (Map)".variations."Unexpected key".steps.Run.raises] |
113 | | -type = "strictyaml.exceptions.YAMLValidationError" |
114 | | -message = "while parsing a mapping\nunexpected key not in schema 'd'\n in \"<unicode string>\", line 4, column 1:\n d: '4'\n ^ (line: 4)" |
| 86 | + given.yaml_snippet = "a: 1\nb: 2\nc: 3\nd: 4" |
| 87 | + [["Mappings with defined keys (Map)".variations."Unexpected key".steps]] |
| 88 | + Run.code = "load(yaml_snippet, schema)" |
| 89 | + Run.raises.type = "strictyaml.exceptions.YAMLValidationError" |
| 90 | + Run.raises.message = """ |
| 91 | +while parsing a mapping |
| 92 | +unexpected key not in schema 'd' |
| 93 | + in "<unicode string>", line 4, column 1: |
| 94 | + d: '4' |
| 95 | + ^ (line: 4)""" |
115 | 96 |
|
116 | 97 | ["Mappings with defined keys (Map)".variations."required key not found"] |
117 | | - |
118 | | -["Mappings with defined keys (Map)".variations."required key not found".given] |
119 | | -yaml_snippet = "a: 1\n" |
120 | | - |
121 | | -[["Mappings with defined keys (Map)".variations."required key not found".steps]] |
122 | | - |
123 | | -["Mappings with defined keys (Map)".variations."required key not found".steps.Run] |
124 | | -code = "load(yaml_snippet, schema)" |
125 | | - |
126 | | -["Mappings with defined keys (Map)".variations."required key not found".steps.Run.raises] |
127 | | -type = "strictyaml.exceptions.YAMLValidationError" |
128 | | -message = "while parsing a mapping\nrequired key(s) 'b', 'c' not found\n in \"<unicode string>\", line 1, column 1:\n a: '1'\n ^ (line: 1)" |
| 98 | + given.yaml_snippet = "a: 1" |
| 99 | + [["Mappings with defined keys (Map)".variations."required key not found".steps]] |
| 100 | + Run.code = "load(yaml_snippet, schema)" |
| 101 | + Run.raises.type = "strictyaml.exceptions.YAMLValidationError" |
| 102 | + Run.raises.message = """ |
| 103 | +while parsing a mapping |
| 104 | +required key(s) 'b', 'c' not found |
| 105 | + in "<unicode string>", line 1, column 1: |
| 106 | + a: '1' |
| 107 | + ^ (line: 1)""" |
129 | 108 |
|
130 | 109 | ["Mappings with defined keys (Map)".variations.iterator] |
131 | | - |
132 | | -["Mappings with defined keys (Map)".variations.iterator.given] |
133 | | -yaml_snippet = "a: 1\nb: 2\nc: 3\n" |
134 | | - |
135 | | -[["Mappings with defined keys (Map)".variations.iterator.steps]] |
136 | | -Run = "assert [item for item in load(yaml_snippet, schema)] == [\"a\", \"b\", \"c\"]\n" |
137 | | - |
138 | | -["Mappings with defined keys (Map)".variations.serialize] |
| 110 | + given.yaml_snippet = "a: 1\nb: 2\nc: 3" |
| 111 | + [["Mappings with defined keys (Map)".variations.iterator.steps]] |
| 112 | + Run = """assert [item for item in load(yaml_snippet, schema)] == ["a", "b", "c"]""" |
139 | 113 |
|
140 | 114 | [["Mappings with defined keys (Map)".variations.serialize.steps]] |
141 | | -Run = "assert as_document(OrderedDict([(u\"â\", 1), (\"b\", 2), (\"c\", 3)]), schema_2).as_yaml() == yaml_snippet\n" |
| 115 | + Run = """assert as_document(OrderedDict([(u"â", 1), ("b", 2), ("c", 3)]), schema_2).as_yaml() == yaml_snippet""" |
0 commit comments