def test_return_raw_queryset_if_form_is_invalid(self): queryset = Mock() params = {'drf__ham__gte': 'Yay!', 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset)
def test_queryset_filters_by_date_from(self): queryset = Mock() params = {'drf__ham__gte': date(2014, 1, 3), 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertItemsEqual([call(ham__gte=date(2014, 1, 3))], queryset.filter.call_args_list)
def test_queryset_ignore_null_fields(self): queryset = Mock() params = {'drf__ham__gte': None, 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertItemsEqual([call()], queryset.filter.call_args_list)
def test_queryset_ignore_null_fields(self): queryset = Mock() params = {'drf__ham__gte': None, 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertCountEqual([call()], queryset.filter.call_args_list)
def test_queryset_filters_by_date_from(self): queryset = Mock() params = {'drf__ham__gte': date(2014, 1, 3), 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertCountEqual([call(ham__gte=date(2014, 1, 3))], queryset.filter.call_args_list)
def test_queryset_filters_by_date_to(self): queryset = Mock() data_end = date(2014, 1, 3) params = {'drf__ham__gte': None, 'drf__ham__lte': data_end} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertEqual((data_end + timedelta(days=1)).strftime('%s'), queryset.filter.call_args_list[0][1]['ham__lt'].strftime('%s'))
def test_queryset_filters_by_date_to(self): queryset = Mock() data_end = date(2014, 1, 3) params = {'drf__ham__gte': None, 'drf__ham__lte': data_end} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertEqual( (data_end + timedelta(days=1)).strftime('%s'), queryset.filter.call_args_list[0][1]['ham__lt'].strftime('%s'))
def setUp(self): self.request = Mock() self.filter_ = DateRangeFilter('spam', self.request, [], Mock(), Mock(), 'egg')
class DateRangeFilterTest(TestCase): def setUp(self): self.request = Mock() self.filter_ = DateRangeFilter('spam', self.request, [], Mock(), Mock(), 'egg') def test_use_correctly_template(self): self.assertEqual(self.filter_.template, 'daterange_filter/filter.html') def test_form_uses_request(self): self.assertEqual(self.filter_.form.request, self.request) def test_choices_is_empty(self): self.assertEqual(self.filter_.choices(Mock()), []) def test_expected_params(self): self.assertCountEqual(self.filter_.expected_parameters(), ['drf__egg__lte', 'drf__egg__gte']) @patch('daterange_filter.filter.DateRangeForm') def test_get_form(self, DateRangeForm): self.filter_.get_form(self.request) self.assertEqual([call(self.request, data=ANY, field_name='egg')], DateRangeForm.call_args_list) def test_queryset_ignore_null_fields(self): queryset = Mock() params = {'drf__ham__gte': None, 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertCountEqual([call()], queryset.filter.call_args_list) def test_queryset_filters_by_date_from(self): queryset = Mock() params = {'drf__ham__gte': date(2014, 1, 3), 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertCountEqual([call(ham__gte=date(2014, 1, 3))], queryset.filter.call_args_list) def test_queryset_filters_by_date_to(self): queryset = Mock() data_end = date(2014, 1, 3) params = {'drf__ham__gte': None, 'drf__ham__lte': data_end} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertEqual( (data_end + timedelta(days=1)).strftime('%s'), queryset.filter.call_args_list[0][1]['ham__lt'].strftime('%s')) def test_return_raw_queryset_if_form_is_invalid(self): queryset = Mock() params = {'drf__ham__gte': 'Yay!', 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset)
class DateRangeFilterTest(BaseTest): def setUp(self): self.request = Mock() self.filter_ = DateRangeFilter('spam', self.request, [], Mock(), Mock(), 'egg') def test_use_correctly_template(self): self.assertEqual(self.filter_.template, 'daterange_filter/filter.html') def test_form_uses_request(self): self.assertEqual(self.filter_.form.request, self.request) def test_choices_is_empty(self): self.assertEqual(self.filter_.choices(Mock()), []) def test_expected_params(self): self.assertItemsEqual(self.filter_.expected_parameters(), ['drf__egg__lte', 'drf__egg__gte']) @patch('daterange_filter.filter.DateRangeForm') def test_get_form(self, DateRangeForm): self.filter_.get_form(self.request) self.assertEqual([call(self.request, data=ANY, field_name='egg')], DateRangeForm.call_args_list) def test_queryset_ignore_null_fields(self): queryset = Mock() params = {'drf__ham__gte': None, 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertItemsEqual([call()], queryset.filter.call_args_list) def test_queryset_filters_by_date_from(self): queryset = Mock() params = {'drf__ham__gte': date(2014, 1, 3), 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertItemsEqual([call(ham__gte=date(2014, 1, 3))], queryset.filter.call_args_list) def test_queryset_filters_by_date_to(self): queryset = Mock() data_end = date(2014, 1, 3) params = {'drf__ham__gte': None, 'drf__ham__lte': data_end} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset.filter.return_value) self.assertEqual((data_end + timedelta(days=1)).strftime('%s'), queryset.filter.call_args_list[0][1]['ham__lt'].strftime('%s')) def test_return_raw_queryset_if_form_is_invalid(self): queryset = Mock() params = {'drf__ham__gte': 'Yay!', 'drf__ham__lte': None} filter_ = DateRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham') return_value = filter_.queryset(self.request, queryset) self.assertEqual(return_value, queryset)