Skip to content

Commit

Permalink
Tests to check data integrity after save. Closes #106
Browse files Browse the repository at this point in the history
  • Loading branch information
bartTC committed Jun 28, 2024
1 parent 14b2b24 commit ca57201
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
39 changes: 38 additions & 1 deletion dynamic_raw_id/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
from typing import TYPE_CHECKING

import pytest
from django.contrib.auth.models import User
from selenium.webdriver.common.by import By

from dynamic_raw_id.tests.testapp.models import (
CharPrimaryKeyModel,
IntPrimaryKeyModel,
UUIDPrimaryKeyModel,
)

if TYPE_CHECKING:
from selenium.webdriver.firefox.webdriver import WebDriver

Expand Down Expand Up @@ -57,8 +64,38 @@ def test_widgets(selenium: WebDriver) -> None:
label = selenium.find_element(By.ID, f"{id_value}_dynamic_raw_id_label")

# Test that the displayed value matches the test value
selenium.implicitly_wait(0.5) # Wait a bit for page reload
selenium.implicitly_wait(0.2) # Wait a bit for page reload
assert label.text == test_value

# For debugging, draw green border around the label and wait a bit
# selenium.execute_script("arguments[0].style.border='3px solid green'", label)

# Save the change form once and make sure, the previously
# entered values are shown correctly.
selenium.find_element(By.NAME, "_continue").click()
selenium.implicitly_wait(0.5) # Wait a bit for page reload

# Check values are all intact
el = selenium.find_element(By.ID, "id_dynamic_raw_id_fk")
assert el.get_attribute("value") == str(User.objects.get(username="user1").pk)

el = selenium.find_element(By.ID, "id_dynamic_raw_id_fk_limited")
assert el.get_attribute("value") == str(User.objects.get(username="admin").pk)

el = selenium.find_element(By.ID, "id_dynamic_raw_id_many")
u1 = User.objects.get(username="user1").pk
u2 = User.objects.get(username="user2").pk
assert el.get_attribute("value") == f"{u1},{u2}"

el = selenium.find_element(By.ID, "id_dynamic_raw_id_fk_int_pk")
assert el.get_attribute("value") == str(IntPrimaryKeyModel.objects.get(num=123).pk)

el = selenium.find_element(By.ID, "id_dynamic_raw_id_fk_char_pk")
assert el.get_attribute("value") == str(
CharPrimaryKeyModel.objects.get(chr="abc").pk
)

el = selenium.find_element(By.ID, "id_dynamic_raw_id_fk_uuid_pk")
assert el.get_attribute("value") == str(
UUIDPrimaryKeyModel.objects.get(uuid="9a10987b-51ba-472f-9dfe-175286e2258a").pk
)
9 changes: 2 additions & 7 deletions dynamic_raw_id/tests/test_django_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@
@pytest.mark.usefixtures("_add_page", "_sample_primary_keys")
def test_widgets(selenium: WebDriver) -> None:
"""
Test the variations of the Dynamic Raw ID Widget.
The tests select the "Lookup" icon, choose the first element in the result set,
and test that the selected element name appears next to the field.
Doing this in a all-in-one testcase, rather a parameterized test, so it's much
faster.
Test the classic Django Foreignkey Widget to make sure,
this application does not break the vanilla behavior.
"""

# Input Field ID, Select Value, Test Value
Expand Down
8 changes: 4 additions & 4 deletions dynamic_raw_id/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def media(self) -> forms.Media:

class DynamicRawIDMultiIdWidget(DynamicRawIDWidget):
def value_from_datadict(
self,
data: dict[str, Any],
files: Any | None,
name: str,
self,
data: dict[str, Any],
files: Any | None,
name: str,
) -> str | None:
value = data.get(name)
if value:
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ selenium = "^4.19.0"
[tool.ruff]
target-version = "py38"
exclude = ["migrations"]
select = ["ALL"]
ignore = [
lint.select = ["ALL"]
lint.ignore = [
"ANN101", # Missing Type Annotation for "self"
"ANN401", # Dynamically typed expressions (typing.Any) are disallowed in `**kwargs`
"ARG001", # Unused function argument (request, ...)
Expand All @@ -60,7 +60,7 @@ ignore = [
"ISC001", # (ruff format) Checks for implicitly concatenated strings on a single line
]

[tool.ruff.extend-per-file-ignores]
[tool.ruff.lint.extend-per-file-ignores]
"test_*.py" = [
"ERA001", # Found commented-out code
"PLR2004", # Magic value used in comparison,
Expand Down

0 comments on commit ca57201

Please sign in to comment.