Created
May 15, 2015 05:07
-
-
Save mjtamlyn/00a34acb07481d8ae28d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyForm(forms.Form): | |
# ... Many fields | |
def clean(self): | |
foo = self.cleaned_data.get('foo') | |
bar = self.cleaned_data.get('bar') | |
if foo and bar and foo > 0 and bar < 0: | |
self.add_error('bar', ValidationError('Some message', code='foo_bar_mismatch')) | |
# ... Other validation rules here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestMyForm(TestCase): | |
def test_foo_bar_mismatch(self): | |
data = { | |
# .. other fields | |
'foo': 1, | |
'bar': -1, | |
} | |
form = MyForm(data=data) | |
self.assertFalse(form.is_valid()) | |
self.assertTrue(form.has_error('bar', code='foo_bar_mismatch')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment