-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] Fixed issues in metric collection (mock requests + empty upgrad…
…es) #360 - Fixed mocking of request library in MockRequestPostRunner - Do not send metrics on post_migrate if no module is upgraded Related to #360 --------- Co-authored-by: Federico Capoano <[email protected]>
- Loading branch information
1 parent
2440f96
commit 4719e07
Showing
3 changed files
with
70 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
from unittest.mock import MagicMock | ||
from unittest.mock import patch | ||
|
||
import requests | ||
from django.test.runner import DiscoverRunner | ||
from openwisp_utils import utils | ||
from openwisp_utils.tests import TimeLoggingTestRunner | ||
|
||
success_response = requests.Response() | ||
success_response.status_code = 204 | ||
|
||
|
||
class MockRequestPostRunner(DiscoverRunner): | ||
class MockRequestPostRunner(TimeLoggingTestRunner): | ||
""" | ||
This runner ensures that usage metrics are | ||
not sent in development when running tests. | ||
""" | ||
|
||
pass | ||
|
||
def setup_databases(self, **kwargs): | ||
utils.requests.post = MagicMock(return_value=success_response) | ||
return super().setup_databases(**kwargs) | ||
utils.requests.Session._original_post = utils.requests.Session.post | ||
with patch.object( | ||
utils.requests.Session, 'post', return_value=success_response | ||
): | ||
return super().setup_databases(**kwargs) | ||
|
||
def run_suite(self, suite, **kwargs): | ||
with patch.object( | ||
utils.requests.Session, 'post', return_value=success_response | ||
): | ||
return super().run_suite(suite) |
This file contains 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