def test_to_representation(self): """ The dict representation is simply passed through without any change. """ field = serializers.TagValueDictField() val = { "namespace_name/tag-name": "a value", } self.assertEqual(val, field.to_representation(val))
def test_run_child_validation_bad_key(self): """ A dictionary with an incorrectly formatted key results in a ValidationError exception being thrown. """ field = serializers.TagValueDictField() val = { "namespace_name--tag-name": "a value", } with self.assertRaises(ValidationError): field.run_child_validation(val)
def test_run_child_validation(self): """ A dictionary containing correctly formatted keys is checked and returned without raising any errors. """ field = serializers.TagValueDictField() val = { "namespace_name/tag-name": "a value", } result = field.run_child_validation(val) self.assertEqual(result, val)
def test_to_internal_value_is_empty(self): """ When validation is called, to_internal raises a ValidationError if the passed in value is not a dictionary. """ field = serializers.TagValueDictField(allow_empty=False) val = {} mock_html = mock.MagicMock() mock_html.is_html_input.return_value = False with mock.patch("api.serializers.html", mock_html): with self.assertRaises(ValidationError): field.to_internal_value(val)
def test_get_value_is_html(self): """ Ensure that getting the value of the field works with HTML form fields. """ field = serializers.TagValueDictField() field.field_name = "test_field" test_dict = {} mock_html = mock.MagicMock() mock_html.parse_html_dict.return_value = "it worked!" with mock.patch("api.serializers.html", mock_html): result = field.get_value(test_dict) self.assertEqual("it worked!", result) mock_html.is_html_input.assert_called_once_with(test_dict) mock_html.parse_html_dict.assert_called_once_with( test_dict, prefix=field.field_name)
def test_get_value(self): """ Ensure that getting the value of the field works with native dictionary representations. """ field = serializers.TagValueDictField() field.field_name = "test_field" test_dict = mock.MagicMock() test_dict.get.return_value = "it worked!" mock_html = mock.MagicMock() mock_html.is_html_input.return_value = False with mock.patch("api.serializers.html", mock_html): result = field.get_value(test_dict) self.assertEqual("it worked!", result) test_dict.get.assert_called_once_with( field.field_name, serializers.serializers.empty)
def test_to_internal_value_html_input(self): """ If the input data is from an HTML form, ensure it is parsed into a dictionary object before further validation occurs. """ field = serializers.TagValueDictField() field.field_name = "test_field" test_dict = "a dict in html" mock_html = mock.MagicMock() mock_html.is_html_input.return_value = True dict_result = { "namespace_name/tag-name": "a value", } mock_html.parse_html_dict.return_value = dict_result with mock.patch("api.serializers.html", mock_html): result = field.to_internal_value(test_dict) self.assertEqual(dict_result, result)