-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_test.py
More file actions
34 lines (31 loc) · 1.34 KB
/
util_test.py
File metadata and controls
34 lines (31 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from content_api.util import get
# See: https://stackoverflow.com/questions/4014621/a-python-class-that-acts-like-dict
class DictLike:
def __init__(self, **dict):
self.__dict__ = dict
def __getitem__(self, key):
return self.__dict__[key]
def __contains__(self, item):
return item in self.__dict__
class NotDictLike:
def __init__(self, **dict):
self.__dict__ = dict
def __getitem__(self, key):
return self.__dict__[key]
def test_get():
assert get(None, ['foo']) == None
assert get({'foo': 1}, None) == None
assert get(None, None) == None
assert get({'foo': 1}, []) == {'foo': 1}
assert get({'foo': 1}, ['foo']) == 1
assert get({'foo': 1}, ['bar']) == None
assert get({'foo': 1}, ['bar'], 'the default') == 'the default'
assert get({'foo': {'bar': 'hello'}}, ['foo', 'bar']) == 'hello'
assert get({'foo': {'bar': 'hello'}}, 'foo.bar') == 'hello'
assert get(DictLike(**{'foo': {'bar': 'hello'}}), 'foo.bar') == 'hello'
assert get(NotDictLike(**{'foo': {'bar': 'hello'}}), 'foo.bar') == None
assert get({'foo': [{'bar': 'hello'}]}, 'foo.0.bar') == 'hello'
assert get({'foo': [{'bar': 'hello'}]}, 'foo.1') == None
assert get({'foo': [{'bar': 'hello'}]}, 'foo.1.bar') == None
assert get(['foo', 'bar'], '1') == 'bar'
assert get(['foo', 'bar'], '2') == None