Exemplo n.º 1
0
    def test_invalid_hex(self):
        value = '1'
        self.validator = HexValidator()

        with self.assertRaises(ValidationError) as err:
            self.validator(value)

        self.assertEqual(str(err.exception), "['Only a hex string is allowed.']")
        self.assertEqual(err.exception.code, 'hex_only')
Exemplo n.º 2
0
    def test_fixed_length(self):
        value = 'abcd'
        self.validator = HexValidator(length=5)

        with self.assertRaises(ValidationError) as err:
            self.validator(value)

        self.assertEqual(str(err.exception), "['Invalid length. Must be 5 characters.']")
        self.assertEqual(err.exception.code, 'hex_only_length')
Exemplo n.º 3
0
    def test_with_max_length(self):
        value = 'abcd'
        self.validator = HexValidator(max_length=2)

        with self.assertRaises(ValidationError) as err:
            self.validator(value)

        self.assertEqual(str(err.exception), "['Ensure that there are no more than 2 characters.']")
        self.assertEqual(err.exception.code, 'hex_only_max_length')
Exemplo n.º 4
0
    def test_invalid_type(self):
        value = 1
        with patch('django_extensions.validators.force_str', return_value=1):
            self.validator = HexValidator()

            with self.assertRaises(ValidationError) as err:
                self.validator(value)

        self.assertEqual(str(err.exception), "['Only a hex string is allowed.']")
        self.assertEqual(err.exception.code, 'hex_only')
Exemplo n.º 5
0
 def test_equality_of_objs_with_same_code_and_message(self):
     self.assertEqual(HexValidator(code='c', message='m'), HexValidator(code='c', message='m'))
Exemplo n.º 6
0
 def test_equality_of_objs_with_different_message(self):
     self.assertNotEqual(HexValidator(code='code', message='a'), HexValidator(code='code', message='acb'))
Exemplo n.º 7
0
 def test_equality_of_objs_with_different_code(self):
     self.assertNotEqual(HexValidator(code='1'), HexValidator(code='a'))
Exemplo n.º 8
0
 def test_equality_of_objs_with_obj_of_different_type(self):
     self.assertNotEqual(TypeError(), HexValidator())
Exemplo n.º 9
0
    def test_custom_message_and_code(self):
        self.validator = HexValidator(message='message', code='code')

        self.assertEqual(self.validator.message, 'message')
        self.assertEqual(self.validator.code, 'code')