Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow CompactAxis in check_axis #9

Merged
merged 10 commits into from
Jan 16, 2024
18 changes: 14 additions & 4 deletions src/covjson_pydantic/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ def check_axis(domain_type, axes, required_axes, allowed_axes, single_value_axes
if axis is None:
raise ValueError(f"A '{domain_type.value}' must have a '{axis_name}'-axis.")
if axis_name in single_value_axes:
if not (isinstance(axis, ValuesAxis) and len(axis.values) == 1):
if isinstance(axis, ValuesAxis) and len(axis.values) != 1:
raise ValueError(
f"The 'values' field of the '{axis_name}'-axis "
f"The 'values' field of the ValuesAxis '{axis_name}'-axis "
f"of a '{domain_type.value}' domain must contain a single value."
)
if isinstance(axis, CompactAxis) and axis.num != 1:
raise ValueError(
f"The 'num' field of the CompactAxis '{axis_name}'-axis "
f"of a '{domain_type.value}' domain must be 1."
)

# Check allowed axes
all_axis = {"x", "y", "z", "t", "composite"}
Expand All @@ -110,11 +115,16 @@ def check_axis(domain_type, axes, required_axes, allowed_axes, single_value_axes
for axis_name in allowed_axes:
axis = getattr(axes, axis_name)
if axis is not None and axis_name in single_value_axes:
if not (isinstance(axis, ValuesAxis) and len(axis.values) == 1):
if isinstance(axis, ValuesAxis) and len(axis.values) != 1:
raise ValueError(
f"If provided, the 'values' field of the '{axis_name}'-axis "
f"If provided, the 'values' field of the ValuesAxis '{axis_name}'-axis "
f"of a '{domain_type.value}' domain must contain a single value."
)
if isinstance(axis, CompactAxis) and axis.num != 1:
raise ValueError(
f"If provided, the 'num' field of the CompactAxis '{axis_name}'-axis "
f"of a '{domain_type.value}' domain must be 1."
)

@model_validator(mode="after")
def check_domain_consistent(self):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
("spec-domain-vertical-profile.json", Domain),
("spec-domain-point-series.json", Domain),
("spec-domain-point.json", Domain),
("spec-domain-point-compact.json", Domain),
("spec-domain-multipoint-series.json", Domain),
("spec-domain-multipoint.json", Domain),
("ndarray-float.json", NdArray),
Expand Down Expand Up @@ -55,7 +56,8 @@ def test_happy_cases(file_name, object_type):
(
"point-series-domain-more-z.json",
Domain,
r"If provided, the 'values' field of the 'z'-axis of a 'PointSeries' domain must contain a single value.",
r"If provided, the 'values' field of the ValuesAxis 'z'-axis of a 'PointSeries' "
+ "domain must contain a single value.",
),
("point-series-domain-no-t.json", Domain, r"A 'PointSeries' must have a 't'-axis."),
]
Expand Down
26 changes: 26 additions & 0 deletions tests/test_data/spec-domain-point-compact.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "Domain",
"domainType": "Point",
"axes": {
"x": {
"start": 1.0,
"stop": 1.0,
"num": 1
},
"y": {
"start": 20.0,
"stop": 20.0,
"num": 1
},
"z": {
"start": 1.8,
"stop": 1.8,
"num": 1
},
"t": {
"values": [
"2008-01-01T04:00:00Z"
]
}
}
}