-
Notifications
You must be signed in to change notification settings - Fork 23.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Meraki - Initial unit tests #55251
Meraki - Initial unit tests #55251
Conversation
The test
|
- Uses pytest's fixtures structure, not classes - Added a test file for importing
|
||
|
||
def test_fetch_url_404(module, mocker): | ||
mocker.patch('ansible.module_utils.urls.fetch_url') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The patch needs to be applied where name is used, not where it is defined:
mocker.patch('ansible.module_utils.urls.fetch_url') | |
mocker.patch('ansible.module_utils.network.meraki.meraki.fetch_url') |
See https://docs.python.org/3/library/unittest.mock.html#where-to-patch for details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To apply the side effect, use this:
mocker.patch('ansible.module_utils.urls.fetch_url') | |
mocker.patch('ansible.module_utils.network.meraki.meraki.fetch_url', side_effect=mocked_fetch_url) |
You'll also need to update mocked_fetch_url
to accept args, like:
def mocked_fetch_url(*args, **kwargs):
|
||
|
||
def mocked_fetch_url(): | ||
response_mock = Mock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this since it doesn't do anything and Mock
isn't defined.
info = {'status': 404, | ||
'body': "404 - Page is missing", | ||
} | ||
return (None, info) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to define info
for both code paths.
|
||
def mocked_fetch_url(): | ||
response_mock = Mock() | ||
if args[0] == 'https://api.meraki.com/404': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't match because your test uses http://
instead of https://
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and args[0]
is the module, so you want to look at args[1]
for the url.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, the url I'm seeing from the test is: https://api.meraki.com/api/v0/http://api.meraki.com/404
e037105
to
39d95c5
Compare
The test
The test
The test
The test
|
1 similar comment
The test
The test
The test
The test
|
The test
|
1 similar comment
The test
|
SUMMARY
This PR adds a few unit tests for Meraki module utility. It is bare bones but will be used on current and future PRs.
ISSUE TYPE
COMPONENT NAME
lib/ansible/module_utils/network/meraki/meraki.py
ADDITIONAL INFORMATION