Skip to content
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

Case insensitive string comparator #220

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions advanced_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ Optionally, validators can return a Failure which evaluates to False, but suppli
| 'le', 'less_than_or_equal' | Less Than Or Equal To | A <= B |
| 'eq', 'equals' | Equals | A == B |
| 'str_eq' | Values are Equal When Converted to String | str(A) == str(B) -- useful for comparing templated numbers/collections |
| 'str_eq_insensitive' | Case Insensitive version of str_eq | str(A).lower() == str(B).lower() |
| 'ne', 'not_equals' | Not Equals | A != B |
| 'ge', 'greater_than_or_equal' | Greater Than Or Equal To | A >= B |
| 'gt', 'greater_than' | Greater Than | A > B |
Expand Down
31 changes: 31 additions & 0 deletions pyresttest/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,5 +577,36 @@ def test_parse_validator_jsonpath_mini_extracttest(self):
self.assertEqual(validation_result.message,
"Extract and test validator failed on test: exists(None)")

def test_validator_string_comparison(self):
""" Tests string comparison """
config = {
'jsonpath_mini': 'key.val',
'comparator': 'str_eq',
'expected': "data"
}
comp_validator = validators.ComparatorValidator.parse(config)
myjson_pass = '{"id": 3, "key": {"val": "data"}}'
myjson_fail = '{"id": 3, "key": {"val": "DATA"}}'

self.assertTrue(comp_validator.validate(body=myjson_pass))
self.assertFalse(comp_validator.validate(body=myjson_fail))

def test_validator_string_insensitive_comparison(self):
""" Tests case-insensitive string comparison """
config = {
'jsonpath_mini': 'key.val',
'comparator': 'str_eq_insensitive',
'expected': "data"
}
comp_validator = validators.ComparatorValidator.parse(config)
myjson_lowercase = '{"id": 3, "key": {"val": "data"}}'
myjson_uppercase = '{"id": 3, "key": {"val": "DATA"}}'
myjson_mixcase = '{"id": 3, "key": {"val": "DaTa"}}'

self.assertTrue(comp_validator.validate(body=myjson_lowercase))
self.assertTrue(comp_validator.validate(body=myjson_uppercase))
self.assertTrue(comp_validator.validate(body=myjson_mixcase))


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions pyresttest/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
'eq': operator.eq,
'equals': operator.eq,
'str_eq': lambda x, y: operator.eq(str(x), str(y)),
'str_eq_insensitive': lambda x, y: operator.eq(str(x).lower(), str(y).lower()),
'ne': operator.ne,
'not_equals': operator.ne,
'ge': operator.ge,
Expand Down