Skip to content

Commit 68d2cc2

Browse files
committed
update item_from_select_fields() to always call .to_item()
1 parent c7f6f4a commit 68d2cc2

File tree

3 files changed

+114
-108
lines changed

3 files changed

+114
-108
lines changed

tests/test_fields.py

+50-50
Original file line numberDiff line numberDiff line change
@@ -672,37 +672,37 @@ async def test_select_fields() -> None:
672672

673673
# When SelectFields isn't set
674674
page = BigPage(response)
675-
assert page.fields_to_extract == ["x", "y"]
675+
assert page.fields_to_ignore == ["z"]
676676
assert await page.to_item() == BigItem(x=1, y=2, z=None)
677677
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=None)
678678
assert page.call_counter == {"x": 2, "y": 2}
679679

680680
# If no field selection directive is given but SelectFields is set, it would
681681
# use the default fields that are not disabled.
682682
page = BigPage(response, select_fields=SelectFields(None))
683-
assert page.fields_to_extract == ["x", "y"]
683+
assert page.fields_to_ignore == ["z"]
684684
assert await page.to_item() == BigItem(x=1, y=2, z=None)
685685
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=None)
686686
assert page.call_counter == {"x": 2, "y": 2}
687687

688688
# Same case as above but given an empty dict
689689
page = BigPage(response, select_fields=SelectFields({}))
690-
assert page.fields_to_extract == ["x", "y"]
690+
assert page.fields_to_ignore == ["z"]
691691
assert await page.to_item() == BigItem(x=1, y=2, z=None)
692692
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=None)
693693
assert page.call_counter == {"x": 2, "y": 2}
694694

695695
# Select all fields
696696
page = BigPage(response, select_fields=SelectFields({"*": True}))
697-
assert page.fields_to_extract == ["x", "y", "z"]
697+
assert page.fields_to_ignore == []
698698
assert await page.to_item() == BigItem(x=1, y=2, z=3)
699699
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=3)
700700
assert page.call_counter == {"x": 2, "y": 2, "z": 2}
701701

702702
# Don't select all fields; but in this case a TypeError is raised since
703703
# required fields aren't supplied to the item
704704
page = BigPage(response, select_fields=SelectFields({"*": False}))
705-
assert page.fields_to_extract == []
705+
assert page.fields_to_ignore == ["x", "y", "z"]
706706
with pytest.raises(TypeError, match=expected_type_error_msg):
707707
await page.to_item()
708708
with pytest.raises(TypeError, match=expected_type_error_msg):
@@ -711,14 +711,14 @@ async def test_select_fields() -> None:
711711

712712
# Exclude all but one (which is the required field in the item)
713713
page = BigPage(response, select_fields=SelectFields({"*": False, "x": True}))
714-
assert page.fields_to_extract == ["x"]
714+
assert page.fields_to_ignore == ["y", "z"]
715715
assert await page.to_item() == BigItem(x=1, y=None, z=None)
716716
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=None)
717717
assert page.call_counter == {"x": 2}
718718

719719
# Include all fields but one
720720
page = BigPage(response, select_fields=SelectFields({"*": True, "y": False}))
721-
assert page.fields_to_extract == ["x", "z"]
721+
assert page.fields_to_ignore == ["y"]
722722
assert await page.to_item() == BigItem(x=1, y=None, z=3)
723723
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=3)
724724
assert page.call_counter == {"x": 2, "z": 2}
@@ -728,14 +728,14 @@ async def test_select_fields() -> None:
728728
response,
729729
select_fields=SelectFields({"*": True, "x": True, "y": True, "z": True}),
730730
)
731-
assert page.fields_to_extract == ["x", "y", "z"]
731+
assert page.fields_to_ignore == []
732732
assert await page.to_item() == BigItem(x=1, y=2, z=3)
733733
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=3)
734734
assert page.call_counter == {"x": 2, "y": 2, "z": 2}
735735

736736
# Excluding a required field throws an error
737737
page = BigPage(response, select_fields=SelectFields({"x": False}))
738-
assert page.fields_to_extract == ["y"]
738+
assert page.fields_to_ignore == ["x", "z"]
739739
with pytest.raises(TypeError, match=expected_type_error_msg):
740740
await page.to_item()
741741
with pytest.raises(TypeError, match=expected_type_error_msg):
@@ -752,7 +752,7 @@ async def test_select_fields() -> None:
752752
select_fields=SelectFields({"x": 0, "y": 0, "z": 1}), # type: ignore[dict-item]
753753
)
754754
with pytest.raises(ValueError, match=expected_non_boolean_value_error_msg):
755-
page.fields_to_extract
755+
page.fields_to_ignore
756756
with pytest.raises(ValueError, match=expected_non_boolean_value_error_msg):
757757
await page.to_item()
758758
with pytest.raises(ValueError, match=expected_non_boolean_value_error_msg):
@@ -769,7 +769,7 @@ async def test_select_fields() -> None:
769769
select_fields="not the instance it's expecting", # type: ignore[arg-type]
770770
)
771771
with pytest.raises(ValueError, match=expected_invalid_instance_value_error_msg):
772-
page.fields_to_extract
772+
page.fields_to_ignore
773773
with pytest.raises(ValueError, match=expected_invalid_instance_value_error_msg):
774774
await page.to_item()
775775
with pytest.raises(ValueError, match=expected_invalid_instance_value_error_msg):
@@ -785,7 +785,7 @@ async def test_select_fields() -> None:
785785
)
786786
page = BigPage(response, select_fields=SelectFields(fields))
787787
with pytest.raises(ValueError, match=expected_value_error_msg):
788-
assert page.fields_to_extract
788+
page.fields_to_ignore
789789
with pytest.raises(ValueError, match=expected_value_error_msg):
790790
await page.to_item()
791791
with pytest.raises(ValueError, match=expected_value_error_msg):
@@ -804,7 +804,7 @@ async def test_select_fields_but_to_item_only() -> None:
804804
``.to_item()`` method and doesn't use the ``@field`` decorators at all.
805805
806806
For the different scenarios in this test, these are consistent:
807-
- ``.fields_to_extract`` returns an empty list.
807+
- ``.fields_to_ignore`` returns an empty list.
808808
- ``.to_item()`` is unaffected by the passed ``SelectFields`` since it
809809
doesn't take it into account as it simply returns the item instance.
810810
"""
@@ -817,33 +817,33 @@ async def test_select_fields_but_to_item_only() -> None:
817817
# When SelectFields isn't set, it should simply extract the non-disabled
818818
# fields.
819819
page = BigToItemOnlyPage(response)
820-
assert page.fields_to_extract == []
820+
assert page.fields_to_ignore == []
821821
assert await page.to_item() == BigItem(x=1, y=2, z=None)
822822
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=None)
823823

824824
# If no field selection directive is given but SelectFields is set, it would
825825
# use the default fields that are not disabled.
826826
page = BigToItemOnlyPage(response, select_fields=SelectFields(None))
827-
assert page.fields_to_extract == []
827+
assert page.fields_to_ignore == []
828828
assert await page.to_item() == BigItem(x=1, y=2, z=None)
829829
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=None)
830830

831831
# Same case as above but given an empty dict
832832
page = BigToItemOnlyPage(response, select_fields=SelectFields({}))
833-
assert page.fields_to_extract == []
833+
assert page.fields_to_ignore == []
834834
assert await page.to_item() == BigItem(x=1, y=2, z=None)
835835
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=None)
836836

837837
# Select all fields
838838
page = BigToItemOnlyPage(response, select_fields=SelectFields({"*": True}))
839-
assert page.fields_to_extract == []
839+
assert page.fields_to_ignore == []
840840
assert await page.to_item() == BigItem(x=1, y=2, z=None)
841841
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=None)
842842

843843
# Don't select all fields; but in this case a TypeError is raised since
844844
# required fields aren't supplied to the item
845845
page = BigToItemOnlyPage(response, select_fields=SelectFields({"*": False}))
846-
assert page.fields_to_extract == []
846+
assert page.fields_to_ignore == []
847847
assert await page.to_item() == BigItem(x=1, y=2, z=None)
848848
with pytest.raises(TypeError, match=expected_type_error_msg):
849849
await item_from_select_fields(page)
@@ -852,15 +852,15 @@ async def test_select_fields_but_to_item_only() -> None:
852852
page = BigToItemOnlyPage(
853853
response, select_fields=SelectFields({"*": False, "x": True})
854854
)
855-
assert page.fields_to_extract == []
855+
assert page.fields_to_ignore == []
856856
assert await page.to_item() == BigItem(x=1, y=2, z=None)
857857
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=None)
858858

859859
# Include all fields but one
860860
page = BigToItemOnlyPage(
861861
response, select_fields=SelectFields({"*": True, "y": False})
862862
)
863-
assert page.fields_to_extract == []
863+
assert page.fields_to_ignore == []
864864
assert await page.to_item() == BigItem(x=1, y=2, z=None)
865865
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=None)
866866

@@ -869,13 +869,13 @@ async def test_select_fields_but_to_item_only() -> None:
869869
response,
870870
select_fields=SelectFields({"*": True, "x": True, "y": True, "z": True}),
871871
)
872-
assert page.fields_to_extract == []
872+
assert page.fields_to_ignore == []
873873
assert await page.to_item() == BigItem(x=1, y=2, z=None)
874874
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=None)
875875

876876
# Excluding a required field throws an error
877877
page = BigToItemOnlyPage(response, select_fields=SelectFields({"x": False}))
878-
assert page.fields_to_extract == []
878+
assert page.fields_to_ignore == []
879879
assert await page.to_item() == BigItem(x=1, y=2, z=None)
880880
with pytest.raises(TypeError, match=expected_type_error_msg):
881881
await item_from_select_fields(page)
@@ -890,7 +890,7 @@ async def test_select_fields_but_to_item_only() -> None:
890890
select_fields=SelectFields({"x": 0, "y": 0, "z": 1}), # type: ignore[dict-item]
891891
)
892892
with pytest.raises(ValueError, match=expected_non_boolean_value_error_msg):
893-
page.fields_to_extract
893+
page.fields_to_ignore
894894
assert await page.to_item() == BigItem(x=1, y=2, z=None)
895895
with pytest.raises(ValueError, match=expected_non_boolean_value_error_msg):
896896
await item_from_select_fields(page)
@@ -905,7 +905,7 @@ async def test_select_fields_but_to_item_only() -> None:
905905
select_fields="not the instance it's expecting", # type: ignore[arg-type]
906906
)
907907
with pytest.raises(ValueError, match=expected_invalid_instance_value_error_msg):
908-
page.fields_to_extract
908+
page.fields_to_ignore
909909
assert await page.to_item() == BigItem(x=1, y=2, z=None)
910910
with pytest.raises(ValueError, match=expected_invalid_instance_value_error_msg):
911911
await item_from_select_fields(page)
@@ -919,7 +919,7 @@ async def test_select_fields_but_to_item_only() -> None:
919919
)
920920
page = BigToItemOnlyPage(response, select_fields=SelectFields(fields))
921921
with pytest.raises(ValueError, match=expected_value_error_msg):
922-
assert page.fields_to_extract
922+
page.fields_to_ignore
923923
assert await page.to_item() == BigItem(x=1, y=2, z=None)
924924
with pytest.raises(ValueError, match=expected_value_error_msg):
925925
await item_from_select_fields(page)
@@ -950,8 +950,8 @@ async def test_select_fields_but_unreliable() -> None:
950950
is overridden as well as ``@field`` decorators are partially used.
951951
952952
For this test, the ``.to_item()`` method is incorrectly made wherein it's
953-
not properly checking the ``.fields_to_extract`` to determine fields to
954-
populate.
953+
not properly checking the ``.fields_to_ignore`` to determine fields to
954+
avoid.
955955
"""
956956
# Required fields from the item cls which are not included raise an TypeError
957957
expected_type_error_msg = (
@@ -961,41 +961,41 @@ async def test_select_fields_but_unreliable() -> None:
961961

962962
# When SelectFields isn't set
963963
page = BigUnreliablePage(response)
964-
assert page.fields_to_extract == ["x"]
964+
assert page.fields_to_ignore == ["z"]
965965
assert await page.to_item() == BigItem(x=1, y=2, z=3)
966966
assert page.call_counter == {"x": 1, "z": 1}
967-
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=None)
968-
assert page.call_counter == {"x": 2, "z": 1}
967+
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=3)
968+
assert page.call_counter == {"x": 2, "z": 2}
969969

