def test_return_raw_queryset_if_form_is_invalid(self):
        queryset = Mock()
        params = {'drf__ham__gte_0': 'Yay!', 'drf__ham__gte_1': None, 'drf__ham__lte_0': None, 'drf__ham__lte_1': None}
        filter_ = DateTimeRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham')

        return_value = filter_.queryset(self.request, queryset)

        self.assertEqual(return_value, queryset)
    def test_queryset_ignore_null_fields(self):
        queryset = Mock()
        params = {'drf__ham__gte_0': None, 'drf__ham__gte_1': None, 'drf__ham__lte_0': None, 'drf__ham__lte_1': None}
        filter_ = DateTimeRangeFilter('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_to(self):
        queryset = Mock()
        data_end = timezone.make_aware(datetime(2014, 1, 2, 3, 4, 5), timezone.get_current_timezone())
        params = {'drf__ham__lte_0': data_end.strftime('%Y-%m-%d'), 'drf__ham__lte_1': data_end.strftime('%H:%M:%S'),
                  'drf__ham__gte_0': None, 'drf__ham__gte_1': None}
        filter_ = DateTimeRangeFilter('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, queryset.filter.call_args_list[0][1]['ham__lte'])
    def test_queryset_filters_by_date_from(self):
        queryset = Mock()
        params = {'drf__ham__gte_0': '2014-01-02', 'drf__ham__gte_1': '03:04:05',
                  'drf__ham__lte_0': None, 'drf__ham__lte_1': None}
        filter_ = DateTimeRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham')

        return_value = filter_.queryset(self.request, queryset)
        timezoned_expected_result = timezone.make_aware(datetime(2014, 1, 2, 3, 4, 5), timezone.get_current_timezone())

        self.assertEqual(return_value, queryset.filter.return_value)
        self.assertItemsEqual([call(ham__gte=timezoned_expected_result)], queryset.filter.call_args_list)
Ejemplo n.º 5
0
    def test_return_raw_queryset_if_form_is_invalid(self):
        queryset = Mock()
        params = {
            'drf__ham__gte_0': 'Yay!',
            'drf__ham__gte_1': None,
            'drf__ham__lte_0': None,
            'drf__ham__lte_1': None
        }
        filter_ = DateTimeRangeFilter('spam', self.request, params, Mock(),
                                      Mock(), 'ham')

        return_value = filter_.queryset(self.request, queryset)

        self.assertEqual(return_value, queryset)
Ejemplo n.º 6
0
    def test_queryset_ignore_null_fields(self):
        queryset = Mock()
        params = {
            'drf__ham__gte_0': None,
            'drf__ham__gte_1': None,
            'drf__ham__lte_0': None,
            'drf__ham__lte_1': None
        }
        filter_ = DateTimeRangeFilter('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)
Ejemplo n.º 7
0
    def test_queryset_filters_by_date_to(self):
        queryset = Mock()
        data_end = timezone.make_aware(datetime(2014, 1, 2, 3, 4, 5),
                                       timezone.get_current_timezone())
        params = {
            'drf__ham__lte_0': data_end.strftime('%Y-%m-%d'),
            'drf__ham__lte_1': data_end.strftime('%H:%M:%S'),
            'drf__ham__gte_0': None,
            'drf__ham__gte_1': None
        }
        filter_ = DateTimeRangeFilter('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,
                         queryset.filter.call_args_list[0][1]['ham__lte'])
Ejemplo n.º 8
0
    def test_queryset_filters_by_date_from(self):
        queryset = Mock()
        params = {
            'drf__ham__gte_0': '2014-01-02',
            'drf__ham__gte_1': '03:04:05',
            'drf__ham__lte_0': None,
            'drf__ham__lte_1': None
        }
        filter_ = DateTimeRangeFilter('spam', self.request, params, Mock(),
                                      Mock(), 'ham')

        return_value = filter_.queryset(self.request, queryset)
        timezoned_expected_result = timezone.make_aware(
            datetime(2014, 1, 2, 3, 4, 5), timezone.get_current_timezone())

        self.assertEqual(return_value, queryset.filter.return_value)
        self.assertCountEqual([call(ham__gte=timezoned_expected_result)],
                              queryset.filter.call_args_list)
Ejemplo n.º 9
0
 def setUp(self):
     self.request = Mock()
     self.filter_ = DateTimeRangeFilter('spam', self.request, {
         'drf__egg__lte_0': None,
         'drf__egg__lte_1': None
     }, Mock(), Mock(), 'egg')
Ejemplo n.º 10
0
class DateTimeRangeFilterTest(TestCase):
    def setUp(self):
        self.request = Mock()
        self.filter_ = DateTimeRangeFilter('spam', self.request, {
            'drf__egg__lte_0': None,
            'drf__egg__lte_1': None
        }, 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_0', 'drf__egg__lte_1', 'drf__egg__gte_0',
            'drf__egg__gte_1'
        ])

    @patch('daterange_filter.filter.DateTimeRangeForm')
    def test_get_form(self, DateTimeRangeForm):
        self.filter_.get_form(self.request)

        self.assertEqual([
            call(self.request,
                 data={
                     'drf__egg__lte_0': None,
                     'drf__egg__lte_1': None
                 },
                 field_name='egg')
        ], DateTimeRangeForm.call_args_list)

    def test_queryset_ignore_null_fields(self):
        queryset = Mock()
        params = {
            'drf__ham__gte_0': None,
            'drf__ham__gte_1': None,
            'drf__ham__lte_0': None,
            'drf__ham__lte_1': None
        }
        filter_ = DateTimeRangeFilter('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_0': '2014-01-02',
            'drf__ham__gte_1': '03:04:05',
            'drf__ham__lte_0': None,
            'drf__ham__lte_1': None
        }
        filter_ = DateTimeRangeFilter('spam', self.request, params, Mock(),
                                      Mock(), 'ham')

        return_value = filter_.queryset(self.request, queryset)
        timezoned_expected_result = timezone.make_aware(
            datetime(2014, 1, 2, 3, 4, 5), timezone.get_current_timezone())

        self.assertEqual(return_value, queryset.filter.return_value)
        self.assertCountEqual([call(ham__gte=timezoned_expected_result)],
                              queryset.filter.call_args_list)

    def test_queryset_filters_by_date_to(self):
        queryset = Mock()
        data_end = timezone.make_aware(datetime(2014, 1, 2, 3, 4, 5),
                                       timezone.get_current_timezone())
        params = {
            'drf__ham__lte_0': data_end.strftime('%Y-%m-%d'),
            'drf__ham__lte_1': data_end.strftime('%H:%M:%S'),
            'drf__ham__gte_0': None,
            'drf__ham__gte_1': None
        }
        filter_ = DateTimeRangeFilter('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,
                         queryset.filter.call_args_list[0][1]['ham__lte'])

    def test_return_raw_queryset_if_form_is_invalid(self):
        queryset = Mock()
        params = {
            'drf__ham__gte_0': 'Yay!',
            'drf__ham__gte_1': None,
            'drf__ham__lte_0': None,
            'drf__ham__lte_1': None
        }
        filter_ = DateTimeRangeFilter('spam', self.request, params, Mock(),
                                      Mock(), 'ham')

        return_value = filter_.queryset(self.request, queryset)

        self.assertEqual(return_value, queryset)
 def setUp(self):
     self.request = Mock()
     self.filter_ = DateTimeRangeFilter('spam', self.request, {'drf__egg__lte_0': None, 'drf__egg__lte_1': None}, Mock(),
                                        Mock(), 'egg')
class DateTimeRangeFilterTest(BaseTest):
    def setUp(self):
        self.request = Mock()
        self.filter_ = DateTimeRangeFilter('spam', self.request, {'drf__egg__lte_0': None, 'drf__egg__lte_1': None}, 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_0', 'drf__egg__lte_1', 'drf__egg__gte_0', 'drf__egg__gte_1'])

    @patch('daterange_filter.filter.DateTimeRangeForm')
    def test_get_form(self, DateTimeRangeForm):
        self.filter_.get_form(self.request)

        self.assertEqual([call(self.request, data={'drf__egg__lte_0': None, 'drf__egg__lte_1': None}, field_name='egg')],
                          DateTimeRangeForm.call_args_list)

    def test_queryset_ignore_null_fields(self):
        queryset = Mock()
        params = {'drf__ham__gte_0': None, 'drf__ham__gte_1': None, 'drf__ham__lte_0': None, 'drf__ham__lte_1': None}
        filter_ = DateTimeRangeFilter('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_0': '2014-01-02', 'drf__ham__gte_1': '03:04:05',
                  'drf__ham__lte_0': None, 'drf__ham__lte_1': None}
        filter_ = DateTimeRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham')

        return_value = filter_.queryset(self.request, queryset)
        timezoned_expected_result = timezone.make_aware(datetime(2014, 1, 2, 3, 4, 5), timezone.get_current_timezone())

        self.assertEqual(return_value, queryset.filter.return_value)
        self.assertItemsEqual([call(ham__gte=timezoned_expected_result)], queryset.filter.call_args_list)

    def test_queryset_filters_by_date_to(self):
        queryset = Mock()
        data_end = timezone.make_aware(datetime(2014, 1, 2, 3, 4, 5), timezone.get_current_timezone())
        params = {'drf__ham__lte_0': data_end.strftime('%Y-%m-%d'), 'drf__ham__lte_1': data_end.strftime('%H:%M:%S'),
                  'drf__ham__gte_0': None, 'drf__ham__gte_1': None}
        filter_ = DateTimeRangeFilter('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, queryset.filter.call_args_list[0][1]['ham__lte'])

    def test_return_raw_queryset_if_form_is_invalid(self):
        queryset = Mock()
        params = {'drf__ham__gte_0': 'Yay!', 'drf__ham__gte_1': None, 'drf__ham__lte_0': None, 'drf__ham__lte_1': None}
        filter_ = DateTimeRangeFilter('spam', self.request, params, Mock(), Mock(), 'ham')

        return_value = filter_.queryset(self.request, queryset)

        self.assertEqual(return_value, queryset)