|
5 | 5 | oas30_write_schema_validators_factory, |
6 | 6 | ) |
7 | 7 | from openapi_core.validation.schemas.exceptions import InvalidSchemaValue |
| 8 | +from openapi_core.validation.schemas.validators import SchemaValidator |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class TestSchemaValidate: |
@@ -356,3 +357,81 @@ def test_enforce_properties_required_applies_to_nested_composed_schemas( |
356 | 357 | schema, |
357 | 358 | enforce_properties_required=True, |
358 | 359 | ).validate({"name": "openapi-core", "meta": {}}) |
| 360 | + |
| 361 | + |
| 362 | +class TestSchemaValidateState: |
| 363 | + SCHEMA_DICT = { |
| 364 | + "type": "object", |
| 365 | + "properties": { |
| 366 | + "x": {"oneOf": [{"type": "string"}, {"type": "integer"}]} |
| 367 | + }, |
| 368 | + } |
| 369 | + VALUE = {"x": "hi"} |
| 370 | + |
| 371 | + @pytest.fixture(autouse=True) |
| 372 | + def clear_cache(self): |
| 373 | + # Keep this class's cache observations isolated from other tests. |
| 374 | + SchemaValidator._needs_state_cache.clear() |
| 375 | + yield |
| 376 | + SchemaValidator._needs_state_cache.clear() |
| 377 | + |
| 378 | + @pytest.fixture |
| 379 | + def cache(self): |
| 380 | + return SchemaValidator._needs_state_cache |
| 381 | + |
| 382 | + @pytest.fixture |
| 383 | + def validator_and_prop_factory(self): |
| 384 | + # Build a validator over a freshly loaded spec and return it |
| 385 | + # alongside the SchemaPath the cache keys on for property "x". |
| 386 | + root = SchemaPath.from_dict({}) |
| 387 | + |
| 388 | + def _build(): |
| 389 | + spec = SchemaPath.from_dict(self.SCHEMA_DICT) |
| 390 | + validator = oas30_write_schema_validators_factory.create( |
| 391 | + root, spec |
| 392 | + ) |
| 393 | + prop = spec / "properties" / "x" |
| 394 | + return validator, prop |
| 395 | + |
| 396 | + return _build |
| 397 | + |
| 398 | + def test_cold_pass_populates_cache( |
| 399 | + self, cache, validator_and_prop_factory |
| 400 | + ): |
| 401 | + validator, prop = validator_and_prop_factory() |
| 402 | + assert prop not in cache |
| 403 | + |
| 404 | + validator.validate_state(self.VALUE) |
| 405 | + |
| 406 | + # oneOf under "x" -> a ValidationState is worthwhile. |
| 407 | + assert cache[prop] is True |
| 408 | + |
| 409 | + def test_warm_pass_reads_cached_answer( |
| 410 | + self, cache, validator_and_prop_factory |
| 411 | + ): |
| 412 | + validator, prop = validator_and_prop_factory() |
| 413 | + validator.validate_state(self.VALUE) # prime |
| 414 | + # Poison the entry: a genuine cache hit returns this value |
| 415 | + # unchanged, whereas a recompute would overwrite it back to True. |
| 416 | + cache[prop] = False |
| 417 | + |
| 418 | + validator.validate_state(self.VALUE) |
| 419 | + |
| 420 | + assert cache[prop] is False |
| 421 | + |
| 422 | + def test_distinct_spec_does_not_collide( |
| 423 | + self, cache, validator_and_prop_factory |
| 424 | + ): |
| 425 | + # Two separately loaded specs with identical contents have |
| 426 | + # distinct identity, so their equally-pathed property schemas |
| 427 | + # occupy separate cache slots instead of colliding. |
| 428 | + validator_a, prop_a = validator_and_prop_factory() |
| 429 | + validator_b, prop_b = validator_and_prop_factory() |
| 430 | + |
| 431 | + validator_a.validate_state(self.VALUE) |
| 432 | + assert prop_a in cache |
| 433 | + assert prop_b not in cache |
| 434 | + |
| 435 | + validator_b.validate_state(self.VALUE) |
| 436 | + assert cache[prop_a] is True |
| 437 | + assert cache[prop_b] is True |
0 commit comments