-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_validation.py
More file actions
651 lines (585 loc) · 14.9 KB
/
test_validation.py
File metadata and controls
651 lines (585 loc) · 14.9 KB
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
"""Tests for the validation system."""
from pathlib import Path
import pytest
from asyncapi_python_codegen.parser.document_loader import extract_all_operations
from asyncapi_python_codegen.validation import (
Severity,
ValidationError,
ValidationIssue,
rule,
validate_spec,
)
from asyncapi_python_codegen.validation.context import ValidationContext
def test_validation_error_raised_for_missing_asyncapi_field(tmp_path: Path):
"""Test that missing asyncapi field raises ValidationError."""
spec_file = tmp_path / "invalid.yaml"
spec_file.write_text(
"""
operations:
myOp:
action: send
"""
)
with pytest.raises(ValueError, match="Missing 'asyncapi' version field"):
extract_all_operations(spec_file)
def test_validation_error_for_invalid_channel_parameters(tmp_path: Path):
"""Test that parameters not in address raise ValidationError."""
spec_file = tmp_path / "invalid_params.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
myChannel:
address: static.address
parameters:
userId:
schema:
type: string
messages:
msg:
payload:
type: object
operations:
myOp:
action: send
channel:
$ref: '#/channels/myChannel'
"""
)
with pytest.raises(ValidationError) as exc_info:
extract_all_operations(spec_file)
assert len(exc_info.value.errors) > 0
assert any(
"not used in address" in error.message for error in exc_info.value.errors
)
def test_validation_passes_for_valid_spec(tmp_path: Path):
"""Test that a valid spec passes validation."""
spec_file = tmp_path / "valid.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
userChannel:
address: user.{userId}
parameters:
userId:
location: $message.payload#/userId
messages:
userMessage:
payload:
type: object
properties:
userId:
type: string
bindings:
amqp:
is: routingKey
operations:
sendUser:
action: send
channel:
$ref: '#/channels/userChannel'
messages:
- $ref: '#/channels/userChannel/messages/userMessage'
"""
)
# Should not raise
operations = extract_all_operations(spec_file)
assert "sendUser" in operations
def test_validation_can_be_disabled(tmp_path: Path):
"""Test that validation can be disabled."""
spec_file = tmp_path / "invalid_params.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
myChannel:
address: static.address
parameters:
userId:
schema:
type: string
messages:
msg:
payload:
type: object
operations:
myOp:
action: send
channel:
$ref: '#/channels/myChannel'
"""
)
# Should not raise when validation is disabled
operations = extract_all_operations(spec_file, validate=False)
assert "myOp" in operations
def test_warnings_do_not_fail_validation(tmp_path: Path):
"""Test that warnings are collected but don't fail validation."""
spec_file = tmp_path / "with_warnings.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
myChannel:
address: user
parameters:
userId:
location: $message.payload#/userId
messages:
userMessage:
payload:
type: object
properties:
userId:
type: string
bindings:
amqp:
is: queue
operations:
myOp:
action: send
channel:
$ref: '#/channels/myChannel'
"""
)
# Should not raise - location warning doesn't fail
operations = extract_all_operations(spec_file)
assert "myOp" in operations
def test_custom_rule_registration():
"""Test that users can register custom validation rules."""
@rule("custom")
def custom_test_rule(ctx: ValidationContext) -> list[ValidationIssue]:
if ctx.spec.get("asyncapi") == "3.0.0":
return [
ValidationIssue(
severity=Severity.INFO,
message="Custom rule triggered",
path="$.asyncapi",
rule="custom-test-rule",
)
]
return []
# Create a simple spec
spec = {
"asyncapi": "3.0.0",
"operations": {},
}
# Validate with custom category
issues = validate_spec(
spec=spec,
operations={},
spec_path=Path("."),
categories=["custom"],
fail_on_error=False,
)
assert len(issues) == 1
assert issues[0].rule == "custom-test-rule"
def test_parameter_with_location_warns_not_implemented(tmp_path: Path):
"""Test that using location field generates a warning."""
spec_file = tmp_path / "location.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
myChannel:
address: user
parameters:
userId:
location: $message.payload#/userId
messages:
userMessage:
payload:
type: object
operations:
myOp:
action: send
channel:
$ref: '#/channels/myChannel'
"""
)
# Should succeed but print warning
operations = extract_all_operations(spec_file, fail_on_error=False)
assert "myOp" in operations
def test_location_path_must_exist_in_all_messages(tmp_path: Path):
"""Test that parameter location path must exist in ALL messages, not just some."""
spec_file = tmp_path / "location_missing_in_some.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
alerts:
address: alerts.{location}
parameters:
location:
location: $message.payload#/location
bindings:
amqp:
is: routingKey
exchange:
name: alerts_exchange
type: topic
messages:
alert1:
payload:
type: object
properties:
location:
type: string
message:
type: string
alert2:
payload:
type: object
properties:
message:
type: string
operations:
sendAlert:
action: send
channel:
$ref: '#/channels/alerts'
"""
)
with pytest.raises(ValidationError) as exc_info:
extract_all_operations(spec_file)
# Should fail because 'location' field is missing in alert2
assert any(
"not found in all message schemas" in error.message
and "alert2" in error.message
for error in exc_info.value.errors
)
def test_location_path_exists_in_all_messages_passes(tmp_path: Path):
"""Test that validation passes when location exists in all messages."""
spec_file = tmp_path / "location_in_all.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
alerts:
address: alerts.{location}
parameters:
location:
location: $message.payload#/location
bindings:
amqp:
is: routingKey
exchange:
name: alerts_exchange
type: topic
messages:
alert1:
payload:
type: object
properties:
location:
type: string
message:
type: string
alert2:
payload:
type: object
properties:
location:
type: string
severity:
type: string
operations:
sendAlert:
action: send
channel:
$ref: '#/channels/alerts'
"""
)
# Should succeed - location exists in both messages
operations = extract_all_operations(spec_file, fail_on_error=True)
assert "sendAlert" in operations
def test_location_path_with_single_message(tmp_path: Path):
"""Test that validation works correctly with single message."""
spec_file = tmp_path / "location_single_message.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
users:
address: users.{userId}
parameters:
userId:
location: $message.payload#/userId
bindings:
amqp:
is: queue
messages:
userEvent:
payload:
type: object
properties:
userId:
type: string
name:
type: string
operations:
publishUser:
action: send
channel:
$ref: '#/channels/users'
"""
)
# Should succeed - location exists in the single message
operations = extract_all_operations(spec_file, fail_on_error=True)
assert "publishUser" in operations
def test_location_path_with_no_messages(tmp_path: Path):
"""Test that validation skips channels with no messages."""
spec_file = tmp_path / "location_no_messages.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
emptyChannel:
address: empty.{param}
parameters:
param:
location: $message.payload#/param
bindings:
amqp:
is: queue
operations:
emptyOp:
action: send
channel:
$ref: '#/channels/emptyChannel'
messages:
- payload:
type: object
properties:
param:
type: string
"""
)
# Should succeed - validation skips channels with no messages
operations = extract_all_operations(spec_file, fail_on_error=True)
assert "emptyOp" in operations
def test_undefined_placeholders_in_address(tmp_path: Path):
"""Test that undefined placeholders in address raise error."""
spec_file = tmp_path / "undefined_params.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
myChannel:
address: user.{userId}.{role}
parameters:
userId:
schema:
type: string
messages:
msg:
payload:
type: object
operations:
myOp:
action: send
channel:
$ref: '#/channels/myChannel'
"""
)
with pytest.raises(ValidationError) as exc_info:
extract_all_operations(spec_file)
assert any(
"undefined parameters" in error.message for error in exc_info.value.errors
)
def test_operation_references_nonexistent_channel(tmp_path: Path):
"""Test that operation referencing non-existent channel raises error."""
spec_file = tmp_path / "bad_channel_ref.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
realChannel:
address: real
messages:
msg:
payload:
type: object
operations:
myOp:
action: send
channel:
$ref: '#/channels/fakeChannel'
"""
)
# Parser will fail when trying to resolve $ref (before validation runs)
with pytest.raises(
RuntimeError, match="JSON pointer segment 'fakeChannel' not found"
):
extract_all_operations(spec_file)
def test_invalid_operation_action(tmp_path: Path):
"""Test that invalid operation action raises error."""
spec_file = tmp_path / "bad_action.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
myChannel:
address: test
messages:
msg:
payload:
type: object
operations:
myOp:
action: publish
channel:
$ref: '#/channels/myChannel'
"""
)
with pytest.raises(ValidationError) as exc_info:
extract_all_operations(spec_file)
assert any(
"must be 'send' or 'receive'" in error.message
for error in exc_info.value.errors
)
def test_amqp_parameterized_channel_without_binding_type_fails(tmp_path: Path):
"""Test that parameterized channel without AMQP binding type fails validation."""
spec_file = tmp_path / "amqp_no_binding_type.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
weatherAlerts:
address: weather.{location}.{severity}
parameters:
location:
location: $message.payload#/location
severity:
location: $message.payload#/severity
messages:
alert:
payload:
type: object
properties:
location:
type: string
severity:
type: string
bindings:
amqp:
# Missing 'is' field!
exchange:
name: weather_alerts
type: topic
operations:
publishAlert:
action: send
channel:
$ref: '#/channels/weatherAlerts'
"""
)
with pytest.raises(ValidationError) as exc_info:
extract_all_operations(spec_file)
assert any("lacks 'is' field" in error.message for error in exc_info.value.errors)
def test_amqp_parameterized_channel_with_routing_key_passes(tmp_path: Path):
"""Test that parameterized channel with is: routingKey passes validation."""
spec_file = tmp_path / "amqp_routing_key.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
weatherAlerts:
address: weather.{location}.{severity}
parameters:
location:
location: $message.payload#/location
severity:
location: $message.payload#/severity
messages:
alert:
payload:
type: object
properties:
location:
type: string
severity:
type: string
bindings:
amqp:
is: routingKey
exchange:
name: weather_alerts
type: topic
operations:
publishAlert:
action: send
channel:
$ref: '#/channels/weatherAlerts'
"""
)
# Should not raise
operations = extract_all_operations(spec_file)
assert "publishAlert" in operations
def test_amqp_parameterized_channel_with_queue_passes(tmp_path: Path):
"""Test that parameterized channel with is: queue passes validation."""
spec_file = tmp_path / "amqp_queue.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
userNotifications:
address: user.{userId}.notifications
parameters:
userId:
location: $message.payload#/userId
messages:
notification:
payload:
type: object
properties:
userId:
type: string
bindings:
amqp:
is: queue
operations:
sendNotification:
action: send
channel:
$ref: '#/channels/userNotifications'
"""
)
# Should not raise
operations = extract_all_operations(spec_file)
assert "sendNotification" in operations
def test_amqp_parameterized_channel_with_invalid_binding_type_fails(tmp_path: Path):
"""Test that parameterized channel with invalid binding type fails validation."""
spec_file = tmp_path / "amqp_invalid_type.yaml"
spec_file.write_text(
"""
asyncapi: 3.0.0
channels:
myChannel:
address: my.{param}.channel
parameters:
param:
location: $message.payload#/param
messages:
msg:
payload:
type: object
properties:
param:
type: string
bindings:
amqp:
is: topic # Invalid! Should be 'routingKey' or 'queue'
operations:
myOp:
action: send
channel:
$ref: '#/channels/myChannel'
"""
)
with pytest.raises(ValidationError) as exc_info:
extract_all_operations(spec_file)
assert any(
"invalid" in error.message and "binding type" in error.message
for error in exc_info.value.errors
)