Skip to content

Commit

Permalink
Merge pull request #2637 from davidszotten/validator_comparisons
Browse files Browse the repository at this point in the history
[1.7.x] Fixed #22588 -- Fix RegexValidator __eq__
  • Loading branch information
andrewgodwin committed May 9, 2014
2 parents 7194d40 + 724a7bf commit d2e96b5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion django/core/validators.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ def __call__(self, value):
raise ValidationError(self.message, code=self.code) raise ValidationError(self.message, code=self.code)


def __eq__(self, other): def __eq__(self, other):
return isinstance(other, RegexValidator) and (self.regex == other.regex) and (self.message == other.message) and (self.code == other.code) return (
isinstance(other, RegexValidator) and
self.regex.pattern == other.regex.pattern and
self.regex.flags == other.regex.flags and
(self.message == other.message) and
(self.code == other.code) and
(self.inverse_match == other.inverse_match)
)

def __ne__(self, other):
return not (self == other)




@deconstructible @deconstructible
Expand Down
27 changes: 27 additions & 0 deletions tests/validators/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -296,6 +296,33 @@ def test_regex_equality(self):
RegexValidator(r'^(?:[a-z0-9\.\-]*)://'), RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
) )


self.assertNotEqual(
RegexValidator('', flags=re.IGNORECASE),
RegexValidator(''),
)

self.assertNotEqual(
RegexValidator(''),
RegexValidator('', inverse_match=True),
)

def test_regex_equality_nocache(self):
pattern = r'^(?:[a-z0-9\.\-]*)://'
left = RegexValidator(pattern)
re.purge()
right = RegexValidator(pattern)

self.assertEqual(
left,
right,
)

def test_regex_equality_blank(self):
self.assertEqual(
RegexValidator(),
RegexValidator(),
)

def test_email_equality(self): def test_email_equality(self):
self.assertEqual( self.assertEqual(
EmailValidator(), EmailValidator(),
Expand Down

0 comments on commit d2e96b5

Please sign in to comment.