def test_filtering_for_this_year(self): qs = mock.Mock(spec=['filter']) with mock.patch('django_filters.filters.now') as mock_now: now_dt = mock_now.return_value f = DateRangeFilter() f.filter(qs, '4') qs.filter.assert_called_once_with(None__year=now_dt.year)
def test_filtering_for_this_month(self): qs = mock.Mock(spec=['filter']) with mock.patch('django_filters.filters.now') as mock_now: now_dt = mock_now.return_value f = DateRangeFilter() f.filter(qs, '3') qs.filter.assert_called_once_with( None__year=now_dt.year, None__month=now_dt.month)
def test_filtering_for_yesterday(self): qs = mock.Mock(spec=['filter']) with mock.patch('django_filters.filters.now') as mock_now: now_dt = mock_now.return_value f = DateRangeFilter() f.filter(qs, '5') qs.filter.assert_called_once_with( None__year=now_dt.year, None__month=now_dt.month, None__day=(now_dt - timedelta(days=1)).day, )
def test_filtering_for_7_days(self): qs = mock.Mock(spec=['filter']) with mock.patch('django_filters.filters.now'): with mock.patch('django_filters.filters.timedelta') as mock_td: with mock.patch( 'django_filters.filters._truncate') as mock_truncate: mock_dt1, mock_dt2 = mock.MagicMock(), mock.MagicMock() mock_truncate.side_effect = [mock_dt1, mock_dt2] f = DateRangeFilter() f.filter(qs, '2') self.assertEqual(mock_td.call_args_list, [mock.call(days=7), mock.call(days=1)]) qs.filter.assert_called_once_with( None__lt=mock_dt2, None__gte=mock_dt1)
class Birthday_serializer(serializers.ModelSerializer): """ This is the serializer for the Birthday service """ birthday = serializers.DateField(input_formats=['%d.%m.%Y']) date_range = DateRangeFilter(field_name='birthday') class Meta: model = Details fields = ['first_name', "last_name", "email", "birthday"]
class NotificationFilter(django_filters.FilterSet): timestamp_choices = [('today', _('Today')), ('yesterday', _('Yesterday')), ('week', _('Past 7 days')), ('month', _('This month'))] timestamp_filters = { 'today': lambda qs, name: qs.filter( **{ '%s__year' % name: now().year, '%s__month' % name: now().month, '%s__day' % name: now().day }), 'yesterday': lambda qs, name: qs.filter( **{ '%s__year' % name: (now() - timedelta(days=1)).year, '%s__month' % name: (now() - timedelta(days=1)).month, '%s__day' % name: (now() - timedelta(days=1)).day, }), 'week': lambda qs, name: qs.filter( **{ '%s__gte' % name: _truncate(now() - timedelta(days=7)), '%s__lt' % name: _truncate(now() + timedelta(days=1)), }), 'month': lambda qs, name: qs.filter(**{ '%s__year' % name: now().year, '%s__month' % name: now().month }) } date = DateRangeFilter(field_name='timestamp', choices=timestamp_choices, filters=timestamp_filters) class Meta: model = Activity fields = {}
def test_filtering_skipped_with_out_of_range_value(self): qs = mock.Mock(spec=[]) f = DateRangeFilter() result = f.filter(qs, 999) self.assertEqual(qs, result)
def test_filtering_skipped_with_blank_value(self): qs = mock.Mock(spec=[]) f = DateRangeFilter() result = f.filter(qs, '') self.assertEqual(qs, result)
def test_filtering(self): qs = mock.Mock(spec=['all']) f = DateRangeFilter() f.filter(qs, '') qs.all.assert_called_once_with()
class F(FilterSet): date = DateRangeFilter() class Meta: model = Comment fields = ['date']
def test_filtering_skipped_with_out_of_range_value(self): # Field validation should prevent this from occuring qs = mock.Mock(spec=[]) f = DateRangeFilter() with self.assertRaises(AssertionError): f.filter(qs, 999)
def test_filtering(self): # skip filtering, as it's an empty value qs = mock.Mock(spec=[]) f = DateRangeFilter() result = f.filter(qs, '') self.assertEqual(qs, result)
def test_creating(self): f = DateRangeFilter() self.assertIn('choices', f.extra) self.assertEqual(len(DateRangeFilter.options), len(f.extra['choices']))
def test_default_field(self): f = DateRangeFilter() field = f.field self.assertIsInstance(field, forms.ChoiceField)
class F(FilterSet): date = DateRangeFilter(widget=LinkWidget) class Meta: model = Comment fields = ['date']
class F(FilterSet): published = DateRangeFilter() class Meta: model = Article
class F(FilterSet): f = DateRangeFilter(method='filter_f') def filter_f(self, qs, name, value): pass