@@ -33,13 +33,30 @@ def test_ingredient_reassignment():
3333 assert set (frying .ingredients ) == {oil , potatoes }
3434
3535
36+ VALID_FRACTIONS = [
37+ NominalReal (1.0 , '' ),
38+ UniformReal (0.5 , 0.6 , '' ),
39+ NormalReal (0.2 , 0.3 , '' )
40+ ]
41+
42+ INVALID_FRACTIONS = [
43+ NominalReal (1.0 , 'm' ),
44+ UniformReal (0.7 , 1.1 , '' ),
45+ NormalReal (- 0.2 , 0.3 , '' )
46+ ]
47+
3648VALID_QUANTITIES = [
37- NominalReal (14.0 , '' ),
38- UniformReal (0.5 , 0.6 , 'm ' ),
39- NormalReal (- 0.3 , 0.6 , "kg" )
49+ NominalReal (14.0 , 'g ' ),
50+ UniformReal (0.5 , 0.6 , 'mol ' ),
51+ NormalReal (0.3 , 0.6 , 'cc' )
4052]
4153
4254INVALID_QUANTITIES = [
55+ NominalReal (14.0 , '' ),
56+ UniformReal (- 0.1 , 0.3 , 'mol' ),
57+ ]
58+
59+ INVALID_TYPES = [
4360 NominalCategorical ("blue" ),
4461 NominalInteger (5 ),
4562 EmpiricalFormula ("CH4" ),
@@ -48,55 +65,79 @@ def test_ingredient_reassignment():
4865]
4966
5067
68+ @pytest .mark .parametrize ("valid_fraction" , VALID_FRACTIONS )
69+ def test_valid_fractions (valid_fraction , caplog ):
70+ """
71+ Check that all fractional quantities must be continuous values.
72+ """
73+ with validation_level (WarningLevel .WARNING ):
74+ ingred = IngredientSpec (name = "name" , mass_fraction = valid_fraction )
75+ assert ingred .mass_fraction == valid_fraction
76+ ingred = IngredientSpec (name = "name" , volume_fraction = valid_fraction )
77+ assert ingred .volume_fraction == valid_fraction
78+ ingred = IngredientSpec (name = "name" , number_fraction = valid_fraction )
79+ assert ingred .number_fraction == valid_fraction
80+ assert ingred .absolute_quantity is None
81+ assert len (caplog .records ) == 0 , "Warned on valid values with WARNING."
82+
83+
5184@pytest .mark .parametrize ("valid_quantity" , VALID_QUANTITIES )
5285def test_valid_quantities (valid_quantity , caplog ):
5386 """
5487 Check that all quantities must be continuous values.
55-
56- There are no restrictions on the value or the units. Although a volume fraction of -5 kg
57- does not make physical sense, it will not throw an error.
5888 """
59- with validation_level (WarningLevel .IGNORE ):
60- ingred = IngredientSpec (name = "name" , mass_fraction = valid_quantity )
61- assert ingred .mass_fraction == valid_quantity
62- ingred = IngredientSpec (name = "name" , volume_fraction = valid_quantity )
63- assert ingred .volume_fraction == valid_quantity
64- ingred = IngredientSpec (name = "name" , number_fraction = valid_quantity )
65- assert ingred .number_fraction == valid_quantity
89+ with validation_level (WarningLevel .WARNING ):
6690 ingred = IngredientSpec (name = "name" , absolute_quantity = valid_quantity )
6791 assert ingred .absolute_quantity == valid_quantity
68- assert len (caplog .records ) == 0 , "Warned when validation set to IGNORE."
92+ assert ingred .mass_fraction is None
93+ assert ingred .number_fraction is None
94+ assert ingred .volume_fraction is None
95+ assert len (caplog .records ) == 0 , "Warned on valid values with WARNING."
6996
7097
71- def test_validation_control (caplog ):
72- """Verify that when validation is requested, limits are enforced."""
98+ @pytest .mark .parametrize ("invalid_fraction" , INVALID_FRACTIONS )
99+ def test_invalid_fractions (invalid_fraction , caplog ):
100+ """
101+ Verify that when validation is requested, limits are enforced for fractions.
102+ """
103+ with validation_level (WarningLevel .IGNORE ):
104+ IngredientSpec (name = "name" , mass_fraction = invalid_fraction )
105+ assert len (caplog .records ) == 0 , f"Warned on invalid values with IGNORE: { invalid_fraction } "
73106 with validation_level (WarningLevel .WARNING ):
74- IngredientSpec (name = "name" , mass_fraction = NominalReal (0.5 , '' ))
75- assert len (caplog .records ) == 0 , "Warned on valid values with WARNING."
76- IngredientSpec (name = "name" , mass_fraction = NominalReal (5 , '' ))
77- assert len (caplog .records ) == 1 , "Didn't warn on invalid values with WARNING."
78- IngredientSpec (name = "name" , mass_fraction = NominalReal (0.5 , 'm' ))
79- assert len (caplog .records ) == 2 , "Didn't warn on invalid units with WARNING."
107+ IngredientSpec (name = "name" , mass_fraction = invalid_fraction )
108+ assert len (caplog .records ) == 1 , f"Didn't warn on invalid values with IGNORE: { invalid_fraction } "
80109 with validation_level (WarningLevel .FATAL ):
81- # The following should not raise an exception
82- IngredientSpec (name = "name" , mass_fraction = NominalReal (0.5 , '' ))
83- with pytest .raises (ValueError ):
84- IngredientSpec (name = "name" , mass_fraction = NominalReal (5 , '' ))
85110 with pytest .raises (ValueError ):
86- IngredientSpec (name = "name" , mass_fraction = NominalReal ( 0.5 , 'm' ) )
111+ IngredientSpec (name = "name" , mass_fraction = invalid_fraction )
87112
88113
89114@pytest .mark .parametrize ("invalid_quantity" , INVALID_QUANTITIES )
90- def test_invalid_quantities (invalid_quantity ):
115+ def test_invalid_quantities (invalid_quantity , caplog ):
116+ """
117+ Verify that when validation is requested, limits are enforced for fractions.
118+ """
119+ with validation_level (WarningLevel .IGNORE ):
120+ IngredientSpec (name = "name" , absolute_quantity = invalid_quantity )
121+ assert len (caplog .records ) == 0 , f"Warned on invalid values with IGNORE: { invalid_quantity } "
122+ with validation_level (WarningLevel .WARNING ):
123+ IngredientSpec (name = "name" , absolute_quantity = invalid_quantity )
124+ assert len (caplog .records ) == 1 , f"Didn't warn on invalid values with IGNORE: { invalid_quantity } "
125+ with validation_level (WarningLevel .FATAL ):
126+ with pytest .raises (ValueError ):
127+ IngredientSpec (name = "name" , absolute_quantity = invalid_quantity )
128+
129+
130+ @pytest .mark .parametrize ("invalid_type" , INVALID_TYPES )
131+ def test_invalid_types (invalid_type ):
91132 """Check that any non-continuous value for a quantity throws a TypeError."""
92133 with pytest .raises (TypeError ):
93- IngredientSpec (name = "name" , mass_fraction = invalid_quantity )
134+ IngredientSpec (name = "name" , mass_fraction = invalid_type )
94135 with pytest .raises (TypeError ):
95- IngredientSpec (name = "name" , volume_fraction = invalid_quantity )
136+ IngredientSpec (name = "name" , volume_fraction = invalid_type )
96137 with pytest .raises (TypeError ):
97- IngredientSpec (name = "name" , number_fraction = invalid_quantity )
138+ IngredientSpec (name = "name" , number_fraction = invalid_type )
98139 with pytest .raises (TypeError ):
99- IngredientSpec (name = "name" , absolute_quantity = invalid_quantity )
140+ IngredientSpec (name = "name" , absolute_quantity = invalid_type )
100141
101142
102143def test_invalid_assignment ():
0 commit comments