Stabilize phone call router tests#7752
Conversation
Greptile SummaryThis PR stabilizes
Confidence Score: 4/5Safe to merge — this is a test-only change that does not touch production code. The change is confined to a test file. The autouse fixture always grants paid-tier access (is_paid=True), which means phone_call_usage_db.increment_current_month is never reached and never mocked. A follow-up test for the free-tier billing path would hit an unmocked database call and fail. No production logic is affected. Only backend/tests/unit/test_phone_calls.py changed. The is_paid=True default in _make_quota_snapshot() and the absence of a phone_call_usage_db mock are worth a second look if free-tier call-usage tests are added later. Important Files Changed
Sequence DiagramsequenceDiagram
participant TF as Test File (module load)
participant Stub as twilio_stub.install_twilio_stub()
participant SysM as sys.modules
participant Router as routers.phone_calls
participant Fixture as isolate_phone_call_quota (autouse)
participant Test as Test Function
TF->>SysM: setdefault ENCRYPTION_SECRET + Twilio env vars
TF->>SysM: setdefault database._client, firebase_admin (MagicMock)
TF->>Stub: install_twilio_stub()
Stub->>SysM: check 'twilio' in sys.modules / find_spec
alt Twilio not available
Stub->>SysM: "register fake twilio.*, twilio.base.exceptions, etc."
else Twilio SDK found
Stub-->>TF: return (no-op)
end
TF->>Router: import routers.phone_calls (binds stub or real Twilio names)
Note over TF,Router: All three quota helpers bound at module level
Test->>Fixture: pytest autouse setup
Fixture->>Router: monkeypatch check_call_access → MagicMock
Fixture->>Router: "monkeypatch get_quota_snapshot → MagicMock (is_paid=True)"
Fixture->>Router: monkeypatch check_destination_allowed → MagicMock(None)
Fixture-->>Test: ready
Test->>Router: HTTP request via TestClient
Router->>Router: check_call_access(uid) → mock
Router->>Router: "get_quota_snapshot(uid) → mock snapshot (is_paid=True)"
Router->>Router: check_destination_allowed(...) → None (mock)
Note over Router: is_paid=True → skip phone_call_usage_db.increment_current_month
Router-->>Test: response
Reviews (1): Last reviewed commit: "Stabilize phone call router tests" | Re-trigger Greptile |
| def _make_quota_snapshot(): | ||
| snapshot = MagicMock() | ||
| snapshot.has_access = True | ||
| snapshot.is_paid = True | ||
| snapshot.max_duration_seconds = None | ||
| snapshot.allowed_countries = [] | ||
| return snapshot |
There was a problem hiding this comment.
phone_call_usage_db unguarded when is_paid is flipped
The autouse fixture always sets snapshot.is_paid = True. In twiml_voice_webhook the only guard keeping phone_call_usage_db.increment_current_month from running is if not snapshot.is_paid:. Because the fixture hard-codes True, the usage DB is never exercised and is never mocked. Any future test that sets is_paid = False (to cover the free-tier billing path) will hit the real phone_call_usage_db call and fail with an uninitialized database connection, since database.phone_call_usage is not in the sys.modules stub table.
There was a problem hiding this comment.
Fixed in aa699f6.
I added a router-level phone_call_usage_db mock in the autouse fixture and made the fixture return the quota snapshot plus usage DB mock. There is now a free-tier TwiML regression test that flips snapshot.is_paid = False and asserts increment_current_month(TEST_UID) is called without touching the real database.
Validation:
python -m pytest tests\unit\test_phone_calls.py -q-> 18 passed, 2 warningspython -m pytest tests\unit\test_phone_calls.py tests\unit\test_twilio_service.py tests\unit\test_twilio_account_deletion.py -q-> 27 passed, 2 warningspython -m py_compile tests\unit\test_phone_calls.pygit diff --check origin/main...HEAD
77915b7 to
aa699f6
Compare
kodjima33
left a comment
There was a problem hiding this comment.
Backend test stabilization (Windows/UTF-8/optional-dep isolation) — approve only per policy.
|
Closing this in favor of #7798. #7798 covers the same |
|
Hey @tianmind-studio 👋 Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request. After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:
Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out. Thank you for being part of the Omi community! 💜 |
Summary
test_phone_calls.pydoes not require the Twilio SDK locallyutils.twilio_serviceis imported, avoiding cross-test import-order pollutionphone_call_usage_dbin the router tests and cover the free-tier TwiML usage-counting path without touching the real databaseTesting
python -m pytest tests\unit\test_phone_calls.py -q-> 18 passed, 2 warningspython -m pytest tests\unit\test_phone_calls.py tests\unit\test_twilio_service.py tests\unit\test_twilio_account_deletion.py -q-> 27 passed, 2 warningspython -m black --line-length 120 --skip-string-normalization tests\unit\test_phone_calls.pypython -m py_compile tests\unit\test_phone_calls.pygit diff --check origin/main...HEAD