mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #58729 from 11chrisadams11/compat_cmp_dict_fix
Compat cmp dict fix
This commit is contained in:
commit
93f871df58
3 changed files with 49 additions and 1 deletions
1
changelog/58729.fixed
Normal file
1
changelog/58729.fixed
Normal file
|
@ -0,0 +1 @@
|
|||
Fixed salt.utils.compat.cmp to work with dictionaries
|
|
@ -2,7 +2,6 @@
|
|||
Compatibility functions for utils
|
||||
"""
|
||||
|
||||
|
||||
import copy
|
||||
import importlib
|
||||
import sys
|
||||
|
@ -61,6 +60,8 @@ def cmp(x, y):
|
|||
|
||||
Return negative if x<y, zero if x==y, positive if x>y.
|
||||
"""
|
||||
if isinstance(x, dict) and isinstance(y, dict):
|
||||
return 0 if x == y else -1
|
||||
return (x > y) - (x < y)
|
||||
|
||||
|
||||
|
|
46
tests/pytests/unit/utils/test_compat.py
Normal file
46
tests/pytests/unit/utils/test_compat.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
"""
|
||||
Unit tests for salt.utils.compat.py
|
||||
"""
|
||||
import pytest
|
||||
|
||||
import salt.utils.compat
|
||||
|
||||
|
||||
def test_cmp_int_x_equals_y():
|
||||
# int x == int y
|
||||
ret = salt.utils.compat.cmp(1, 1)
|
||||
assert ret == 0
|
||||
|
||||
|
||||
def test_cmp_int_x_less_than_y():
|
||||
# int x < int y
|
||||
ret = salt.utils.compat.cmp(1, 2)
|
||||
assert ret == -1
|
||||
|
||||
|
||||
def test_cmp_int_x_greater_than_y():
|
||||
# int x > int y
|
||||
ret = salt.utils.compat.cmp(2, 1)
|
||||
assert ret == 1
|
||||
|
||||
|
||||
def test_cmp_dict_x_equals_y():
|
||||
# dict x == dict y
|
||||
dict1 = {"foo": "bar", "baz": "qux"}
|
||||
dict2 = {"baz": "qux", "foo": "bar"}
|
||||
ret = salt.utils.compat.cmp(dict1, dict2)
|
||||
assert ret == 0
|
||||
|
||||
|
||||
def test_cmp_dict_x_not_equals_y():
|
||||
# dict x != dict y
|
||||
dict1 = {"foo": "bar", "baz": "qux"}
|
||||
dict2 = {"foobar": "bar", "baz": "qux"}
|
||||
ret = salt.utils.compat.cmp(dict1, dict2)
|
||||
assert ret == -1
|
||||
|
||||
|
||||
def test_cmp_dict_x_not_equals_int_y():
|
||||
# dict x != int y
|
||||
dict1 = {"foo": "bar", "baz": "qux"}
|
||||
pytest.raises(TypeError, salt.utils.compat.cmp, dict1, 1)
|
Loading…
Add table
Reference in a new issue