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_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'))