Skip to content

Commit

Permalink
Merge "Adding dict.items() concatenate validation"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Jan 15, 2015
2 parents 9a483cd + 21dc900 commit 6643102
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/hacking/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ Rally Specific Commandments
* [N335] - Ensure that ``xrange`` is not used
* [N336] - Ensure that ``string.lowercase`` and ``string.uppercase`` are not used
* [N337] - Ensure that ``next()`` method on iterator objects is not used
* [N338] - Ensure that ``+`` operand is not used to concatenate dict.items()
* [N340] - Ensure that we are importing always ``from rally import objects``
18 changes: 18 additions & 0 deletions tests/hacking/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
re_string_lower_upper_case_method = re.compile(
r"(^|[(,\s=])string\.(lower|upper)case([)\[,\s]|$)")
re_next_on_iterator_method = re.compile(r"\.next\(\)")
re_concatenate_dict = re.compile(
r".*\.items\(\)(\s*\+\s*.*\.items\(\))+.*")


def _parse_assert_mock_str(line):
Expand Down Expand Up @@ -345,6 +347,21 @@ def check_next_on_iterator_method(logical_line):
yield (0, "N337: Use next(iterator) rather than iterator.next().")


def check_concatenate_dict_with_plus_operand(logical_line):
"""Check if a dict is being sum with + operator
Python 3 dict.items() return a dict_items object instead of a list, and
this object, doesn't support + operator. Need to use the update method
instead in order to concatenate two dictionaries.
N338
"""
res = re_concatenate_dict.search(logical_line)
if res:
yield (0, "N338: Use update() method instead of '+'' operand to "
"concatenate dictionaries")


def check_no_direct_rally_objects_import(logical_line, filename):
"""Check if rally.objects are properly imported.
Expand Down Expand Up @@ -381,3 +398,4 @@ def factory(register):
register(check_string_lower_upper_case_method)
register(check_next_on_iterator_method)
register(check_no_direct_rally_objects_import)
register(check_concatenate_dict_with_plus_operand)
27 changes: 27 additions & 0 deletions tests/unit/test_hacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ def test_check_iteritems_method(self):
self.assertEqual(len(list(checks.check_iteritems_method(
"dict.items()"))), 0)

def test_check_concatenate_dict_with_plus_operand(self):
self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"dict1.items() + dict2.items()"))), 1)

self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"dict(self.endpoint.to_dict().items() + "
"endpoint.items())"))), 1)

self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"dict(self.endpoint.to_dict().items() + "
"endpoint.items() + something.items())"))), 1)

self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"dict1.item() + dict2.item()"))), 0)

self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"obj.getitems() + obj.getitems()"))), 0)

self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"obj.getitems() + dict2.items()"))), 0)

def test_check_basestring_method(self):
self.assertEqual(len(list(checks.check_basestring_method(
"basestring"))), 1)
Expand Down

0 comments on commit 6643102

Please sign in to comment.