forked from lxhunter/ansible-filter-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_utils.py
65 lines (45 loc) · 1.38 KB
/
collection_utils.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
import unittest
def where(entries, properties):
ret = []
if type(properties) is str:
for entry in entries:
if properties in entry:
ret.append(entry[properties])
if type(properties) is list:
for entry in entries:
obj = {}
for p in properties:
if p in entry:
obj[p] = entry[p]
ret.append(obj)
return ret
def initial(array):
return array[0:len(array) - 1]
def rest(array):
return array[1:len(array)]
class FilterModule(object):
""" utility filters for operating on hashes """
def filters(self):
return {
'where': where,
'initial': initial,
'rest': rest
}
class TestCollectionUtils(unittest.TestCase):
def test_where(self):
l = [
{'foo': 'bar', 'bar': 'quuux', 'baz': 'qux'},
{'foo': 'baz', 'bar': 'quuuux', 'baz': 'quux'}
]
self.assertEqual(where(l, 'foo'), ['bar', 'baz'])
self.assertEqual(where(l, ['foo', 'baz']), [
{'foo': 'bar', 'baz': 'qux'},
{'foo': 'baz', 'baz': 'quux'}
])
def test_initial(self):
self.assertEqual(initial([1, 2, 3]), [1, 2])
def test_rest(self):
self.assertEqual(rest([1, 2, 3]), [2, 3])
if __name__ == '__main__':
unittest.main()