970970
# If no field selection directive is given but SelectFields is set, it would
971971
# use the default fields that are not disabled.
972972
page = BigUnreliablePage(response, select_fields=SelectFields(None))
973-
assert page.fields_to_extract == ["x"]
973+
assert page.fields_to_ignore == ["z"]
974974
assert await page.to_item() == BigItem(x=1, y=2, z=3)
975975
assert page.call_counter == {"x": 1, "z": 1}
976-
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=None)
977-
assert page.call_counter == {"x": 2, "z": 1}
976+
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=3)
977+
assert page.call_counter == {"x": 2, "z": 2}
978978

979979
# Same case as above but given an empty dict
980980
page = BigUnreliablePage(response, select_fields=SelectFields({}))
981-
assert page.fields_to_extract == ["x"]
981+
assert page.fields_to_ignore == ["z"]
982982
assert await page.to_item() == BigItem(x=1, y=2, z=3)
983983
assert page.call_counter == {"x": 1, "z": 1}
984-
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=None)
985-
assert page.call_counter == {"x": 2, "z": 1}
984+
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=3)
985+
assert page.call_counter == {"x": 2, "z": 2}
986986

987987
# Select all fields
988988
page = BigUnreliablePage(response, select_fields=SelectFields({"*": True}))
989-
assert page.fields_to_extract == ["x", "z"]
989+
assert page.fields_to_ignore == []
990990
assert await page.to_item() == BigItem(x=1, y=2, z=3)
991991
assert page.call_counter == {"x": 1, "z": 1}
992-
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=3)
992+
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=3)
993993
assert page.call_counter == {"x": 2, "z": 2}
994994

