Permalink
Browse files

Fix dashboard report form when date range is incomplete, add tests.

  • Loading branch information...
1 parent 84ff76c commit 5ef5570873d6d092db4757d2ec984a8220bc2358 @sasha0 sasha0 committed Nov 3, 2016
Showing with 53 additions and 2 deletions.
  1. +3 −2 src/oscar/apps/dashboard/reports/forms.py
  2. +50 −0 tests/unit/dashboard/reports_form_tests.py
@@ -31,8 +31,9 @@ class ReportForm(forms.Form):
download = forms.BooleanField(label=_("Download"), required=False)
def clean(self):
- if ('date_from' in self.cleaned_data and 'date_to' in self.cleaned_data
- and self.cleaned_data['date_from'] >
+ date_from = self.cleaned_data.get('date_from', None)
+ date_to = self.cleaned_data.get('date_to', None)
+ if (all([date_from, date_to]) and self.cleaned_data['date_from'] >
self.cleaned_data['date_to']):
raise forms.ValidationError(_("Your start date must be before your"
" end date"))
@@ -0,0 +1,50 @@
+from django.test import TestCase
+
+from oscar.apps.dashboard.reports import forms
+
+
+class TestReportsForm(TestCase):
+
+ def test_date_range_empty(self):
+ form = forms.ReportForm(data={})
+ self.assertFalse(form.is_valid())
+
+ def test_date_range_complete(self):
+ form = forms.ReportForm(data={
+ 'date_from': '2016-11-02',
+ 'date_to': '2016-11-03',
+ 'report_type': 'order_report'
+ })
+ self.assertTrue(form.is_valid())
+
+ def test_date_range_incomplete(self):
+ form = forms.ReportForm(data={
+ 'report_type': 'order_report',
+ 'date_to': '',
+ 'date_from': '2016-11-02'
+ })
+ self.assertTrue(form.is_valid(), form.errors)
+ form = forms.ReportForm(data={
+ 'date_from': '',
+ 'date_to': '2016-11-03',
+ 'report_type': 'order_report'
+ })
+ self.assertTrue(form.is_valid())
+ form = forms.ReportForm(data={
+ 'date_from': '2016-11-02',
+ 'report_type': 'order_report'
+ })
+ self.assertTrue(form.is_valid())
+ form = forms.ReportForm(data={
+ 'date_to': '2016-11-03',
+ 'report_type': 'order_report'
+ })
+ self.assertTrue(form.is_valid())
+
+ def test_date_range_incorrect(self):
+ form = forms.ReportForm(data={
+ 'date_from': '2016-11-03',
+ 'date_to': '2016-11-02',
+ 'report_type': 'order_report'
+ })
+ self.assertFalse(form.is_valid())

0 comments on commit 5ef5570

Please sign in to comment.