Skip to content

Commit 0f60507

Browse files
author
David Andersson
committed
add support for validation cache
1 parent ceaf174 commit 0f60507

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

open_alchemy/schemas/validation/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import typing
44

5+
from ... import cache
56
from ... import exceptions as _exceptions
67
from ... import types as _oa_types
78
from ..helpers import iterate
@@ -87,14 +88,21 @@ def _other_schemas_checks(*, schemas: _oa_types.Schemas) -> types.Result:
8788
return types.Result(valid=True, reason=None)
8889

8990

90-
def process(*, schemas: _oa_types.Schemas) -> None:
91+
def process(
92+
*, schemas: _oa_types.Schemas, spec_filename: typing.Optional[str] = None
93+
) -> None:
9194
"""
9295
Validate schemas.
9396
9497
Args:
9598
schemas: The schemas to validate.
99+
spec_filename: The filename of the spec, used to cache the result.
96100
97101
"""
102+
if spec_filename is not None:
103+
if cache.schemas_valid(spec_filename):
104+
return
105+
98106
schemas_result = schemas_validation.check(schemas=schemas)
99107
if not schemas_result.valid:
100108
raise _exceptions.MalformedSchemaError(schemas_result.reason)
@@ -121,6 +129,9 @@ def process(*, schemas: _oa_types.Schemas) -> None:
121129
if not other_results_result.valid:
122130
raise _exceptions.MalformedSchemaError(other_results_result.reason)
123131

132+
if spec_filename is not None:
133+
cache.schemas_are_valid(spec_filename)
134+
124135

125136
def check_one_model(*, schemas: _oa_types.Schemas) -> types.Result:
126137
"""

tests/open_alchemy/schemas/validation/test_validation.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tests for validation rules."""
22

3+
import pathlib
4+
35
import pytest
46

57
from open_alchemy import exceptions
@@ -259,6 +261,27 @@ def test_process(schemas, raises):
259261
validation.process(schemas=schemas)
260262

261263

264+
def test_process_cache(tmpdir):
265+
"""
266+
GIVEN spec filename
267+
WHEN process is called with the schemas and filename twice
268+
THEN the schemas are not checked on the second run.
269+
"""
270+
tmpdir_path = pathlib.Path(tmpdir)
271+
spec_file = tmpdir_path / "spec.json"
272+
spec_file.write_text("spec 1", encoding="utf-8")
273+
schemas = {
274+
"Schema1": {
275+
"type": "object",
276+
"x-tablename": "schema_1",
277+
"properties": {"prop_1": {"type": "integer"}},
278+
}
279+
}
280+
281+
validation.process(schemas=schemas, spec_filename=str(spec_file))
282+
validation.process(schemas={}, spec_filename=str(spec_file))
283+
284+
262285
CHECK_TESTS = [
263286
pytest.param(
264287
True,

0 commit comments

Comments
 (0)