MCPcopy
hub / github.com/django/django / assertFieldOutput

Method assertFieldOutput

django/test/testcases.py:902–953  ·  view source on GitHub ↗

Assert that a form field behaves correctly with various inputs. Args: fieldclass: the class of the field to be tested. valid: a dictionary mapping valid inputs to their expected cleaned values. invalid: a dictionary mapping in

(
        self,
        fieldclass,
        valid,
        invalid,
        field_args=None,
        field_kwargs=None,
        empty_value="",
    )

Source from the content-addressed store, hash-verified

900 )
901
902 def assertFieldOutput(
903 self,
904 fieldclass,
905 valid,
906 invalid,
907 field_args=None,
908 field_kwargs=None,
909 empty_value="",
910 ):
911 """
912 Assert that a form field behaves correctly with various inputs.
913
914 Args:
915 fieldclass: the class of the field to be tested.
916 valid: a dictionary mapping valid inputs to their expected
917 cleaned values.
918 invalid: a dictionary mapping invalid inputs to one or more
919 raised error messages.
920 field_args: the args passed to instantiate the field
921 field_kwargs: the kwargs passed to instantiate the field
922 empty_value: the expected clean output for inputs in empty_values
923 """
924 if field_args is None:
925 field_args = []
926 if field_kwargs is None:
927 field_kwargs = {}
928 required = fieldclass(*field_args, **field_kwargs)
929 optional = fieldclass(*field_args, **{**field_kwargs, "required": False})
930 # test valid inputs
931 for input, output in valid.items():
932 self.assertEqual(required.clean(input), output)
933 self.assertEqual(optional.clean(input), output)
934 # test invalid inputs
935 for input, errors in invalid.items():
936 with self.assertRaises(ValidationError) as context_manager:
937 required.clean(input)
938 self.assertEqual(context_manager.exception.messages, errors)
939
940 with self.assertRaises(ValidationError) as context_manager:
941 optional.clean(input)
942 self.assertEqual(context_manager.exception.messages, errors)
943 # test required inputs
944 error_required = [required.error_messages["required"]]
945 for e in required.empty_values:
946 with self.assertRaises(ValidationError) as context_manager:
947 required.clean(e)
948 self.assertEqual(context_manager.exception.messages, error_required)
949 self.assertEqual(optional.clean(e), empty_value)
950 # test that max_length and min_length are always accepted
951 if issubclass(fieldclass, CharField):
952 field_kwargs.update({"min_length": 2, "max_length": 20})
953 self.assertIsInstance(fieldclass(*field_args, **field_kwargs), fieldclass)
954
955 def assertHTMLEqual(self, html1, html2, msg=None):
956 """

Callers 2

Calls 3

itemsMethod · 0.45
cleanMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected