summaryrefslogtreecommitdiff
path: root/tests/admin_views/tests.py
diff options
context:
space:
mode:
authormgaligniana <marcelogaligniana@gmail.com>2022-04-13 15:27:21 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-04-15 07:46:37 +0200
commitc72f6f36c13a21f6db3d4f85d2d3cec87bad45e6 (patch)
tree5a97d9e0ca88e853fec2436731b1ef0dcf206d93 /tests/admin_views/tests.py
parentdeedf5bbc347e47b3be6e15783fc43a9c0a69256 (diff)
Fixed #11803 -- Allowed admin select widgets to display new related objects.
Adjusted admin javascript to add newly created related objects to already loaded select widgets. In this version, applies only where limit_choices_to is not set.
Diffstat (limited to 'tests/admin_views/tests.py')
-rw-r--r--tests/admin_views/tests.py141
1 files changed, 141 insertions, 0 deletions
diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py
index 6fb917bd34..72fac695dd 100644
--- a/tests/admin_views/tests.py
+++ b/tests/admin_views/tests.py
@@ -134,6 +134,7 @@ from .models import (
Telegram,
TitleTranslation,
Topping,
+ Traveler,
UnchangeableObject,
UndeletableObject,
UnorderedObject,
@@ -6275,6 +6276,146 @@ class SeleniumTests(AdminSeleniumTestCase):
finally:
self.selenium.set_window_size(current_size["width"], current_size["height"])
+ def test_updating_related_objects_updates_fk_selects(self):
+ from selenium.webdriver.common.by import By
+ from selenium.webdriver.support.ui import Select
+
+ born_country_select_id = "id_born_country"
+ living_country_select_id = "id_living_country"
+ favorite_country_to_vacation_select_id = "id_favorite_country_to_vacation"
+ continent_select_id = "id_continent"
+
+ def _get_HTML_inside_element_by_id(id_):
+ return self.selenium.find_element(By.ID, id_).get_attribute("innerHTML")
+
+ self.admin_login(
+ username="super", password="secret", login_url=reverse("admin:index")
+ )
+ add_url = reverse("admin:admin_views_traveler_add")
+ self.selenium.get(self.live_server_url + add_url)
+
+ # Add new Country from the born_country select.
+ self.selenium.find_element(By.ID, f"add_{born_country_select_id}").click()
+ self.wait_for_and_switch_to_popup()
+ self.selenium.find_element(By.ID, "id_name").send_keys("Argentina")
+ continent_select = Select(
+ self.selenium.find_element(By.ID, continent_select_id)
+ )
+ continent_select.select_by_visible_text("South America")
+ self.selenium.find_element(By.CSS_SELECTOR, '[type="submit"]').click()
+ self.selenium.switch_to.window(self.selenium.window_handles[0])
+
+ self.assertHTMLEqual(
+ _get_HTML_inside_element_by_id(born_country_select_id),
+ """
+ <option value="" selected="">---------</option>
+ <option value="1" selected="">Argentina</option>
+ """,
+ )
+ self.assertHTMLEqual(
+ _get_HTML_inside_element_by_id(living_country_select_id),
+ """
+ <option value="" selected="">---------</option>
+ <option value="1">Argentina</option>
+ """,
+ )
+ # Argentina won't appear because favorite_country_to_vacation field has
+ # limit_choices_to.
+ self.assertHTMLEqual(
+ _get_HTML_inside_element_by_id(favorite_country_to_vacation_select_id),
+ '<option value="" selected="">---------</option>',
+ )
+
+ # Add new Country from the living_country select.
+ self.selenium.find_element(By.ID, f"add_{living_country_select_id}").click()
+ self.wait_for_and_switch_to_popup()
+ self.selenium.find_element(By.ID, "id_name").send_keys("Spain")
+ continent_select = Select(
+ self.selenium.find_element(By.ID, continent_select_id)
+ )
+ continent_select.select_by_visible_text("Europe")
+ self.selenium.find_element(By.CSS_SELECTOR, '[type="submit"]').click()
+ self.selenium.switch_to.window(self.selenium.window_handles[0])
+
+ self.assertHTMLEqual(
+ _get_HTML_inside_element_by_id(born_country_select_id),
+ """
+ <option value="" selected="">---------</option>
+ <option value="1" selected="">Argentina</option>
+ <option value="2">Spain</option>
+ """,
+ )
+ self.assertHTMLEqual(
+ _get_HTML_inside_element_by_id(living_country_select_id),
+ """
+ <option value="" selected="">---------</option>
+ <option value="1">Argentina</option>
+ <option value="2" selected="">Spain</option>
+ """,
+ )
+ # Spain won't appear because favorite_country_to_vacation field has
+ # limit_choices_to.
+ self.assertHTMLEqual(
+ _get_HTML_inside_element_by_id(favorite_country_to_vacation_select_id),
+ '<option value="" selected="">---------</option>',
+ )
+
+ # Edit second Country created from living_country select.
+ favorite_select = Select(
+ self.selenium.find_element(By.ID, living_country_select_id)
+ )
+ favorite_select.select_by_visible_text("Spain")
+ self.selenium.find_element(By.ID, f"change_{living_country_select_id}").click()
+ self.wait_for_and_switch_to_popup()
+ favorite_name_input = self.selenium.find_element(By.ID, "id_name")
+ favorite_name_input.clear()
+ favorite_name_input.send_keys("Italy")
+ self.selenium.find_element(By.CSS_SELECTOR, '[type="submit"]').click()
+ self.selenium.switch_to.window(self.selenium.window_handles[0])
+
+ self.assertHTMLEqual(
+ _get_HTML_inside_element_by_id(born_country_select_id),
+ """
+ <option value="" selected="">---------</option>
+ <option value="1" selected="">Argentina</option>
+ <option value="2">Italy</option>
+ """,
+ )
+ self.assertHTMLEqual(
+ _get_HTML_inside_element_by_id(living_country_select_id),
+ """
+ <option value="" selected="">---------</option>
+ <option value="1">Argentina</option>
+ <option value="2" selected="">Italy</option>
+ """,
+ )
+ # favorite_country_to_vacation field has no options.
+ self.assertHTMLEqual(
+ _get_HTML_inside_element_by_id(favorite_country_to_vacation_select_id),
+ '<option value="" selected="">---------</option>',
+ )
+
+ # Add a new Asian country.
+ self.selenium.find_element(
+ By.ID, f"add_{favorite_country_to_vacation_select_id}"
+ ).click()
+ self.wait_for_and_switch_to_popup()
+ favorite_name_input = self.selenium.find_element(By.ID, "id_name")
+ favorite_name_input.send_keys("Qatar")
+ continent_select = Select(
+ self.selenium.find_element(By.ID, continent_select_id)
+ )
+ continent_select.select_by_visible_text("Asia")
+ self.selenium.find_element(By.CSS_SELECTOR, '[type="submit"]').click()
+ self.selenium.switch_to.window(self.selenium.window_handles[0])
+
+ # Submit the new Traveler.
+ self.selenium.find_element(By.CSS_SELECTOR, '[name="_save"]').click()
+ traveler = Traveler.objects.get()
+ self.assertEqual(traveler.born_country.name, "Argentina")
+ self.assertEqual(traveler.living_country.name, "Italy")
+ self.assertEqual(traveler.favorite_country_to_vacation.name, "Qatar")
+
@override_settings(ROOT_URLCONF="admin_views.urls")
class ReadonlyTest(AdminFieldExtractionMixin, TestCase):