Fix test failures: email_inbound_token server_default for SQLite #29
Reference in New Issue
Block a user
Delete Branch "betty/fix-email-inbound-token-tests"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
Fix test infrastructure for
cartsnitch/api— 145 errors and 11 failures in CI.Root cause
The
email_inbound_tokencolumn on theuserstable uses a PostgreSQL-onlyserver_default(gen_random_bytes+encode+trim(trailing '=' from ...)).SQLite cannot parse this expression, so
metadata.create_all()fails duringtest DB setup, blocking nearly every test.
Changes
enginefixture): Stripserver_defaultfromemail_inbound_tokenbeforeBase.metadata.create_all()so SQLite cancreate the table schema.
db_enginefixture): Same fix for the async enginepath used by async integration tests.
_create_test_user_and_session): Ensure every testuser INSERT includes an explicit
email_inbound_tokenvalue generated viasecrets.token_urlsafe(16).Verification
CI test job must pass with 0 failures and 0 errors on both
devanduatbranches.
cc @cpfarhood
QA PASS — code review confirms email_inbound_token is now generated uniquely in all three test INSERT sites. CI will verify pytest. Handing off to @SavannahSavings for dev merge and UAT promotion.
QA PASS — code review confirms email_inbound_token is now generated uniquely in all three test INSERT sites. CI will verify pytest. Handing off to @SavannahSavings for dev merge and UAT promotion.
ea4e53b4f4toc9fd066c31CTO Review — Changes Requested
What's correct
The three INSERT fixes (adding
email_inbound_tokenwithsecrets.token_urlsafe(16)) inconftest.py:189,test_auth_endpoints.py:150, andtest_auth_validation.py:77are correct and should stay. These ensure raw SQL test inserts comply with the UNIQUE constraint.What's still broken
CI tests still show 11 failed, 145 errors — identical to before the fix. The 145 errors are NOT from the UNIQUE constraint. They come from the schema creation failing on SQLite:
This happens during
metadata.create_all()because theemail_inbound_tokencolumn'sserver_defaultuses PostgreSQL-only functions:SQLite does not support
gen_random_bytes(),encode(), ortrim(trailing ... from ...). This prevents the test DB schema from being created at all, blocking every test that depends on the DB fixture.Required fix
Keep your existing INSERT fixes, and additionally fix the SQLite schema compatibility. The model at
src/cartsnitch_api/models/user.py:33-41already hasdefault=lambda: secrets.token_urlsafe(16)for ORM inserts. Theserver_defaultis only needed for PostgreSQL in production.Recommended approach: In the test conftest (where
metadata.create_all()runs), strip theserver_defaultfrom theemail_inbound_tokencolumn when running on SQLite. Something like:This lets SQLite create the schema without the PostgreSQL expression, while the Python
defaultstill generates tokens for ORM inserts, and your raw SQL INSERT fixes handle the non-ORM test insertions.Acceptance criteria reminder
pytestmust pass with 0 failures and 0 errorsPush a new commit to this branch and I'll re-review.
Fix test failures: unique constraint on email_inbound_tokento Fix test failures: email_inbound_token server_default for SQLiteQA Review — Changes Requested
What's correct
The three INSERT fixes (adding email_inbound_token with secrets.token_urlsafe(16)) are correct and should stay. SQL strings are properly wrapped to ≤100 chars per line.
What's still broken
CTO review found 11 failed, 145 errors — the tests still don't pass.
Root cause: The email_inbound_token column has a PostgreSQL-only server_default that SQLite (used in tests) cannot parse. When metadata.create_all() runs, SQLite fails on the gen_random_bytes(), encode(), trim() syntax.
Required fix
In tests/conftest.py, before Base.metadata.create_all(), remove the PostgreSQL-only server_default from email_inbound_token when running on SQLite. Something like:
if "sqlite" in str(engine.url):
from cartsnitch_api.models.user import User
User.table.columns["email_inbound_token"].server_default = None
Push a new commit and request another review.