-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_models_against_schemas.py
More file actions
580 lines (424 loc) · 20.4 KB
/
Copy pathtest_models_against_schemas.py
File metadata and controls
580 lines (424 loc) · 20.4 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
"""Schema conformance tests for core Pydantic models.
This test suite validates that core Pydantic models both accept valid inputs
and reject invalid ones in strict alignment with their corresponding JSON
Schemas. The tests are intentionally verbose and repetitive to ensure full
coverage and explicit failure modes.
"""
# pylint: disable=line-too-long,missing-function-docstring
# pylint: disable=redefined-outer-name,global-statement
from __future__ import annotations
import json
from pathlib import Path
from typing import Any, TypeVar
import pytest
from jsonschema import ValidationError as JsonSchemaValidationError
from jsonschema import validate as jsonschema_validate
from pydantic import TypeAdapter
from pydantic import ValidationError as PydanticValidationError
from referencing import Registry, Resource
from referencing.jsonschema import DRAFT202012
from trading_framework.core.domain.types import (
FillEvent,
MarketEvent,
OrderIntent,
OrderStateEvent,
RiskConstraints,
)
SCHEMA_REGISTRY = Registry()
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def load_schema(name: str) -> dict:
"""
Load JSON schema from project root.
"""
global SCHEMA_REGISTRY
root = Path(__file__).parent.parent.parent.parent # /workspaces/trading-framework
name = "trading_framework/core/schemas/" + name
schema_path = root / name
with schema_path.open("r", encoding="utf-8") as f:
schema = json.load(f)
schema_id = schema.get("$id")
if isinstance(schema_id, str) and schema_id:
resource = Resource.from_contents(schema, default_specification=DRAFT202012)
SCHEMA_REGISTRY = SCHEMA_REGISTRY.with_resource(schema_id, resource)
return schema
def dump_for_jsonschema(model: Any) -> dict:
"""
Dump a Pydantic model to a JSON-compatible dict for schema validation.
Excludes None values so optional fields are omitted instead of null.
"""
# All validated objects in this test suite are BaseModel instances.
return model.model_dump(mode="json", exclude_none=True)
T = TypeVar("T")
def pydantic_validate(model_type: Any, data: dict[str, Any]) -> Any:
"""
Validate input using Pydantic for both:
- BaseModel subclasses (e.g., MarketEvent)
- Discriminated unions (e.g., OrderIntent is an Annotated Union)
"""
adapter = TypeAdapter(model_type)
return adapter.validate_python(data)
def assert_pydantic_then_schema_ok(model_type: Any, data: dict[str, Any], schema: dict[str, Any]) -> dict:
"""
Validate with Pydantic first, then validate the dumped instance with JSON Schema.
Returns the dumped instance.
"""
obj = pydantic_validate(model_type, data)
instance = dump_for_jsonschema(obj)
jsonschema_validate(instance=instance, schema=schema, registry=SCHEMA_REGISTRY)
return instance
def assert_schema_invalid_but_pydantic_rejects(model_type: Any, data: dict[str, Any], schema: dict[str, Any]):
"""
Ensures Pydantic is at least as strict as the JSON Schema for the given input.
If schema rejects, Pydantic must reject too (otherwise model is too lax).
"""
with pytest.raises(JsonSchemaValidationError):
jsonschema_validate(instance=data, schema=schema, registry=SCHEMA_REGISTRY)
with pytest.raises(PydanticValidationError):
pydantic_validate(model_type, data)
def mk_price(value: float, currency: str = "USD") -> dict[str, Any]:
return {"currency": currency, "value": value}
def mk_qty(value: float, unit: str = "contracts") -> dict[str, Any]:
return {"value": value, "unit": unit}
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture(scope="module", autouse=True)
def _load_common_schema() -> None:
load_schema("common.schema.json")
@pytest.fixture(scope="module")
def market_event_schema() -> dict:
return load_schema("market_event.schema.json")
@pytest.fixture(scope="module")
def order_intent_schema() -> dict:
return load_schema("order_intent.schema.json")
@pytest.fixture(scope="module")
def risk_constraints_schema() -> dict:
return load_schema("risk_constraints.schema.json")
@pytest.fixture(scope="module")
def fill_event_schema() -> dict:
return load_schema("fill_event.schema.json")
@pytest.fixture(scope="module")
def order_state_event_schema() -> dict:
return load_schema("order_state_event.schema.json")
# ---------------------------------------------------------------------------
# MarketEvent
# ---------------------------------------------------------------------------
def make_book_event(**book_overrides) -> dict[str, Any]:
book = {
"book_type": "snapshot",
"bids": [{"price": mk_price(100.0), "quantity": mk_qty(1.0)}],
"asks": [{"price": mk_price(100.5), "quantity": mk_qty(1.5)}],
}
book.update(book_overrides)
return {
"ts_ns_local": 123456789,
"ts_ns_exch": 123456089,
"instrument": "BTC-USD",
"event_type": "book",
"book": book,
}
def make_trade_event(**trade_overrides) -> dict[str, Any]:
trade = {
"side": "buy",
"price": mk_price(100.25),
"quantity": mk_qty(0.5),
}
trade.update(trade_overrides)
return {
"ts_ns_local": 123456790,
"ts_ns_exch": 123456789,
"instrument": "BTC-USD",
"event_type": "trade",
"trade": trade,
}
def test_market_event_book_valid_minimal(market_event_schema):
assert_pydantic_then_schema_ok(MarketEvent, make_book_event(), market_event_schema)
def test_market_event_trade_valid_minimal(market_event_schema):
assert_pydantic_then_schema_ok(MarketEvent, make_trade_event(trade_id="T123"), market_event_schema)
def test_market_event_enforces_payload_presence():
with pytest.raises(PydanticValidationError):
pydantic_validate(
MarketEvent,
{"ts_ns_local": 2, "ts_ns_exch": 1, "instrument": "BTC-USD", "event_type": "book"},
)
with pytest.raises(PydanticValidationError):
pydantic_validate(
MarketEvent,
{"ts_ns_local": 2, "ts_ns_exch": 1, "instrument": "BTC-USD", "event_type": "trade"},
)
def test_market_event_rejects_extra_top_level_fields(market_event_schema):
data = make_book_event()
data["unexpected"] = 123
with pytest.raises(PydanticValidationError):
pydantic_validate(MarketEvent, data)
with pytest.raises(JsonSchemaValidationError):
jsonschema_validate(instance=data, schema=market_event_schema, registry=SCHEMA_REGISTRY)
def test_market_event_book_depth_optional_valid_values(market_event_schema):
assert_pydantic_then_schema_ok(MarketEvent, make_book_event(depth=0), market_event_schema)
assert_pydantic_then_schema_ok(MarketEvent, make_book_event(depth=1), market_event_schema)
def test_market_event_book_depth_negative_rejected(market_event_schema):
bad = make_book_event(depth=-1)
assert_schema_invalid_but_pydantic_rejects(MarketEvent, bad, market_event_schema)
def test_market_event_trade_id_min_length(market_event_schema):
assert_pydantic_then_schema_ok(MarketEvent, make_trade_event(trade_id="X"), market_event_schema)
bad = make_trade_event(trade_id="")
assert_schema_invalid_but_pydantic_rejects(MarketEvent, bad, market_event_schema)
def test_market_event_instrument_min_length(market_event_schema):
bad = make_book_event()
bad["instrument"] = ""
assert_schema_invalid_but_pydantic_rejects(MarketEvent, bad, market_event_schema)
def test_market_event_ts_ns_exclusive_minimum(market_event_schema):
bad = make_book_event()
bad["ts_ns_local"] = 0
assert_schema_invalid_but_pydantic_rejects(MarketEvent, bad, market_event_schema)
def test_market_event_xor_book_trade_enforced(market_event_schema):
# Schema forbids having both book and trade.
bad = make_book_event()
bad["trade"] = make_trade_event()["trade"]
with pytest.raises(JsonSchemaValidationError):
jsonschema_validate(instance=bad, schema=market_event_schema, registry=SCHEMA_REGISTRY)
with pytest.raises(PydanticValidationError):
pydantic_validate(MarketEvent, bad)
# ---------------------------------------------------------------------------
# OrderIntent
# ---------------------------------------------------------------------------
def make_new_intent(**overrides) -> dict[str, Any]:
"""
Build a valid minimal 'new' intent (limit by default).
Note: intended_price is required for both limit and market orders in this system.
"""
data: dict[str, Any] = {
"ts_ns_local": 123456790,
"client_order_id": "C-1",
"instrument": "BTC-USD",
"intent_type": "new",
"order_type": "limit",
"side": "buy",
"intended_qty": mk_qty(1.0),
"intended_price": mk_price(100.0),
"time_in_force": "GTC",
}
data.update(overrides)
return data
def make_cancel_intent(**overrides) -> dict[str, Any]:
"""
Build a valid minimal 'cancel' intent.
"""
data: dict[str, Any] = {
"ts_ns_local": 123456791,
"client_order_id": "C-1",
"instrument": "BTC-USD",
"intent_type": "cancel",
}
data.update(overrides)
return data
def make_replace_intent(**overrides) -> dict[str, Any]:
"""
Build a valid minimal 'replace' intent (limit-only).
time_in_force is not allowed because it is not modifiable by the execution binding.
"""
data: dict[str, Any] = {
"ts_ns_local": 123456792,
"client_order_id": "C-1",
"instrument": "BTC-USD",
"intent_type": "replace",
"side": "buy",
"order_type": "limit",
"intended_qty": mk_qty(2.0),
"intended_price": mk_price(101.0),
}
data.update(overrides)
return data
def test_order_intent_valid_new_limit_with_optional_correlation_id(order_intent_schema):
data = make_new_intent(intents_correlation_id="CORR-1")
assert_pydantic_then_schema_ok(OrderIntent, data, order_intent_schema)
def test_order_intent_new_market_requires_intended_price(order_intent_schema):
data = make_new_intent(order_type="market")
assert_pydantic_then_schema_ok(OrderIntent, data, order_intent_schema)
bad = make_new_intent(order_type="market")
bad.pop("intended_price", None)
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad, order_intent_schema)
def test_order_intent_new_limit_requires_intended_price(order_intent_schema):
bad = make_new_intent(order_type="limit")
bad.pop("intended_price", None)
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad, order_intent_schema)
def test_order_intent_cancel_valid_minimal(order_intent_schema):
assert_pydantic_then_schema_ok(OrderIntent, make_cancel_intent(), order_intent_schema)
def test_order_intent_cancel_forbids_order_fields(order_intent_schema):
# Cancel must not contain order-creation fields.
bad = make_cancel_intent(side="buy")
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad, order_intent_schema)
bad = make_cancel_intent(order_type="limit")
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad, order_intent_schema)
bad = make_cancel_intent(intended_qty=1.0)
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad, order_intent_schema)
bad = make_cancel_intent(intended_price=100.0)
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad, order_intent_schema)
bad = make_cancel_intent(time_in_force="GTC")
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad, order_intent_schema)
def test_order_intent_replace_valid_minimal(order_intent_schema):
assert_pydantic_then_schema_ok(OrderIntent, make_replace_intent(), order_intent_schema)
def test_order_intent_replace_requires_limit(order_intent_schema):
bad = make_replace_intent(order_type="market")
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad, order_intent_schema)
def test_order_intent_replace_forbids_time_in_force(order_intent_schema):
# Replace must not contain time_in_force (not modifiable).
bad = make_replace_intent(time_in_force="GTC")
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad, order_intent_schema)
def test_order_intent_min_length_optionals(order_intent_schema):
bad_corr = make_new_intent(intents_correlation_id="")
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad_corr, order_intent_schema)
def test_order_intent_exclusive_minimum_constraints(order_intent_schema):
bad_ts = make_new_intent(ts_ns_local=0)
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad_ts, order_intent_schema)
bad_price = make_new_intent(intended_price=mk_price(-1.0))
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad_price, order_intent_schema)
bad_qty = make_new_intent(intended_qty=mk_qty(-1.0))
assert_schema_invalid_but_pydantic_rejects(OrderIntent, bad_qty, order_intent_schema)
def test_order_intent_rejects_additional_properties(order_intent_schema):
data = make_new_intent()
data["unexpected"] = "x"
with pytest.raises(PydanticValidationError):
pydantic_validate(OrderIntent, data)
with pytest.raises(JsonSchemaValidationError):
jsonschema_validate(instance=data, schema=order_intent_schema, registry=SCHEMA_REGISTRY)
# ---------------------------------------------------------------------------
# RiskConstraints
# ---------------------------------------------------------------------------
def make_risk_constraints(**overrides) -> dict[str, Any]:
data = {
"ts_ns_local": 987654321,
"scope": "BTC-USD:default",
"trading_enabled": True,
}
data.update(overrides)
return data
def test_risk_constraints_valid_with_optionals(risk_constraints_schema):
data = make_risk_constraints(
notional_limits={
"currency": "USD",
"max_gross_notional": 1_000_000.0,
"max_single_order_notional": 100_000.0,
},
position_limits={"currency": "BTC", "max_position": 10.0},
quote_limits={
"currency": "USD",
"max_gross_quote_notional": 500_000.0,
"max_net_quote_notional": 0.0,
"max_active_quotes": 100,
},
order_rate_limits={"max_orders_per_second": 50.0, "max_cancels_per_second": 100.0},
max_loss={"currency": "USD", "max_drawdown": -10_000.0, "rolling_loss": -1000.0, "rolling_loss_window": 60},
extra={"desk": "alpha", "risk_mode": "conservative", "debug": None},
)
assert_pydantic_then_schema_ok(RiskConstraints, data, risk_constraints_schema)
def test_risk_constraints_missing_optional_notional_limits_is_ok(risk_constraints_schema):
data = make_risk_constraints()
assert_pydantic_then_schema_ok(RiskConstraints, data, risk_constraints_schema)
def test_risk_constraints_minimum_constraints(risk_constraints_schema):
bad_pos = make_risk_constraints(position_limits={"currency": "BTC", "max_position": -1.0})
assert_schema_invalid_but_pydantic_rejects(RiskConstraints, bad_pos, risk_constraints_schema)
bad_notional = make_risk_constraints(notional_limits={"currency": "USD", "max_gross_notional": -1.0})
assert_schema_invalid_but_pydantic_rejects(RiskConstraints, bad_notional, risk_constraints_schema)
bad_quotes = make_risk_constraints(quote_limits={"currency": "USD", "max_gross_quote_notional": -1.0})
assert_schema_invalid_but_pydantic_rejects(RiskConstraints, bad_quotes, risk_constraints_schema)
bad_active = make_risk_constraints(quote_limits={"currency": "USD", "max_gross_quote_notional": 1.0, "max_active_quotes": -1})
assert_schema_invalid_but_pydantic_rejects(RiskConstraints, bad_active, risk_constraints_schema)
bad_rate = make_risk_constraints(order_rate_limits={"max_orders_per_second": -0.1})
assert_schema_invalid_but_pydantic_rejects(RiskConstraints, bad_rate, risk_constraints_schema)
def test_risk_constraints_rejects_additional_properties(risk_constraints_schema):
data = make_risk_constraints()
data["unexpected"] = 1
with pytest.raises(PydanticValidationError):
pydantic_validate(RiskConstraints, data)
with pytest.raises(JsonSchemaValidationError):
jsonschema_validate(instance=data, schema=risk_constraints_schema, registry=SCHEMA_REGISTRY)
def test_risk_constraints_extra_values_are_schema_compatible(risk_constraints_schema):
data = make_risk_constraints(extra={"ok": "x", "n": 1, "b": True, "z": None})
assert_pydantic_then_schema_ok(RiskConstraints, data, risk_constraints_schema)
bad = make_risk_constraints(extra={"nested": {"a": 1}})
with pytest.raises(JsonSchemaValidationError):
jsonschema_validate(instance=bad, schema=risk_constraints_schema, registry=SCHEMA_REGISTRY)
# Pydantic will reject too because extra is typed as primitive union.
# ---------------------------------------------------------------------------
# FillEvent
# ---------------------------------------------------------------------------
def make_fill(**overrides) -> dict[str, Any]:
data = {
"ts_ns_local": 123456789,
"ts_ns_exch": 123456089,
"instrument": "BTC-USD",
"client_order_id": "C-1",
"side": "buy",
"filled_price": mk_price(100.5),
"cum_filled_qty": mk_qty(0.25),
"time_in_force": "GTC",
"liquidity_flag": "maker",
}
data.update(overrides)
return data
def test_fill_event_valid_with_all_optionals(fill_event_schema):
data = make_fill(
intended_price=mk_price(100.0),
intended_qty=mk_qty(1.0),
remaining_qty=mk_qty(0.75),
fee={"currency": "USD", "amount": -0.1},
)
assert_pydantic_then_schema_ok(FillEvent, data, fill_event_schema)
def test_fill_event_exclusive_minimum_constraints(fill_event_schema):
bad_ts = make_fill(ts_ns_local=0)
assert_schema_invalid_but_pydantic_rejects(FillEvent, bad_ts, fill_event_schema)
def test_fill_event_min_length_optionals(fill_event_schema):
bad_order_id = make_fill(client_order_id="")
assert_schema_invalid_but_pydantic_rejects(FillEvent, bad_order_id, fill_event_schema)
def test_fill_event_rejects_additional_properties(fill_event_schema):
data = make_fill()
data["unexpected"] = "x"
with pytest.raises(PydanticValidationError):
pydantic_validate(FillEvent, data)
with pytest.raises(JsonSchemaValidationError):
jsonschema_validate(instance=data, schema=fill_event_schema, registry=SCHEMA_REGISTRY)
# ---------------------------------------------------------------------------
# OrderStateEvent
# ---------------------------------------------------------------------------
def make_state(**overrides) -> dict[str, Any]:
data = {
"ts_ns_local": 123456789,
"ts_ns_exch": 123456089,
"instrument": "BTC-USD",
"client_order_id": "C-1",
"order_type": "limit",
"state_type": "accepted",
"side": "buy",
"intended_price": mk_price(100.0),
"intended_qty": mk_qty(1.0),
"time_in_force": "GTC",
}
data.update(overrides)
return data
def test_order_state_event_valid_with_all_optionals(order_state_event_schema):
data = make_state(
filled_price=mk_price(100.1),
cum_filled_qty=mk_qty(0.5),
remaining_qty=mk_qty(0.5),
reason="Partial fill",
raw={"venue_status": "PARTIAL"},
)
assert_pydantic_then_schema_ok(OrderStateEvent, data, order_state_event_schema)
def test_order_state_event_min_length_optionals(order_state_event_schema):
bad_order_id = make_state(client_order_id="")
assert_schema_invalid_but_pydantic_rejects(OrderStateEvent, bad_order_id, order_state_event_schema)
bad_reason = make_state(reason="")
assert_schema_invalid_but_pydantic_rejects(OrderStateEvent, bad_reason, order_state_event_schema)
def test_order_state_event_exclusive_minimum_ts(order_state_event_schema):
bad = make_state(ts_ns_local=0)
assert_schema_invalid_but_pydantic_rejects(OrderStateEvent, bad, order_state_event_schema)
def test_order_state_event_rejects_additional_properties(order_state_event_schema):
data = make_state()
data["unexpected"] = 1
with pytest.raises(PydanticValidationError):
pydantic_validate(OrderStateEvent, data)
with pytest.raises(JsonSchemaValidationError):
jsonschema_validate(instance=data, schema=order_state_event_schema, registry=SCHEMA_REGISTRY)