@@ -378,21 +378,44 @@ def test_setting_without_event_loop():
378378
379379
380380@pytest .mark .parametrize ("prop_info" , CONSTRAINED_PROPS )
381- def test_constrained_properties (prop_info ):
382- """Test that constraints on property values generate correct models."""
381+ def test_constrained_properties (prop_info , mocker ):
382+ """Test that constraints on property values generate correct models.
383+
384+ This also tests the `validate` method and checks validation happens
385+ on assignment to the property in Python. Further checks over http
386+ are made in later tests.
387+ """
383388 prop = prop_info .prop
384389 assert prop .value_type is prop_info .value_type
385390 m = prop .model
386391 assert issubclass (m , RootModel )
392+ mock_thing = mocker .Mock (spec = PropertyTestThing )
393+ mock_thing ._thing_server_interface = mocker .Mock ()
394+ descriptorinfo = prop .descriptor_info (mock_thing )
395+ assert isinstance (descriptorinfo , PropertyInfo )
387396 for ann in prop_info .constraints :
388397 assert any (meta == ann for meta in m .model_fields ["root" ].metadata )
389398 for valid in prop_info .valid_values :
399+ # Check the model can be created
390400 instance = m (root = valid )
401+ # Check the value passes through the model
391402 validated = instance .model_dump ()
392403 assert validated == valid or validated is valid # `is` for NaN
404+ # Check the descriptorinfo object also validates
405+ # (this is what we get from thing.properties["name"])
406+ validated = descriptorinfo .validate (valid )
407+ assert validated == valid or validated is valid # `is` for NaN
408+ # Check that assignment works
409+ prop .__set__ (mock_thing , valid )
410+ validated = prop .__get__ (mock_thing )
411+ assert validated == valid or validated is valid # `is` for NaN
393412 for invalid in prop_info .invalid_values :
394413 with pytest .raises (ValidationError ):
395414 _ = m (root = invalid )
415+ with pytest .raises (ValidationError ):
416+ descriptorinfo .validate (invalid )
417+ with pytest .raises (ValidationError ):
418+ prop .__set__ (mock_thing , invalid )
396419
397420
398421def convert_inf_nan (value ):
0 commit comments