Debian Codegraph @

hitch/story/map.story

raw
1 Mappings with defined keys (Map):
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.
7
8 Using StrictYAML's 'Map' you can validate that a mapping
9 contains the right keys and the right *type* of values.
10
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.
13 given:
14 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
19
20 schema = Map({"a": Int(), "b": Int(), "c": Int()})
21
22 schema_2 = Map({u"â": Int(), "b": Int(), "c": Int()})
23 yaml_snippet: |
24 â: 1
25 b: 2
26 c: 3
27
28 variations:
29 one key mapping:
30 given:
31 yaml_snippet: 'x: 1'
32 steps:
33 - Run: |
34 Ensure(load(yaml_snippet, Map({"x": Int()})).data).equals(OrderedDict([('x', 1)]))
35
36 key value:
37 steps:
38 - Run: |
39 Ensure(load(yaml_snippet, schema_2)[u'â']).equals(1)
40
41 get item key not found:
42 steps:
43 - Run:
44 code: load(yaml_snippet, schema_2)['keynotfound']
45 raises:
46 message: "'keynotfound'"
47
48 cannot use .text:
49 steps:
50 - Run:
51 code: load(yaml_snippet, schema_2).text
52 raises:
53 type:
54 in python 3: builtins.TypeError
55 in python 2: exceptions.TypeError
56 message:
57 in python 3: "YAML({'â': 1, 'b': 2, 'c': 3}) is a mapping, has no text value."
58 in python 2: "YAML({u'\\xe2': 1, 'b': 2, 'c': 3}) is a mapping, has no text value."
59
60 parse snippet where key is not found in schema:
61 given:
62 yaml_snippet: |
63 a: 1
64 b: 2
65 â: 3
66 steps:
67 - Run:
68 code: load(yaml_snippet, schema)
69 raises:
70 type: strictyaml.exceptions.YAMLValidationError
71 message: |-
72 while parsing a mapping
73 unexpected key not in schema 'â'
74 in "<unicode string>", line 3, column 1:
75 "\xE2": '3'
76 ^ (line: 3)
77
78 sequence not expected when parsing:
79 given:
80 yaml_snippet: |
81 - 1
82 - 2
83 - 3
84 steps:
85 - Run:
86 code: load(yaml_snippet, schema)
87 raises:
88 type: strictyaml.exceptions.YAMLValidationError
89 message: |-
90 when expecting a mapping
91 in "<unicode string>", line 1, column 1:
92 - '1'
93 ^ (line: 1)
94 found a sequence
95 in "<unicode string>", line 3, column 1:
96 - '3'
97 ^ (line: 3)
98
99 List not expected when serializing:
100 steps:
101 - Run:
102 code: as_document([1, 2, 3], schema)
103 raises:
104 type: strictyaml.exceptions.YAMLSerializationError
105 message: Expected a dict, found '[1, 2, 3]'
106
107 Empty dict not valid when serializing:
108 steps:
109 - Run:
110 code: as_document({}, schema)
111 raises:
112 type: strictyaml.exceptions.YAMLSerializationError
113 message: "Expected a non-empty dict, found an empty dict.\nUse EmptyDict\
114 \ validator to serialize empty dicts."
115 Unexpected key:
116 given:
117 yaml_snippet: |
118 a: 1
119 b: 2
120 c: 3
121 d: 4
122 steps:
123 - Run:
124 code: load(yaml_snippet, schema)
125 raises:
126 type: strictyaml.exceptions.YAMLValidationError
127 message: |-
128 while parsing a mapping
129 unexpected key not in schema 'd'
130 in "<unicode string>", line 4, column 1:
131 d: '4'
132 ^ (line: 4)
133
134
135 required key not found:
136 given:
137 yaml_snippet: |
138 a: 1
139 steps:
140 - Run:
141 code: load(yaml_snippet, schema)
142 raises:
143 type: strictyaml.exceptions.YAMLValidationError
144 message: |-
145 while parsing a mapping
146 required key(s) 'b', 'c' not found
147 in "<unicode string>", line 1, column 1:
148 a: '1'
149 ^ (line: 1)
150
151
152
153 iterator:
154 given:
155 yaml_snippet: |
156 a: 1
157 b: 2
158 c: 3
159 steps:
160 - Run: |
161 assert [item for item in load(yaml_snippet, schema)] == ["a", "b", "c"]
162
163 serialize:
164 steps:
165 - Run: |
166 assert as_document(OrderedDict([(u"â", 1), ("b", 2), ("c", 3)]), schema_2).as_yaml() == yaml_snippet