@@ -742,16 +742,39 @@ async def test_select_fields() -> None:
742742 await item_from_select_fields (page )
743743 assert page .call_counter == {"y" : 2 }
744744
745- # Boolean-like values are not supported. They are simply ignored and the
746- # page would revert back to the default field directives.
745+ # Boolean-like values are not supported.
746+ expected_non_boolean_value_error_msg = (
747+ "SelectField only allows boolean values as keys. "
748+ "Got: {'x': 0, 'y': 0, 'z': 1}"
749+ )
747750 page = BigPage (
748751 response ,
749752 select_fields = SelectFields ({"x" : 0 , "y" : 0 , "z" : 1 }), # type: ignore[dict-item]
750753 )
751- assert page .fields_to_extract == ["x" , "y" ]
752- assert await page .to_item () == BigItem (x = 1 , y = 2 , z = None )
753- assert await item_from_select_fields (page ) == BigItem (x = 1 , y = 2 , z = None )
754- assert page .call_counter == {"x" : 2 , "y" : 2 }
754+ with pytest .raises (ValueError , match = expected_non_boolean_value_error_msg ):
755+ page .fields_to_extract
756+ with pytest .raises (ValueError , match = expected_non_boolean_value_error_msg ):
757+ await page .to_item ()
758+ with pytest .raises (ValueError , match = expected_non_boolean_value_error_msg ):
759+ await item_from_select_fields (page )
760+ assert page .call_counter == {}
761+
762+ # If an invalid SelectFields value was passed to `select_fields` parameter
763+ expected_invalid_instance_value_error_msg = (
764+ r"The select_fields.fields parameter is expecting a Mapping. "
765+ r'Got SelectFields\(fields="not the instance it\'s expecting"\).'
766+ )
767+ page = BigPage (
768+ response ,
769+ select_fields = "not the instance it's expecting" , # type: ignore[arg-type]
770+ )
771+ with pytest .raises (ValueError , match = expected_invalid_instance_value_error_msg ):
772+ page .fields_to_extract
773+ with pytest .raises (ValueError , match = expected_invalid_instance_value_error_msg ):
774+ await page .to_item ()
775+ with pytest .raises (ValueError , match = expected_invalid_instance_value_error_msg ):
776+ await item_from_select_fields (page )
777+ assert page .call_counter == {}
755778
756779 # If the item class doesn't have a field, it would error out.
757780 fields = {"x" : True , "not_existing" : True }
@@ -857,15 +880,35 @@ async def test_select_fields_but_to_item_only() -> None:
857880 with pytest .raises (TypeError , match = expected_type_error_msg ):
858881 await item_from_select_fields (page )
859882
860- # Boolean-like values are not supported. They are simply ignored and the
861- # page would revert back to the default field directives.
883+ # Boolean-like values are not supported.
884+ expected_non_boolean_value_error_msg = (
885+ "SelectField only allows boolean values as keys. "
886+ "Got: {'x': 0, 'y': 0, 'z': 1}"
887+ )
862888 page = BigToItemOnlyPage (
863889 response ,
864890 select_fields = SelectFields ({"x" : 0 , "y" : 0 , "z" : 1 }), # type: ignore[dict-item]
865891 )
866- assert page .fields_to_extract == []
892+ with pytest .raises (ValueError , match = expected_non_boolean_value_error_msg ):
893+ page .fields_to_extract
867894 assert await page .to_item () == BigItem (x = 1 , y = 2 , z = None )
868- assert await item_from_select_fields (page ) == BigItem (x = 1 , y = 2 , z = None )
895+ with pytest .raises (ValueError , match = expected_non_boolean_value_error_msg ):
896+ await item_from_select_fields (page )
897+
898+ # If an invalid SelectFields value was passed to `select_fields` parameter
899+ expected_invalid_instance_value_error_msg = (
900+ r"The select_fields.fields parameter is expecting a Mapping. "
901+ r'Got SelectFields\(fields="not the instance it\'s expecting"\).'
902+ )
903+ page = BigToItemOnlyPage (
904+ response ,
905+ select_fields = "not the instance it's expecting" , # type: ignore[arg-type]
906+ )
907+ with pytest .raises (ValueError , match = expected_invalid_instance_value_error_msg ):
908+ page .fields_to_extract
909+ assert await page .to_item () == BigItem (x = 1 , y = 2 , z = None )
910+ with pytest .raises (ValueError , match = expected_invalid_instance_value_error_msg ):
911+ await item_from_select_fields (page )
869912
870913 # If the item class doesn't have a field, it would error out.
871914 fields = {"x" : True , "not_existing" : True }
@@ -999,17 +1042,37 @@ async def test_select_fields_but_unreliable() -> None:
9991042 await item_from_select_fields (page )
10001043 assert page .call_counter == {"x" : 2 , "z" : 2 }
10011044
1002- # Boolean-like values are not supported. They are simply ignored and the
1003- # page would revert back to the default field directives.
1045+ # Boolean-like values are not supported.
1046+ expected_non_boolean_value_error_msg = (
1047+ "SelectField only allows boolean values as keys. "
1048+ "Got: {'x': 0, 'y': 0, 'z': 1}"
1049+ )
10041050 page = BigUnreliablePage (
10051051 response ,
10061052 select_fields = SelectFields ({"x" : 0 , "y" : 0 , "z" : 1 }), # type: ignore[dict-item]
10071053 )
1008- assert page .fields_to_extract == ["x" ]
1054+ with pytest .raises (ValueError , match = expected_non_boolean_value_error_msg ):
1055+ page .fields_to_extract
1056+ assert await page .to_item () == BigItem (x = 1 , y = 2 , z = 3 )
1057+ with pytest .raises (ValueError , match = expected_non_boolean_value_error_msg ):
1058+ await item_from_select_fields (page )
1059+ assert page .call_counter == {"x" : 1 , "z" : 1 }
1060+
1061+ # If an invalid SelectFields value was passed to `select_fields` parameter
1062+ expected_invalid_instance_value_error_msg = (
1063+ r"The select_fields.fields parameter is expecting a Mapping. "
1064+ r'Got SelectFields\(fields="not the instance it\'s expecting"\).'
1065+ )
1066+ page = BigUnreliablePage (
1067+ response ,
1068+ select_fields = "not the instance it's expecting" , # type: ignore[arg-type]
1069+ )
1070+ with pytest .raises (ValueError , match = expected_invalid_instance_value_error_msg ):
1071+ page .fields_to_extract
10091072 assert await page .to_item () == BigItem (x = 1 , y = 2 , z = 3 )
1073+ with pytest .raises (ValueError , match = expected_invalid_instance_value_error_msg ):
1074+ await item_from_select_fields (page )
10101075 assert page .call_counter == {"x" : 1 , "z" : 1 }
1011- assert await item_from_select_fields (page ) == BigItem (x = 1 , y = None , z = None )
1012- assert page .call_counter == {"x" : 2 , "z" : 1 }
10131076
10141077 # If the item class doesn't have a field, it would error out.
10151078 fields = {"x" : True , "not_existing" : True }
0 commit comments