/
test_schema_validator.rb
464 lines (386 loc) · 16.3 KB
/
test_schema_validator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'test_help'
class TestSchema < Test::Unit::TestCase
def validate!(schema, value)
Avro::SchemaValidator.validate!(schema, value)
end
def validate_simple!(schema, value)
Avro::SchemaValidator.validate!(schema, value, recursive: false)
end
def hash_to_schema(hash)
Avro::Schema.parse(hash.to_json)
end
def assert_failed_validation(messages)
error = assert_raise(Avro::SchemaValidator::ValidationError) { yield }
assert_messages = [messages].flatten
result_errors = error.result.errors
assert_messages.each do |message|
assert(result_errors.include?(message), "expected '#{message}' to be in '#{result_errors}'")
end
assert_equal(assert_messages.size, result_errors.size)
end
def assert_valid_schema(schema, valid, invalid, simple = false)
valid.each do |value|
assert_nothing_raised { Avro::SchemaValidator.validate!(schema, value) }
assert_nothing_raised { Avro::SchemaValidator.validate!(schema, value, recursive: false) } if simple
end
invalid.each do |value|
assert_raise { Avro::SchemaValidator.validate!(schema, value) }
assert_raise { Avro::SchemaValidator.validate!(schema, value, recursive: false) } if simple
assert_nothing_raised { Avro::SchemaValidator.validate!(schema, value, recursive: false) } unless simple
end
end
def test_validate_nil
schema = hash_to_schema(type: 'null', name: 'name')
assert_nothing_raised { validate!(schema, nil) }
assert_nothing_raised { validate_simple!(schema, nil) }
assert_failed_validation('at . expected type null, got int with value 1') do
validate!(schema, 1)
end
assert_failed_validation('at . expected type null, got int with value 1') do
validate_simple!(schema, 1)
end
end
def test_validate_boolean
schema = hash_to_schema(type: 'boolean', name: 'name')
assert_nothing_raised { validate!(schema, true) }
assert_nothing_raised { validate!(schema, false) }
assert_nothing_raised { validate_simple!(schema, true) }
assert_nothing_raised { validate_simple!(schema, false) }
assert_failed_validation('at . expected type boolean, got int with value 1') do
validate!(schema, 1)
end
assert_failed_validation('at . expected type boolean, got int with value 1') do
validate_simple!(schema, 1)
end
assert_failed_validation('at . expected type boolean, got null') do
validate!(schema, nil)
end
assert_failed_validation('at . expected type boolean, got null') do
validate_simple!(schema, nil)
end
end
def test_fixed_size_string
schema = hash_to_schema(type: 'fixed', name: 'some', size: 3)
assert_nothing_raised { validate!(schema, 'baf') }
assert_nothing_raised { validate_simple!(schema, 'baf') }
assert_failed_validation('at . expected fixed with size 3, got "some" with size 4') do
validate!(schema, 'some')
end
assert_failed_validation('at . expected fixed with size 3, got "some" with size 4') do
validate_simple!(schema, 'some')
end
assert_failed_validation('at . expected fixed with size 3, got null') do
validate!(schema, nil)
end
assert_failed_validation('at . expected fixed with size 3, got null') do
validate_simple!(schema, nil)
end
end
def test_original_validate_nil
schema = hash_to_schema(type: 'null', name: 'name')
assert_valid_schema(schema, [nil], ['something'], true)
end
def test_original_validate_boolean
schema = hash_to_schema(type: 'boolean', name: 'name')
assert_valid_schema(schema, [true, false], [nil, 1], true)
end
def test_validate_string
schema = hash_to_schema(type: 'string', name: 'name')
assert_valid_schema(schema, ['string'], [nil, 1], true)
end
def test_validate_bytes
schema = hash_to_schema(type: 'bytes', name: 'name')
assert_valid_schema(schema, ['string'], [nil, 1], true)
end
def test_validate_int
schema = hash_to_schema(type: 'int', name: 'name')
assert_valid_schema(
schema,
[Avro::Schema::INT_MIN_VALUE, Avro::Schema::INT_MAX_VALUE, 1],
[Avro::Schema::LONG_MIN_VALUE, Avro::Schema::LONG_MAX_VALUE, 'string'],
true
)
assert_failed_validation('at . out of bound value 9223372036854775807') do
validate!(schema, Avro::Schema::LONG_MAX_VALUE)
end
assert_failed_validation('at . out of bound value 9223372036854775807') do
validate_simple!(schema, Avro::Schema::LONG_MAX_VALUE)
end
end
def test_validate_long
schema = hash_to_schema(type: 'long', name: 'name')
assert_valid_schema(schema, [Avro::Schema::LONG_MIN_VALUE, Avro::Schema::LONG_MAX_VALUE, 1], [1.1, 'string'], true)
end
def test_validate_float
schema = hash_to_schema(type: 'float', name: 'name')
assert_valid_schema(schema, [1.1, 1, Avro::Schema::LONG_MAX_VALUE], ['string'], true)
end
def test_validate_double
schema = hash_to_schema(type: 'double', name: 'name')
assert_valid_schema(schema, [1.1, 1, Avro::Schema::LONG_MAX_VALUE], ['string'], true)
end
def test_validate_fixed
schema = hash_to_schema(type: 'fixed', name: 'name', size: 3)
assert_valid_schema(schema, ['abc'], ['ab', 1, 1.1, true], true)
end
def test_validate_original_num
schema = hash_to_schema(type: 'enum', name: 'name', symbols: %w(a b))
assert_valid_schema(schema, ['a', 'b'], ['c'], true)
end
def test_validate_record
schema = hash_to_schema(type: 'record', name: 'name', fields: [{ type: 'null', name: 'sub' }])
assert_valid_schema(schema, [{ 'sub' => nil }], [{ 'sub' => 1 }])
end
def test_validate_shallow_record
schema = hash_to_schema(
type: 'record', name: 'name', fields: [{ type: 'int', name: 'sub' }]
)
assert_nothing_raised { validate!(schema, 'sub' => 1) }
assert_nothing_raised { validate_simple!(schema, 'sub' => 1) }
assert_failed_validation('at .sub expected type int, got null') do
validate!(schema, {})
end
assert_nothing_raised { validate_simple!(schema, {}) }
assert_failed_validation('at . expected type record, got float with value 1.2') do
validate!(schema, 1.2)
end
assert_nothing_raised { validate_simple!(schema, 1.2) }
assert_failed_validation('at .sub expected type int, got float with value 1.2') do
validate!(schema, 'sub' => 1.2)
end
assert_nothing_raised { validate_simple!(schema, 'sub' => 1.2) }
end
def test_validate_array
schema = hash_to_schema(type: 'array',
name: 'person',
items: [{ type: 'int', name: 'height' }])
assert_nothing_raised { validate!(schema, []) }
assert_nothing_raised { validate_simple!(schema, []) }
assert_failed_validation 'at . expected type array, got null' do
validate!(schema, nil)
end
assert_nothing_raised { validate_simple!(schema, nil) }
assert_failed_validation('at .[0] expected type int, got null') do
validate!(schema, [nil])
end
assert_nothing_raised { validate_simple!(schema, [nil]) }
assert_failed_validation('at .[3] expected type int, got string with value "so wrong"') do
validate!(schema, [1, 3, 9, 'so wrong'])
end
assert_nothing_raised { validate_simple!(schema, [1, 3, 9, 'so wrong']) }
end
def test_validate_enum
schema = hash_to_schema(type: 'enum',
name: 'person',
symbols: %w(one two three))
assert_nothing_raised { validate!(schema, 'one') }
assert_nothing_raised { validate_simple!(schema, 'one') }
assert_failed_validation('at . expected enum with values ["one", "two", "three"], got string with value "five"') do
validate!(schema, 'five')
end
assert_failed_validation('at . expected enum with values ["one", "two", "three"], got string with value "five"') do
validate_simple!(schema, 'five')
end
end
def test_validate_union_on_primitive_types
schema = hash_to_schema(
name: 'should_not_matter',
type: 'record',
fields: [
{ name: 'what_ever', type: %w(long string) }
]
)
assert_failed_validation('at .what_ever expected union of [\'long\', \'string\'], got null') {
validate!(schema, 'what_ever' => nil)
}
assert_nothing_raised { validate_simple!(schema, 'what_ever' => nil) }
end
def test_validate_union_of_nil_and_record_inside_array
schema = hash_to_schema(
name: 'this does not matter',
type: 'record',
fields: [
{
name: 'person',
type: {
name: 'person_entry',
type: 'record',
fields: [
{
name: 'houses',
type: [
'null',
{
name: 'houses_entry',
type: 'array',
items: [
{
name: 'house_entry',
type: 'record',
fields: [
{ name: 'number_of_rooms', type: 'long' }
]
}
]
}
],
}
]
}
}
]
)
assert_failed_validation('at .person expected type record, got null') {
validate!(schema, 'not at all' => nil)
}
assert_nothing_raised { validate_simple!(schema, 'person' => {}) }
assert_nothing_raised { validate!(schema, 'person' => {}) }
assert_nothing_raised { validate!(schema, 'person' => { houses: [] }) }
assert_nothing_raised { validate!(schema, 'person' => { 'houses' => [{ 'number_of_rooms' => 1 }] }) }
assert_nothing_raised { validate_simple!(schema, 'person' => {}) }
assert_nothing_raised { validate_simple!(schema, 'person' => { houses: [] }) }
assert_nothing_raised { validate_simple!(schema, 'person' => { 'houses' => [{ 'number_of_rooms' => 1 }] }) }
message = 'at .person.houses[1].number_of_rooms expected type long, got string with value "not valid at all"'
datum = {
'person' => {
'houses' => [
{ 'number_of_rooms' => 2 },
{ 'number_of_rooms' => 'not valid at all' }
]
}
}
assert_failed_validation(message) { validate!(schema, datum) }
assert_nothing_raised { validate_simple!(schema, datum) }
end
def test_validate_map
schema = hash_to_schema(type: 'map',
name: 'numbers',
values: [
{ name: 'some', type: 'int' }
])
assert_nothing_raised { validate!(schema, 'some' => 1) }
assert_nothing_raised { validate_simple!(schema, 'some' => 1) }
assert_failed_validation('at .some expected type int, got string with value "nope"') do
validate!(schema, 'some' => 'nope')
end
assert_nothing_raised { validate_simple!(schema, 'some' => 'nope')}
assert_failed_validation("at . unexpected key type 'Symbol' in map") do
validate!(schema, some: 1)
end
assert_nothing_raised { validate_simple!(schema, some: 1) }
assert_failed_validation('at . expected type map, got null') do
validate!(schema, nil)
end
assert_nothing_raised { validate_simple!(schema, nil) }
end
def test_validate_deep_record
schema = hash_to_schema(type: 'record',
name: 'person',
fields: [
{
name: 'head',
type: {
name: 'head',
type: 'record',
fields: [
{
name: 'hair',
type: {
name: 'hair',
type: 'record',
fields: [
{
name: 'color',
type: 'string'
}
]
}
}
]
}
}
])
assert_nothing_raised { validate!(schema, 'head' => { 'hair' => { 'color' => 'black' } }) }
assert_nothing_raised { validate_simple!(schema, 'head' => { 'hair' => { 'color' => 'black' } }) }
assert_failed_validation('at .head.hair.color expected type string, got null') do
validate!(schema, 'head' => { 'hair' => { 'color' => nil } })
end
assert_nothing_raised { validate_simple!(schema, 'head' => { 'hair' => { 'color' => nil } }) }
assert_failed_validation('at .head.hair.color expected type string, got null') do
validate!(schema, 'head' => { 'hair' => {} })
end
assert_nothing_raised { validate_simple!(schema, 'head' => { 'hair' => {} }) }
assert_failed_validation('at .head.hair expected type record, got null') do
validate!(schema, 'head' => {})
end
assert_nothing_raised { validate_simple!(schema, 'head' => {}) }
assert_failed_validation('at . expected type record, got null') do
validate!(schema, nil)
end
assert_nothing_raised { validate_simple!(schema, nil) }
end
def test_validate_deep_record_with_array
schema = hash_to_schema(type: 'record',
name: 'fruits',
fields: [
{
name: 'fruits',
type: {
name: 'fruits',
type: 'array',
items: [
{
name: 'fruit',
type: 'record',
fields: [
{ name: 'name', type: 'string' },
{ name: 'weight', type: 'float' }
]
}
]
}
}
])
assert_nothing_raised { validate!(schema, 'fruits' => [{ 'name' => 'apple', 'weight' => 30.2 }]) }
assert_nothing_raised { validate_simple!(schema, 'fruits' => [{ 'name' => 'apple', 'weight' => 30.2 }]) }
assert_failed_validation('at .fruits[0].name expected type string, got null') do
validate!(schema, 'fruits' => [{ 'name' => nil, 'weight' => 30.2 }])
end
assert_nothing_raised { validate_simple!(schema, 'fruits' => [{ 'name' => nil, 'weight' => 30.2 }]) }
assert_failed_validation('at .fruits expected type array, got int with value 1') do
validate!(schema, 'fruits' => 1)
end
assert_nothing_raised { validate_simple!(schema, 'fruits' => 1) }
end
def test_validate_multiple_errors
schema = hash_to_schema(type: 'array',
name: 'ages',
items: [
{ type: 'int', name: 'age' }
])
exception = assert_raise(Avro::SchemaValidator::ValidationError) do
validate!(schema, [nil, 'e'])
end
assert_nothing_raised { validate_simple!(schema, [nil, 'e']) }
assert_equal 2, exception.result.errors.size
assert_equal(
"at .[0] expected type int, got null\nat .[1] expected type int, got string with value \"e\"",
exception.to_s
)
end
end