Merge branch 'master' into mail

This commit is contained in:
debugtalk
2017-08-03 23:58:39 +08:00
3 changed files with 62 additions and 33 deletions

View File

@@ -159,22 +159,6 @@ def match_expected(value, expected, comparator="eq", check_item=""):
if comparator in ["eq", "equals", "=="]:
assert value == expected
elif comparator in ["str_eq", "string_equals"]:
assert str(value) == str(expected)
elif comparator in ["ne", "not_equals"]:
assert value != expected
elif comparator in ["len_eq", "length_equal", "count_eq"]:
assert len(value) == expected
elif comparator in ["len_gt", "count_gt", "length_greater_than", "count_greater_than"]:
assert len(value) > expected
elif comparator in ["len_ge", "count_ge", "length_greater_than_or_equals", \
"count_greater_than_or_equals"]:
assert len(value) >= expected
elif comparator in ["len_lt", "count_lt", "length_less_than", "count_less_than"]:
assert len(value) < expected
elif comparator in ["len_le", "count_le", "length_less_than_or_equals", \
"count_less_than_or_equals"]:
assert len(value) <= expected
elif comparator in ["lt", "less_than"]:
assert value < expected
elif comparator in ["le", "less_than_or_equals"]:
@@ -183,16 +167,43 @@ def match_expected(value, expected, comparator="eq", check_item=""):
assert value > expected
elif comparator in ["ge", "greater_than_or_equals"]:
assert value >= expected
elif comparator in ["ne", "not_equals"]:
assert value != expected
elif comparator in ["str_eq", "string_equals"]:
assert str(value) == str(expected)
elif comparator in ["len_eq", "length_equals", "count_eq"]:
assert isinstance(expected, int)
assert len(value) == expected
elif comparator in ["len_gt", "count_gt", "length_greater_than", "count_greater_than"]:
assert isinstance(expected, int)
assert len(value) > expected
elif comparator in ["len_ge", "count_ge", "length_greater_than_or_equals", \
"count_greater_than_or_equals"]:
assert isinstance(expected, int)
assert len(value) >= expected
elif comparator in ["len_lt", "count_lt", "length_less_than", "count_less_than"]:
assert isinstance(expected, int)
assert len(value) < expected
elif comparator in ["len_le", "count_le", "length_less_than_or_equals", \
"count_less_than_or_equals"]:
assert isinstance(expected, int)
assert len(value) <= expected
elif comparator in ["contains"]:
assert isinstance(value, (list,tuple,dict,string_type))
assert expected in value
elif comparator in ["contained_by"]:
assert isinstance(expected, (list,tuple,dict,string_type))
assert value in expected
elif comparator in ["type"]:
assert isinstance(value, expected)
elif comparator in ["regex"]:
assert isinstance(expected, string_type)
assert isinstance(value, string_type)
assert re.match(expected, value)
elif comparator in ["str_len", "string_length"]:
assert len(value) == int(expected)
elif comparator in ["startswith"]:
assert str(value).startswith(str(expected))
elif comparator in ["endswith"]:
assert str(expected).startswith(str(value))
else:
raise exception.ParamsError("comparator not supported!")

22
docs/comparator.md Normal file
View File

