1 | """Test the validation for the core configuration schema.""" |
||
2 | |||
3 | import json |
||
4 | import pytest |
||
5 | |||
6 | from flask_jsondash import charts_builder as app |
||
7 | |||
8 | |||
9 | def _schema(**vals): |
||
10 | """Default schema.""" |
||
11 | data = dict( |
||
12 | id='a-b-c-d-e', |
||
13 | date="2016-08-23 15:03:49.178000", |
||
14 | layout="grid", |
||
15 | name="testlayout", |
||
16 | modules=[], |
||
17 | ) |
||
18 | data.update(**vals) |
||
19 | return json.dumps(data) |
||
20 | |||
21 | |||
22 | @pytest.mark.schema |
||
23 | def test_validate_raw_json_valid_empty_modules(): |
||
24 | assert app.validate_raw_json(_schema()) |
||
25 | |||
26 | |||
27 | @pytest.mark.schema |
||
28 | def test_validate_raw_json_valid_freeform(): |
||
29 | d = _schema( |
||
30 | layout='freeform', |
||
31 | modules=[ |
||
32 | dict(guid='a-b-c-d-e', name='foo', dataSource='foo', |
||
33 | width=1, height=1, type='line', |
||
34 | family='C3')] |
||
35 | ) |
||
36 | assert app.validate_raw_json(d) |
||
37 | |||
38 | |||
39 | @pytest.mark.schema |
||
40 | def test_validate_raw_json_valid_fixed(): |
||
41 | d = _schema( |
||
42 | layout='freeform', |
||
43 | modules=[ |
||
44 | dict(guid='a-b-c-d-e', name='foo', dataSource='foo', |
||
45 | width='1', height=1, type='line', |
||
46 | family='C3')] |
||
47 | ) |
||
48 | assert app.validate_raw_json(d) |
||
49 | |||
50 | |||
51 | View Code Duplication | @pytest.mark.schema |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
52 | @pytest.mark.parametrize('field', [ |
||
53 | 'type', |
||
54 | 'family', |
||
55 | 'width', |
||
56 | 'height', |
||
57 | 'dataSource', |
||
58 | ]) |
||
59 | def test_validate_raw_json_missing_required_module_keys(field): |
||
60 | module = dict( |
||
61 | guid='a-b-c-d-e', |
||
62 | name='foo', dataSource='foo', |
||
63 | width='col-1', height=1, type='line', |
||
64 | family='C3') |
||
65 | del module[field] |
||
66 | d = _schema( |
||
67 | layout='grid', |
||
68 | modules=[module] |
||
69 | ) |
||
70 | with pytest.raises(app.InvalidSchemaError): |
||
71 | app.validate_raw_json(d) |
||
72 | |||
73 | |||
74 | View Code Duplication | @pytest.mark.schema |
|
0 ignored issues
–
show
|
|||
75 | @pytest.mark.parametrize('field', [ |
||
76 | 'row', |
||
77 | ]) |
||
78 | def test_validate_raw_json_missing_required_fixedgrid_module_keys(field): |
||
79 | module = dict( |
||
80 | guid='a-b-c-d-e', |
||
81 | name='foo', dataSource='foo', |
||
82 | width='col-1', height=1, type='line', |
||
83 | row=1, family='C3') |
||
84 | del module[field] |
||
85 | d = _schema( |
||
86 | layout='grid', |
||
87 | modules=[module] |
||
88 | ) |
||
89 | with pytest.raises(app.InvalidSchemaError): |
||
90 | app.validate_raw_json(d) |
||
91 | |||
92 | |||
93 | View Code Duplication | @pytest.mark.schema |
|
0 ignored issues
–
show
|
|||
94 | @pytest.mark.parametrize('field', [ |
||
95 | 'row', |
||
96 | ]) |
||
97 | def test_validate_raw_json_missing_optional_freeform_module_keys(field): |
||
98 | # Ensure that required fields for fixed grid |
||
99 | # are not required for freeform layouts. |
||
100 | module = dict( |
||
101 | guid='a-b-c-d-e', |
||
102 | name='foo', dataSource='foo', |
||
103 | width=1, height=1, type='line', |
||
104 | row=1, family='C3') |
||
105 | del module[field] |
||
106 | d = _schema( |
||
107 | layout='freeform', |
||
108 | modules=[module] |
||
109 | ) |
||
110 | assert app.validate_raw_json(d) |
||
111 | |||
112 | |||
113 | View Code Duplication | @pytest.mark.schema |
|
0 ignored issues
–
show
|
|||
114 | @pytest.mark.parametrize('field', [ |
||
115 | 'id', |
||
116 | 'layout', |
||
117 | 'name', |
||
118 | 'modules', |
||
119 | ]) |
||
120 | def test_validate_raw_json_invalid_missing_toplevel_keys(field): |
||
121 | module = dict( |
||
122 | guid='a-b-c-d-e', |
||
123 | layout='freeform', |
||
124 | name='foo', dataSource='foo', |
||
125 | width=1, height=1, type='line', family='C3', |
||
126 | ) |
||
127 | config = _schema( |
||
128 | layout='freeform', |
||
129 | modules=[module] |
||
130 | ) |
||
131 | config = json.loads(config) |
||
132 | del config[field] |
||
133 | with pytest.raises(app.InvalidSchemaError) as exc: |
||
134 | app.validate_raw_json(json.dumps(config)) |
||
135 | assert "{'" + field + "': ['required field']}" in str(exc.value) |
||
136 | |||
137 | |||
138 | View Code Duplication | @pytest.mark.schema |
|
0 ignored issues
–
show
|
|||
139 | def test_validate_raw_json_invalid_mixed_use_freeform_with_rows(): |
||
140 | # Ensure `row` in modules and layout `freeform` cannot be mixed. |
||
141 | module = dict( |
||
142 | guid='a-b-c-d-e', |
||
143 | name='foo', dataSource='foo', |
||
144 | width=1, height=1, type='line', |
||
145 | row=1, family='C3', |
||
146 | ) |
||
147 | config = _schema( |
||
148 | layout='freeform', |
||
149 | modules=[module] |
||
150 | ) |
||
151 | with pytest.raises(app.InvalidSchemaError) as exc: |
||
152 | app.validate_raw_json(config) |
||
153 | assert 'Cannot mix' in str(exc.value) |
||
154 | |||
155 | |||
156 | View Code Duplication | @pytest.mark.schema |
|
0 ignored issues
–
show
|
|||
157 | def test_validate_raw_json_missing_row_for_layout_grid(): |
||
158 | module = dict( |
||
159 | guid='a-b-c-d-e', |
||
160 | name='foo', dataSource='foo', |
||
161 | width='col-1', height=1, type='line', layout='grid', family='C3', |
||
162 | ) |
||
163 | config = _schema( |
||
164 | layout='grid', |
||
165 | modules=[module] |
||
166 | ) |
||
167 | with pytest.raises(app.InvalidSchemaError) as exc: |
||
168 | app.validate_raw_json(config) |
||
169 | assert 'Invalid row value for module "foo"' in str(exc.value) |
||
170 | |||
171 | |||
172 | @pytest.mark.schema |
||
173 | def test_validate_raw_json_invalid_grid_nonconsencutive_rows(): |
||
174 | # Ensure row numbers can't "skip", e.g. [1, 2, 10] |
||
175 | config = _schema( |
||
176 | layout='grid', |
||
177 | modules=[ |
||
178 | dict(guid='a-b-c-d-e', name='f', dataSource='f', width='col-1', |
||
179 | row=1, height=1, family='C3', type='line'), |
||
180 | dict(guid='a-b-c-d-e', name='f', dataSource='f', width='col-1', |
||
181 | row=2, height=1, family='C3', type='line'), |
||
182 | dict(guid='a-b-c-d-e', name='f', dataSource='f', width='col-1', |
||
183 | row=10, height=1, family='C3', type='line'), |
||
184 | ] |
||
185 | ) |
||
186 | with pytest.raises(app.InvalidSchemaError) as exc: |
||
187 | app.validate_raw_json(config) |
||
188 | assert 'Row order is not consecutive' in str(exc.value) |
||
189 | |||
190 | |||
191 | @pytest.mark.schema |
||
192 | def test_validate_raw_json_invalid_grid_consecutive_but_duplicate_rows(): |
||
193 | # Ensure duplicate row numbers are consecutive, IF they were unique. |
||
194 | # e.g. [1, 1, 2, 2, 3] is valid. |
||
195 | config = _schema( |
||
196 | layout='grid', |
||
197 | id='a-b-c-d-e', |
||
198 | modules=[ |
||
199 | dict(guid='a-b-c-d-e', name='f', dataSource='f', width='col-1', |
||
200 | row=1, height=1, family='C3', type='line'), |
||
201 | dict(guid='a-b-c-d-e', name='f', dataSource='f', width='col-1', |
||
202 | row=1, height=1, family='C3', type='line'), |
||
203 | dict(guid='a-b-c-d-e', name='f', dataSource='f', width='col-1', |
||
204 | row=2, height=1, family='C3', type='line'), |
||
205 | dict(guid='a-b-c-d-e', name='f', dataSource='f', width='col-1', |
||
206 | row=2, height=1, family='C3', type='line'), |
||
207 | dict(guid='a-b-c-d-e', name='f', dataSource='f', width='col-1', |
||
208 | row=3, height=1, family='C3', type='line'), |
||
209 | ] |
||
210 | ) |
||
211 | assert app.validate_raw_json(config) |
||
212 | |||
213 | |||
214 | @pytest.mark.schema |
||
215 | def test_validate_raw_json_invalid_family(): |
||
216 | config = _schema( |
||
217 | layout='grid', |
||
218 | modules=[ |
||
219 | dict(guid='a-b-c-d-e', name='f', dataSource='f', width='col-1', |
||
220 | row=1, height=1, family='LOLWUT', type='line'), |
||
221 | ] |
||
222 | ) |
||
223 | with pytest.raises(app.InvalidSchemaError) as exc: |
||
224 | app.validate_raw_json(config) |
||
225 | assert 'unallowed value LOLWUT' in str(exc.value) |
||
226 | |||
227 | |||
228 | @pytest.mark.schema |
||
229 | def test_validate_raw_json_invalid_width_string_cols_for_freeform_type(): |
||
230 | config = _schema( |
||
231 | layout='freeform', |
||
232 | modules=[ |
||
233 | dict(guid='a-b-c-d-e', |
||
234 | name='f', |
||
235 | dataSource='f', |
||
236 | width='col-12', |
||
237 | height=1, |
||
238 | family='C3', |
||
239 | type='line'), |
||
240 | ] |
||
241 | ) |
||
242 | with pytest.raises(app.InvalidSchemaError) as exc: |
||
243 | app.validate_raw_json(config) |
||
244 | err = str(exc.value) |
||
245 | assert 'Invalid value for width in `freeform` layout.' in err |
||
246 |