test: add pytest suite for utils.py and fix AttributeError on missing Content-Type header (T415715)#93
Open
devjadiya wants to merge 1 commit into
Open
Conversation
… Content-Type header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes T415715 (test infrastructure)
What this PR does
Adds the first pytest test suite to the project. Zero automated tests currently exist in the codebase. This PR adds 18 tests covering all three core functions in utils.py, with all HTTP calls mocked via unittest.mock so no live network or database is needed.
Bug found and fixed by the tests
test_content_type_missing_returns_safely failed on the first run, catching a real production crash:
In download_image(), the line:
crashes whenever the Wikimedia server returns a response without a Content-Type header, or returns one with a charset suffix like image/jpeg; charset=utf-8.
Fix applied in utils.py:
This also handles the charset suffix case which the original code would have turned into a filename like 2026-01-01.jpeg; charset=utf-8.
Tests added (18 total, all pass)
getHeader()
download_image()
process_upload()
get_localized_wikitext()
Test run output
18 passed in 0.41s
How to run
pip install pytest mwparserfromhell requests
pytest test_utils.py -v