| 1 |
import contextlib |
| 2 |
import datetime |
| 3 |
import unittest |
| 4 |
from operator import attrgetter |
| 5 |
from unittest import mock |
| 6 |
|
| 7 |
import django |
| 8 |
from django import forms |
| 9 |
from django.http import QueryDict |
| 10 |
from django.test import TestCase, override_settings |
| 11 |
from django.utils import timezone |
| 12 |
from django.utils.timezone import make_aware, now |
| 13 |
|
| 14 |
from django_filters.filters import ( |
| 15 |
AllValuesFilter, |
| 16 |
AllValuesMultipleFilter, |
| 17 |
BaseInFilter, |
| 18 |
CharFilter, |
| 19 |
ChoiceFilter, |
| 20 |
DateFromToRangeFilter, |
| 21 |
DateRangeFilter, |
| 22 |
DateTimeFromToRangeFilter, |
| 23 |
DurationFilter, |
| 24 |
IsoDateTimeFromToRangeFilter, |
| 25 |
LookupChoiceFilter, |
| 26 |
ModelChoiceFilter, |
| 27 |
ModelMultipleChoiceFilter, |
| 28 |
MultipleChoiceFilter, |
| 29 |
OrderingFilter, |
| 30 |
RangeFilter, |
| 31 |
TimeRangeFilter, |
| 32 |
TypedMultipleChoiceFilter, |
| 33 |
) |
| 34 |
from django_filters.filterset import FilterSet |
| 35 |
from django_filters.widgets import QueryArrayWidget |
| 36 |
|
| 37 |
from .models import ( |
| 38 |
STATUS_CHOICES, |
| 39 |
Account, |
| 40 |
Article, |
| 41 |
BankAccount, |
| 42 |
Book, |
| 43 |
Comment, |
| 44 |
Company, |
| 45 |
DirectedNode, |
| 46 |
Location, |
| 47 |
Node, |
| 48 |
Profile, |
| 49 |
SpacewalkRecord, |
| 50 |
User, |
| 51 |
) |
| 52 |
from .utils import MockQuerySet |
| 53 |
|
| 54 |
|
| 55 |
class CharFilterTests(TestCase): |
| 56 |
def test_filtering(self): |
| 57 |
b1 = Book.objects.create(title="Ender's Game", price="1.00", average_rating=3.0) |
| 58 |
b2 = Book.objects.create(title="Rainbow Six", price="1.00", average_rating=3.0) |
| 59 |
b3 = Book.objects.create(title="Snowcrash", price="1.00", average_rating=3.0) |
| 60 |
|
| 61 |
class F(FilterSet): |
| 62 |
class Meta: |
| 63 |
model = Book |
| 64 |
fields = ["title"] |
| 65 |
|
| 66 |
qs = Book.objects.all() |
| 67 |
f = F(queryset=qs) |
| 68 |
self.assertQuerySetEqual( |
| 69 |
f.qs, [b1.pk, b2.pk, b3.pk], lambda o: o.pk, ordered=False |
| 70 |
) |
| 71 |
f = F({"title": "Snowcrash"}, queryset=qs) |
| 72 |
self.assertQuerySetEqual(f.qs, [b3.pk], lambda o: o.pk) |
| 73 |
|
| 74 |
|
| 75 |
class IntegerFilterTest(TestCase): |
| 76 |
def test_filtering(self): |
| 77 |
default_values = { |
| 78 |
"in_good_standing": True, |
| 79 |
"friendly": False, |
| 80 |
} |
| 81 |
b1 = BankAccount.objects.create(amount_saved=0, **default_values) |
| 82 |
b2 = BankAccount.objects.create(amount_saved=3, **default_values) |
| 83 |
b3 = BankAccount.objects.create(amount_saved=10, **default_values) |
| 84 |
|
| 85 |
class F(FilterSet): |
| 86 |
class Meta: |
| 87 |
model = BankAccount |
| 88 |
fields = ["amount_saved"] |
| 89 |
|
| 90 |
qs = BankAccount.objects.all() |
| 91 |
f = F(queryset=qs) |
| 92 |
self.assertQuerySetEqual( |
| 93 |
f.qs, [b1.pk, b2.pk, b3.pk], lambda o: o.pk, ordered=False |
| 94 |
) |
| 95 |
f = F({"amount_saved": "10"}, queryset=qs) |
| 96 |
self.assertQuerySetEqual(f.qs, [b3.pk], lambda o: o.pk) |
| 97 |
f = F({"amount_saved": "0"}, queryset=qs) |
| 98 |
self.assertQuerySetEqual(f.qs, [b1.pk], lambda o: o.pk) |
| 99 |
|
| 100 |
|
| 101 |
class BooleanFilterTests(TestCase): |
| 102 |
def test_filtering(self): |
| 103 |
User.objects.create(username="alex", is_active=False) |
| 104 |
User.objects.create(username="jacob", is_active=True) |
| 105 |
User.objects.create(username="aaron", is_active=False) |
| 106 |
|
| 107 |
class F(FilterSet): |
| 108 |
class Meta: |
| 109 |
model = User |
| 110 |
fields = ["is_active"] |
| 111 |
|
| 112 |
qs = User.objects.all() |
| 113 |
|
| 114 |
# '2' and '3' are how the field expects the data from the browser |
| 115 |
f = F({"is_active": "2"}, queryset=qs) |
| 116 |
self.assertQuerySetEqual(f.qs, ["jacob"], lambda o: o.username, False) |
| 117 |
|
| 118 |
f = F({"is_active": "3"}, queryset=qs) |
| 119 |
self.assertQuerySetEqual(f.qs, ["alex", "aaron"], lambda o: o.username, False) |
| 120 |
|
| 121 |
f = F({"is_active": "1"}, queryset=qs) |
| 122 |
self.assertQuerySetEqual( |
| 123 |
f.qs, ["alex", "aaron", "jacob"], lambda o: o.username, False |
| 124 |
) |
| 125 |
|
| 126 |
|
| 127 |
class ChoiceFilterTests(TestCase): |
| 128 |
@classmethod |
| 129 |
def setUpTestData(cls): |
| 130 |
User.objects.create(username="alex", status=1) |
| 131 |
User.objects.create(username="jacob", status=2) |
| 132 |
User.objects.create(username="aaron", status=2) |
| 133 |
User.objects.create(username="carl", status=0) |
| 134 |
|
| 135 |
Article.objects.create(author_id=1, published=now()) |
| 136 |
Article.objects.create(author_id=2, published=now()) |
| 137 |
Article.objects.create(author_id=3, published=now()) |
| 138 |
Article.objects.create(author_id=4, published=now()) |
| 139 |
Article.objects.create(author_id=None, published=now()) |
| 140 |
|
| 141 |
def test_filtering(self): |
| 142 |
class F(FilterSet): |
| 143 |
class Meta: |
| 144 |
model = User |
| 145 |
fields = ["status"] |
| 146 |
|
| 147 |
f = F() |
| 148 |
self.assertQuerySetEqual( |
| 149 |
f.qs, ["aaron", "alex", "jacob", "carl"], lambda o: o.username, False |
| 150 |
) |
| 151 |
f = F({"status": "1"}) |
| 152 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username, False) |
| 153 |
|
| 154 |
f = F({"status": "2"}) |
| 155 |
self.assertQuerySetEqual(f.qs, ["jacob", "aaron"], lambda o: o.username, False) |
| 156 |
|
| 157 |
f = F({"status": "0"}) |
| 158 |
self.assertQuerySetEqual(f.qs, ["carl"], lambda o: o.username, False) |
| 159 |
|
| 160 |
def test_filtering_on_explicitly_defined_field(self): |
| 161 |
""" |
| 162 |
Test for #30. |
| 163 |
|
| 164 |
If you explicitly declare ChoiceFilter fields you **MUST** pass `choices`. |
| 165 |
""" |
| 166 |
|
| 167 |
class F(FilterSet): |
| 168 |
status = ChoiceFilter(choices=STATUS_CHOICES) |
| 169 |
|
| 170 |
class Meta: |
| 171 |
model = User |
| 172 |
fields = ["status"] |
| 173 |
|
| 174 |
f = F() |
| 175 |
self.assertQuerySetEqual( |
| 176 |
f.qs, ["aaron", "alex", "jacob", "carl"], lambda o: o.username, False |
| 177 |
) |
| 178 |
f = F({"status": "1"}) |
| 179 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username, False) |
| 180 |
|
| 181 |
f = F({"status": "2"}) |
| 182 |
self.assertQuerySetEqual(f.qs, ["jacob", "aaron"], lambda o: o.username, False) |
| 183 |
|
| 184 |
f = F({"status": "0"}) |
| 185 |
self.assertQuerySetEqual(f.qs, ["carl"], lambda o: o.username, False) |
| 186 |
|
| 187 |
def test_filtering_on_empty_choice(self): |
| 188 |
class F(FilterSet): |
| 189 |
class Meta: |
| 190 |
model = User |
| 191 |
fields = ["status"] |
| 192 |
|
| 193 |
f = F({"status": ""}) |
| 194 |
self.assertQuerySetEqual( |
| 195 |
f.qs, ["aaron", "alex", "jacob", "carl"], lambda o: o.username, False |
| 196 |
) |
| 197 |
|
| 198 |
def test_filtering_on_null_choice(self): |
| 199 |
choices = [(u.pk, str(u)) for u in User.objects.order_by("id")] |
| 200 |
|
| 201 |
class F(FilterSet): |
| 202 |
author = ChoiceFilter( |
| 203 |
choices=choices, |
| 204 |
null_value="null", |
| 205 |
null_label="NULL", |
| 206 |
) |
| 207 |
|
| 208 |
class Meta: |
| 209 |
model = Article |
| 210 |
fields = ["author"] |
| 211 |
|
| 212 |
# sanity check to make sure the filter is setup correctly |
| 213 |
f = F({"author": "1"}) |
| 214 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: str(o.author), False) |
| 215 |
|
| 216 |
f = F({"author": "null"}) |
| 217 |
self.assertQuerySetEqual(f.qs, [None], lambda o: o.author, False) |
| 218 |
|
| 219 |
|
| 220 |
class MultipleChoiceFilterTests(TestCase): |
| 221 |
def test_filtering(self): |
| 222 |
User.objects.create(username="alex", status=1) |
| 223 |
User.objects.create(username="jacob", status=2) |
| 224 |
User.objects.create(username="aaron", status=2) |
| 225 |
User.objects.create(username="carl", status=0) |
| 226 |
|
| 227 |
class F(FilterSet): |
| 228 |
status = MultipleChoiceFilter(choices=STATUS_CHOICES) |
| 229 |
|
| 230 |
class Meta: |
| 231 |
model = User |
| 232 |
fields = ["status"] |
| 233 |
|
| 234 |
qs = User.objects.all().order_by("username") |
| 235 |
f = F(queryset=qs) |
| 236 |
self.assertQuerySetEqual( |
| 237 |
f.qs, ["aaron", "jacob", "alex", "carl"], lambda o: o.username, False |
| 238 |
) |
| 239 |
|
| 240 |
f = F({"status": ["0"]}, queryset=qs) |
| 241 |
self.assertQuerySetEqual(f.qs, ["carl"], lambda o: o.username) |
| 242 |
|
| 243 |
f = F({"status": ["0", "1"]}, queryset=qs) |
| 244 |
self.assertQuerySetEqual(f.qs, ["alex", "carl"], lambda o: o.username) |
| 245 |
|
| 246 |
f = F({"status": ["0", "1", "2"]}, queryset=qs) |
| 247 |
self.assertQuerySetEqual( |
| 248 |
f.qs, ["aaron", "alex", "carl", "jacob"], lambda o: o.username |
| 249 |
) |
| 250 |
|
| 251 |
def test_filtering_on_null_choice(self): |
| 252 |
User.objects.create(username="alex", status=1) |
| 253 |
User.objects.create(username="jacob", status=2) |
| 254 |
User.objects.create(username="aaron", status=2) |
| 255 |
User.objects.create(username="carl", status=0) |
| 256 |
|
| 257 |
Article.objects.create(author_id=1, published=now()) |
| 258 |
Article.objects.create(author_id=2, published=now()) |
| 259 |
Article.objects.create(author_id=3, published=now()) |
| 260 |
Article.objects.create(author_id=4, published=now()) |
| 261 |
Article.objects.create(author_id=None, published=now()) |
| 262 |
|
| 263 |
choices = [(u.pk, str(u)) for u in User.objects.order_by("id")] |
| 264 |
|
| 265 |
class F(FilterSet): |
| 266 |
author = MultipleChoiceFilter( |
| 267 |
choices=choices, |
| 268 |
null_value="null", |
| 269 |
null_label="NULL", |
| 270 |
) |
| 271 |
|
| 272 |
class Meta: |
| 273 |
model = Article |
| 274 |
fields = ["author"] |
| 275 |
|
| 276 |
# sanity check to make sure the filter is setup correctly |
| 277 |
f = F({"author": ["1"]}) |
| 278 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: str(o.author), False) |
| 279 |
|
| 280 |
f = F({"author": ["null"]}) |
| 281 |
self.assertQuerySetEqual(f.qs, [None], lambda o: o.author, False) |
| 282 |
|
| 283 |
f = F({"author": ["1", "null"]}) |
| 284 |
self.assertQuerySetEqual( |
| 285 |
f.qs, ["alex", None], lambda o: o.author and str(o.author), False |
| 286 |
) |
| 287 |
|
| 288 |
|
| 289 |
class TypedMultipleChoiceFilterTests(TestCase): |
| 290 |
def test_filtering(self): |
| 291 |
User.objects.create(username="alex", status=1) |
| 292 |
User.objects.create(username="jacob", status=2) |
| 293 |
User.objects.create(username="aaron", status=2) |
| 294 |
User.objects.create(username="carl", status=0) |
| 295 |
|
| 296 |
class F(FilterSet): |
| 297 |
status = TypedMultipleChoiceFilter( |
| 298 |
choices=STATUS_CHOICES, coerce=lambda x: x[0:2] |
| 299 |
) |
| 300 |
|
| 301 |
class Meta: |
| 302 |
model = User |
| 303 |
fields = ["status"] |
| 304 |
|
| 305 |
qs = User.objects.all().order_by("username") |
| 306 |
f = F(queryset=qs) |
| 307 |
self.assertQuerySetEqual( |
| 308 |
f.qs, ["aa", "ja", "al", "ca"], lambda o: o.username[0:2], False |
| 309 |
) |
| 310 |
|
| 311 |
f = F({"status": ["0"]}, queryset=qs) |
| 312 |
self.assertQuerySetEqual(f.qs, ["ca"], lambda o: o.username[0:2]) |
| 313 |
|
| 314 |
f = F({"status": ["0", "1"]}, queryset=qs) |
| 315 |
self.assertQuerySetEqual(f.qs, ["al", "ca"], lambda o: o.username[0:2]) |
| 316 |
|
| 317 |
f = F({"status": ["0", "1", "2"]}, queryset=qs) |
| 318 |
self.assertQuerySetEqual( |
| 319 |
f.qs, ["aa", "al", "ca", "ja"], lambda o: o.username[0:2] |
| 320 |
) |
| 321 |
|
| 322 |
|
| 323 |
class DateFilterTests(TestCase): |
| 324 |
def test_filtering(self): |
| 325 |
today = now().date() |
| 326 |
timestamp = now().time().replace(microsecond=0) |
| 327 |
last_week = today - datetime.timedelta(days=7) |
| 328 |
check_date = str(last_week) |
| 329 |
u = User.objects.create(username="alex") |
| 330 |
Comment.objects.create(author=u, time=timestamp, date=today) |
| 331 |
c2 = Comment.objects.create(author=u, time=timestamp, date=last_week) |
| 332 |
Comment.objects.create(author=u, time=timestamp, date=today) |
| 333 |
c4 = Comment.objects.create(author=u, time=timestamp, date=last_week) |
| 334 |
|
| 335 |
class F(FilterSet): |
| 336 |
class Meta: |
| 337 |
model = Comment |
| 338 |
fields = ["date"] |
| 339 |
|
| 340 |
f = F({"date": check_date}, queryset=Comment.objects.all()) |
| 341 |
self.assertEqual(len(f.qs), 2) |
| 342 |
self.assertQuerySetEqual(f.qs, [c2.pk, c4.pk], lambda o: o.pk, False) |
| 343 |
|
| 344 |
|
| 345 |
class TimeFilterTests(TestCase): |
| 346 |
def test_filtering(self): |
| 347 |
today = now().date() |
| 348 |
now_time = now().time().replace(microsecond=0) |
| 349 |
ten_min_ago = now() - datetime.timedelta(minutes=10) |
| 350 |
fixed_time = ten_min_ago.time().replace(microsecond=0) |
| 351 |
check_time = str(fixed_time) |
| 352 |
u = User.objects.create(username="alex") |
| 353 |
Comment.objects.create(author=u, time=now_time, date=today) |
| 354 |
c2 = Comment.objects.create(author=u, time=fixed_time, date=today) |
| 355 |
Comment.objects.create(author=u, time=now_time, date=today) |
| 356 |
c4 = Comment.objects.create(author=u, time=fixed_time, date=today) |
| 357 |
|
| 358 |
class F(FilterSet): |
| 359 |
class Meta: |
| 360 |
model = Comment |
| 361 |
fields = ["time"] |
| 362 |
|
| 363 |
f = F({"time": check_time}, queryset=Comment.objects.all()) |
| 364 |
self.assertEqual(len(f.qs), 2) |
| 365 |
self.assertQuerySetEqual(f.qs, [c2.pk, c4.pk], lambda o: o.pk, False) |
| 366 |
|
| 367 |
|
| 368 |
class DateTimeFilterTests(TestCase): |
| 369 |
def test_filtering(self): |
| 370 |
now_dt = now() |
| 371 |
ten_min_ago = now_dt - datetime.timedelta(minutes=10) |
| 372 |
one_day_ago = now_dt - datetime.timedelta(days=1) |
| 373 |
u = User.objects.create(username="alex") |
| 374 |
Article.objects.create(author=u, published=now_dt) |
| 375 |
a2 = Article.objects.create(author=u, published=ten_min_ago) |
| 376 |
Article.objects.create(author=u, published=one_day_ago) |
| 377 |
|
| 378 |
tz = timezone.get_current_timezone() |
| 379 |
# make naive, like a browser would send |
| 380 |
local_ten_min_ago = timezone.make_naive(ten_min_ago, tz) |
| 381 |
check_dt = str(local_ten_min_ago) |
| 382 |
|
| 383 |
class F(FilterSet): |
| 384 |
class Meta: |
| 385 |
model = Article |
| 386 |
fields = ["published"] |
| 387 |
|
| 388 |
qs = Article.objects.all() |
| 389 |
f = F({"published": ten_min_ago}, queryset=qs) |
| 390 |
self.assertEqual(len(f.qs), 1) |
| 391 |
self.assertQuerySetEqual(f.qs, [a2.pk], lambda o: o.pk) |
| 392 |
|
| 393 |
# this is how it would come through a browser |
| 394 |
f = F({"published": check_dt}, queryset=qs) |
| 395 |
self.assertEqual( |
| 396 |
len(f.qs), 1, "%s isn't matching %s when cleaned" % (check_dt, ten_min_ago) |
| 397 |
) |
| 398 |
self.assertQuerySetEqual(f.qs, [a2.pk], lambda o: o.pk) |
| 399 |
|
| 400 |
|
| 401 |
class DurationFilterTests(TestCase): |
| 402 |
"""Duration filter tests. |
| 403 |
|
| 404 |
The preferred format for durations in Django is '%d %H:%M:%S.%f'. |
| 405 |
See django.utils.dateparse.parse_duration |
| 406 |
|
| 407 |
Django is not fully ISO 8601 compliant (yet): year, month, and |
| 408 |
week designators are not supported, so a duration string |
| 409 |
like "P3Y6M4DT12H30M5S" cannot be used. |
| 410 |
See https://en.wikipedia.org/wiki/ISO_8601#Durations |
| 411 |
|
| 412 |
""" |
| 413 |
|
| 414 |
def setUp(self): |
| 415 |
self.r1 = SpacewalkRecord.objects.create( |
| 416 |
astronaut="Anatoly Solovyev", |
| 417 |
duration=datetime.timedelta(hours=82, minutes=22), |
| 418 |
) |
| 419 |
self.r2 = SpacewalkRecord.objects.create( |
| 420 |
astronaut="Michael Lopez-Alegria", |
| 421 |
duration=datetime.timedelta(hours=67, minutes=40), |
| 422 |
) |
| 423 |
self.r3 = SpacewalkRecord.objects.create( |
| 424 |
astronaut="Jerry L. Ross", duration=datetime.timedelta(hours=58, minutes=32) |
| 425 |
) |
| 426 |
self.r4 = SpacewalkRecord.objects.create( |
| 427 |
astronaut="John M. Grunsfeld", |
| 428 |
duration=datetime.timedelta(hours=58, minutes=30), |
| 429 |
) |
| 430 |
self.r5 = SpacewalkRecord.objects.create( |
| 431 |
astronaut="Richard Mastracchio", |
| 432 |
duration=datetime.timedelta(hours=53, minutes=4), |
| 433 |
) |
| 434 |
|
| 435 |
def test_filtering(self): |
| 436 |
class F(FilterSet): |
| 437 |
class Meta: |
| 438 |
model = SpacewalkRecord |
| 439 |
fields = ["duration"] |
| 440 |
|
| 441 |
qs = SpacewalkRecord.objects.all() |
| 442 |
|
| 443 |
# Django style: 3 days, 10 hours, 22 minutes. |
| 444 |
f = F({"duration": "3 10:22:00"}, queryset=qs) |
| 445 |
self.assertQuerySetEqual(f.qs, [self.r1], lambda x: x) |
| 446 |
|
| 447 |
# ISO 8601: 3 days, 10 hours, 22 minutes. |
| 448 |
f = F({"duration": "P3DT10H22M"}, queryset=qs) |
| 449 |
self.assertQuerySetEqual(f.qs, [self.r1], lambda x: x) |
| 450 |
|
| 451 |
# Django style: 82 hours, 22 minutes. |
| 452 |
f = F({"duration": "82:22:00"}, queryset=qs) |
| 453 |
self.assertQuerySetEqual(f.qs, [self.r1], lambda x: x) |
| 454 |
|
| 455 |
# ISO 8601: 82 hours, 22 minutes. |
| 456 |
f = F({"duration": "PT82H22M"}, queryset=qs) |
| 457 |
self.assertQuerySetEqual(f.qs, [self.r1], lambda x: x) |
| 458 |
|
| 459 |
def test_filtering_with_single_lookup_expr_dictionary(self): |
| 460 |
class F(FilterSet): |
| 461 |
class Meta: |
| 462 |
model = SpacewalkRecord |
| 463 |
fields = {"duration": ["gt", "gte", "lt", "lte"]} |
| 464 |
|
| 465 |
qs = SpacewalkRecord.objects.order_by("-duration") |
| 466 |
|
| 467 |
f = F({"duration__gt": "PT58H30M"}, queryset=qs) |
| 468 |
self.assertQuerySetEqual(f.qs, [self.r1, self.r2, self.r3], lambda x: x) |
| 469 |
|
| 470 |
f = F({"duration__gte": "PT58H30M"}, queryset=qs) |
| 471 |
self.assertQuerySetEqual( |
| 472 |
f.qs, [self.r1, self.r2, self.r3, self.r4], lambda x: x |
| 473 |
) |
| 474 |
|
| 475 |
f = F({"duration__lt": "PT58H30M"}, queryset=qs) |
| 476 |
self.assertQuerySetEqual(f.qs, [self.r5], lambda x: x) |
| 477 |
|
| 478 |
f = F({"duration__lte": "PT58H30M"}, queryset=qs) |
| 479 |
self.assertQuerySetEqual(f.qs, [self.r4, self.r5], lambda x: x) |
| 480 |
|
| 481 |
def test_filtering_with_multiple_lookup_exprs(self): |
| 482 |
class F(FilterSet): |
| 483 |
min_duration = DurationFilter(field_name="duration", lookup_expr="gte") |
| 484 |
max_duration = DurationFilter(field_name="duration", lookup_expr="lte") |
| 485 |
|
| 486 |
class Meta: |
| 487 |
model = SpacewalkRecord |
| 488 |
fields = "__all__" |
| 489 |
|
| 490 |
qs = SpacewalkRecord.objects.order_by("duration") |
| 491 |
|
| 492 |
f = F({"min_duration": "PT55H", "max_duration": "PT60H"}, queryset=qs) |
| 493 |
self.assertQuerySetEqual(f.qs, [self.r4, self.r3], lambda x: x) |
| 494 |
|
| 495 |
|
| 496 |
class ModelChoiceFilterTests(TestCase): |
| 497 |
def test_filtering(self): |
| 498 |
alex = User.objects.create(username="alex") |
| 499 |
jacob = User.objects.create(username="jacob") |
| 500 |
date = now().date() |
| 501 |
time = now().time() |
| 502 |
c1 = Comment.objects.create(author=jacob, time=time, date=date) |
| 503 |
Comment.objects.create(author=alex, time=time, date=date) |
| 504 |
c3 = Comment.objects.create(author=jacob, time=time, date=date) |
| 505 |
|
| 506 |
class F(FilterSet): |
| 507 |
class Meta: |
| 508 |
model = Comment |
| 509 |
fields = ["author"] |
| 510 |
|
| 511 |
qs = Comment.objects.all() |
| 512 |
f = F({"author": jacob.pk}, queryset=qs) |
| 513 |
|
| 514 |
self.assertQuerySetEqual(f.qs, [c1.pk, c3.pk], lambda o: o.pk, False) |
| 515 |
|
| 516 |
@override_settings(FILTERS_NULL_CHOICE_LABEL="No Author") |
| 517 |
def test_filtering_null(self): |
| 518 |
Article.objects.create(published=now()) |
| 519 |
alex = User.objects.create(username="alex") |
| 520 |
Article.objects.create(author=alex, published=now()) |
| 521 |
|
| 522 |
class F(FilterSet): |
| 523 |
class Meta: |
| 524 |
model = Article |
| 525 |
fields = ["author", "name"] |
| 526 |
|
| 527 |
qs = Article.objects.all() |
| 528 |
f = F({"author": "null"}, queryset=qs) |
| 529 |
self.assertQuerySetEqual(f.qs, [None], lambda o: o.author, False) |
| 530 |
|
| 531 |
def test_callable_queryset(self): |
| 532 |
# Sanity check for callable queryset arguments. |
| 533 |
# Ensure that nothing is improperly cached |
| 534 |
alex = User.objects.create(username="alex") |
| 535 |
jacob = User.objects.create(username="jacob") |
| 536 |
aaron = User.objects.create(username="aaron") |
| 537 |
|
| 538 |
def users(request): |
| 539 |
return User.objects.filter(pk__lt=request.user.pk) |
| 540 |
|
| 541 |
class F(FilterSet): |
| 542 |
author = ModelChoiceFilter(field_name="author", queryset=users) |
| 543 |
|
| 544 |
class Meta: |
| 545 |
model = Comment |
| 546 |
fields = ["author"] |
| 547 |
|
| 548 |
qs = Comment.objects.all() |
| 549 |
request = mock.Mock() |
| 550 |
|
| 551 |
request.user = jacob |
| 552 |
f = F(queryset=qs, request=request).filters["author"].field |
| 553 |
self.assertQuerySetEqual(f.queryset, [alex.pk], lambda o: o.pk, False) |
| 554 |
|
| 555 |
request.user = aaron |
| 556 |
f = F(queryset=qs, request=request).filters["author"].field |
| 557 |
self.assertQuerySetEqual(f.queryset, [alex.pk, jacob.pk], lambda o: o.pk, False) |
| 558 |
|
| 559 |
|
| 560 |
class ModelMultipleChoiceFilterTests(TestCase): |
| 561 |
def setUp(self): |
| 562 |
self.alex = User.objects.create(username="alex") |
| 563 |
User.objects.create(username="jacob") |
| 564 |
aaron = User.objects.create(username="aaron") |
| 565 |
self.b1 = Book.objects.create( |
| 566 |
title="Ender's Game", |
| 567 |
price="1.00", |
| 568 |
average_rating=3.0, |
| 569 |
) |
| 570 |
self.b2 = Book.objects.create( |
| 571 |
title="Rainbow Six", |
| 572 |
price="1.00", |
| 573 |
average_rating=3.0, |
| 574 |
) |
| 575 |
self.b3 = Book.objects.create( |
| 576 |
title="Snowcrash", |
| 577 |
price="1.00", |
| 578 |
average_rating=3.0, |
| 579 |
) |
| 580 |
self.b4 = Book.objects.create( |
| 581 |
title="Stranger in a Strage Land", |
| 582 |
price="1.00", |
| 583 |
average_rating=3.0, |
| 584 |
) |
| 585 |
self.alex.favorite_books.add(self.b1, self.b2) |
| 586 |
aaron.favorite_books.add(self.b1, self.b3) |
| 587 |
|
| 588 |
def test_filtering(self): |
| 589 |
class F(FilterSet): |
| 590 |
class Meta: |
| 591 |
model = User |
| 592 |
fields = ["favorite_books"] |
| 593 |
|
| 594 |
qs = User.objects.all().order_by("username") |
| 595 |
f = F({"favorite_books": [self.b1.pk]}, queryset=qs) |
| 596 |
self.assertQuerySetEqual(f.qs, ["aaron", "alex"], lambda o: o.username) |
| 597 |
|
| 598 |
f = F({"favorite_books": [self.b1.pk, self.b3.pk]}, queryset=qs) |
| 599 |
self.assertQuerySetEqual(f.qs, ["aaron", "alex"], lambda o: o.username) |
| 600 |
|
| 601 |
f = F({"favorite_books": [self.b2.pk]}, queryset=qs) |
| 602 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username) |
| 603 |
|
| 604 |
f = F({"favorite_books": [self.b4.pk]}, queryset=qs) |
| 605 |
self.assertQuerySetEqual(f.qs, [], lambda o: o.username) |
| 606 |
|
| 607 |
@override_settings(FILTERS_NULL_CHOICE_LABEL="No Favorites") |
| 608 |
def test_filtering_null(self): |
| 609 |
class F(FilterSet): |
| 610 |
class Meta: |
| 611 |
model = User |
| 612 |
fields = ["favorite_books"] |
| 613 |
|
| 614 |
qs = User.objects.all() |
| 615 |
f = F({"favorite_books": ["null"]}, queryset=qs) |
| 616 |
|
| 617 |
self.assertQuerySetEqual(f.qs, ["jacob"], lambda o: o.username) |
| 618 |
|
| 619 |
def test_filtering_dictionary(self): |
| 620 |
class F(FilterSet): |
| 621 |
class Meta: |
| 622 |
model = User |
| 623 |
fields = {"favorite_books": ["exact"]} |
| 624 |
|
| 625 |
qs = User.objects.all().order_by("username") |
| 626 |
f = F({"favorite_books": [self.b1.pk]}, queryset=qs) |
| 627 |
self.assertQuerySetEqual(f.qs, ["aaron", "alex"], lambda o: o.username) |
| 628 |
|
| 629 |
f = F({"favorite_books": [self.b1.pk, self.b3.pk]}, queryset=qs) |
| 630 |
self.assertQuerySetEqual(f.qs, ["aaron", "alex"], lambda o: o.username) |
| 631 |
|
| 632 |
f = F({"favorite_books": [self.b2.pk]}, queryset=qs) |
| 633 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username) |
| 634 |
|
| 635 |
f = F({"favorite_books": [self.b4.pk]}, queryset=qs) |
| 636 |
self.assertQuerySetEqual(f.qs, [], lambda o: o.username) |
| 637 |
|
| 638 |
def test_filtering_on_all_of_subset_of_choices(self): |
| 639 |
parent = self |
| 640 |
|
| 641 |
class F(FilterSet): |
| 642 |
class Meta: |
| 643 |
model = User |
| 644 |
fields = ["favorite_books"] |
| 645 |
|
| 646 |
def __init__(self, *args, **kwargs): |
| 647 |
super().__init__(*args, **kwargs) |
| 648 |
# This filter has a limited number of choices. |
| 649 |
self.filters["favorite_books"].extra.update( |
| 650 |
{ |
| 651 |
"queryset": Book.objects.filter( |
| 652 |
id__in=[parent.b1.pk, parent.b2.pk] |
| 653 |
) |
| 654 |
} |
| 655 |
) |
| 656 |
|
| 657 |
self.filters["favorite_books"].extra["required"] = True |
| 658 |
|
| 659 |
qs = User.objects.all().order_by("username") |
| 660 |
|
| 661 |
# Select all the given choices. |
| 662 |
f = F({"favorite_books": [self.b1.pk, self.b2.pk]}, queryset=qs) |
| 663 |
|
| 664 |
# The results should only include matching users - not Jacob. |
| 665 |
self.assertQuerySetEqual(f.qs, ["aaron", "alex"], lambda o: o.username) |
| 666 |
|
| 667 |
def test_filtering_on_non_required_fields(self): |
| 668 |
# See issue #132 - filtering with all options on a non-required |
| 669 |
# field should exclude any results where the field is null. |
| 670 |
class F(FilterSet): |
| 671 |
author = ModelMultipleChoiceFilter(queryset=User.objects.all()) |
| 672 |
|
| 673 |
class Meta: |
| 674 |
model = Article |
| 675 |
fields = ["author"] |
| 676 |
|
| 677 |
published = now() |
| 678 |
Article.objects.create(published=published, author=self.alex) |
| 679 |
Article.objects.create(published=published, author=self.alex) |
| 680 |
Article.objects.create(published=published) |
| 681 |
|
| 682 |
qs = Article.objects.all() |
| 683 |
|
| 684 |
# Select all authors. |
| 685 |
authors = [str(user.id) for user in User.objects.all()] |
| 686 |
f = F({"author": authors}, queryset=qs) |
| 687 |
|
| 688 |
# The results should not include anonymous articles |
| 689 |
self.assertEqual( |
| 690 |
set(f.qs), |
| 691 |
set(Article.objects.exclude(author__isnull=True)), |
| 692 |
) |
| 693 |
|
| 694 |
|
| 695 |
class NumberFilterTests(TestCase): |
| 696 |
def setUp(self): |
| 697 |
Book.objects.create( |
| 698 |
title="Ender's Game", price="10.0", average_rating=4.7999999999999998 |
| 699 |
) |
| 700 |
Book.objects.create( |
| 701 |
title="Rainbow Six", price="15.0", average_rating=4.5999999999999996 |
| 702 |
) |
| 703 |
Book.objects.create( |
| 704 |
title="Snowcrash", price="20.0", average_rating=4.2999999999999998 |
| 705 |
) |
| 706 |
|
| 707 |
def test_filtering(self): |
| 708 |
class F(FilterSet): |
| 709 |
class Meta: |
| 710 |
model = Book |
| 711 |
fields = ["price"] |
| 712 |
|
| 713 |
f = F({"price": 10}, queryset=Book.objects.all()) |
| 714 |
self.assertQuerySetEqual(f.qs, ["Ender's Game"], lambda o: o.title) |
| 715 |
|
| 716 |
|
| 717 |
class RangeFilterTests(TestCase): |
| 718 |
def setUp(self): |
| 719 |
Book.objects.create( |
| 720 |
title="Ender's Game", price="10.0", average_rating=4.7999999999999998 |
| 721 |
) |
| 722 |
Book.objects.create( |
| 723 |
title="Rainbow Six", price="15.0", average_rating=4.5999999999999996 |
| 724 |
) |
| 725 |
Book.objects.create( |
| 726 |
title="Snowcrash", price="20.0", average_rating=4.2999999999999998 |
| 727 |
) |
| 728 |
Book.objects.create(title="Refund", price="-10.0", average_rating=5.0) |
| 729 |
Book.objects.create(title="Free Book", price="0.0", average_rating=0.0) |
| 730 |
|
| 731 |
def test_filtering(self): |
| 732 |
class F(FilterSet): |
| 733 |
price = RangeFilter() |
| 734 |
|
| 735 |
class Meta: |
| 736 |
model = Book |
| 737 |
fields = ["price"] |
| 738 |
|
| 739 |
qs = Book.objects.all().order_by("title") |
| 740 |
f = F(queryset=qs) |
| 741 |
self.assertQuerySetEqual( |
| 742 |
f.qs, |
| 743 |
["Ender's Game", "Free Book", "Rainbow Six", "Refund", "Snowcrash"], |
| 744 |
lambda o: o.title, |
| 745 |
) |
| 746 |
f = F({"price_min": "5", "price_max": "15"}, queryset=qs) |
| 747 |
self.assertQuerySetEqual( |
| 748 |
f.qs, ["Ender's Game", "Rainbow Six"], lambda o: o.title |
| 749 |
) |
| 750 |
|
| 751 |
f = F({"price_min": "11"}, queryset=qs) |
| 752 |
self.assertQuerySetEqual(f.qs, ["Rainbow Six", "Snowcrash"], lambda o: o.title) |
| 753 |
f = F({"price_max": "19"}, queryset=qs) |
| 754 |
self.assertQuerySetEqual( |
| 755 |
f.qs, |
| 756 |
["Ender's Game", "Free Book", "Rainbow Six", "Refund"], |
| 757 |
lambda o: o.title, |
| 758 |
) |
| 759 |
|
| 760 |
f = F({"price_min": "0", "price_max": "12"}, queryset=qs) |
| 761 |
self.assertQuerySetEqual(f.qs, ["Ender's Game", "Free Book"], lambda o: o.title) |
| 762 |
f = F({"price_min": "-11", "price_max": "0"}, queryset=qs) |
| 763 |
self.assertQuerySetEqual(f.qs, ["Free Book", "Refund"], lambda o: o.title) |
| 764 |
f = F({"price_min": "0", "price_max": "0"}, queryset=qs) |
| 765 |
self.assertQuerySetEqual(f.qs, ["Free Book"], lambda o: o.title) |
| 766 |
|
| 767 |
|
| 768 |
class DateRangeFilterTests(TestCase): |
| 769 |
class CommentFilter(FilterSet): |
| 770 |
date = DateRangeFilter() |
| 771 |
|
| 772 |
class Meta: |
| 773 |
model = Comment |
| 774 |
fields = ["date"] |
| 775 |
|
| 776 |
@contextlib.contextmanager |
| 777 |
def relative_to(self, today): |
| 778 |
today = make_aware(today) |
| 779 |
yesterday = today - datetime.timedelta(days=1) |
| 780 |
five_days_ago = today - datetime.timedelta(days=5) |
| 781 |
two_weeks_ago = today - datetime.timedelta(days=14) |
| 782 |
two_months_ago = today - datetime.timedelta(days=62) |
| 783 |
two_years_ago = today - datetime.timedelta(days=800) |
| 784 |
|
| 785 |
alex = User.objects.create(username="alex") |
| 786 |
time = now().time() |
| 787 |
self.c1 = Comment.objects.create(date=two_weeks_ago, author=alex, time=time) |
| 788 |
Comment.objects.create(date=two_years_ago, author=alex, time=time) |
| 789 |
self.c3 = Comment.objects.create(date=five_days_ago, author=alex, time=time) |
| 790 |
self.c4 = Comment.objects.create(date=today, author=alex, time=time) |
| 791 |
self.c5 = Comment.objects.create(date=yesterday, author=alex, time=time) |
| 792 |
self.c6 = Comment.objects.create(date=two_months_ago, author=alex, time=time) |
| 793 |
|
| 794 |
with mock.patch("django_filters.filters.now") as mock_now: |
| 795 |
mock_now.return_value = today |
| 796 |
yield |
| 797 |
|
| 798 |
def test_filtering_for_year(self): |
| 799 |
f = self.CommentFilter({"date": "year"}) |
| 800 |
with self.relative_to(datetime.datetime(now().year, 4, 1)): |
| 801 |
self.assertQuerySetEqual( |
| 802 |
f.qs, |
| 803 |
[self.c1.pk, self.c3.pk, self.c4.pk, self.c5.pk, self.c6.pk], |
| 804 |
lambda o: o.pk, |
| 805 |
False, |
| 806 |
) |
| 807 |
|
| 808 |
def test_filtering_for_month(self): |
| 809 |
f = self.CommentFilter({"date": "month"}) |
| 810 |
with self.relative_to(datetime.datetime(now().year, 4, 21)): |
| 811 |
self.assertQuerySetEqual( |
| 812 |
f.qs, |
| 813 |
[self.c1.pk, self.c3.pk, self.c4.pk, self.c5.pk], |
| 814 |
lambda o: o.pk, |
| 815 |
False, |
| 816 |
) |
| 817 |
|
| 818 |
def test_filtering_for_week(self): |
| 819 |
f = self.CommentFilter({"date": "week"}) |
| 820 |
with self.relative_to(datetime.datetime(now().year, 1, 1)): |
| 821 |
self.assertQuerySetEqual( |
| 822 |
f.qs, [self.c3.pk, self.c4.pk, self.c5.pk], lambda o: o.pk, False |
| 823 |
) |
| 824 |
|
| 825 |
def test_filtering_for_yesterday(self): |
| 826 |
f = self.CommentFilter({"date": "yesterday"}) |
| 827 |
with self.relative_to(datetime.datetime(now().year, 1, 1)): |
| 828 |
self.assertQuerySetEqual(f.qs, [self.c5.pk], lambda o: o.pk, False) |
| 829 |
|
| 830 |
def test_filtering_for_today(self): |
| 831 |
f = self.CommentFilter({"date": "today"}) |
| 832 |
with self.relative_to(datetime.datetime(now().year, 1, 1)): |
| 833 |
self.assertQuerySetEqual(f.qs, [self.c4.pk], lambda o: o.pk, False) |
| 834 |
|
| 835 |
|
| 836 |
class DateFromToRangeFilterTests(TestCase): |
| 837 |
def test_filtering(self): |
| 838 |
adam = User.objects.create(username="adam") |
| 839 |
kwargs = {"text": "test", "author": adam, "time": "10:00"} |
| 840 |
Comment.objects.create(date=datetime.date(2016, 1, 1), **kwargs) |
| 841 |
Comment.objects.create(date=datetime.date(2016, 1, 2), **kwargs) |
| 842 |
Comment.objects.create(date=datetime.date(2016, 1, 3), **kwargs) |
| 843 |
Comment.objects.create(date=datetime.date(2016, 1, 3), **kwargs) |
| 844 |
|
| 845 |
class F(FilterSet): |
| 846 |
published = DateFromToRangeFilter(field_name="date") |
| 847 |
|
| 848 |
class Meta: |
| 849 |
model = Comment |
| 850 |
fields = ["date"] |
| 851 |
|
| 852 |
results = F( |
| 853 |
data={"published_after": "2016-01-02", "published_before": "2016-01-03"} |
| 854 |
) |
| 855 |
self.assertEqual(len(results.qs), 3) |
| 856 |
|
| 857 |
def test_filtering_ignores_time(self): |
| 858 |
tz = timezone.get_current_timezone() |
| 859 |
Article.objects.create( |
| 860 |
published=datetime.datetime(2016, 1, 1, 10, 0, tzinfo=tz) |
| 861 |
) |
| 862 |
Article.objects.create( |
| 863 |
published=datetime.datetime(2016, 1, 2, 12, 45, tzinfo=tz) |
| 864 |
) |
| 865 |
Article.objects.create( |
| 866 |
published=datetime.datetime(2016, 1, 3, 18, 15, tzinfo=tz) |
| 867 |
) |
| 868 |
Article.objects.create( |
| 869 |
published=datetime.datetime(2016, 1, 3, 19, 30, tzinfo=tz) |
| 870 |
) |
| 871 |
|
| 872 |
class F(FilterSet): |
| 873 |
published = DateFromToRangeFilter() |
| 874 |
|
| 875 |
class Meta: |
| 876 |
model = Article |
| 877 |
fields = ["published"] |
| 878 |
|
| 879 |
results = F( |
| 880 |
data={"published_after": "2016-01-02", "published_before": "2016-01-03"} |
| 881 |
) |
| 882 |
self.assertEqual(len(results.qs), 3) |
| 883 |
|
| 884 |
@unittest.skipUnless(django.VERSION < (5, 0), "is_dst removed in Django 5.0") |
| 885 |
@override_settings(TIME_ZONE="America/Sao_Paulo") |
| 886 |
def test_filtering_dst_start_midnight(self): |
| 887 |
tz = timezone.get_default_timezone() |
| 888 |
Article.objects.create( |
| 889 |
published=make_aware(datetime.datetime(2017, 10, 14, 23, 59), tz, False) |
| 890 |
) |
| 891 |
Article.objects.create( |
| 892 |
published=make_aware(datetime.datetime(2017, 10, 15, 0, 0), tz, False) |
| 893 |
) |
| 894 |
Article.objects.create( |
| 895 |
published=make_aware(datetime.datetime(2017, 10, 15, 1, 0), tz, False) |
| 896 |
) |
| 897 |
Article.objects.create( |
| 898 |
published=make_aware(datetime.datetime(2017, 10, 16, 0, 0), tz, False) |
| 899 |
) |
| 900 |
|
| 901 |
class F(FilterSet): |
| 902 |
published = DateFromToRangeFilter() |
| 903 |
|
| 904 |
class Meta: |
| 905 |
model = Article |
| 906 |
fields = ["published"] |
| 907 |
|
| 908 |
results = F( |
| 909 |
data={"published_after": "2017-10-15", "published_before": "2017-10-15"} |
| 910 |
) |
| 911 |
self.assertEqual(len(results.qs), 2) |
| 912 |
|
| 913 |
@unittest.skipUnless(django.VERSION < (5, 0), "is_dst removed in Django 5.0") |
| 914 |
@override_settings(TIME_ZONE="America/Sao_Paulo") |
| 915 |
def test_filtering_dst_ends_midnight(self): |
| 916 |
tz = timezone.get_default_timezone() |
| 917 |
Article.objects.create( |
| 918 |
published=make_aware(datetime.datetime(2017, 2, 19, 0, 0), tz, False) |
| 919 |
) |
| 920 |
Article.objects.create( |
| 921 |
published=make_aware(datetime.datetime(2017, 2, 18, 23, 0), tz, False) |
| 922 |
) |
| 923 |
Article.objects.create( |
| 924 |
published=make_aware(datetime.datetime(2017, 2, 18, 0, 0), tz, False) |
| 925 |
) |
| 926 |
Article.objects.create( |
| 927 |
published=make_aware(datetime.datetime(2017, 2, 17, 15, 0), tz, False) |
| 928 |
) |
| 929 |
|
| 930 |
class F(FilterSet): |
| 931 |
published = DateFromToRangeFilter() |
| 932 |
|
| 933 |
class Meta: |
| 934 |
model = Article |
| 935 |
fields = ["published"] |
| 936 |
|
| 937 |
results = F( |
| 938 |
data={"published_after": "2017-02-18", "published_before": "2017-02-18"} |
| 939 |
) |
| 940 |
self.assertEqual(len(results.qs), 2) |
| 941 |
|
| 942 |
@unittest.skipUnless(django.VERSION < (5, 0), "is_dst removed in Django 5.0") |
| 943 |
@override_settings(TIME_ZONE="Europe/Paris") |
| 944 |
def test_filtering_dst_start(self): |
| 945 |
tz = timezone.get_default_timezone() |
| 946 |
Article.objects.create( |
| 947 |
published=make_aware(datetime.datetime(2017, 3, 25, 23, 59), tz, False) |
| 948 |
) |
| 949 |
Article.objects.create( |
| 950 |
published=make_aware(datetime.datetime(2017, 3, 26, 0, 0), tz, False) |
| 951 |
) |
| 952 |
Article.objects.create( |
| 953 |
published=make_aware(datetime.datetime(2017, 3, 26, 2, 0), tz, False) |
| 954 |
) |
| 955 |
Article.objects.create( |
| 956 |
published=make_aware(datetime.datetime(2017, 3, 26, 3, 0), tz, False) |
| 957 |
) |
| 958 |
Article.objects.create( |
| 959 |
published=make_aware(datetime.datetime(2017, 3, 27, 0, 0), tz, False) |
| 960 |
) |
| 961 |
|
| 962 |
class F(FilterSet): |
| 963 |
published = DateFromToRangeFilter() |
| 964 |
|
| 965 |
class Meta: |
| 966 |
model = Article |
| 967 |
fields = ["published"] |
| 968 |
|
| 969 |
results = F( |
| 970 |
data={"published_after": "2017-3-26", "published_before": "2017-3-26"} |
| 971 |
) |
| 972 |
self.assertEqual(len(results.qs), 3) |
| 973 |
|
| 974 |
@unittest.skipUnless(django.VERSION < (5, 0), "is_dst removed in Django 5.0") |
| 975 |
@override_settings(TIME_ZONE="Europe/Paris") |
| 976 |
def test_filtering_dst_end(self): |
| 977 |
tz = timezone.get_default_timezone() |
| 978 |
Article.objects.create( |
| 979 |
published=make_aware(datetime.datetime(2017, 10, 28, 23, 59), tz, False) |
| 980 |
) |
| 981 |
Article.objects.create( |
| 982 |
published=make_aware(datetime.datetime(2017, 10, 29, 0, 0), tz, False) |
| 983 |
) |
| 984 |
Article.objects.create( |
| 985 |
published=make_aware(datetime.datetime(2017, 10, 29, 2, 0), tz, False) |
| 986 |
) |
| 987 |
Article.objects.create( |
| 988 |
published=make_aware(datetime.datetime(2017, 10, 29, 3, 0), tz, False) |
| 989 |
) |
| 990 |
Article.objects.create( |
| 991 |
published=make_aware(datetime.datetime(2017, 10, 30, 0, 0), tz, False) |
| 992 |
) |
| 993 |
|
| 994 |
class F(FilterSet): |
| 995 |
published = DateFromToRangeFilter() |
| 996 |
|
| 997 |
class Meta: |
| 998 |
model = Article |
| 999 |
fields = ["published"] |
| 1000 |
|
| 1001 |
results = F( |
| 1002 |
data={"published_after": "2017-10-29", "published_before": "2017-10-29"} |
| 1003 |
) |
| 1004 |
self.assertEqual(len(results.qs), 3) |
| 1005 |
|
| 1006 |
|
| 1007 |
class DateTimeFromToRangeFilterTests(TestCase): |
| 1008 |
def test_filtering(self): |
| 1009 |
tz = timezone.get_current_timezone() |
| 1010 |
Article.objects.create( |
| 1011 |
published=datetime.datetime(2016, 1, 1, 10, 0, tzinfo=tz) |
| 1012 |
) |
| 1013 |
Article.objects.create( |
| 1014 |
published=datetime.datetime(2016, 1, 2, 12, 45, tzinfo=tz) |
| 1015 |
) |
| 1016 |
Article.objects.create( |
| 1017 |
published=datetime.datetime(2016, 1, 3, 18, 15, tzinfo=tz) |
| 1018 |
) |
| 1019 |
Article.objects.create( |
| 1020 |
published=datetime.datetime(2016, 1, 3, 19, 30, tzinfo=tz) |
| 1021 |
) |
| 1022 |
|
| 1023 |
class F(FilterSet): |
| 1024 |
published = DateTimeFromToRangeFilter() |
| 1025 |
|
| 1026 |
class Meta: |
| 1027 |
model = Article |
| 1028 |
fields = ["published"] |
| 1029 |
|
| 1030 |
results = F( |
| 1031 |
data={ |
| 1032 |
"published_after": "2016-01-02 10:00", |
| 1033 |
"published_before": "2016-01-03 19:00", |
| 1034 |
} |
| 1035 |
) |
| 1036 |
self.assertEqual(len(results.qs), 2) |
| 1037 |
|
| 1038 |
|
| 1039 |
class IsoDateTimeFromToRangeFilterTests(TestCase): |
| 1040 |
def test_filtering(self): |
| 1041 |
tz = timezone.get_current_timezone() |
| 1042 |
Article.objects.create( |
| 1043 |
published=datetime.datetime(2016, 1, 1, 10, 0, tzinfo=tz) |
| 1044 |
) |
| 1045 |
Article.objects.create( |
| 1046 |
published=datetime.datetime(2016, 1, 2, 12, 45, tzinfo=tz) |
| 1047 |
) |
| 1048 |
Article.objects.create( |
| 1049 |
published=datetime.datetime(2016, 1, 3, 18, 15, tzinfo=tz) |
| 1050 |
) |
| 1051 |
Article.objects.create( |
| 1052 |
published=datetime.datetime(2016, 1, 3, 19, 30, tzinfo=tz) |
| 1053 |
) |
| 1054 |
|
| 1055 |
class F(FilterSet): |
| 1056 |
published = IsoDateTimeFromToRangeFilter() |
| 1057 |
|
| 1058 |
class Meta: |
| 1059 |
model = Article |
| 1060 |
fields = ["published"] |
| 1061 |
|
| 1062 |
dt = datetime.datetime.now(tz=tz) |
| 1063 |
results = F( |
| 1064 |
data={ |
| 1065 |
"published_after": "2016-01-02T10:00:00.000000" + dt.strftime("%z"), |
| 1066 |
"published_before": "2016-01-03T19:00:00.000000" + dt.strftime("%z"), |
| 1067 |
} |
| 1068 |
) |
| 1069 |
self.assertEqual(len(results.qs), 2) |
| 1070 |
|
| 1071 |
|
| 1072 |
class TimeRangeFilterTests(TestCase): |
| 1073 |
def test_filtering(self): |
| 1074 |
adam = User.objects.create(username="adam") |
| 1075 |
kwargs = {"text": "test", "author": adam, "date": datetime.date.today()} |
| 1076 |
Comment.objects.create(time="7:30", **kwargs) |
| 1077 |
Comment.objects.create(time="8:00", **kwargs) |
| 1078 |
Comment.objects.create(time="9:30", **kwargs) |
| 1079 |
Comment.objects.create(time="11:00", **kwargs) |
| 1080 |
|
| 1081 |
class F(FilterSet): |
| 1082 |
time = TimeRangeFilter() |
| 1083 |
|
| 1084 |
class Meta: |
| 1085 |
model = Comment |
| 1086 |
fields = ["time"] |
| 1087 |
|
| 1088 |
results = F(data={"time_after": "8:00", "time_before": "10:00"}) |
| 1089 |
self.assertEqual(len(results.qs), 2) |
| 1090 |
|
| 1091 |
|
| 1092 |
class AllValuesFilterTests(TestCase): |
| 1093 |
def test_filtering(self): |
| 1094 |
User.objects.create(username="alex") |
| 1095 |
User.objects.create(username="jacob") |
| 1096 |
User.objects.create(username="aaron") |
| 1097 |
|
| 1098 |
class F(FilterSet): |
| 1099 |
username = AllValuesFilter() |
| 1100 |
|
| 1101 |
class Meta: |
| 1102 |
model = User |
| 1103 |
fields = ["username"] |
| 1104 |
|
| 1105 |
self.assertEqual(list(F().qs), list(User.objects.all())) |
| 1106 |
self.assertEqual( |
| 1107 |
list(F({"username": "alex"}).qs), [User.objects.get(username="alex")] |
| 1108 |
) |
| 1109 |
|
| 1110 |
# invalid choice |
| 1111 |
self.assertFalse(F({"username": "jose"}).is_valid()) |
| 1112 |
self.assertEqual(list(F({"username": "jose"}).qs), list(User.objects.all())) |
| 1113 |
|
| 1114 |
|
| 1115 |
class AllValuesMultipleFilterTests(TestCase): |
| 1116 |
def test_filtering(self): |
| 1117 |
User.objects.create(username="alex") |
| 1118 |
User.objects.create(username="jacob") |
| 1119 |
User.objects.create(username="aaron") |
| 1120 |
|
| 1121 |
class F(FilterSet): |
| 1122 |
username = AllValuesMultipleFilter() |
| 1123 |
|
| 1124 |
class Meta: |
| 1125 |
model = User |
| 1126 |
fields = ["username"] |
| 1127 |
|
| 1128 |
self.assertEqual(list(F().qs), list(User.objects.all())) |
| 1129 |
self.assertEqual( |
| 1130 |
list(F({"username": ["alex"]}).qs), [User.objects.get(username="alex")] |
| 1131 |
) |
| 1132 |
self.assertCountEqual( |
| 1133 |
list(F({"username": ["alex", "jacob"]}).qs), |
| 1134 |
list(User.objects.filter(username__in=["alex", "jacob"])), |
| 1135 |
) |
| 1136 |
|
| 1137 |
# invalid choice |
| 1138 |
self.assertFalse(F({"username": "jose"}).is_valid()) |
| 1139 |
self.assertEqual(list(F({"username": "jose"}).qs), list(User.objects.all())) |
| 1140 |
|
| 1141 |
|
| 1142 |
class FilterMethodTests(TestCase): |
| 1143 |
def setUp(self): |
| 1144 |
User.objects.create(username="alex") |
| 1145 |
User.objects.create(username="jacob") |
| 1146 |
User.objects.create(username="aaron") |
| 1147 |
|
| 1148 |
def test_filtering(self): |
| 1149 |
class F(FilterSet): |
| 1150 |
username = CharFilter(method="filter_username") |
| 1151 |
|
| 1152 |
class Meta: |
| 1153 |
model = User |
| 1154 |
fields = ["username"] |
| 1155 |
|
| 1156 |
def filter_username(self, queryset, name, value): |
| 1157 |
return queryset.filter(**{name: value}) |
| 1158 |
|
| 1159 |
self.assertEqual(list(F().qs), list(User.objects.all())) |
| 1160 |
self.assertEqual( |
| 1161 |
list(F({"username": "alex"}).qs), [User.objects.get(username="alex")] |
| 1162 |
) |
| 1163 |
self.assertEqual(list(F({"username": "jose"}).qs), list()) |
| 1164 |
|
| 1165 |
def test_filtering_callable(self): |
| 1166 |
def filter_username(queryset, name, value): |
| 1167 |
return queryset.filter(**{name: value}) |
| 1168 |
|
| 1169 |
class F(FilterSet): |
| 1170 |
username = CharFilter(method=filter_username) |
| 1171 |
|
| 1172 |
class Meta: |
| 1173 |
model = User |
| 1174 |
fields = ["username"] |
| 1175 |
|
| 1176 |
self.assertEqual(list(F().qs), list(User.objects.all())) |
| 1177 |
self.assertEqual( |
| 1178 |
list(F({"username": "alex"}).qs), [User.objects.get(username="alex")] |
| 1179 |
) |
| 1180 |
self.assertEqual(list(F({"username": "jose"}).qs), list()) |
| 1181 |
|
| 1182 |
|
| 1183 |
class O2ORelationshipTests(TestCase): |
| 1184 |
def setUp(self): |
| 1185 |
self.a1 = Account.objects.create( |
| 1186 |
name="account1", in_good_standing=False, friendly=False |
| 1187 |
) |
| 1188 |
self.a2 = Account.objects.create( |
| 1189 |
name="account2", in_good_standing=True, friendly=True |
| 1190 |
) |
| 1191 |
self.a3 = Account.objects.create( |
| 1192 |
name="account3", in_good_standing=True, friendly=False |
| 1193 |
) |
| 1194 |
self.a4 = Account.objects.create( |
| 1195 |
name="account4", in_good_standing=False, friendly=True |
| 1196 |
) |
| 1197 |
self.p1 = Profile.objects.create( |
| 1198 |
account=self.a1, likes_coffee=True, likes_tea=False |
| 1199 |
) |
| 1200 |
self.p2 = Profile.objects.create( |
| 1201 |
account=self.a2, likes_coffee=False, likes_tea=True |
| 1202 |
) |
| 1203 |
self.p3 = Profile.objects.create( |
| 1204 |
account=self.a3, likes_coffee=True, likes_tea=True |
| 1205 |
) |
| 1206 |
Profile.objects.create(account=self.a4, likes_coffee=False, likes_tea=False) |
| 1207 |
|
| 1208 |
def test_o2o_relation(self): |
| 1209 |
class F(FilterSet): |
| 1210 |
class Meta: |
| 1211 |
model = Profile |
| 1212 |
fields = ("account",) |
| 1213 |
|
| 1214 |
f = F() |
| 1215 |
self.assertEqual(f.qs.count(), 4) |
| 1216 |
|
| 1217 |
f = F({"account": self.a1.pk}) |
| 1218 |
self.assertEqual(f.qs.count(), 1) |
| 1219 |
self.assertQuerySetEqual(f.qs, [self.p1.pk], lambda o: o.pk) |
| 1220 |
|
| 1221 |
def test_o2o_relation_dictionary(self): |
| 1222 |
class F(FilterSet): |
| 1223 |
class Meta: |
| 1224 |
model = Profile |
| 1225 |
fields = { |
| 1226 |
"account": ["exact"], |
| 1227 |
} |
| 1228 |
|
| 1229 |
f = F() |
| 1230 |
self.assertEqual(f.qs.count(), 4) |
| 1231 |
|
| 1232 |
f = F({"account": self.a1.pk}) |
| 1233 |
self.assertEqual(f.qs.count(), 1) |
| 1234 |
self.assertQuerySetEqual(f.qs, [self.p1.pk], lambda o: o.pk) |
| 1235 |
|
| 1236 |
def test_reverse_o2o_relation(self): |
| 1237 |
class F(FilterSet): |
| 1238 |
class Meta: |
| 1239 |
model = Account |
| 1240 |
fields = ("profile",) |
| 1241 |
|
| 1242 |
f = F() |
| 1243 |
self.assertEqual(f.qs.count(), 4) |
| 1244 |
|
| 1245 |
f = F({"profile": self.p1.pk}) |
| 1246 |
self.assertEqual(f.qs.count(), 1) |
| 1247 |
self.assertQuerySetEqual(f.qs, [self.a1.pk], lambda o: o.pk) |
| 1248 |
|
| 1249 |
def test_o2o_relation_attribute(self): |
| 1250 |
class F(FilterSet): |
| 1251 |
class Meta: |
| 1252 |
model = Profile |
| 1253 |
fields = ("account__in_good_standing",) |
| 1254 |
|
| 1255 |
f = F() |
| 1256 |
self.assertEqual(f.qs.count(), 4) |
| 1257 |
|
| 1258 |
f = F({"account__in_good_standing": "2"}) |
| 1259 |
self.assertEqual(f.qs.count(), 2) |
| 1260 |
self.assertQuerySetEqual(f.qs, [self.p2.pk, self.p3.pk], lambda o: o.pk, False) |
| 1261 |
|
| 1262 |
def test_o2o_relation_attribute2(self): |
| 1263 |
class F(FilterSet): |
| 1264 |
class Meta: |
| 1265 |
model = Profile |
| 1266 |
fields = ( |
| 1267 |
"account__in_good_standing", |
| 1268 |
"account__friendly", |
| 1269 |
) |
| 1270 |
|
| 1271 |
f = F() |
| 1272 |
self.assertEqual(f.qs.count(), 4) |
| 1273 |
|
| 1274 |
f = F({"account__in_good_standing": "2", "account__friendly": "2"}) |
| 1275 |
self.assertEqual(f.qs.count(), 1) |
| 1276 |
self.assertQuerySetEqual(f.qs, [self.p2.pk], lambda o: o.pk) |
| 1277 |
|
| 1278 |
def test_reverse_o2o_relation_attribute(self): |
| 1279 |
class F(FilterSet): |
| 1280 |
class Meta: |
| 1281 |
model = Account |
| 1282 |
fields = ("profile__likes_coffee",) |
| 1283 |
|
| 1284 |
f = F() |
| 1285 |
self.assertEqual(f.qs.count(), 4) |
| 1286 |
|
| 1287 |
f = F({"profile__likes_coffee": "2"}) |
| 1288 |
self.assertEqual(f.qs.count(), 2) |
| 1289 |
self.assertQuerySetEqual(f.qs, [self.a1.pk, self.a3.pk], lambda o: o.pk, False) |
| 1290 |
|
| 1291 |
def test_reverse_o2o_relation_attribute2(self): |
| 1292 |
class F(FilterSet): |
| 1293 |
class Meta: |
| 1294 |
model = Account |
| 1295 |
fields = ("profile__likes_coffee", "profile__likes_tea") |
| 1296 |
|
| 1297 |
f = F() |
| 1298 |
self.assertEqual(f.qs.count(), 4) |
| 1299 |
|
| 1300 |
f = F({"profile__likes_coffee": "2", "profile__likes_tea": "2"}) |
| 1301 |
self.assertEqual(f.qs.count(), 1) |
| 1302 |
self.assertQuerySetEqual(f.qs, [self.a3.pk], lambda o: o.pk) |
| 1303 |
|
| 1304 |
|
| 1305 |
class FKRelationshipTests(TestCase): |
| 1306 |
def test_fk_relation(self): |
| 1307 |
company1 = Company.objects.create(name="company1") |
| 1308 |
company2 = Company.objects.create(name="company2") |
| 1309 |
loc1 = Location.objects.create( |
| 1310 |
company=company1, open_days="some", zip_code="90210" |
| 1311 |
) |
| 1312 |
Location.objects.create(company=company2, open_days="WEEKEND", zip_code="11111") |
| 1313 |
loc3 = Location.objects.create( |
| 1314 |
company=company1, open_days="monday", zip_code="12345" |
| 1315 |
) |
| 1316 |
|
| 1317 |
class F(FilterSet): |
| 1318 |
class Meta: |
| 1319 |
model = Location |
| 1320 |
fields = ("company",) |
| 1321 |
|
| 1322 |
f = F() |
| 1323 |
self.assertEqual(f.qs.count(), 3) |
| 1324 |
|
| 1325 |
f = F({"company": company1.pk}) |
| 1326 |
self.assertEqual(f.qs.count(), 2) |
| 1327 |
self.assertQuerySetEqual(f.qs, [loc1.pk, loc3.pk], lambda o: o.pk, False) |
| 1328 |
|
| 1329 |
def test_reverse_fk_relation(self): |
| 1330 |
alex = User.objects.create(username="alex") |
| 1331 |
self.jacob = User.objects.create(username="jacob") |
| 1332 |
date = now().date() |
| 1333 |
time = now().time() |
| 1334 |
Comment.objects.create( |
| 1335 |
text="comment 1", author=self.jacob, time=time, date=date |
| 1336 |
) |
| 1337 |
self.comment = Comment.objects.create( |
| 1338 |
text="comment 2", author=alex, time=time, date=date |
| 1339 |
) |
| 1340 |
Comment.objects.create( |
| 1341 |
text="comment 3", author=self.jacob, time=time, date=date |
| 1342 |
) |
| 1343 |
|
| 1344 |
class F(FilterSet): |
| 1345 |
class Meta: |
| 1346 |
model = User |
| 1347 |
fields = ["comments"] |
| 1348 |
|
| 1349 |
qs = User.objects.all() |
| 1350 |
f = F({"comments": [self.comment.pk]}, queryset=qs) |
| 1351 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username) |
| 1352 |
|
| 1353 |
class F(FilterSet): |
| 1354 |
comments = AllValuesFilter() |
| 1355 |
|
| 1356 |
class Meta: |
| 1357 |
model = User |
| 1358 |
fields = ["comments"] |
| 1359 |
|
| 1360 |
f = F({"comments": self.comment.pk}, queryset=qs) |
| 1361 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username) |
| 1362 |
|
| 1363 |
def test_fk_relation_attribute(self): |
| 1364 |
now_dt = now() |
| 1365 |
alex = User.objects.create(username="alex") |
| 1366 |
jacob = User.objects.create(username="jacob") |
| 1367 |
User.objects.create(username="aaron") |
| 1368 |
|
| 1369 |
Article.objects.create(author=alex, published=now_dt) |
| 1370 |
Article.objects.create(author=jacob, published=now_dt) |
| 1371 |
Article.objects.create(author=alex, published=now_dt) |
| 1372 |
|
| 1373 |
class F(FilterSet): |
| 1374 |
class Meta: |
| 1375 |
model = Article |
| 1376 |
fields = ["author__username"] |
| 1377 |
|
| 1378 |
self.assertEqual(list(F.base_filters), ["author__username"]) |
| 1379 |
self.assertEqual(F({"author__username": "alex"}).qs.count(), 2) |
| 1380 |
self.assertEqual(F({"author__username": "jacob"}).qs.count(), 1) |
| 1381 |
|
| 1382 |
class F(FilterSet): |
| 1383 |
author__username = AllValuesFilter() |
| 1384 |
|
| 1385 |
class Meta: |
| 1386 |
model = Article |
| 1387 |
fields = ["author__username"] |
| 1388 |
|
| 1389 |
self.assertEqual(F({"author__username": "alex"}).qs.count(), 2) |
| 1390 |
|
| 1391 |
def test_reverse_fk_relation_attribute(self): |
| 1392 |
alex = User.objects.create(username="alex") |
| 1393 |
jacob = User.objects.create(username="jacob") |
| 1394 |
date = now().date() |
| 1395 |
time = now().time() |
| 1396 |
Comment.objects.create(text="comment 1", author=jacob, time=time, date=date) |
| 1397 |
Comment.objects.create(text="comment 2", author=alex, time=time, date=date) |
| 1398 |
Comment.objects.create(text="comment 3", author=jacob, time=time, date=date) |
| 1399 |
|
| 1400 |
class F(FilterSet): |
| 1401 |
class Meta: |
| 1402 |
model = User |
| 1403 |
fields = ["comments__text"] |
| 1404 |
|
| 1405 |
qs = User.objects.all() |
| 1406 |
f = F({"comments__text": "comment 2"}, queryset=qs) |
| 1407 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username) |
| 1408 |
|
| 1409 |
class F(FilterSet): |
| 1410 |
comments__text = AllValuesFilter() |
| 1411 |
|
| 1412 |
class Meta: |
| 1413 |
model = User |
| 1414 |
fields = ["comments__text"] |
| 1415 |
|
| 1416 |
f = F({"comments__text": "comment 2"}, queryset=qs) |
| 1417 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username) |
| 1418 |
|
| 1419 |
@unittest.skip("todo - need correct models") |
| 1420 |
def test_fk_relation_multiple_attributes(self): |
| 1421 |
pass |
| 1422 |
|
| 1423 |
@unittest.expectedFailure |
| 1424 |
def test_reverse_fk_relation_multiple_attributes(self): |
| 1425 |
company = Company.objects.create(name="company") |
| 1426 |
Location.objects.create(company=company, open_days="some", zip_code="90210") |
| 1427 |
Location.objects.create(company=company, open_days="WEEKEND", zip_code="11111") |
| 1428 |
|
| 1429 |
class F(FilterSet): |
| 1430 |
class Meta: |
| 1431 |
model = Company |
| 1432 |
fields = ("locations__zip_code", "locations__open_days") |
| 1433 |
|
| 1434 |
f = F({"locations__zip_code": "90210", "locations__open_days": "WEEKEND"}) |
| 1435 |
self.assertEqual(f.qs.count(), 0) |
| 1436 |
|
| 1437 |
|
| 1438 |
class M2MRelationshipTests(TestCase): |
| 1439 |
def setUp(self): |
| 1440 |
self.alex = User.objects.create(username="alex", status=1) |
| 1441 |
User.objects.create(username="jacob", status=1) |
| 1442 |
aaron = User.objects.create(username="aaron", status=1) |
| 1443 |
self.b1 = Book.objects.create( |
| 1444 |
title="Ender's Game", price="1.00", average_rating=3.0 |
| 1445 |
) |
| 1446 |
self.b2 = Book.objects.create( |
| 1447 |
title="Rainbow Six", price="2.00", average_rating=4.0 |
| 1448 |
) |
| 1449 |
self.b3 = Book.objects.create( |
| 1450 |
title="Snowcrash", price="1.00", average_rating=4.0 |
| 1451 |
) |
| 1452 |
self.b4 = Book.objects.create( |
| 1453 |
title="Stranger in a Strage Land", price="2.00", average_rating=3.0 |
| 1454 |
) |
| 1455 |
self.alex.favorite_books.add(self.b1, self.b2) |
| 1456 |
aaron.favorite_books.add(self.b1, self.b3) |
| 1457 |
|
| 1458 |
def test_m2m_relation(self): |
| 1459 |
class F(FilterSet): |
| 1460 |
class Meta: |
| 1461 |
model = User |
| 1462 |
fields = ["favorite_books"] |
| 1463 |
|
| 1464 |
qs = User.objects.all().order_by("username") |
| 1465 |
f = F({"favorite_books": [self.b1.pk]}, queryset=qs) |
| 1466 |
self.assertQuerySetEqual(f.qs, ["aaron", "alex"], lambda o: o.username) |
| 1467 |
|
| 1468 |
f = F({"favorite_books": [self.b1.pk, self.b3.pk]}, queryset=qs) |
| 1469 |
self.assertQuerySetEqual(f.qs, ["aaron", "alex"], lambda o: o.username) |
| 1470 |
|
| 1471 |
f = F({"favorite_books": [self.b2.pk]}, queryset=qs) |
| 1472 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username) |
| 1473 |
|
| 1474 |
f = F({"favorite_books": [self.b4.pk]}, queryset=qs) |
| 1475 |
self.assertQuerySetEqual(f.qs, [], lambda o: o.username) |
| 1476 |
|
| 1477 |
def test_reverse_m2m_relation(self): |
| 1478 |
class F(FilterSet): |
| 1479 |
class Meta: |
| 1480 |
model = Book |
| 1481 |
fields = ["lovers"] |
| 1482 |
|
| 1483 |
qs = Book.objects.all().order_by("title") |
| 1484 |
f = F({"lovers": [self.alex.pk]}, queryset=qs) |
| 1485 |
self.assertQuerySetEqual( |
| 1486 |
f.qs, ["Ender's Game", "Rainbow Six"], lambda o: o.title |
| 1487 |
) |
| 1488 |
|
| 1489 |
class F(FilterSet): |
| 1490 |
lovers = AllValuesFilter() |
| 1491 |
|
| 1492 |
class Meta: |
| 1493 |
model = Book |
| 1494 |
fields = ["lovers"] |
| 1495 |
|
| 1496 |
f = F({"lovers": self.alex.pk}, queryset=qs) |
| 1497 |
self.assertQuerySetEqual( |
| 1498 |
f.qs, ["Ender's Game", "Rainbow Six"], lambda o: o.title |
| 1499 |
) |
| 1500 |
|
| 1501 |
def test_m2m_relation_attribute(self): |
| 1502 |
class F(FilterSet): |
| 1503 |
class Meta: |
| 1504 |
model = User |
| 1505 |
fields = ["favorite_books__title"] |
| 1506 |
|
| 1507 |
qs = User.objects.all().order_by("username") |
| 1508 |
f = F({"favorite_books__title": "Ender's Game"}, queryset=qs) |
| 1509 |
self.assertQuerySetEqual(f.qs, ["aaron", "alex"], lambda o: o.username) |
| 1510 |
|
| 1511 |
f = F({"favorite_books__title": "Rainbow Six"}, queryset=qs) |
| 1512 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username) |
| 1513 |
|
| 1514 |
# No choices given, so filtering does nothing. |
| 1515 |
class F(FilterSet): |
| 1516 |
favorite_books__title = MultipleChoiceFilter() |
| 1517 |
|
| 1518 |
class Meta: |
| 1519 |
model = User |
| 1520 |
fields = ["favorite_books__title"] |
| 1521 |
|
| 1522 |
f = F() |
| 1523 |
self.assertEqual(len(f.filters["favorite_books__title"].field.choices), 0) |
| 1524 |
|
| 1525 |
# Specifying choices allows filter to work. (See also AllValues variants.) |
| 1526 |
class F(FilterSet): |
| 1527 |
favorite_books__title = MultipleChoiceFilter( |
| 1528 |
choices=[(b.title, b.title) for b in Book.objects.all()] |
| 1529 |
) |
| 1530 |
|
| 1531 |
class Meta: |
| 1532 |
model = User |
| 1533 |
fields = ["favorite_books__title"] |
| 1534 |
|
| 1535 |
f = F({"favorite_books__title": ["Ender's Game", "Snowcrash"]}, queryset=qs) |
| 1536 |
self.assertIs( |
| 1537 |
True, |
| 1538 |
f.form.is_valid(), |
| 1539 |
list(f.filters["favorite_books__title"].field.choices), |
| 1540 |
) |
| 1541 |
self.assertQuerySetEqual( |
| 1542 |
f.qs, |
| 1543 |
["aaron", "alex"], |
| 1544 |
lambda o: o.username, |
| 1545 |
) |
| 1546 |
|
| 1547 |
class F(FilterSet): |
| 1548 |
favorite_books__title = AllValuesFilter() |
| 1549 |
|
| 1550 |
class Meta: |
| 1551 |
model = User |
| 1552 |
fields = ["favorite_books__title"] |
| 1553 |
|
| 1554 |
f = F({"favorite_books__title": "Snowcrash"}, queryset=qs) |
| 1555 |
self.assertQuerySetEqual(f.qs, ["aaron"], lambda o: o.username) |
| 1556 |
|
| 1557 |
def test_reverse_m2m_relation_attribute(self): |
| 1558 |
class F(FilterSet): |
| 1559 |
class Meta: |
| 1560 |
model = Book |
| 1561 |
fields = ["lovers__username"] |
| 1562 |
|
| 1563 |
qs = Book.objects.all().order_by("title") |
| 1564 |
f = F({"lovers__username": "alex"}, queryset=qs) |
| 1565 |
self.assertQuerySetEqual( |
| 1566 |
f.qs, ["Ender's Game", "Rainbow Six"], lambda o: o.title |
| 1567 |
) |
| 1568 |
|
| 1569 |
f = F({"lovers__username": "jacob"}, queryset=qs) |
| 1570 |
self.assertQuerySetEqual(f.qs, [], lambda o: o.title) |
| 1571 |
|
| 1572 |
class F(FilterSet): |
| 1573 |
lovers__username = MultipleChoiceFilter() |
| 1574 |
|
| 1575 |
class Meta: |
| 1576 |
model = Book |
| 1577 |
fields = ["lovers__username"] |
| 1578 |
|
| 1579 |
f = F() |
| 1580 |
self.assertEqual(len(f.filters["lovers__username"].field.choices), 0) |
| 1581 |
# f = F({'lovers__username': ['1', '3']}, |
| 1582 |
# queryset=qs) |
| 1583 |
# self.assertQuerySetEqual( |
| 1584 |
# f.qs, ["Ender's Game", "Rainbow Six"], lambda o: o.title) |
| 1585 |
|
| 1586 |
class F(FilterSet): |
| 1587 |
lovers__username = AllValuesFilter() |
| 1588 |
|
| 1589 |
class Meta: |
| 1590 |
model = Book |
| 1591 |
fields = ["lovers__username"] |
| 1592 |
|
| 1593 |
f = F({"lovers__username": "alex"}, queryset=qs) |
| 1594 |
self.assertQuerySetEqual( |
| 1595 |
f.qs, ["Ender's Game", "Rainbow Six"], lambda o: o.title |
| 1596 |
) |
| 1597 |
|
| 1598 |
@unittest.expectedFailure |
| 1599 |
def test_m2m_relation_multiple_attributes(self): |
| 1600 |
class F(FilterSet): |
| 1601 |
class Meta: |
| 1602 |
model = User |
| 1603 |
fields = ["favorite_books__price", "favorite_books__average_rating"] |
| 1604 |
|
| 1605 |
qs = User.objects.all().order_by("username") |
| 1606 |
f = F( |
| 1607 |
{"favorite_books__price": "1.00", "favorite_books__average_rating": 4.0}, |
| 1608 |
queryset=qs, |
| 1609 |
) |
| 1610 |
self.assertQuerySetEqual(f.qs, ["aaron"], lambda o: o.username) |
| 1611 |
|
| 1612 |
f = F( |
| 1613 |
{"favorite_books__price": "3.00", "favorite_books__average_rating": 4.0}, |
| 1614 |
queryset=qs, |
| 1615 |
) |
| 1616 |
self.assertQuerySetEqual(f.qs, [], lambda o: o.username) |
| 1617 |
|
| 1618 |
@unittest.expectedFailure |
| 1619 |
def test_reverse_m2m_relation_multiple_attributes(self): |
| 1620 |
class F(FilterSet): |
| 1621 |
class Meta: |
| 1622 |
model = Book |
| 1623 |
fields = ["lovers__status", "lovers__username"] |
| 1624 |
|
| 1625 |
qs = Book.objects.all().order_by("title") |
| 1626 |
f = F({"lovers__status": 1, "lovers__username": "alex"}, queryset=qs) |
| 1627 |
self.assertQuerySetEqual( |
| 1628 |
f.qs, ["Ender's Game", "Rainbow Six"], lambda o: o.title |
| 1629 |
) |
| 1630 |
|
| 1631 |
f = F({"lovers__status": 1, "lovers__username": "jacob"}, queryset=qs) |
| 1632 |
self.assertQuerySetEqual(f.qs, [], lambda o: o.title) |
| 1633 |
|
| 1634 |
@unittest.skip("todo") |
| 1635 |
def test_fk_relation_on_m2m_relation(self): |
| 1636 |
pass |
| 1637 |
|
| 1638 |
@unittest.skip("todo") |
| 1639 |
def test_fk_relation_attribute_on_m2m_relation(self): |
| 1640 |
pass |
| 1641 |
|
| 1642 |
|
| 1643 |
class SymmetricalSelfReferentialRelationshipTests(TestCase): |
| 1644 |
def setUp(self): |
| 1645 |
self.n1 = Node.objects.create(name="one") |
| 1646 |
self.n2 = Node.objects.create(name="two") |
| 1647 |
n3 = Node.objects.create(name="three") |
| 1648 |
self.n4 = Node.objects.create(name="four") |
| 1649 |
self.n1.adjacents.add(self.n2) |
| 1650 |
self.n2.adjacents.add(n3) |
| 1651 |
self.n2.adjacents.add(self.n4) |
| 1652 |
self.n4.adjacents.add(self.n1) |
| 1653 |
|
| 1654 |
def test_relation(self): |
| 1655 |
class F(FilterSet): |
| 1656 |
class Meta: |
| 1657 |
model = Node |
| 1658 |
fields = ["adjacents"] |
| 1659 |
|
| 1660 |
qs = Node.objects.all().order_by("pk") |
| 1661 |
f = F({"adjacents": [self.n1.pk]}, queryset=qs) |
| 1662 |
self.assertQuerySetEqual(f.qs, [self.n2.pk, self.n4.pk], lambda o: o.pk) |
| 1663 |
|
| 1664 |
|
| 1665 |
class NonSymmetricalSelfReferentialRelationshipTests(TestCase): |
| 1666 |
def setUp(self): |
| 1667 |
self.n1 = DirectedNode.objects.create(name="one") |
| 1668 |
self.n2 = DirectedNode.objects.create(name="two") |
| 1669 |
n3 = DirectedNode.objects.create(name="three") |
| 1670 |
self.n4 = DirectedNode.objects.create(name="four") |
| 1671 |
self.n1.outbound_nodes.add(self.n2) |
| 1672 |
self.n2.outbound_nodes.add(n3) |
| 1673 |
self.n2.outbound_nodes.add(self.n4) |
| 1674 |
self.n4.outbound_nodes.add(self.n1) |
| 1675 |
|
| 1676 |
def test_forward_relation(self): |
| 1677 |
class F(FilterSet): |
| 1678 |
class Meta: |
| 1679 |
model = DirectedNode |
| 1680 |
fields = ["outbound_nodes"] |
| 1681 |
|
| 1682 |
qs = DirectedNode.objects.all().order_by("pk") |
| 1683 |
f = F({"outbound_nodes": [self.n1.pk]}, queryset=qs) |
| 1684 |
self.assertQuerySetEqual(f.qs, [self.n4.pk], lambda o: o.pk) |
| 1685 |
|
| 1686 |
def test_reverse_relation(self): |
| 1687 |
class F(FilterSet): |
| 1688 |
class Meta: |
| 1689 |
model = DirectedNode |
| 1690 |
fields = ["inbound_nodes"] |
| 1691 |
|
| 1692 |
qs = DirectedNode.objects.all().order_by("pk") |
| 1693 |
f = F({"inbound_nodes": [self.n1.pk]}, queryset=qs) |
| 1694 |
self.assertQuerySetEqual(f.qs, [self.n2.pk], lambda o: o.pk) |
| 1695 |
|
| 1696 |
|
| 1697 |
@override_settings(TIME_ZONE="UTC") |
| 1698 |
class TransformedQueryExpressionFilterTests(TestCase): |
| 1699 |
def test_filtering(self): |
| 1700 |
now_dt = now() |
| 1701 |
after_5pm = now_dt.replace(hour=18) |
| 1702 |
before_5pm = now_dt.replace(hour=16) |
| 1703 |
|
| 1704 |
u = User.objects.create(username="alex") |
| 1705 |
a = Article.objects.create(author=u, published=after_5pm) |
| 1706 |
Article.objects.create(author=u, published=before_5pm) |
| 1707 |
|
| 1708 |
class F(FilterSet): |
| 1709 |
class Meta: |
| 1710 |
model = Article |
| 1711 |
fields = {"published": ["hour__gte"]} |
| 1712 |
|
| 1713 |
qs = Article.objects.all() |
| 1714 |
f = F({"published__hour__gte": 17}, queryset=qs) |
| 1715 |
self.assertEqual(len(f.qs), 1) |
| 1716 |
self.assertQuerySetEqual(f.qs, [a.pk], lambda o: o.pk) |
| 1717 |
|
| 1718 |
|
| 1719 |
class LookupChoiceFilterTests(TestCase): |
| 1720 |
class BookFilter(FilterSet): |
| 1721 |
price = LookupChoiceFilter( |
| 1722 |
lookup_choices=["lt", "gt"], field_class=forms.DecimalField |
| 1723 |
) |
| 1724 |
|
| 1725 |
class Meta: |
| 1726 |
model = Book |
| 1727 |
fields = ["price"] |
| 1728 |
|
| 1729 |
@classmethod |
| 1730 |
def setUpTestData(cls): |
| 1731 |
Book.objects.create( |
| 1732 |
title="Ender's Game", price="10.0", average_rating=4.7999999999999998 |
| 1733 |
) |
| 1734 |
Book.objects.create( |
| 1735 |
title="Rainbow Six", price="15.0", average_rating=4.5999999999999996 |
| 1736 |
) |
| 1737 |
Book.objects.create( |
| 1738 |
title="Snowcrash", price="20.0", average_rating=4.2999999999999998 |
| 1739 |
) |
| 1740 |
|
| 1741 |
def test_filtering(self): |
| 1742 |
F = self.BookFilter |
| 1743 |
|
| 1744 |
f = F({"price": "15", "price_lookup": "lt"}) |
| 1745 |
self.assertQuerySetEqual(f.qs, ["Ender's Game"], lambda o: o.title) |
| 1746 |
f = F({"price": "15", "price_lookup": "lt"}) |
| 1747 |
self.assertQuerySetEqual(f.qs, ["Ender's Game"], lambda o: o.title) |
| 1748 |
f = F({"price": "", "price_lookup": "lt"}) |
| 1749 |
self.assertTrue(f.is_valid()) |
| 1750 |
self.assertQuerySetEqual( |
| 1751 |
f.qs, |
| 1752 |
["Ender's Game", "Rainbow Six", "Snowcrash"], |
| 1753 |
lambda o: o.title, |
| 1754 |
ordered=False, |
| 1755 |
) |
| 1756 |
f = F({"price": "15"}) |
| 1757 |
self.assertFalse(f.is_valid()) |
| 1758 |
self.assertQuerySetEqual( |
| 1759 |
f.qs, |
| 1760 |
["Ender's Game", "Rainbow Six", "Snowcrash"], |
| 1761 |
lambda o: o.title, |
| 1762 |
ordered=False, |
| 1763 |
) |
| 1764 |
|
| 1765 |
def test_inner_field_class_validation(self): |
| 1766 |
f = self.BookFilter({"price": "asdf", "price_lookup": "lt"}) |
| 1767 |
self.assertFalse(f.is_valid()) |
| 1768 |
self.assertEqual( |
| 1769 |
f.errors, |
| 1770 |
{ |
| 1771 |
"price": ["Enter a number."], |
| 1772 |
}, |
| 1773 |
) |
| 1774 |
|
| 1775 |
def test_lookup_choices_validation(self): |
| 1776 |
f = self.BookFilter({"price": "1", "price_lookup": "asdf"}) |
| 1777 |
self.assertFalse(f.is_valid()) |
| 1778 |
self.assertEqual( |
| 1779 |
f.errors, |
| 1780 |
{ |
| 1781 |
"price": [ |
| 1782 |
"Select a valid choice. asdf is not one of the available choices." |
| 1783 |
], |
| 1784 |
}, |
| 1785 |
) |
| 1786 |
|
| 1787 |
def test_lookup_omitted(self): |
| 1788 |
f = self.BookFilter({"price": "1"}) |
| 1789 |
self.assertFalse(f.is_valid()) |
| 1790 |
self.assertEqual( |
| 1791 |
f.errors, |
| 1792 |
{ |
| 1793 |
"price": ["Select a lookup."], |
| 1794 |
}, |
| 1795 |
) |
| 1796 |
|
| 1797 |
|
| 1798 |
@override_settings(TIME_ZONE="UTC") |
| 1799 |
class CSVFilterTests(TestCase): |
| 1800 |
def setUp(self): |
| 1801 |
self.u1 = User.objects.create(username="alex", status=1) |
| 1802 |
self.u2 = User.objects.create(username="jacob", status=2) |
| 1803 |
self.u3 = User.objects.create(username="aaron", status=2) |
| 1804 |
self.u4 = User.objects.create(username="carl", status=0) |
| 1805 |
|
| 1806 |
now_dt = now() |
| 1807 |
after_5pm = now_dt.replace(hour=18) |
| 1808 |
before_5pm = now_dt.replace(hour=16) |
| 1809 |
|
| 1810 |
self.a1 = Article.objects.create(author=self.u1, published=after_5pm) |
| 1811 |
self.a2 = Article.objects.create(author=self.u2, published=after_5pm) |
| 1812 |
self.a3 = Article.objects.create(author=self.u1, published=before_5pm) |
| 1813 |
self.a4 = Article.objects.create(author=self.u2, published=before_5pm) |
| 1814 |
|
| 1815 |
class UserFilter(FilterSet): |
| 1816 |
class Meta: |
| 1817 |
model = User |
| 1818 |
fields = { |
| 1819 |
"username": ["in"], |
| 1820 |
"status": ["in"], |
| 1821 |
} |
| 1822 |
|
| 1823 |
class ArticleFilter(FilterSet): |
| 1824 |
class Meta: |
| 1825 |
model = Article |
| 1826 |
fields = { |
| 1827 |
"author": ["in"], |
| 1828 |
"published": ["in"], |
| 1829 |
} |
| 1830 |
|
| 1831 |
self.user_filter = UserFilter |
| 1832 |
self.article_filter = ArticleFilter |
| 1833 |
|
| 1834 |
self.after_5pm = after_5pm.strftime("%Y-%m-%d %H:%M:%S.%f") |
| 1835 |
self.before_5pm = before_5pm.strftime("%Y-%m-%d %H:%M:%S.%f") |
| 1836 |
|
| 1837 |
def test_numeric_filtering(self): |
| 1838 |
F = self.user_filter |
| 1839 |
qs = User.objects.order_by("pk") |
| 1840 |
|
| 1841 |
cases = [ |
| 1842 |
(None, [self.u1.pk, self.u2.pk, self.u3.pk, self.u4.pk]), |
| 1843 |
(QueryDict("status__in=1&status__in=2"), [self.u2.pk, self.u3.pk]), |
| 1844 |
({"status__in": ""}, [self.u1.pk, self.u2.pk, self.u3.pk, self.u4.pk]), |
| 1845 |
({"status__in": ","}, []), |
| 1846 |
({"status__in": "0"}, [self.u4.pk]), |
| 1847 |
({"status__in": "0,2"}, [self.u2.pk, self.u3.pk, self.u4.pk]), |
| 1848 |
({"status__in": "0,,1"}, [self.u1.pk, self.u4.pk]), |
| 1849 |
({"status__in": "2"}, [self.u2.pk, self.u3.pk]), |
| 1850 |
] |
| 1851 |
|
| 1852 |
for params, expected in cases: |
| 1853 |
with self.subTest(params=params, expected=expected): |
| 1854 |
self.assertQuerySetEqual( |
| 1855 |
F(params, queryset=qs).qs, expected, attrgetter("pk") |
| 1856 |
) |
| 1857 |
|
| 1858 |
def test_string_filtering(self): |
| 1859 |
F = self.user_filter |
| 1860 |
qs = User.objects.order_by("pk") |
| 1861 |
|
| 1862 |
cases = [ |
| 1863 |
(None, [self.u1.pk, self.u2.pk, self.u3.pk, self.u4.pk]), |
| 1864 |
(QueryDict("username__in=alex&username__in=aaron"), [self.u3.pk]), |
| 1865 |
({"username__in": ""}, [self.u1.pk, self.u2.pk, self.u3.pk, self.u4.pk]), |
| 1866 |
({"username__in": ","}, []), |
| 1867 |
({"username__in": "alex"}, [self.u1.pk]), |
| 1868 |
({"username__in": "alex,aaron"}, [self.u1.pk, self.u3.pk]), |
| 1869 |
({"username__in": "alex,,aaron"}, [self.u1.pk, self.u3.pk]), |
| 1870 |
({"username__in": "alex,"}, [self.u1.pk]), |
| 1871 |
] |
| 1872 |
|
| 1873 |
for params, expected in cases: |
| 1874 |
with self.subTest(params=params, expected=expected): |
| 1875 |
self.assertQuerySetEqual( |
| 1876 |
F(params, queryset=qs).qs, expected, attrgetter("pk") |
| 1877 |
) |
| 1878 |
|
| 1879 |
def test_datetime_filtering(self): |
| 1880 |
F = self.article_filter |
| 1881 |
qs = Article.objects.order_by("pk") |
| 1882 |
|
| 1883 |
after = self.after_5pm |
| 1884 |
before = self.before_5pm |
| 1885 |
|
| 1886 |
cases = [ |
| 1887 |
(None, [self.a1.pk, self.a2.pk, self.a3.pk, self.a4.pk]), |
| 1888 |
( |
| 1889 |
QueryDict("published__in=%s&published__in=%s" % (after, before)), |
| 1890 |
[self.a3.pk, self.a4.pk], |
| 1891 |
), |
| 1892 |
({"published__in": ""}, [self.a1.pk, self.a2.pk, self.a3.pk, self.a4.pk]), |
| 1893 |
({"published__in": ","}, []), |
| 1894 |
({"published__in": "%s" % (after,)}, [self.a1.pk, self.a2.pk]), |
| 1895 |
( |
| 1896 |
{ |
| 1897 |
"published__in": "%s,%s" |
| 1898 |
% ( |
| 1899 |
after, |
| 1900 |
before, |
| 1901 |
) |
| 1902 |
}, |
| 1903 |
[self.a1.pk, self.a2.pk, self.a3.pk, self.a4.pk], |
| 1904 |
), |
| 1905 |
( |
| 1906 |
{ |
| 1907 |
"published__in": "%s,,%s" |
| 1908 |
% ( |
| 1909 |
after, |
| 1910 |
before, |
| 1911 |
) |
| 1912 |
}, |
| 1913 |
[self.a1.pk, self.a2.pk, self.a3.pk, self.a4.pk], |
| 1914 |
), |
| 1915 |
({"published__in": "%s," % (after,)}, [self.a1.pk, self.a2.pk]), |
| 1916 |
] |
| 1917 |
|
| 1918 |
for params, expected in cases: |
| 1919 |
with self.subTest(params=params, expected=expected): |
| 1920 |
self.assertQuerySetEqual( |
| 1921 |
F(params, queryset=qs).qs, expected, attrgetter("pk") |
| 1922 |
) |
| 1923 |
|
| 1924 |
def test_related_filtering(self): |
| 1925 |
F = self.article_filter |
| 1926 |
qs = Article.objects.order_by("pk") |
| 1927 |
|
| 1928 |
cases = [ |
| 1929 |
(None, [self.a1.pk, self.a2.pk, self.a3.pk, self.a4.pk]), |
| 1930 |
( |
| 1931 |
QueryDict(f"author__in={self.u1.pk}&author__in={self.u2.pk}"), |
| 1932 |
[self.a2.pk, self.a4.pk], |
| 1933 |
), |
| 1934 |
({"author__in": ""}, [self.a1.pk, self.a2.pk, self.a3.pk, self.a4.pk]), |
| 1935 |
({"author__in": ","}, []), |
| 1936 |
({"author__in": f"{self.u1.pk}"}, [self.a1.pk, self.a3.pk]), |
| 1937 |
( |
| 1938 |
{"author__in": f"{self.u1.pk},{self.u2.pk}"}, |
| 1939 |
[self.a1.pk, self.a2.pk, self.a3.pk, self.a4.pk], |
| 1940 |
), |
| 1941 |
( |
| 1942 |
{"author__in": f"{self.u1.pk},,{self.u2.pk}"}, |
| 1943 |
[self.a1.pk, self.a2.pk, self.a3.pk, self.a4.pk], |
| 1944 |
), |
| 1945 |
({"author__in": f"{self.u1.pk},"}, [self.a1.pk, self.a3.pk]), |
| 1946 |
] |
| 1947 |
|
| 1948 |
for params, expected in cases: |
| 1949 |
with self.subTest(params=params, expected=expected): |
| 1950 |
self.assertQuerySetEqual( |
| 1951 |
F(params, queryset=qs).qs, expected, attrgetter("pk") |
| 1952 |
) |
| 1953 |
|
| 1954 |
|
| 1955 |
@override_settings(TIME_ZONE="UTC") |
| 1956 |
class CSVRangeFilterTests(TestCase): |
| 1957 |
class ArticleFilter(FilterSet): |
| 1958 |
class Meta: |
| 1959 |
model = Article |
| 1960 |
fields = { |
| 1961 |
"published": ["range"], |
| 1962 |
} |
| 1963 |
|
| 1964 |
@classmethod |
| 1965 |
def setUpTestData(cls): |
| 1966 |
u1 = User.objects.create(username="alex", status=1) |
| 1967 |
u2 = User.objects.create(username="jacob", status=2) |
| 1968 |
User.objects.create(username="aaron", status=2) |
| 1969 |
User.objects.create(username="carl", status=0) |
| 1970 |
|
| 1971 |
now_dt = now() |
| 1972 |
after_5pm = now_dt.replace(hour=18) |
| 1973 |
around_5pm = now_dt.replace(hour=17) |
| 1974 |
before_5pm = now_dt.replace(hour=16) |
| 1975 |
|
| 1976 |
Article.objects.create(author=u1, published=after_5pm) |
| 1977 |
Article.objects.create(author=u2, published=around_5pm) |
| 1978 |
Article.objects.create(author=u1, published=around_5pm) |
| 1979 |
Article.objects.create(author=u2, published=before_5pm) |
| 1980 |
|
| 1981 |
cls.after_5pm = after_5pm.strftime("%Y-%m-%d %H:%M:%S.%f") |
| 1982 |
cls.around_5pm = around_5pm.strftime("%Y-%m-%d %H:%M:%S.%f") |
| 1983 |
cls.before_5pm = before_5pm.strftime("%Y-%m-%d %H:%M:%S.%f") |
| 1984 |
|
| 1985 |
def test_filtering(self): |
| 1986 |
F = self.ArticleFilter |
| 1987 |
|
| 1988 |
f = F() |
| 1989 |
self.assertEqual(f.qs.count(), 4) |
| 1990 |
|
| 1991 |
# empty value is a noop |
| 1992 |
f = F({"published__range": ""}) |
| 1993 |
self.assertTrue(f.is_valid()) |
| 1994 |
self.assertEqual(f.qs.count(), 4) |
| 1995 |
|
| 1996 |
# empty values are interpreted as None types |
| 1997 |
f = F({"published__range": ","}) |
| 1998 |
self.assertEqual(f.qs.count(), 0) |
| 1999 |
|
| 2000 |
f = F({"published__range": "%s" % (self.before_5pm,)}) |
| 2001 |
self.assertFalse(f.is_valid()) |
| 2002 |
|
| 2003 |
f = F( |
| 2004 |
{ |
| 2005 |
"published__range": "%s,%s" |
| 2006 |
% ( |
| 2007 |
self.before_5pm, |
| 2008 |
self.around_5pm, |
| 2009 |
) |
| 2010 |
} |
| 2011 |
) |
| 2012 |
self.assertEqual(f.qs.count(), 3) |
| 2013 |
|
| 2014 |
f = F( |
| 2015 |
{ |
| 2016 |
"published__range": "%s,,%s" |
| 2017 |
% ( |
| 2018 |
self.before_5pm, |
| 2019 |
self.after_5pm, |
| 2020 |
) |
| 2021 |
} |
| 2022 |
) |
| 2023 |
self.assertFalse(f.is_valid()) |
| 2024 |
|
| 2025 |
# empty value is interpreted as None type |
| 2026 |
f = F({"published__range": "%s," % (self.before_5pm,)}) |
| 2027 |
self.assertEqual(f.qs.count(), 0) |
| 2028 |
|
| 2029 |
|
| 2030 |
class OrderingFilterTests(TestCase): |
| 2031 |
def setUp(self): |
| 2032 |
User.objects.create(username="alex", status=1) |
| 2033 |
User.objects.create(username="jacob", status=2) |
| 2034 |
User.objects.create(username="aaron", status=2) |
| 2035 |
User.objects.create(username="carl", status=0) |
| 2036 |
|
| 2037 |
def test_ordering(self): |
| 2038 |
class F(FilterSet): |
| 2039 |
o = OrderingFilter(fields=("username",)) |
| 2040 |
|
| 2041 |
class Meta: |
| 2042 |
model = User |
| 2043 |
fields = ["username"] |
| 2044 |
|
| 2045 |
qs = User.objects.all() |
| 2046 |
tests = [ |
| 2047 |
{"o": "username"}, |
| 2048 |
QueryDict("o=username,"), |
| 2049 |
] |
| 2050 |
for data in tests: |
| 2051 |
with self.subTest(data=data): |
| 2052 |
f = F(data, queryset=qs) |
| 2053 |
names = f.qs.values_list("username", flat=True) |
| 2054 |
self.assertEqual(list(names), ["aaron", "alex", "carl", "jacob"]) |
| 2055 |
|
| 2056 |
def test_ordering_with_select_widget(self): |
| 2057 |
class F(FilterSet): |
| 2058 |
o = OrderingFilter(widget=forms.Select, fields=("username",)) |
| 2059 |
|
| 2060 |
class Meta: |
| 2061 |
model = User |
| 2062 |
fields = ["username"] |
| 2063 |
|
| 2064 |
qs = User.objects.all() |
| 2065 |
f = F({"o": "username"}, queryset=qs) |
| 2066 |
names = f.qs.values_list("username", flat=True) |
| 2067 |
self.assertEqual(list(names), ["aaron", "alex", "carl", "jacob"]) |
| 2068 |
|
| 2069 |
def test_csv_input(self): |
| 2070 |
class F(FilterSet): |
| 2071 |
o = OrderingFilter( |
| 2072 |
widget=forms.Select, |
| 2073 |
fields=("username",), |
| 2074 |
) |
| 2075 |
|
| 2076 |
class Meta: |
| 2077 |
model = User |
| 2078 |
fields = ["username"] |
| 2079 |
|
| 2080 |
qs = User.objects.all() |
| 2081 |
tests = [ |
| 2082 |
{"o": ","}, |
| 2083 |
QueryDict("o=%2c"), |
| 2084 |
QueryDict("o=,"), |
| 2085 |
] |
| 2086 |
for data in tests: |
| 2087 |
with self.subTest(data=data): |
| 2088 |
f = F(data, queryset=qs) |
| 2089 |
self.assertIs(True, f.is_valid()) |
| 2090 |
names = f.qs.values_list("username", flat=True) |
| 2091 |
self.assertEqual(list(names), ["alex", "jacob", "aaron", "carl"]) |
| 2092 |
|
| 2093 |
|
| 2094 |
class MiscFilterSetTests(TestCase): |
| 2095 |
def setUp(self): |
| 2096 |
User.objects.create(username="alex", last_name="johnson", status=1) |
| 2097 |
User.objects.create(username="jacob", last_name="johnson", status=2) |
| 2098 |
User.objects.create(username="aaron", last_name="white", status=2) |
| 2099 |
User.objects.create(username="carl", last_name="black", status=0) |
| 2100 |
|
| 2101 |
def test_filtering_with_declared_filters(self): |
| 2102 |
class F(FilterSet): |
| 2103 |
account = CharFilter(field_name="username") |
| 2104 |
|
| 2105 |
class Meta: |
| 2106 |
model = User |
| 2107 |
fields = ["account"] |
| 2108 |
|
| 2109 |
qs = MockQuerySet() |
| 2110 |
F({"account": "jdoe"}, queryset=qs).qs |
| 2111 |
qs.all.return_value.filter.assert_called_with(username__exact="jdoe") |
| 2112 |
|
| 2113 |
def test_filtering_without_meta(self): |
| 2114 |
class F(FilterSet): |
| 2115 |
username = CharFilter() |
| 2116 |
|
| 2117 |
f = F({"username": "alex"}, queryset=User.objects.all()) |
| 2118 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username) |
| 2119 |
|
| 2120 |
def test_filtering_with_multiple_filters(self): |
| 2121 |
class F(FilterSet): |
| 2122 |
class Meta: |
| 2123 |
model = User |
| 2124 |
fields = ["status", "username"] |
| 2125 |
|
| 2126 |
qs = User.objects.all() |
| 2127 |
|
| 2128 |
f = F({"username": "alex", "status": "1"}, queryset=qs) |
| 2129 |
self.assertQuerySetEqual(f.qs, ["alex"], lambda o: o.username) |
| 2130 |
|
| 2131 |
f = F({"username": "alex", "status": "2"}, queryset=qs) |
| 2132 |
self.assertQuerySetEqual(f.qs, [], lambda o: o.pk) |
| 2133 |
|
| 2134 |
def test_filter_with_initial(self): |
| 2135 |
# Initial values are a form presentation option - the FilterSet should |
| 2136 |
# not use an initial value as a default value to filter by. |
| 2137 |
class F(FilterSet): |
| 2138 |
status = ChoiceFilter(choices=STATUS_CHOICES, initial=1) |
| 2139 |
|
| 2140 |
class Meta: |
| 2141 |
model = User |
| 2142 |
fields = ["status"] |
| 2143 |
|
| 2144 |
qs = User.objects.all() |
| 2145 |
users = ["alex", "jacob", "aaron", "carl"] |
| 2146 |
|
| 2147 |
f = F(queryset=qs) |
| 2148 |
self.assertQuerySetEqual(f.qs.order_by("pk"), users, lambda o: o.username) |
| 2149 |
|
| 2150 |
f = F({"status": 0}, queryset=qs) |
| 2151 |
self.assertQuerySetEqual(f.qs, ["carl"], lambda o: o.username) |
| 2152 |
|
| 2153 |
def test_qs_count(self): |
| 2154 |
class F(FilterSet): |
| 2155 |
class Meta: |
| 2156 |
model = User |
| 2157 |
fields = ["status"] |
| 2158 |
|
| 2159 |
qs = User.objects.all() |
| 2160 |
f = F(queryset=qs) |
| 2161 |
self.assertEqual(len(f.qs), 4) |
| 2162 |
self.assertEqual(f.qs.count(), 4) |
| 2163 |
|
| 2164 |
f = F({"status": "0"}, queryset=qs) |
| 2165 |
self.assertEqual(len(f.qs), 1) |
| 2166 |
self.assertEqual(f.qs.count(), 1) |
| 2167 |
|
| 2168 |
f = F({"status": "1"}, queryset=qs) |
| 2169 |
self.assertEqual(len(f.qs), 1) |
| 2170 |
self.assertEqual(f.qs.count(), 1) |
| 2171 |
|
| 2172 |
f = F({"status": "2"}, queryset=qs) |
| 2173 |
self.assertEqual(len(f.qs), 2) |
| 2174 |
self.assertEqual(f.qs.count(), 2) |
| 2175 |
|
| 2176 |
def test_filtering_with_widgets(self): |
| 2177 |
class CharInFilter(BaseInFilter, CharFilter): |
| 2178 |
pass |
| 2179 |
|
| 2180 |
class F(FilterSet): |
| 2181 |
last_name = CharInFilter(widget=QueryArrayWidget) |
| 2182 |
username = CharInFilter() |
| 2183 |
|
| 2184 |
class Meta: |
| 2185 |
model = User |
| 2186 |
fields = ["last_name", "username"] |
| 2187 |
|
| 2188 |
qs = User.objects.all() |
| 2189 |
|
| 2190 |
f = F({"last_name": ["johnson"]}, queryset=qs) |
| 2191 |
self.assertQuerySetEqual( |
| 2192 |
f.qs, ["alex", "jacob"], lambda o: o.username, ordered=False |
| 2193 |
) |
| 2194 |
|
| 2195 |
f = F({"last_name": ["johnson"], "username": "carl"}, queryset=qs) |
| 2196 |
self.assertQuerySetEqual(f.qs, [], lambda o: o.username, ordered=False) |
| 2197 |
|
| 2198 |
f = F({"last_name": ["johnson"], "username": "jacob"}, queryset=qs) |
| 2199 |
self.assertQuerySetEqual(f.qs, ["jacob"], lambda o: o.username, ordered=False) |
| 2200 |
|
| 2201 |
f = F( |
| 2202 |
{"last_name": ["johnson", "white"], "username": "jacob, carl, aaron"}, |
| 2203 |
queryset=qs, |
| 2204 |
) |
| 2205 |
self.assertQuerySetEqual( |
| 2206 |
f.qs, ["jacob", "aaron"], lambda o: o.username, ordered=False |
| 2207 |
) |