Refactor Duplicate Validation and Decouple Database Access
main.py has three handler functions: register_user, update_profile, and create_post. They all work — but the code has two problems.
First, the same username validation block is copy-pasted into all three handlers. If the validation rules ever change, you have to update three places and hope you don't miss one.
Second, every handler imports and instantiates Database directly. That makes them impossible to unit test without hitting the real database, and it tightly couples business logic to infrastructure.
Refactor both problems. Extract the shared validation into a validators.py module. Decouple the database dependency so handlers receive their save mechanism from the outside instead of reaching for it themselves.
register_user,update_profile, andcreate_postmust return identical responses to the originals for all valid and invalid inputs.- The username validation logic must not be duplicated — it must live in exactly one place.
- A
validators.pyfile must exist and contain the extracted validation logic. main.pymust not importDatabasefromdb.py.- Each handler must accept its save mechanism as an external dependency so tests can pass a mock without touching the real database.
register_user({'username': 'alice', 'email': 'a@b.com'}, save=mock_save){'status': 'ok', 'data': {'username': 'alice', 'email': 'a@b.com'}}register_user({'username': 'x'}, save=mock_save){'error': 'username must be between 3 and 20 characters'}- Do not change the response shape or error messages.
- Do not modify
db.py.
If you needed to support multiple database backends (Postgres, SQLite, in-memory), what would you change about the interface you designed?
Keep moving through related backend basics problems and build a stronger search-friendly practice loop around this topic.
View track →