@@ -0,0 +1,22 @@
# Comparator
| comparator | Description | A(check), B(expected) | examples |
|:------------:|-----------|------------------------------------|-----------|
| `eq`, `==` | value is equal | A == B | 9 eq 9 |
| `lt` | less than | A < B | 7 lt 8 |
| `le` | less than or equals | A <= B | 7 le 8, 8 le 8 |
| `gt` | greater than | A > B | 8 gt 7 |
| `ge` | greater than or equals | A >= B | 8 ge 7, 8 ge 8 |
| `ne` | not equals | A != B | 6 ne 9 |
| `str_eq` | string equals | str(A) == str(B) | 123 str_eq '123' |
| `len_eq`, `count_eq` | length or count equals | len(A) == B | 'abc' len_eq 3<br/>[1,2] len_eq 2 |
| `len_gt`, `count_gt` | length greater than | len(A) > B | 'abc' len_gt 2<br/>[1,2,3] len_gt 2 |
| `len_ge`, `count_ge` | length greater than or equals | len(A) >= B | 'abc' len_ge 3<br/>[1,2,3] len_gt 3 |
| `len_lt`, `count_lt` | length less than | len(A) < B | 'abc' len_lt 4<br/>[1,2,3] len_lt 4 |
| `len_le`, `count_le` | length less than or equals | len(A) <= B | 'abc' len_le 3<br/>[1,2,3] len_le 3 |
| `contains` | contains | B in A | [1, 2] contains 1<br/>'abc' contains 'a' |
| `contained_by` | contained by | A in B | 1 contained_by [1,2]<br/>'a' contained_by 'abc' |
| `type` | type of A is instance of B | isinstance(A, B) | 123 type 'int' |
| `regex` | regex matches | re.match(B, A) | 'abcdef' regex 'a\w+d' |
| `startswith` | starts with | A.startswith(B) is True | 'abc' startswith 'ab' |
| `endswith` | ends with | A.endswith(B) is True | 'abc' endswith 'bc' |

View File

@@ -156,42 +156,38 @@ class TestUtils(ApiServerUnittest):
def test_match_expected(self):
self.assertTrue(utils.match_expected(1, 1, "eq"))
self.assertTrue(utils.match_expected("abc", "abc", "eq"))
self.assertTrue(utils.match_expected("abc", "abc", "=="))
self.assertTrue(utils.match_expected("abc", "abc"))
with self.assertRaises(exception.ValidationError):
utils.match_expected(123, "123", "eq")
with self.assertRaises(exception.ValidationError):
utils.match_expected(123, "123")
self.assertTrue(utils.match_expected("123", 3, "len_eq"))
self.assertTrue(utils.match_expected(123, "123", "str_eq"))
self.assertTrue(utils.match_expected(123, "123", "ne"))
self.assertTrue(utils.match_expected("123", 2, "len_gt"))
self.assertTrue(utils.match_expected("123", 3, "len_ge"))
self.assertTrue(utils.match_expected("123", 4, "len_lt"))
self.assertTrue(utils.match_expected("123", 3, "len_le"))
self.assertTrue(utils.match_expected(1, 2, "lt"))
self.assertTrue(utils.match_expected(1, 1, "le"))
self.assertTrue(utils.match_expected(2, 1, "gt"))
self.assertTrue(utils.match_expected(1, 1, "ge"))
self.assertTrue(utils.match_expected(123, "123", "ne"))
self.assertTrue(utils.match_expected("123", 3, "len_eq"))
self.assertTrue(utils.match_expected("123", 2, "len_gt"))
self.assertTrue(utils.match_expected("123", 3, "len_ge"))
self.assertTrue(utils.match_expected("123", 4, "len_lt"))
self.assertTrue(utils.match_expected("123", 3, "len_le"))
self.assertTrue(utils.match_expected("123abc456", "3ab", "contains"))
self.assertTrue(utils.match_expected(['1', '2'], "1", "contains"))
self.assertTrue(utils.match_expected({'a':1, 'b':2}, "a", "contains"))
self.assertTrue(utils.match_expected("3ab", "123abc456", "contained_by"))
self.assertTrue(utils.match_expected("123abc456", "^123.*456$", "regex"))
self.assertTrue(utils.match_expected("123abc456", "^123\w+456$", "regex"))
with self.assertRaises(exception.ValidationError):
utils.match_expected("123abc456", "^12b.*456$", "regex")
with self.assertRaises(exception.ParamsError):
utils.match_expected(1, 2, "not_supported_comparator")
self.assertTrue(utils.match_expected("2017-06-29 17:29:58", 19, "str_len"))
self.assertTrue(utils.match_expected("2017-06-29 17:29:58", "19", "str_len"))
self.assertTrue(utils.match_expected("abc123", "ab", "startswith"))
self.assertTrue(utils.match_expected("123abc", 12, "startswith"))
self.assertTrue(utils.match_expected(12345, 123, "startswith"))