Skip to content

Commit

Permalink
Merge pull request #9 from ways/master
Browse files Browse the repository at this point in the history
Allow CompactAxis in check_axis
  • Loading branch information
lukas-phaf authored Jan 16, 2024
2 parents e74abaa + e38ea22 commit 08b5445
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
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"
]
}
}
}

0 comments on commit 08b5445

Please sign in to comment.