Skip to content

Commit

Permalink
fix: Max length validation on HexValidator (django-extensions#1669)
Browse files Browse the repository at this point in the history
* fix: Max length validation on HexValidator

- also add tests for this validator.
- fix a bug on the equality function that would be caused due to the absence of a default error message.

* fix: mypy builds

- since mypy > 0.900 doesn't ship with third-party dependencies, add them to the tox mypy environment.

Co-authored-by: Camilo Nova <camilo.nova@gmail.com>
abhiabhi94 and camilonova authored Jun 11, 2022
1 parent 8292ffc commit cfd2220
Showing 3 changed files with 94 additions and 5 deletions.
4 changes: 3 additions & 1 deletion django_extensions/validators.py
Original file line number Diff line number Diff line change
@@ -83,6 +83,8 @@ def __init__(self, length=None, min_length=None, max_length=None, message=None,
self.max_length = max_length
if message:
self.message = message
else:
self.message = self.messages['invalid']
if code:
self.code = code

@@ -92,7 +94,7 @@ def __call__(self, value):
raise ValidationError(self.messages['length'], code='hex_only_length', params={'length': self.length})
if self.min_length and len(value) < self.min_length:
raise ValidationError(self.messages['min_length'], code='hex_only_min_length', params={'min': self.min_length})
if self.max_length and len(value) < self.max_length:
if self.max_length and len(value) > self.max_length:
raise ValidationError(self.messages['max_length'], code='hex_only_max_length', params={'max': self.max_length})

try:
85 changes: 83 additions & 2 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
from unittest.mock import patch

from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test import TestCase, SimpleTestCase

from django_extensions.validators import NoControlCharactersValidator, NoWhitespaceValidator
from django_extensions.validators import NoControlCharactersValidator, NoWhitespaceValidator, HexValidator


class NoControlCharactersValidatorTests(TestCase):
@@ -72,3 +74,82 @@ def test_should_not_raise_if_value_doesnt_have_leading_or_trailing_whitespaces(s
result = self.validator(value_without_leading_or_trailing_whitespaces)

self.assertIsNone(result)


class TestHexValidator(SimpleTestCase):
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')

def test_equality_of_objs_with_obj_of_different_type(self):
self.assertNotEqual(TypeError(), HexValidator())

def test_equality_of_objs_with_different_code(self):
self.assertNotEqual(HexValidator(code='1'), HexValidator(code='a'))

def test_equality_of_objs_with_different_message(self):
self.assertNotEqual(HexValidator(code='code', message='a'), HexValidator(code='code', message='acb'))

def test_equality_of_objs_with_same_code_and_message(self):
self.assertEqual(HexValidator(code='c', message='m'), HexValidator(code='c', message='m'))

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')

def test_min_length(self):
value = 'a'
self.validator = HexValidator(min_length=5)

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

self.assertEqual(str(err.exception), "['Ensure that there are more than 5 characters.']")
self.assertEqual(err.exception.code, 'hex_only_min_length')

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')

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')

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')

def test_valid_hex(self):
value = 'b901ef'
self.validator = HexValidator()

result = self.validator(value)

self.assertIsNone(result)
10 changes: 8 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ envlist =

[testenv]
commands = make test
whitelist_externals = make
allowlist_externals = make
passenv =
DJANGO_EXTENSIONS_DATABASE_ENGINE
DJANGO_EXTENSIONS_DATABASE_NAME
@@ -57,14 +57,20 @@ deps =
commands = safety check --full-report

[testenv:mypy]
allowlist_externals=
echo
deps =
pip >= 21.1
mypy
types-requests
types-boto
types-PyYAML
types-Werkzeug
-rrequirements-dev.txt
commands = mypy --ignore-missing-imports django_extensions

[testenv:compile-catalog]
whitelist_externals =
allowlist_externals =
make
deps =
pip >= 21.1

0 comments on commit cfd2220

Please sign in to comment.