| 1 |
import datetime |
| 2 |
import json |
| 3 |
from contextlib import contextmanager |
| 4 |
|
| 5 |
from django.contrib import admin |
| 6 |
from django.contrib.admin.exceptions import NotRegistered |
| 7 |
from django.contrib.admin.tests import AdminSeleniumTestCase |
| 8 |
from django.contrib.admin.views.autocomplete import AutocompleteJsonView |
| 9 |
from django.contrib.auth.models import Permission, User |
| 10 |
from django.contrib.contenttypes.models import ContentType |
| 11 |
from django.core.exceptions import PermissionDenied |
| 12 |
from django.http import Http404 |
| 13 |
from django.test import RequestFactory, override_settings |
| 14 |
from django.urls import reverse, reverse_lazy |
| 15 |
|
| 16 |
from .admin import AnswerAdmin, QuestionAdmin |
| 17 |
from .models import ( |
| 18 |
Answer, |
| 19 |
Author, |
| 20 |
Authorship, |
| 21 |
Bonus, |
| 22 |
Book, |
| 23 |
Employee, |
| 24 |
Manager, |
| 25 |
Parent, |
| 26 |
PKChild, |
| 27 |
Question, |
| 28 |
Toy, |
| 29 |
WorkHour, |
| 30 |
) |
| 31 |
from .tests import AdminViewBasicTestCase |
| 32 |
|
| 33 |
PAGINATOR_SIZE = AutocompleteJsonView.paginate_by |
| 34 |
|
| 35 |
|
| 36 |
class AuthorAdmin(admin.ModelAdmin): |
| 37 |
ordering = ["id"] |
| 38 |
search_fields = ["id"] |
| 39 |
|
| 40 |
|
| 41 |
class AuthorshipInline(admin.TabularInline): |
| 42 |
model = Authorship |
| 43 |
autocomplete_fields = ["author"] |
| 44 |
|
| 45 |
|
| 46 |
class BookAdmin(admin.ModelAdmin): |
| 47 |
inlines = [AuthorshipInline] |
| 48 |
|
| 49 |
|
| 50 |
site = admin.AdminSite(name="autocomplete_admin") |
| 51 |
site.register(Question, QuestionAdmin) |
| 52 |
site.register(Answer, AnswerAdmin) |
| 53 |
site.register(Author, AuthorAdmin) |
| 54 |
site.register(Book, BookAdmin) |
| 55 |
site.register(Employee, search_fields=["name"]) |
| 56 |
site.register(WorkHour, autocomplete_fields=["employee"]) |
| 57 |
site.register(Manager, search_fields=["name"]) |
| 58 |
site.register(Bonus, autocomplete_fields=["recipient"]) |
| 59 |
site.register(PKChild, search_fields=["name"]) |
| 60 |
site.register(Toy, autocomplete_fields=["child"]) |
| 61 |
|
| 62 |
|
| 63 |
@contextmanager |
| 64 |
def model_admin(model, model_admin, admin_site=site): |
| 65 |
try: |
| 66 |
org_admin = admin_site.get_model_admin(model) |
| 67 |
except NotRegistered: |
| 68 |
org_admin = None |
| 69 |
else: |
| 70 |
admin_site.unregister(model) |
| 71 |
admin_site.register(model, model_admin) |
| 72 |
try: |
| 73 |
yield |
| 74 |
finally: |
| 75 |
if org_admin: |
| 76 |
admin_site._registry[model] = org_admin |
| 77 |
|
| 78 |
|
| 79 |
class AutocompleteJsonViewTests(AdminViewBasicTestCase): |
| 80 |
as_view_args = {"admin_site": site} |
| 81 |
opts = { |
| 82 |
"app_label": Answer._meta.app_label, |
| 83 |
"model_name": Answer._meta.model_name, |
| 84 |
"field_name": "question", |
| 85 |
} |
| 86 |
factory = RequestFactory() |
| 87 |
url = reverse_lazy("autocomplete_admin:autocomplete") |
| 88 |
|
| 89 |
@classmethod |
| 90 |
def setUpTestData(cls): |
| 91 |
cls.user = User.objects.create_user( |
| 92 |
username="user", |
| 93 |
password="secret", |
| 94 |
email="[email protected]", |
| 95 |
is_staff=True, |
| 96 |
) |
| 97 |
super().setUpTestData() |
| 98 |
|
| 99 |
def test_success(self): |
| 100 |
q = Question.objects.create(question="Is this a question?") |
| 101 |
request = self.factory.get(self.url, {"term": "is", **self.opts}) |
| 102 |
request.user = self.superuser |
| 103 |
response = AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 104 |
self.assertEqual(response.status_code, 200) |
| 105 |
data = json.loads(response.text) |
| 106 |
self.assertEqual( |
| 107 |
data, |
| 108 |
{ |
| 109 |
"results": [{"id": str(q.pk), "text": q.question}], |
| 110 |
"pagination": {"more": False}, |
| 111 |
}, |
| 112 |
) |
| 113 |
|
| 114 |
def test_custom_to_field(self): |
| 115 |
q = Question.objects.create(question="Is this a question?") |
| 116 |
request = self.factory.get( |
| 117 |
self.url, |
| 118 |
{"term": "is", **self.opts, "field_name": "question_with_to_field"}, |
| 119 |
) |
| 120 |
request.user = self.superuser |
| 121 |
response = AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 122 |
self.assertEqual(response.status_code, 200) |
| 123 |
data = json.loads(response.text) |
| 124 |
self.assertEqual( |
| 125 |
data, |
| 126 |
{ |
| 127 |
"results": [{"id": str(q.uuid), "text": q.question}], |
| 128 |
"pagination": {"more": False}, |
| 129 |
}, |
| 130 |
) |
| 131 |
|
| 132 |
def test_custom_to_field_permission_denied(self): |
| 133 |
Question.objects.create(question="Is this a question?") |
| 134 |
request = self.factory.get( |
| 135 |
self.url, |
| 136 |
{"term": "is", **self.opts, "field_name": "question_with_to_field"}, |
| 137 |
) |
| 138 |
request.user = self.user |
| 139 |
with self.assertRaises(PermissionDenied): |
| 140 |
AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 141 |
|
| 142 |
def test_custom_to_field_custom_pk(self): |
| 143 |
q = Question.objects.create(question="Is this a question?") |
| 144 |
opts = { |
| 145 |
"app_label": Question._meta.app_label, |
| 146 |
"model_name": Question._meta.model_name, |
| 147 |
"field_name": "related_questions", |
| 148 |
} |
| 149 |
request = self.factory.get(self.url, {"term": "is", **opts}) |
| 150 |
request.user = self.superuser |
| 151 |
response = AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 152 |
self.assertEqual(response.status_code, 200) |
| 153 |
data = json.loads(response.text) |
| 154 |
self.assertEqual( |
| 155 |
data, |
| 156 |
{ |
| 157 |
"results": [{"id": str(q.big_id), "text": q.question}], |
| 158 |
"pagination": {"more": False}, |
| 159 |
}, |
| 160 |
) |
| 161 |
|
| 162 |
def test_to_field_resolution_with_mti(self): |
| 163 |
""" |
| 164 |
to_field resolution should correctly resolve for target models using |
| 165 |
MTI. Tests for single and multi-level cases. |
| 166 |
""" |
| 167 |
tests = [ |
| 168 |
(Employee, WorkHour, "employee"), |
| 169 |
(Manager, Bonus, "recipient"), |
| 170 |
] |
| 171 |
for Target, Remote, related_name in tests: |
| 172 |
with self.subTest( |
| 173 |
target_model=Target, remote_model=Remote, related_name=related_name |
| 174 |
): |
| 175 |
o = Target.objects.create( |
| 176 |
name="Frida Kahlo", gender=2, code="painter", alive=False |
| 177 |
) |
| 178 |
opts = { |
| 179 |
"app_label": Remote._meta.app_label, |
| 180 |
"model_name": Remote._meta.model_name, |
| 181 |
"field_name": related_name, |
| 182 |
} |
| 183 |
request = self.factory.get(self.url, {"term": "frida", **opts}) |
| 184 |
request.user = self.superuser |
| 185 |
response = AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 186 |
self.assertEqual(response.status_code, 200) |
| 187 |
data = json.loads(response.text) |
| 188 |
self.assertEqual( |
| 189 |
data, |
| 190 |
{ |
| 191 |
"results": [{"id": str(o.pk), "text": o.name}], |
| 192 |
"pagination": {"more": False}, |
| 193 |
}, |
| 194 |
) |
| 195 |
|
| 196 |
def test_to_field_resolution_with_fk_pk(self): |
| 197 |
p = Parent.objects.create(name="Bertie") |
| 198 |
c = PKChild.objects.create(parent=p, name="Anna") |
| 199 |
opts = { |
| 200 |
"app_label": Toy._meta.app_label, |
| 201 |
"model_name": Toy._meta.model_name, |
| 202 |
"field_name": "child", |
| 203 |
} |
| 204 |
request = self.factory.get(self.url, {"term": "anna", **opts}) |
| 205 |
request.user = self.superuser |
| 206 |
response = AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 207 |
self.assertEqual(response.status_code, 200) |
| 208 |
data = json.loads(response.text) |
| 209 |
self.assertEqual( |
| 210 |
data, |
| 211 |
{ |
| 212 |
"results": [{"id": str(c.pk), "text": c.name}], |
| 213 |
"pagination": {"more": False}, |
| 214 |
}, |
| 215 |
) |
| 216 |
|
| 217 |
def test_field_does_not_exist(self): |
| 218 |
request = self.factory.get( |
| 219 |
self.url, {"term": "is", **self.opts, "field_name": "does_not_exist"} |
| 220 |
) |
| 221 |
request.user = self.superuser |
| 222 |
with self.assertRaises(PermissionDenied): |
| 223 |
AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 224 |
|
| 225 |
def test_field_no_related_field(self): |
| 226 |
request = self.factory.get( |
| 227 |
self.url, {"term": "is", **self.opts, "field_name": "answer"} |
| 228 |
) |
| 229 |
request.user = self.superuser |
| 230 |
with self.assertRaises(PermissionDenied): |
| 231 |
AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 232 |
|
| 233 |
def test_field_does_not_allowed(self): |
| 234 |
request = self.factory.get( |
| 235 |
self.url, {"term": "is", **self.opts, "field_name": "related_questions"} |
| 236 |
) |
| 237 |
request.user = self.superuser |
| 238 |
with self.assertRaises(PermissionDenied): |
| 239 |
AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 240 |
|
| 241 |
def test_limit_choices_to(self): |
| 242 |
# Answer.question_with_to_field defines limit_choices_to to "those not |
| 243 |
# starting with 'not'". |
| 244 |
q = Question.objects.create(question="Is this a question?") |
| 245 |
Question.objects.create(question="Not a question.") |
| 246 |
request = self.factory.get( |
| 247 |
self.url, |
| 248 |
{"term": "is", **self.opts, "field_name": "question_with_to_field"}, |
| 249 |
) |
| 250 |
request.user = self.superuser |
| 251 |
response = AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 252 |
self.assertEqual(response.status_code, 200) |
| 253 |
data = json.loads(response.text) |
| 254 |
self.assertEqual( |
| 255 |
data, |
| 256 |
{ |
| 257 |
"results": [{"id": str(q.uuid), "text": q.question}], |
| 258 |
"pagination": {"more": False}, |
| 259 |
}, |
| 260 |
) |
| 261 |
|
| 262 |
def test_must_be_logged_in(self): |
| 263 |
response = self.client.get(self.url, {"term": "", **self.opts}) |
| 264 |
self.assertEqual(response.status_code, 200) |
| 265 |
self.client.logout() |
| 266 |
response = self.client.get(self.url, {"term": "", **self.opts}) |
| 267 |
self.assertEqual(response.status_code, 302) |
| 268 |
|
| 269 |
def test_has_view_or_change_permission_required(self): |
| 270 |
""" |
| 271 |
Users require the change permission for the related model to the |
| 272 |
autocomplete view for it. |
| 273 |
""" |
| 274 |
request = self.factory.get(self.url, {"term": "is", **self.opts}) |
| 275 |
request.user = self.user |
| 276 |
with self.assertRaises(PermissionDenied): |
| 277 |
AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 278 |
for permission in ("view", "change"): |
| 279 |
with self.subTest(permission=permission): |
| 280 |
self.user.user_permissions.clear() |
| 281 |
p = Permission.objects.get( |
| 282 |
content_type=ContentType.objects.get_for_model(Question), |
| 283 |
codename="%s_question" % permission, |
| 284 |
) |
| 285 |
self.user.user_permissions.add(p) |
| 286 |
request.user = User.objects.get(pk=self.user.pk) |
| 287 |
response = AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 288 |
self.assertEqual(response.status_code, 200) |
| 289 |
|
| 290 |
def test_search_use_distinct(self): |
| 291 |
""" |
| 292 |
Searching across model relations use QuerySet.distinct() to avoid |
| 293 |
duplicates. |
| 294 |
""" |
| 295 |
q1 = Question.objects.create(question="question 1") |
| 296 |
q2 = Question.objects.create(question="question 2") |
| 297 |
q2.related_questions.add(q1) |
| 298 |
q3 = Question.objects.create(question="question 3") |
| 299 |
q3.related_questions.add(q1) |
| 300 |
request = self.factory.get(self.url, {"term": "question", **self.opts}) |
| 301 |
request.user = self.superuser |
| 302 |
|
| 303 |
class DistinctQuestionAdmin(QuestionAdmin): |
| 304 |
search_fields = ["related_questions__question", "question"] |
| 305 |
|
| 306 |
with model_admin(Question, DistinctQuestionAdmin): |
| 307 |
response = AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 308 |
self.assertEqual(response.status_code, 200) |
| 309 |
data = json.loads(response.text) |
| 310 |
self.assertEqual(len(data["results"]), 3) |
| 311 |
|
| 312 |
def test_missing_search_fields(self): |
| 313 |
class EmptySearchAdmin(QuestionAdmin): |
| 314 |
search_fields = [] |
| 315 |
|
| 316 |
with model_admin(Question, EmptySearchAdmin): |
| 317 |
msg = "EmptySearchAdmin must have search_fields for the autocomplete_view." |
| 318 |
with self.assertRaisesMessage(Http404, msg): |
| 319 |
site.autocomplete_view( |
| 320 |
self.factory.get(self.url, {"term": "", **self.opts}) |
| 321 |
) |
| 322 |
|
| 323 |
def test_get_paginator(self): |
| 324 |
"""Search results are paginated.""" |
| 325 |
|
| 326 |
class PKOrderingQuestionAdmin(QuestionAdmin): |
| 327 |
ordering = ["pk"] |
| 328 |
|
| 329 |
Question.objects.bulk_create( |
| 330 |
Question(question=str(i)) for i in range(PAGINATOR_SIZE + 10) |
| 331 |
) |
| 332 |
# The first page of results. |
| 333 |
request = self.factory.get(self.url, {"term": "", **self.opts}) |
| 334 |
request.user = self.superuser |
| 335 |
with model_admin(Question, PKOrderingQuestionAdmin): |
| 336 |
response = AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 337 |
self.assertEqual(response.status_code, 200) |
| 338 |
data = json.loads(response.text) |
| 339 |
self.assertEqual( |
| 340 |
data, |
| 341 |
{ |
| 342 |
"results": [ |
| 343 |
{"id": str(q.pk), "text": q.question} |
| 344 |
for q in Question.objects.all()[:PAGINATOR_SIZE] |
| 345 |
], |
| 346 |
"pagination": {"more": True}, |
| 347 |
}, |
| 348 |
) |
| 349 |
# The second page of results. |
| 350 |
request = self.factory.get(self.url, {"term": "", "page": "2", **self.opts}) |
| 351 |
request.user = self.superuser |
| 352 |
with model_admin(Question, PKOrderingQuestionAdmin): |
| 353 |
response = AutocompleteJsonView.as_view(**self.as_view_args)(request) |
| 354 |
self.assertEqual(response.status_code, 200) |
| 355 |
data = json.loads(response.text) |
| 356 |
self.assertEqual( |
| 357 |
data, |
| 358 |
{ |
| 359 |
"results": [ |
| 360 |
{"id": str(q.pk), "text": q.question} |
| 361 |
for q in Question.objects.all()[PAGINATOR_SIZE:] |
| 362 |
], |
| 363 |
"pagination": {"more": False}, |
| 364 |
}, |
| 365 |
) |
| 366 |
|
| 367 |
def test_serialize_result(self): |
| 368 |
class AutocompleteJsonSerializeResultView(AutocompleteJsonView): |
| 369 |
def serialize_result(self, obj, to_field_name): |
| 370 |
return { |
| 371 |
**super().serialize_result(obj, to_field_name), |
| 372 |
"posted": str(obj.posted), |
| 373 |
} |
| 374 |
|
| 375 |
Question.objects.create(question="Question 1", posted=datetime.date(2021, 8, 9)) |
| 376 |
Question.objects.create(question="Question 2", posted=datetime.date(2021, 8, 7)) |
| 377 |
request = self.factory.get(self.url, {"term": "question", **self.opts}) |
| 378 |
request.user = self.superuser |
| 379 |
response = AutocompleteJsonSerializeResultView.as_view(**self.as_view_args)( |
| 380 |
request |
| 381 |
) |
| 382 |
self.assertEqual(response.status_code, 200) |
| 383 |
data = json.loads(response.text) |
| 384 |
self.assertEqual( |
| 385 |
data, |
| 386 |
{ |
| 387 |
"results": [ |
| 388 |
{"id": str(q.pk), "text": q.question, "posted": str(q.posted)} |
| 389 |
for q in Question.objects.order_by("-posted") |
| 390 |
], |
| 391 |
"pagination": {"more": False}, |
| 392 |
}, |
| 393 |
) |
| 394 |
|
| 395 |
|
| 396 |
@override_settings(ROOT_URLCONF="admin_views.urls") |
| 397 |
class SeleniumTests(AdminSeleniumTestCase): |
| 398 |
available_apps = ["admin_views"] + AdminSeleniumTestCase.available_apps |
| 399 |
|
| 400 |
def setUp(self): |
| 401 |
self.superuser = User.objects.create_superuser( |
| 402 |
username="super", |
| 403 |
password="secret", |
| 404 |
email="[email protected]", |
| 405 |
) |
| 406 |
self.admin_login( |
| 407 |
username="super", |
| 408 |
password="secret", |
| 409 |
login_url=reverse("autocomplete_admin:index"), |
| 410 |
) |
| 411 |
|
| 412 |
@contextmanager |
| 413 |
def select2_ajax_wait(self, timeout=10): |
| 414 |
from selenium.common.exceptions import NoSuchElementException |
| 415 |
from selenium.webdriver.common.by import By |
| 416 |
from selenium.webdriver.support import expected_conditions as ec |
| 417 |
|
| 418 |
yield |
| 419 |
with self.disable_implicit_wait(): |
| 420 |
try: |
| 421 |
loading_element = self.selenium.find_element( |
| 422 |
By.CSS_SELECTOR, "li.select2-results__option.loading-results" |
| 423 |
) |
| 424 |
except NoSuchElementException: |
| 425 |
pass |
| 426 |
else: |
| 427 |
self.wait_until(ec.staleness_of(loading_element), timeout=timeout) |
| 428 |
|
| 429 |
def test_select(self): |
| 430 |
from selenium.webdriver.common.by import By |
| 431 |
from selenium.webdriver.common.keys import Keys |
| 432 |
from selenium.webdriver.support.ui import Select |
| 433 |
|
| 434 |
self.selenium.get( |
| 435 |
self.live_server_url + reverse("autocomplete_admin:admin_views_answer_add") |
| 436 |
) |
| 437 |
elem = self.selenium.find_element(By.CSS_SELECTOR, ".select2-selection") |
| 438 |
with self.select2_ajax_wait(): |
| 439 |
elem.click() # Open the autocomplete dropdown. |
| 440 |
results = self.selenium.find_element(By.CSS_SELECTOR, ".select2-results") |
| 441 |
self.assertTrue(results.is_displayed()) |
| 442 |
option = self.selenium.find_element(By.CSS_SELECTOR, ".select2-results__option") |
| 443 |
self.assertEqual(option.text, "No results found") |
| 444 |
with self.select2_ajax_wait(): |
| 445 |
elem.click() # Close the autocomplete dropdown. |
| 446 |
q1 = Question.objects.create(question="Who am I?") |
| 447 |
Question.objects.bulk_create( |
| 448 |
Question(question=str(i)) for i in range(PAGINATOR_SIZE + 10) |
| 449 |
) |
| 450 |
with self.select2_ajax_wait(): |
| 451 |
elem.click() # Reopen the dropdown now that some objects exist. |
| 452 |
result_container = self.selenium.find_element( |
| 453 |
By.CSS_SELECTOR, ".select2-results" |
| 454 |
) |
| 455 |
self.assertTrue(result_container.is_displayed()) |
| 456 |
# PAGINATOR_SIZE results and "Loading more results". |
| 457 |
self.assertCountSeleniumElements( |
| 458 |
".select2-results__option", |
| 459 |
PAGINATOR_SIZE + 1, |
| 460 |
root_element=result_container, |
| 461 |
) |
| 462 |
search = self.selenium.find_element(By.CSS_SELECTOR, ".select2-search__field") |
| 463 |
# Load next page of results by scrolling to the bottom of the list. |
| 464 |
for _ in range(PAGINATOR_SIZE + 1): |
| 465 |
with self.select2_ajax_wait(): |
| 466 |
search.send_keys(Keys.ARROW_DOWN) |
| 467 |
# All objects are now loaded. |
| 468 |
self.assertCountSeleniumElements( |
| 469 |
".select2-results__option", |
| 470 |
PAGINATOR_SIZE + 11, |
| 471 |
root_element=result_container, |
| 472 |
) |
| 473 |
# Limit the results with the search field. |
| 474 |
with self.select2_ajax_wait(): |
| 475 |
search.send_keys("Who") |
| 476 |
# Ajax request is delayed. |
| 477 |
self.assertTrue(result_container.is_displayed()) |
| 478 |
self.assertCountSeleniumElements( |
| 479 |
".select2-results__option", |
| 480 |
PAGINATOR_SIZE + 12, |
| 481 |
root_element=result_container, |
| 482 |
) |
| 483 |
self.assertTrue(result_container.is_displayed()) |
| 484 |
self.assertCountSeleniumElements( |
| 485 |
".select2-results__option", 1, root_element=result_container |
| 486 |
) |
| 487 |
# Select the result. |
| 488 |
with self.select2_ajax_wait(): |
| 489 |
search.send_keys(Keys.RETURN) |
| 490 |
select = Select(self.selenium.find_element(By.ID, "id_question")) |
| 491 |
self.assertEqual( |
| 492 |
select.first_selected_option.get_attribute("value"), str(q1.pk) |
| 493 |
) |
| 494 |
|
| 495 |
def test_select_multiple(self): |
| 496 |
from selenium.common import NoSuchElementException |
| 497 |
from selenium.webdriver.common.by import By |
| 498 |
from selenium.webdriver.common.keys import Keys |
| 499 |
from selenium.webdriver.support.ui import Select |
| 500 |
|
| 501 |
self.selenium.get( |
| 502 |
self.live_server_url |
| 503 |
+ reverse("autocomplete_admin:admin_views_question_add") |
| 504 |
) |
| 505 |
elem = self.selenium.find_element(By.CSS_SELECTOR, ".select2-selection") |
| 506 |
with self.select2_ajax_wait(): |
| 507 |
elem.click() # Open the autocomplete dropdown. |
| 508 |
results = self.selenium.find_element(By.CSS_SELECTOR, ".select2-results") |
| 509 |
self.assertTrue(results.is_displayed()) |
| 510 |
option = self.selenium.find_element(By.CSS_SELECTOR, ".select2-results__option") |
| 511 |
self.assertEqual(option.text, "No results found") |
| 512 |
with self.select2_ajax_wait(): |
| 513 |
elem.click() # Close the autocomplete dropdown. |
| 514 |
Question.objects.create(question="Who am I?") |
| 515 |
Question.objects.bulk_create( |
| 516 |
Question(question=str(i)) for i in range(PAGINATOR_SIZE + 10) |
| 517 |
) |
| 518 |
with self.select2_ajax_wait(): |
| 519 |
elem.click() # Reopen the dropdown now that some objects exist. |
| 520 |
result_container = self.selenium.find_element( |
| 521 |
By.CSS_SELECTOR, ".select2-results" |
| 522 |
) |
| 523 |
self.assertIs(result_container.is_displayed(), True) |
| 524 |
self.assertCountSeleniumElements( |
| 525 |
".select2-results__option", |
| 526 |
PAGINATOR_SIZE + 1, |
| 527 |
root_element=result_container, |
| 528 |
) |
| 529 |
search = self.selenium.find_element(By.CSS_SELECTOR, ".select2-search__field") |
| 530 |
# Load next page of results by scrolling to the bottom of the list. |
| 531 |
for _ in range(PAGINATOR_SIZE + 1): |
| 532 |
with self.select2_ajax_wait(): |
| 533 |
search.send_keys(Keys.ARROW_DOWN) |
| 534 |
self.assertCountSeleniumElements( |
| 535 |
".select2-results__option", |
| 536 |
PAGINATOR_SIZE + 11, |
| 537 |
root_element=result_container, |
| 538 |
) |
| 539 |
# Limit the results with the search field. |
| 540 |
with self.select2_ajax_wait(): |
| 541 |
search.send_keys("Who") |
| 542 |
# Ajax request is delayed. |
| 543 |
self.assertIs(result_container.is_displayed(), True) |
| 544 |
self.assertCountSeleniumElements( |
| 545 |
".select2-results__option", |
| 546 |
PAGINATOR_SIZE + 12, |
| 547 |
root_element=result_container, |
| 548 |
) |
| 549 |
self.assertIs(result_container.is_displayed(), True) |
| 550 |
|
| 551 |
self.assertCountSeleniumElements( |
| 552 |
".select2-results__option", 1, root_element=result_container |
| 553 |
) |
| 554 |
with self.select2_ajax_wait(): |
| 555 |
# Select the result. |
| 556 |
search.send_keys(Keys.RETURN) |
| 557 |
with self.disable_implicit_wait(): |
| 558 |
with self.assertRaises(NoSuchElementException): |
| 559 |
self.selenium.find_element(By.CSS_SELECTOR, ".select2-results") |
| 560 |
with self.select2_ajax_wait(): |
| 561 |
# Reopen the dropdown. |
| 562 |
elem.click() |
| 563 |
result_container = self.selenium.find_element( |
| 564 |
By.CSS_SELECTOR, ".select2-results" |
| 565 |
) |
| 566 |
self.assertIs(result_container.is_displayed(), True) |
| 567 |
with self.select2_ajax_wait(): |
| 568 |
# Add the first result to the selection. |
| 569 |
search.send_keys(Keys.ARROW_DOWN) |
| 570 |
search.send_keys(Keys.RETURN) |
| 571 |
select = Select(self.selenium.find_element(By.ID, "id_related_questions")) |
| 572 |
self.assertEqual(len(select.all_selected_options), 2) |
| 573 |
|
| 574 |
def test_inline_add_another_widgets(self): |
| 575 |
from selenium.webdriver.common.by import By |
| 576 |
|
| 577 |
def assertNoResults(row): |
| 578 |
elem = row.find_element(By.CSS_SELECTOR, ".select2-selection") |
| 579 |
with self.select2_ajax_wait(): |
| 580 |
elem.click() # Open the autocomplete dropdown. |
| 581 |
results = self.selenium.find_element(By.CSS_SELECTOR, ".select2-results") |
| 582 |
self.assertTrue(results.is_displayed()) |
| 583 |
option = self.selenium.find_element( |
| 584 |
By.CSS_SELECTOR, ".select2-results__option" |
| 585 |
) |
| 586 |
self.assertEqual(option.text, "No results found") |
| 587 |
|
| 588 |
# Autocomplete works in rows present when the page loads. |
| 589 |
self.selenium.get( |
| 590 |
self.live_server_url + reverse("autocomplete_admin:admin_views_book_add") |
| 591 |
) |
| 592 |
rows = self.selenium.find_elements(By.CSS_SELECTOR, ".dynamic-authorship_set") |
| 593 |
self.assertEqual(len(rows), 3) |
| 594 |
assertNoResults(rows[0]) |
| 595 |
# Autocomplete works in rows added using the "Add another" button. |
| 596 |
self.selenium.find_element(By.LINK_TEXT, "Add another Authorship").click() |
| 597 |
rows = self.selenium.find_elements(By.CSS_SELECTOR, ".dynamic-authorship_set") |
| 598 |
self.assertEqual(len(rows), 4) |
| 599 |
assertNoResults(rows[-1]) |