995995
# Don't select all fields; but in this case a TypeError is raised since
996996
# required fields aren't supplied to the item
997997
page = BigUnreliablePage(response, select_fields=SelectFields({"*": False}))
998-
assert page.fields_to_extract == []
998+
assert page.fields_to_ignore == ["x", "z"]
999999
assert await page.to_item() == BigItem(x=1, y=2, z=3)
10001000
assert page.call_counter == {"x": 1, "z": 1}
10011001
with pytest.raises(TypeError, match=expected_type_error_msg):
@@ -1006,36 +1006,36 @@ async def test_select_fields_but_unreliable() -> None:
10061006
page = BigUnreliablePage(
10071007
response, select_fields=SelectFields({"*": False, "x": True})
10081008
)
1009-
assert page.fields_to_extract == ["x"]
1009+
assert page.fields_to_ignore == ["z"]
10101010
assert await page.to_item() == BigItem(x=1, y=2, z=3)
10111011
assert page.call_counter == {"x": 1, "z": 1}
10121012
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=None)
1013-
assert page.call_counter == {"x": 2, "z": 1}
1013+
assert page.call_counter == {"x": 2, "z": 2}
10141014

10151015
# Include all fields but one
10161016
page = BigUnreliablePage(
10171017
response, select_fields=SelectFields({"*": True, "z": False})
10181018
)
1019-
assert page.fields_to_extract == ["x"]
1019+
assert page.fields_to_ignore == ["z"]
10201020
assert await page.to_item() == BigItem(x=1, y=2, z=3)
10211021
assert page.call_counter == {"x": 1, "z": 1}
1022-
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=None)
1023-
assert page.call_counter == {"x": 2, "z": 1}
1022+
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=None)
1023+
assert page.call_counter == {"x": 2, "z": 2}
10241024

10251025
# overlapping directives on the same field should be okay
10261026
page = BigUnreliablePage(
10271027
response,
10281028
select_fields=SelectFields({"*": True, "x": True, "y": True, "z": True}),
10291029
)
1030-
assert page.fields_to_extract == ["x", "z"]
1030+
assert page.fields_to_ignore == []
10311031
assert await page.to_item() == BigItem(x=1, y=2, z=3)
10321032
assert page.call_counter == {"x": 1, "z": 1}
1033-
assert await item_from_select_fields(page) == BigItem(x=1, y=None, z=3)
1033+
assert await item_from_select_fields(page) == BigItem(x=1, y=2, z=3)
10341034
assert page.call_counter == {"x": 2, "z": 2}
10351035

10361036
# Excluding a required field throws an error
10371037
page = BigUnreliablePage(response, select_fields=SelectFields({"x": False}))
1038-
assert page.fields_to_extract == []
1038+
assert page.fields_to_ignore == ["x", "z"]
10391039
assert await page.to_item() == BigItem(x=1, y=2, z=3)
10401040
assert page.call_counter == {"x": 1, "z": 1}
10411041
with pytest.raises(TypeError, match=expected_type_error_msg):
@@ -1052,7 +1052,7 @@ async def test_select_fields_but_unreliable() -> None:
10521052
select_fields=SelectFields({"x": 0, "y": 0, "z": 1}), # type: ignore[dict-item]
10531053
)
10541054
with pytest.raises(ValueError, match=expected_non_boolean_value_error_msg):
1055-
page.fields_to_extract
1055+
page.fields_to_ignore
10561056
assert await page.to_item() == BigItem(x=1, y=2, z=3)
10571057
with pytest.raises(ValueError, match=expected_non_boolean_value_error_msg):
10581058
await item_from_select_fields(page)
@@ -1068,7 +1068,7 @@ async def test_select_fields_but_unreliable() -> None:
10681068
select_fields="not the instance it's expecting", # type: ignore[arg-type]
10691069
)
10701070
with pytest.raises(ValueError, match=expected_invalid_instance_value_error_msg):
1071-
page.fields_to_extract
1071+
page.fields_to_ignore
10721072
assert await page.to_item() == BigItem(x=1, y=2, z=3)
10731073
with pytest.raises(ValueError, match=expected_invalid_instance_value_error_msg):
10741074
await item_from_select_fields(page)
@@ -1083,7 +1083,7 @@ async def test_select_fields_but_unreliable() -> None:
10831083
)
10841084
page = BigUnreliablePage(response, select_fields=SelectFields(fields))
10851085
with pytest.raises(ValueError, match=expected_value_error_msg):
1086-
assert page.fields_to_extract
1086+
page.fields_to_ignore
10871087
assert await page.to_item() == BigItem(x=1, y=2, z=3)
10881088
with pytest.raises(ValueError, match=expected_value_error_msg):
10891089
await item_from_select_fields(page)

0 commit comments

Comments
 (0)