Skip to content

Commit

Permalink
Test of full desired functionality, make sure errors don't pass
Browse files Browse the repository at this point in the history
  • Loading branch information
uellue authored and sk1p committed Sep 3, 2024
1 parent 6df1891 commit f259dc1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 14 deletions.
7 changes: 3 additions & 4 deletions src/libertem_schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
BaseModel,
GetCoreSchemaHandler,
GetJsonSchemaHandler,
ValidationError,
AfterValidator,
BeforeValidator,
WrapValidator,
ValidationInfo,
ValidatorFunctionWrapHandler,
Expand Down Expand Up @@ -82,6 +79,7 @@ def _make_handler(dimensionality: str):
def is_matching(
q: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo
) -> pint.Quantity:
# Ensure target type
if isinstance(q, pint.Quantity):
pass
elif isinstance(q, Sequence):
Expand All @@ -90,9 +88,10 @@ def is_matching(
q = m * ureg(u)
else:
raise ValueError(f"Don't know how to interpret type {type(q)}.")

# Check dimension
if not q.check(dimensionality):
raise DimensionError(f"Expected dimensionality {dimensionality}, got quantity {q}.")
# Return target type
return q

return is_matching
Expand Down
115 changes: 105 additions & 10 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import pytest

from pint import UnitRegistry
from pint import UnitRegistry, Quantity
from pydantic_core import from_json
from pydantic import ValidationError

from libertem_schema import Simple4DSTEMParams, DimensionError
from libertem_schema import Simple4DSTEMParams

ureg = UnitRegistry()

Expand All @@ -17,32 +17,127 @@ def test_smoke():
scan_pixel_pitch=0.000001 * ureg.meter,
camera_length=0.15 * ureg.meter,
detector_pixel_pitch=0.000050 * ureg.meter,
semiconv=0.020 * ureg.radian, # rad
semiconv=0.020 * ureg.radian,
scan_rotation=330. * ureg.degree,
flip_y=False,
# Offset to avoid subchip gap in butted detectors
cy=(32 - 2) * ureg.pixel,
cx=(32 - 2) * ureg.pixel,
)
as_json = params.model_dump_json()
print(as_json)
pprint.pprint(("as json", as_json))
from_j = from_json(as_json)
assert Simple4DSTEMParams.model_validate(from_j)
pprint.pprint(("from json", from_j))
res = Simple4DSTEMParams.model_validate(from_j)
pprint.pprint(("validated", res))
assert isinstance(res.overfocus, Quantity)
assert isinstance(res.flip_y, bool)
assert res == params


def test_missing():
with pytest.raises(ValidationError):
Simple4DSTEMParams(
# Missing!
# overfocus=0.0015 * ureg.meter,
scan_pixel_pitch=0.000001 * ureg.meter,
camera_length=0.15 * ureg.meter,
detector_pixel_pitch=0.000050 * ureg.meter,
semiconv=0.020 * ureg.radian,
scan_rotation=330. * ureg.degree,
flip_y=False,
cy=(32 - 2) * ureg.pixel,
cx=(32 - 2) * ureg.pixel,
)


def test_carrots():
with pytest.raises(ValidationError):
Simple4DSTEMParams(
overfocus=0.0015, # carrots
scan_pixel_pitch=0.000001 * ureg.meter,
camera_length=0.15 * ureg.meter,
detector_pixel_pitch=0.000050 * ureg.meter,
semiconv=0.020 * ureg.radian,
scan_rotation=330. * ureg.degree,
flip_y=False,
cy=(32 - 2) * ureg.pixel,
cx=(32 - 2) * ureg.pixel,
)


def test_dimensionality():
with pytest.raises(ValidationError):
Simple4DSTEMParams(
###
overfocus=0.0015 * ureg.degree, # mismatch
# dimensionality mismatch!
overfocus=0.0015 * ureg.degree,
###
scan_pixel_pitch=0.000001 * ureg.meter,
camera_length=0.15 * ureg.meter,
detector_pixel_pitch=0.000050 * ureg.meter,
semiconv=0.020 * ureg.radian, # rad
semiconv=0.020 * ureg.radian,
scan_rotation=330. * ureg.degree,
flip_y=False,
# Offset to avoid subchip gap in butted detectors
cy=(32 - 2) * ureg.pixel,
cx=(32 - 2) * ureg.pixel,
)


def test_json_1():
params = Simple4DSTEMParams(
overfocus=0.0015 * ureg.meter,
scan_pixel_pitch=0.000001 * ureg.meter,
camera_length=0.15 * ureg.meter,
detector_pixel_pitch=0.000050 * ureg.meter,
semiconv=0.020 * ureg.radian,
scan_rotation=330. * ureg.degree,
flip_y=False,
cy=(32 - 2) * ureg.pixel,
cx=(32 - 2) * ureg.pixel,
)
as_json = params.model_dump_json()
from_j = from_json(as_json)
# Mess up dimensionality
from_j['overfocus'][1] = 'degree'
with pytest.raises(ValidationError):
Simple4DSTEMParams.model_validate(from_j)


def test_json_2():
params = Simple4DSTEMParams(
overfocus=0.0015 * ureg.meter,
scan_pixel_pitch=0.000001 * ureg.meter,
camera_length=0.15 * ureg.meter,
detector_pixel_pitch=0.000050 * ureg.meter,
semiconv=0.020 * ureg.radian,
scan_rotation=330. * ureg.degree,
flip_y=False,
cy=(32 - 2) * ureg.pixel,
cx=(32 - 2) * ureg.pixel,
)
as_json = params.model_dump_json()
from_j = from_json(as_json)
# Mess up plain type representation of Quantity as (float, str)
from_j['overfocus'].append('hurz')
with pytest.raises(ValidationError):
Simple4DSTEMParams.model_validate(from_j)


def test_json_3():
params = Simple4DSTEMParams(
overfocus=0.0015 * ureg.meter,
scan_pixel_pitch=0.000001 * ureg.meter,
camera_length=0.15 * ureg.meter,
detector_pixel_pitch=0.000050 * ureg.meter,
semiconv=0.020 * ureg.radian,
scan_rotation=330. * ureg.degree,
flip_y=False,
# Offset to avoid subchip gap in butted detectors
cy=(32 - 2) * ureg.pixel,
cx=(32 - 2) * ureg.pixel,
)
as_json = params.model_dump_json()
from_j = from_json(as_json)
# Missing key
del from_j['overfocus']
with pytest.raises(ValidationError):
Simple4DSTEMParams.model_validate(from_j)

0 comments on commit f259dc1

Please sign in to comment.