Skip to content

refactor: remove dead code and cover untested branches - #144

Open
CuriousLearner wants to merge 1 commit into
masterfrom
refactor/dead-code-and-test-gaps
Open

refactor: remove dead code and cover untested branches#144
CuriousLearner wants to merge 1 commit into
masterfrom
refactor/dead-code-and-test-gaps

Conversation

@CuriousLearner

Copy link
Copy Markdown
Owner

Summary

Accept-Language parsing took the first language in the header and ignored quality values, so de;q=0.5,en;q=0.9 selected German even though the client ranked English higher. It now ranks entries by their q weight per RFC 7231, treats q=0 as "not acceptable", ignores malformed entries instead of raising, and keeps header order for ties. An absent or unusable header still yields no language, preserving the documented behavior that the message is sent unlocalized when no language is set.

The ugettext_lazy import shim in models.py and serializers.py was both dead and backwards. gettext_lazy has existed since Django 2.0, below the project's floor of 2.1.5, so the fallback never ran; meanwhile on versions where ugettext_lazy still existed the shim selected the deprecated alias, which Django removed in 4.0. Both are replaced with a direct import.

Also removes an unused logger in serializers.py and reduces an always-true hasattr/callable guard in services.py to a single getattr, keeping a minimal guard so duck-typed backends that do not inherit from BaseBackend still work.

conftest.py built its backend parametrization from set literals, so collection order changed between runs under Python's per-process string hash randomization, making -x, --lf, and xdist ordering irreproducible. They are now lists; every consumer only does membership tests.

Test plan

  • phone_verify/admin.py goes from 0% to 100% coverage, the largest gap in the project. Exercising it required adding django.contrib.admin to the test settings, since the @admin.register decorator resolves the default admin site at import time.
  • constants.py and the cleanup command each had one uncovered branch, both now covered. Total coverage 96% to 99%.
  • The quality-value change is covered by a parametrized test over 13 headers. Verified it fails against the previous first-token-wins implementation.
  • Parametrization determinism confirmed by collecting under different PYTHONHASHSEED values.
  • Full suite: 168 passed. Ruff clean.

Replace the `ugettext_lazy`/`gettext_lazy` try/except shims in `models.py` and
`serializers.py` with a direct `gettext_lazy` import. `gettext_lazy` predates the
project's Django floor, so the fallback was unreachable and the shim preferred the
deprecated alias wherever it still existed. Drop the unused `logging` import and
module-level `logger` from `serializers.py`, and reduce the always-true
`hasattr`/`callable` guard in `PhoneVerificationService._generate_message` to a single
`getattr` lookup that still supports duck-typed backends.

Parse `Accept-Language` by quality value in `VerificationViewSet.register` so the
highest priority language wins instead of the first one listed, per RFC 7231. Entries
with equal or absent `q` keep their header order, malformed entries are skipped, and a
header with no usable language still yields `None` so the message is sent unlocalized.

Build the parametrized backend lists in `conftest.py` as lists rather than sets, so
test ordering no longer depends on per-process string hashing.

Add coverage for `phone_verify/admin.py`, the expiration default in
`get_security_code_expiration`, and the truncated dry-run preview in the
`cleanup_phone_verifications` command. Testing the admin requires
`django.contrib.admin` in the test settings, since the module cannot be imported
without it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant