Fix test_check_up_debug_fail - #157
Conversation
WalkthroughA test in the Changes
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
tests/test_mwdeploy.py(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: RhinosF1
PR: miraheze/python-functions#4
File: tests/test_iaupload.py:50-59
Timestamp: 2024-07-26T21:00:05.787Z
Learning: When testing the `upload` method of the `ArchiveUploader` class, mock the `internetarchive.get_item` and other network-related calls to make the tests more reliable and faster.
Learnt from: RhinosF1
PR: miraheze/python-functions#4
File: tests/test_iaupload.py:50-59
Timestamp: 2024-10-08T17:05:15.088Z
Learning: When testing the `upload` method of the `ArchiveUploader` class, mock the `internetarchive.get_item` and other network-related calls to make the tests more reliable and faster.
Learnt from: BlankEclair
PR: miraheze/python-functions#119
File: miraheze/mediawiki/mwimport.py:59-66
Timestamp: 2025-03-07T09:54:27.055Z
Learning: The MediaWiki import tools in miraheze/python-functions are specifically designed to run only on mw* servers, where system dependencies like `/usr/local/bin/getMWVersion` and `/usr/local/bin/logsalmsg` are available.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build (3.14-dev)
| def test_check_up_debug_fail() -> None: | ||
| assert not mwdeploy.check_up(nolog=True, Debug='mwtask181', domain='httpstat.us/500', force=True) | ||
| assert not mwdeploy.check_up(nolog=True, Debug='mwtask181', domain='meta.miraheze.org/cache', force=True) |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Consider mocking network calls instead of relying on external services.
Changing from httpstat.us/500 to meta.miraheze.org/cache introduces network dependency that can make the test unreliable. The real endpoint may return successful responses, potentially causing the test to fail unexpectedly since it expects check_up to return False.
For more reliable testing, consider mocking the network call to ensure consistent behaviour regardless of external service availability.
-def test_check_up_debug_fail() -> None:
- assert not mwdeploy.check_up(nolog=True, Debug='mwtask181', domain='meta.miraheze.org/cache', force=True)
+@patch('miraheze.mediawiki.mwdeploy.requests.get')
+def test_check_up_debug_fail(mock_get) -> None:
+ mock_get.return_value.status_code = 500
+ assert not mwdeploy.check_up(nolog=True, Debug='mwtask181', domain='meta.miraheze.org/cache', force=True)🏁 Script executed:
#!/bin/bash
# Locate the definition of check_up and its HTTP call
rg -n "def check_up" -A5
# Locate where requests is imported in mwdeploy
rg -n "import requests" -A2Length of output: 776
🏁 Script executed:
#!/bin/bash
# Show the full body of check_up to confirm how HTTP requests are made
sed -n '178,260p' miraheze/mediawiki/mwdeploy.pyLength of output: 3449
Mock the HTTP request in test_check_up_debug_fail to remove external dependencies
Relying on a live endpoint makes this test fragile. Instead, patch requests.get so you can control the response:
• Import the patch decorator:
from unittest.mock import patch• In the test, set up the fake response to return a 500 status and empty body/headers so check_up(..., force=True) cleanly returns False without hitting the network.
Suggested diff:
-from tests/test_mwdeploy.py
+from unittest.mock import patch
+
@patch('miraheze.mediawiki.mwdeploy.requests.get')
def test_check_up_debug_fail(mock_get) -> None:
- # direct network call to meta.miraheze.org/cache is flaky
- assert not mwdeploy.check_up(nolog=True, Debug='mwtask181',
- domain='meta.miraheze.org/cache', force=True)
+ # simulate an HTTP 500 from the server
+ mock_resp = mock_get.return_value
+ mock_resp.status_code = 500
+ mock_resp.text = ''
+ mock_resp.headers = {}
+
+ assert not mwdeploy.check_up(
+ nolog=True,
+ Debug='mwtask181',
+ domain='meta.miraheze.org/cache',
+ force=True
+ )This ensures the test is deterministic and fast, without relying on external services.
🤖 Prompt for AI Agents
In tests/test_mwdeploy.py at lines 157-158, the test_check_up_debug_fail
function currently makes a real HTTP request, causing fragility. To fix this,
import patch from unittest.mock and decorate the test function to mock
requests.get. Configure the mock to return a response with status code 500 and
empty content and headers, ensuring check_up returns False without making an
actual network call. This will make the test deterministic and remove external
dependencies.
Summary by CodeRabbit