Skip to content

Commit f3057aa

Browse files
author
Taejun Lee
authored
Merge pull request #186 from box/add_contains_method_to_base_api_json_object
Add ability to perform contains checks on BaseAPIJsonObject
2 parents baf536c + 9741601 commit f3057aa

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

boxsdk/object/api_json_object.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,3 @@ class APIJSONObject(with_metaclass(APIJSONObjectMeta, BaseAPIJSONObject, Mapping
2222

2323
def __len__(self):
2424
return len(self._response_object)
25-
26-
def __iter__(self):
27-
return iter(self._response_object)

boxsdk/object/base_api_json_object.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ def __getitem__(self, item):
9595
"""
9696
return self._response_object[item]
9797

98+
def __contains__(self, item):
99+
"""
100+
Does the response object contains this item attribute?
101+
102+
:param item:
103+
The attribute to check for in the API response object.
104+
:type item:
105+
`unicode`
106+
"""
107+
return item in self._response_object
108+
109+
def __iter__(self):
110+
"""
111+
Get all of the keys of the API response object.
112+
"""
113+
return iter(self._response_object)
114+
98115
def __repr__(self):
99116
"""Base class override. Return a human-readable representation using the Box ID or name of the object."""
100117
extra_description = ' - {0}'.format(self._description) if self._description else ''

test/unit/object/test_base_api_json_object.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ def test_getitem(base_api_json_object):
2626
assert test_object[key] == dictionary_response[key]
2727

2828

29+
def test_contains(base_api_json_object):
30+
dictionary_response, test_object = base_api_json_object
31+
for key in dictionary_response:
32+
assert key in test_object
33+
assert 'some_key_that_doesnt_exist' not in test_object
34+
35+
36+
def test_iter(base_api_json_object):
37+
dictionary_response, test_object = base_api_json_object
38+
all_test_object_keys = [key for key in test_object]
39+
all_dictionary_response_keys = [key for key in dictionary_response]
40+
assert set(all_test_object_keys) == set(all_dictionary_response_keys)
41+
42+
2943
def test_meta_registers_new_item_type_in_default_translator(default_translator, original_default_translator):
3044
item_type = u'ƒøø'
3145

0 commit comments

Comments
 (0)