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
Add unit tests for unsafe_proxy #47887
Conversation
Hi @sivel, thank you for submitting this pull-request! |
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.
I recommend changing all is not
assertions to assert what is expected. I've used the new Suggested Changes feature to mark all of those, along with removing an unused import.
I also recommend changing all assertions of AnsibleUnsafe
to the more specific AnsibleUnsafeText
, except for the last one in test_Wrap_var_unsafe
. I didn't call those out individually though.
from ansible.module_utils.six import PY3 | ||
from ansible.utils.unsafe_proxy import AnsibleUnsafe, AnsibleUnsafeText, UnsafeProxy, wrap_var | ||
|
||
import pytest |
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.
import pytest |
assert isinstance(wrap_var('foo'), AnsibleUnsafe) | ||
assert isinstance(wrap_var(u'foo'), AnsibleUnsafe) | ||
if PY3: | ||
assert not isinstance(wrap_var(b'foo'), AnsibleUnsafe) |
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.
assert not isinstance(wrap_var(b'foo'), AnsibleUnsafe) | |
assert isinstance(wrap_var(b'foo'), type(b'')) |
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.
I used the not isinstance
syntax, so that if someone were to implement something like:
class AnsibleUnsafeBytes(binary_type, AnsibleUnsafe):
pass
The tests would have to be knowingly updated, because they would fail.
The change you recommend, would silently pass, due to making an assumption about the data.
The current method would fail, as it causes a change in behavior.
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.
Ah, that makes sense. It would still be good to add the assertions while keeping the existing ones. In line with that, should most of the isinstance
assertions which aren't negated check for AnsibleUnsafeText
instead of just AnsibleUnsafe
?
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.
Feedback should be addressed.
|
||
|
||
def test_wrap_var_dict(): | ||
assert not isinstance(wrap_var(dict(foo='bar')), AnsibleUnsafe) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
||
|
||
def test_wrap_var_dict_None(): | ||
assert not isinstance(wrap_var(dict(foo=None))['foo'], AnsibleUnsafe) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
||
|
||
def test_wrap_var_None(): | ||
assert not isinstance(wrap_var(None), AnsibleUnsafe) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
||
|
||
def test_wrap_var_list(): | ||
assert not isinstance(wrap_var(['foo']), AnsibleUnsafe) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
||
|
||
def test_wrap_var_set(): | ||
assert not isinstance(wrap_var(set(['foo'])), AnsibleUnsafe) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
||
def test_wrap_var_set_None(): | ||
for item in wrap_var(set([None])): | ||
assert not isinstance(item, AnsibleUnsafe) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
||
|
||
def test_wrap_var_tuple(): | ||
assert not isinstance(wrap_var(('foo',)), AnsibleUnsafe) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
||
def test_wrap_var_tuple(): | ||
assert not isinstance(wrap_var(('foo',)), AnsibleUnsafe) | ||
assert not isinstance(wrap_var(('foo',))[0], AnsibleUnsafe) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
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.
Looks good, although I wonder if there should be a test to assert that AnsibleUnsafeText inherits AnsibleUnsafe?
* Add unit tests for unsafe_proxy * Remove unused import * Address comments * Add inheritance test
SUMMARY
Add unit tests for unsafe_proxy
ISSUE TYPE
COMPONENT NAME
test/units/utils/test_unsafe_proxy.py
ANSIBLE VERSION
ADDITIONAL INFORMATION