cedarkit-query 0.2.1__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cedarkit_query-0.2.1/src/cedarkit_query.egg-info → cedarkit_query-0.2.2}/PKG-INFO +1 -1
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/pyproject.toml +1 -1
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/__init__.py +2 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/model.py +39 -12
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/resources.py +10 -3
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2/src/cedarkit_query.egg-info}/PKG-INFO +1 -1
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/tests/test_resources.py +113 -4
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/LICENSE +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/MANIFEST.in +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/README.md +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/docs/normalization-v1.md +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/docs/protocol-v1.md +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/docs/using-v1.md +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/examples/catalog.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/examples/request.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/scripts/build_schemas.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/scripts/check_dist.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/setup.cfg +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/_json.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/errors.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/normalize.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/parameters.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/catalog-v1.schema.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/common-v1.schema.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/context-v1.schema.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/error-v1.schema.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/explanation-v1.schema.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/mappings-v1.schema.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/parameters-v1.schema.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/query-v1.schema.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/report-v1.schema.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/request-v1.schema.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/validation.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query.egg-info/SOURCES.txt +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query.egg-info/dependency_links.txt +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query.egg-info/requires.txt +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query.egg-info/top_level.txt +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/tests/conftest.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/tests/test_normalize.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/tests/test_parameters.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/tests/test_protocol.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/tests/test_scopes.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/tests/test_vectors.py +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/tests/vectors/v1/invalid.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/tests/vectors/v1/normalization.json +0 -0
- {cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/tests/vectors/v1/valid.json +0 -0
|
@@ -13,6 +13,7 @@ from .model import (
|
|
|
13
13
|
Request,
|
|
14
14
|
dump_value,
|
|
15
15
|
get_schema,
|
|
16
|
+
load_prevalidated_resource,
|
|
16
17
|
load_request,
|
|
17
18
|
load_value,
|
|
18
19
|
validate_value,
|
|
@@ -45,6 +46,7 @@ __all__ = [
|
|
|
45
46
|
"validate_resource_set",
|
|
46
47
|
"Context",
|
|
47
48
|
"load_value",
|
|
49
|
+
"load_prevalidated_resource",
|
|
48
50
|
"load_request",
|
|
49
51
|
"dump_value",
|
|
50
52
|
"get_schema",
|
|
@@ -332,20 +332,24 @@ def _format(path):
|
|
|
332
332
|
return "json" if suffix == ".json" else "yaml"
|
|
333
333
|
|
|
334
334
|
|
|
335
|
+
def _parse_text(text, kind, format):
|
|
336
|
+
try:
|
|
337
|
+
return parse(text, format)
|
|
338
|
+
except (ValueError, YAMLError) as exc:
|
|
339
|
+
raise QueryError(
|
|
340
|
+
"invalid_request"
|
|
341
|
+
if kind == "request"
|
|
342
|
+
else "invalid_query"
|
|
343
|
+
if kind == "query"
|
|
344
|
+
else "invalid_config",
|
|
345
|
+
"",
|
|
346
|
+
str(exc),
|
|
347
|
+
) from exc
|
|
348
|
+
|
|
349
|
+
|
|
335
350
|
def load_value(value, kind, *, format=None, base_dir=None):
|
|
336
351
|
if isinstance(value, str):
|
|
337
|
-
|
|
338
|
-
value = parse(value, format)
|
|
339
|
-
except (ValueError, YAMLError) as exc:
|
|
340
|
-
raise QueryError(
|
|
341
|
-
"invalid_request"
|
|
342
|
-
if kind == "request"
|
|
343
|
-
else "invalid_query"
|
|
344
|
-
if kind == "query"
|
|
345
|
-
else "invalid_config",
|
|
346
|
-
"",
|
|
347
|
-
str(exc),
|
|
348
|
-
) from exc
|
|
352
|
+
value = _parse_text(value, kind, format)
|
|
349
353
|
elif format is not None:
|
|
350
354
|
raise ValueError("format is only valid for text input")
|
|
351
355
|
return (
|
|
@@ -355,6 +359,29 @@ def load_value(value, kind, *, format=None, base_dir=None):
|
|
|
355
359
|
)
|
|
356
360
|
|
|
357
361
|
|
|
362
|
+
def load_prevalidated_resource(text, kind):
|
|
363
|
+
"""Load packaged resource JSON previously validated with `load_value`.
|
|
364
|
+
|
|
365
|
+
The caller selects a trusted catalog, parameter registry, or mapping
|
|
366
|
+
resource validated against this protocol version. Return the same immutable
|
|
367
|
+
model as `load_value`, with JSON parsing and the protocol version check.
|
|
368
|
+
"""
|
|
369
|
+
if kind not in {"catalog", "parameters", "mappings"}:
|
|
370
|
+
raise ValueError(f"unknown resource kind: {kind}")
|
|
371
|
+
if not isinstance(text, str):
|
|
372
|
+
raise TypeError("prevalidated resource requires JSON text")
|
|
373
|
+
value = _parse_text(text, kind, "json")
|
|
374
|
+
if not isinstance(value, dict):
|
|
375
|
+
raise QueryError("invalid_config", "", "resource must be a JSON object")
|
|
376
|
+
if value.get("api_version") != VERSIONS[kind]:
|
|
377
|
+
raise QueryError(
|
|
378
|
+
"unsupported_version", "/api_version", "unsupported format version"
|
|
379
|
+
)
|
|
380
|
+
result = object.__new__(MODELS[kind])
|
|
381
|
+
object.__setattr__(result, "_value", freeze(value))
|
|
382
|
+
return result
|
|
383
|
+
|
|
384
|
+
|
|
358
385
|
def load_request(value, *, format=None, base_dir=None):
|
|
359
386
|
return load_value(value, "request", format=format, base_dir=base_dir)
|
|
360
387
|
|
|
@@ -146,7 +146,8 @@ def validate_resource_set(parameters, mappings, catalog, dataset_id):
|
|
|
146
146
|
from .model import Catalog, Mappings
|
|
147
147
|
|
|
148
148
|
effective = effective_parameters(parameters)
|
|
149
|
-
|
|
149
|
+
catalog = catalog if type(catalog) is Catalog else Catalog(catalog)
|
|
150
|
+
datasets = {row["id"]: row for row in catalog.to_dict()["datasets"]}
|
|
150
151
|
if dataset_id not in datasets:
|
|
151
152
|
_error("/datasets", "dataset is absent from catalog")
|
|
152
153
|
products = {
|
|
@@ -167,7 +168,12 @@ def validate_resource_set(parameters, mappings, catalog, dataset_id):
|
|
|
167
168
|
_error(f"/datasets/products/{key}", "unknown catalog parameter ID")
|
|
168
169
|
|
|
169
170
|
resources = _index(
|
|
170
|
-
[
|
|
171
|
+
[
|
|
172
|
+
(value if type(value) is Mappings else Mappings(value)).to_dict()
|
|
173
|
+
for value in mappings
|
|
174
|
+
],
|
|
175
|
+
"backend",
|
|
176
|
+
"/mappings",
|
|
171
177
|
)
|
|
172
178
|
applicable = {identity: [] for identity in products}
|
|
173
179
|
for resource in resources.values():
|
|
@@ -265,4 +271,5 @@ def effective_parameters(parameters):
|
|
|
265
271
|
"""Expand validated parameter definitions for binding."""
|
|
266
272
|
from .model import Parameters
|
|
267
273
|
|
|
268
|
-
|
|
274
|
+
parameters = parameters if type(parameters) is Parameters else Parameters(parameters)
|
|
275
|
+
return _effective(parameters.to_dict())
|
|
@@ -1,5 +1,17 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
1
3
|
import pytest
|
|
2
|
-
from cedarkit_query import
|
|
4
|
+
from cedarkit_query import (
|
|
5
|
+
Catalog,
|
|
6
|
+
Mappings,
|
|
7
|
+
Parameters,
|
|
8
|
+
QueryError,
|
|
9
|
+
load_prevalidated_resource,
|
|
10
|
+
load_value,
|
|
11
|
+
prepare_queries,
|
|
12
|
+
validate_resource_set,
|
|
13
|
+
)
|
|
14
|
+
from cedarkit_query import model
|
|
3
15
|
from cedarkit_query.resources import effective_parameters
|
|
4
16
|
|
|
5
17
|
BASE = {"parameter_id": "cedarkit.t", "name": "t", "unit": "K"}
|
|
@@ -21,6 +33,52 @@ def snapshot(rows):
|
|
|
21
33
|
}
|
|
22
34
|
|
|
23
35
|
|
|
36
|
+
def test_prevalidated_resources_match_validated_models(catalog, monkeypatch):
|
|
37
|
+
resources = {
|
|
38
|
+
"catalog": catalog,
|
|
39
|
+
"parameters": snapshot([BASE, VARIANT]),
|
|
40
|
+
"mappings": {
|
|
41
|
+
"api_version": "cedarkit.mappings/v1",
|
|
42
|
+
"id": "test.grib2",
|
|
43
|
+
"version": "1",
|
|
44
|
+
"backend": "grib2",
|
|
45
|
+
"tables": [],
|
|
46
|
+
"mappings": [],
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
expected = {kind: load_value(value, kind) for kind, value in resources.items()}
|
|
50
|
+
|
|
51
|
+
def reject_validation(*args):
|
|
52
|
+
pytest.fail("prevalidated resource ran the full validator")
|
|
53
|
+
|
|
54
|
+
monkeypatch.setattr(model, "validate_value", reject_validation)
|
|
55
|
+
for kind, value in resources.items():
|
|
56
|
+
resource = load_prevalidated_resource(json.dumps(value), kind)
|
|
57
|
+
assert resource == expected[kind]
|
|
58
|
+
loaded = resource.to_dict()
|
|
59
|
+
loaded["id"] = "changed"
|
|
60
|
+
assert resource == expected[kind]
|
|
61
|
+
with pytest.raises(AttributeError):
|
|
62
|
+
resource.id = "changed"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@pytest.mark.parametrize(
|
|
66
|
+
"text,code",
|
|
67
|
+
[
|
|
68
|
+
("[]", "invalid_config"),
|
|
69
|
+
('{"api_version":"cedarkit.catalog/v2"}', "unsupported_version"),
|
|
70
|
+
(
|
|
71
|
+
'{"api_version":"cedarkit.catalog/v1","api_version":"cedarkit.catalog/v1"}',
|
|
72
|
+
"invalid_config",
|
|
73
|
+
),
|
|
74
|
+
],
|
|
75
|
+
)
|
|
76
|
+
def test_prevalidated_resource_checks_json_and_protocol_version(text, code):
|
|
77
|
+
with pytest.raises(QueryError) as caught:
|
|
78
|
+
load_prevalidated_resource(text, "catalog")
|
|
79
|
+
assert caught.value.code == code
|
|
80
|
+
|
|
81
|
+
|
|
24
82
|
def test_parameters_inherit_without_depending_on_record_order():
|
|
25
83
|
parameters = Parameters(snapshot([VARIANT, BASE]))
|
|
26
84
|
index = effective_parameters(parameters)
|
|
@@ -29,6 +87,60 @@ def test_parameters_inherit_without_depending_on_record_order():
|
|
|
29
87
|
assert query.to_dict() == {"parameter": "cedarkit.t2m", **HEIGHT}
|
|
30
88
|
|
|
31
89
|
|
|
90
|
+
def test_resource_set_reuses_validated_models(monkeypatch):
|
|
91
|
+
parameters = Parameters(snapshot([BASE, VARIANT]))
|
|
92
|
+
product = {
|
|
93
|
+
"id": "original",
|
|
94
|
+
"version": "1",
|
|
95
|
+
"scope": {},
|
|
96
|
+
"provides": [{"parameter": "cedarkit.t2m"}],
|
|
97
|
+
"populations": [],
|
|
98
|
+
"methods": [],
|
|
99
|
+
"evidence": [],
|
|
100
|
+
"sources": [{"id": "file", "kind": "file"}],
|
|
101
|
+
}
|
|
102
|
+
catalog = Catalog(
|
|
103
|
+
{
|
|
104
|
+
"api_version": "cedarkit.catalog/v1",
|
|
105
|
+
"id": "test.catalog",
|
|
106
|
+
"version": "1",
|
|
107
|
+
"datasets": [{"id": "test", "products": [product]}],
|
|
108
|
+
}
|
|
109
|
+
)
|
|
110
|
+
mapping = {
|
|
111
|
+
"id": "t2m",
|
|
112
|
+
"parameter_id": "cedarkit.t2m",
|
|
113
|
+
"backend": "grib2",
|
|
114
|
+
"applies_to": {},
|
|
115
|
+
"when": {},
|
|
116
|
+
"evidence": [],
|
|
117
|
+
"native": {"match": {"shortName": "2t"}},
|
|
118
|
+
}
|
|
119
|
+
mappings = Mappings(
|
|
120
|
+
{
|
|
121
|
+
"api_version": "cedarkit.mappings/v1",
|
|
122
|
+
"id": "test.grib2",
|
|
123
|
+
"version": "1",
|
|
124
|
+
"backend": "grib2",
|
|
125
|
+
"tables": [],
|
|
126
|
+
"mappings": [mapping],
|
|
127
|
+
}
|
|
128
|
+
)
|
|
129
|
+
expected = validate_resource_set(
|
|
130
|
+
parameters.to_dict(), [mappings.to_dict()], catalog.to_dict(), "test"
|
|
131
|
+
)
|
|
132
|
+
validate_value = model.validate_value
|
|
133
|
+
|
|
134
|
+
def reject_revalidation(value, kind):
|
|
135
|
+
assert kind not in {"parameters", "catalog", "mappings"}
|
|
136
|
+
return validate_value(value, kind)
|
|
137
|
+
|
|
138
|
+
monkeypatch.setattr(model, "validate_value", reject_revalidation)
|
|
139
|
+
assert validate_resource_set(parameters, [mappings], catalog, "test") == expected
|
|
140
|
+
with pytest.raises(QueryError, match="dataset is absent"):
|
|
141
|
+
validate_resource_set(parameters, [mappings], catalog, "missing")
|
|
142
|
+
|
|
143
|
+
|
|
32
144
|
@pytest.mark.parametrize(
|
|
33
145
|
"rows,path",
|
|
34
146
|
[
|
|
@@ -109,10 +221,7 @@ def test_time_statistic_can_inherit_operation_and_refine_period():
|
|
|
109
221
|
],
|
|
110
222
|
)
|
|
111
223
|
def test_service_statistics_config_has_the_same_three_resource_forms(config):
|
|
112
|
-
import json
|
|
113
|
-
|
|
114
224
|
import yaml
|
|
115
|
-
from cedarkit_query import Mappings, load_value
|
|
116
225
|
|
|
117
226
|
resource = {
|
|
118
227
|
"api_version": "cedarkit.mappings/v1",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/catalog-v1.schema.json
RENAMED
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/common-v1.schema.json
RENAMED
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/context-v1.schema.json
RENAMED
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/error-v1.schema.json
RENAMED
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/explanation-v1.schema.json
RENAMED
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/mappings-v1.schema.json
RENAMED
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/parameters-v1.schema.json
RENAMED
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/query-v1.schema.json
RENAMED
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/report-v1.schema.json
RENAMED
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query/schemas/request-v1.schema.json
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{cedarkit_query-0.2.1 → cedarkit_query-0.2.2}/src/cedarkit_query.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|