Django 2.1 SimpleTestCase, assertFieldOutput for DateField - AssertionError: ValidationError not raised
I have this simple Form (Django 2.1, Python 3.6.5).
class RenewBookForm(forms.Form):
renewal_date = forms.DateField(
help_text="Enter a date between now and 4 weeks (default 3)."
)
def clean_renewal_date(self):
data = self.cleaned_data['renewal_date']
# Check if a date is not in the past.
if data < datetime.date.today():
raise ValidationError(_('Invalid date - renewal in past'))
# Check if a date is in the allowed range (+4 weeks from today).
if data > datetime.date.today() + datetime.timedelta(weeks=4):
raise ValidationError(_(
'Invalid date - renewal more than 4 weeks ahead'))
# Remember to always return the cleaned data.
return data
And I have the test based on SimpleTestCase class:
import datetime
from django.forms import DateField
from django.test import SimpleTestCase
from django.utils import timezone
from catalog.forms import RenewBookForm
class RenewBookFormTest(SimpleTestCase):
def test_renew_form_date_is_not_in_the_past(self):
error_invalid = ['Invalid date - renewal in past']
past_date = datetime.date.today() - datetime.timedelta(days=1)
valid_date = datetime.date.today() + datetime.timedelta(days=21)
valid = { valid_date: valid_date }
invalid = { past_date: error_invalid }
self.assertFieldOutput(DateField, valid, invalid)
Error, after running this test:
Traceback (most recent call last):
File "locallibrary/catalog/tests/test_forms.py", line 53, in test_renew_form_date_is_not_in_the_past
self.assertFieldOutput(DateField, valid, invalid)
File "~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/test/testcases.py", line 660, in assertFieldOutput
required.clean(input)
AssertionError: ValidationError not raised
What is wrong in this test? Thank you.
django validation datefield django-unittest
add a comment |
I have this simple Form (Django 2.1, Python 3.6.5).
class RenewBookForm(forms.Form):
renewal_date = forms.DateField(
help_text="Enter a date between now and 4 weeks (default 3)."
)
def clean_renewal_date(self):
data = self.cleaned_data['renewal_date']
# Check if a date is not in the past.
if data < datetime.date.today():
raise ValidationError(_('Invalid date - renewal in past'))
# Check if a date is in the allowed range (+4 weeks from today).
if data > datetime.date.today() + datetime.timedelta(weeks=4):
raise ValidationError(_(
'Invalid date - renewal more than 4 weeks ahead'))
# Remember to always return the cleaned data.
return data
And I have the test based on SimpleTestCase class:
import datetime
from django.forms import DateField
from django.test import SimpleTestCase
from django.utils import timezone
from catalog.forms import RenewBookForm
class RenewBookFormTest(SimpleTestCase):
def test_renew_form_date_is_not_in_the_past(self):
error_invalid = ['Invalid date - renewal in past']
past_date = datetime.date.today() - datetime.timedelta(days=1)
valid_date = datetime.date.today() + datetime.timedelta(days=21)
valid = { valid_date: valid_date }
invalid = { past_date: error_invalid }
self.assertFieldOutput(DateField, valid, invalid)
Error, after running this test:
Traceback (most recent call last):
File "locallibrary/catalog/tests/test_forms.py", line 53, in test_renew_form_date_is_not_in_the_past
self.assertFieldOutput(DateField, valid, invalid)
File "~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/test/testcases.py", line 660, in assertFieldOutput
required.clean(input)
AssertionError: ValidationError not raised
What is wrong in this test? Thank you.
django validation datefield django-unittest
add a comment |
I have this simple Form (Django 2.1, Python 3.6.5).
class RenewBookForm(forms.Form):
renewal_date = forms.DateField(
help_text="Enter a date between now and 4 weeks (default 3)."
)
def clean_renewal_date(self):
data = self.cleaned_data['renewal_date']
# Check if a date is not in the past.
if data < datetime.date.today():
raise ValidationError(_('Invalid date - renewal in past'))
# Check if a date is in the allowed range (+4 weeks from today).
if data > datetime.date.today() + datetime.timedelta(weeks=4):
raise ValidationError(_(
'Invalid date - renewal more than 4 weeks ahead'))
# Remember to always return the cleaned data.
return data
And I have the test based on SimpleTestCase class:
import datetime
from django.forms import DateField
from django.test import SimpleTestCase
from django.utils import timezone
from catalog.forms import RenewBookForm
class RenewBookFormTest(SimpleTestCase):
def test_renew_form_date_is_not_in_the_past(self):
error_invalid = ['Invalid date - renewal in past']
past_date = datetime.date.today() - datetime.timedelta(days=1)
valid_date = datetime.date.today() + datetime.timedelta(days=21)
valid = { valid_date: valid_date }
invalid = { past_date: error_invalid }
self.assertFieldOutput(DateField, valid, invalid)
Error, after running this test:
Traceback (most recent call last):
File "locallibrary/catalog/tests/test_forms.py", line 53, in test_renew_form_date_is_not_in_the_past
self.assertFieldOutput(DateField, valid, invalid)
File "~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/test/testcases.py", line 660, in assertFieldOutput
required.clean(input)
AssertionError: ValidationError not raised
What is wrong in this test? Thank you.
django validation datefield django-unittest
I have this simple Form (Django 2.1, Python 3.6.5).
class RenewBookForm(forms.Form):
renewal_date = forms.DateField(
help_text="Enter a date between now and 4 weeks (default 3)."
)
def clean_renewal_date(self):
data = self.cleaned_data['renewal_date']
# Check if a date is not in the past.
if data < datetime.date.today():
raise ValidationError(_('Invalid date - renewal in past'))
# Check if a date is in the allowed range (+4 weeks from today).
if data > datetime.date.today() + datetime.timedelta(weeks=4):
raise ValidationError(_(
'Invalid date - renewal more than 4 weeks ahead'))
# Remember to always return the cleaned data.
return data
And I have the test based on SimpleTestCase class:
import datetime
from django.forms import DateField
from django.test import SimpleTestCase
from django.utils import timezone
from catalog.forms import RenewBookForm
class RenewBookFormTest(SimpleTestCase):
def test_renew_form_date_is_not_in_the_past(self):
error_invalid = ['Invalid date - renewal in past']
past_date = datetime.date.today() - datetime.timedelta(days=1)
valid_date = datetime.date.today() + datetime.timedelta(days=21)
valid = { valid_date: valid_date }
invalid = { past_date: error_invalid }
self.assertFieldOutput(DateField, valid, invalid)
Error, after running this test:
Traceback (most recent call last):
File "locallibrary/catalog/tests/test_forms.py", line 53, in test_renew_form_date_is_not_in_the_past
self.assertFieldOutput(DateField, valid, invalid)
File "~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/test/testcases.py", line 660, in assertFieldOutput
required.clean(input)
AssertionError: ValidationError not raised
What is wrong in this test? Thank you.
django validation datefield django-unittest
django validation datefield django-unittest
asked Nov 15 '18 at 17:29
JanJan
54
54
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I found that self.assertFieldOutput(DateField, valid, invalid)
is used to test field class derived from Django class. So it is not applicable for your case.
The test should be as following, assuming '/form/
is the url to your form and 'form'
is the context key in your view.
class RenewBookFormTest(SimpleTestCase):
def test_renew_form_date_is_not_in_the_past(self):
error_invalid = ['Invalid date - renewal in past']
past_date = datetime.date.today() - datetime.timedelta(days=1)
valid_date = datetime.date.today() + datetime.timedelta(days=21)
response = self.client.post('/form/', data={'renewal_date': past_date})
context_form = 'form'
self.assertFormError(response, context_form, 'renewal_date',
error_invalid)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324941%2fdjango-2-1-simpletestcase-assertfieldoutput-for-datefield-assertionerror-val%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I found that self.assertFieldOutput(DateField, valid, invalid)
is used to test field class derived from Django class. So it is not applicable for your case.
The test should be as following, assuming '/form/
is the url to your form and 'form'
is the context key in your view.
class RenewBookFormTest(SimpleTestCase):
def test_renew_form_date_is_not_in_the_past(self):
error_invalid = ['Invalid date - renewal in past']
past_date = datetime.date.today() - datetime.timedelta(days=1)
valid_date = datetime.date.today() + datetime.timedelta(days=21)
response = self.client.post('/form/', data={'renewal_date': past_date})
context_form = 'form'
self.assertFormError(response, context_form, 'renewal_date',
error_invalid)
add a comment |
I found that self.assertFieldOutput(DateField, valid, invalid)
is used to test field class derived from Django class. So it is not applicable for your case.
The test should be as following, assuming '/form/
is the url to your form and 'form'
is the context key in your view.
class RenewBookFormTest(SimpleTestCase):
def test_renew_form_date_is_not_in_the_past(self):
error_invalid = ['Invalid date - renewal in past']
past_date = datetime.date.today() - datetime.timedelta(days=1)
valid_date = datetime.date.today() + datetime.timedelta(days=21)
response = self.client.post('/form/', data={'renewal_date': past_date})
context_form = 'form'
self.assertFormError(response, context_form, 'renewal_date',
error_invalid)
add a comment |
I found that self.assertFieldOutput(DateField, valid, invalid)
is used to test field class derived from Django class. So it is not applicable for your case.
The test should be as following, assuming '/form/
is the url to your form and 'form'
is the context key in your view.
class RenewBookFormTest(SimpleTestCase):
def test_renew_form_date_is_not_in_the_past(self):
error_invalid = ['Invalid date - renewal in past']
past_date = datetime.date.today() - datetime.timedelta(days=1)
valid_date = datetime.date.today() + datetime.timedelta(days=21)
response = self.client.post('/form/', data={'renewal_date': past_date})
context_form = 'form'
self.assertFormError(response, context_form, 'renewal_date',
error_invalid)
I found that self.assertFieldOutput(DateField, valid, invalid)
is used to test field class derived from Django class. So it is not applicable for your case.
The test should be as following, assuming '/form/
is the url to your form and 'form'
is the context key in your view.
class RenewBookFormTest(SimpleTestCase):
def test_renew_form_date_is_not_in_the_past(self):
error_invalid = ['Invalid date - renewal in past']
past_date = datetime.date.today() - datetime.timedelta(days=1)
valid_date = datetime.date.today() + datetime.timedelta(days=21)
response = self.client.post('/form/', data={'renewal_date': past_date})
context_form = 'form'
self.assertFormError(response, context_form, 'renewal_date',
error_invalid)
answered Nov 16 '18 at 2:37
Hai LangHai Lang
46026
46026
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324941%2fdjango-2-1-simpletestcase-assertfieldoutput-for-datefield-assertionerror-val%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown