-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
176 lines (128 loc) · 4.3 KB
/
test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import cStringIO
import json
import tempfile
import os
import chopmunk as L
def test_timing():
lst = []
pipe = L.list_sink(lst)
pipe = L.timify(pipe)
pipe.send({})
assert len(lst) == 1, 'nothing added to sink'
assert 'datetime' in lst[0]
def test_tagging():
lst = []
pipe = L.list_sink(lst)
pipe = L.taggify(pipe, tags=['bla', 'blubb'])
pipe.send({})
assert len(lst) == 1, 'nothing added to sink'
tags = lst[0]['tags']
assert tags == ['bla', 'blubb'], 'tags did not get through: %s' % tags
pipe = L.taggify(pipe, tags='hopp')
pipe.send({})
tags = lst[-1]['tags']
assert tags == ['hopp', 'bla', 'blubb'], 'tags did not get through: %s' % tags
def test_jsonify():
lst = []
pipe = L.list_sink(lst)
pipe = L.jsonify(pipe)
info = {'2': [3], '4': 5.}
pipe.send(info)
received_info = json.loads(lst[0])
assert received_info == info, 'jsonify did mutate info: %s' % received_info
def test_broadcast():
lst1 = []
lst2 = []
pipe1 = L.list_sink(lst1)
pipe2 = L.list_sink(lst2)
pipe = L.broadcast(pipe1, pipe2)
info = {'und alle so': 'yeah'}
pipe.send(info)
assert lst1[0] == info, 'list 1 did not receive info'
assert lst2[0] == info, 'list 2 did not receive info'
def test_filelike_sink():
flo = cStringIO.StringIO()
info = {'und alle so': 'yeah'}
pipe = L.filelike_sink(flo, '')
pipe.send(info)
assert flo.getvalue() == str(info)
flo = cStringIO.StringIO()
info = {'und alle so': 'yeah'}
pipe = L.filelike_sink(flo)
pipe.send(info)
assert flo.getvalue() == str(info) + '\n'
def test_file_sink():
info = {'und alle so': 'yeah'}
# 1. Test without newline.
# Create a file on the file system.
tf = tempfile.NamedTemporaryFile('r', delete=False)
tf.close()
# Send info into file.
pipe = L.file_sink(tf.name, suffix='')
pipe.send(info)
del pipe
with open(tf.name) as fp:
content = fp.read()
assert content == str(info)
# Delete file.
os.remove(tf.name)
# 2. Test with newline.
# Create a file on the file system.
tf = tempfile.NamedTemporaryFile('r', delete=False)
tf.close()
# Send info into file.
pipe = L.file_sink(tf.name, suffix='\n')
pipe.send(info)
del pipe
with open(tf.name) as fp:
content = fp.read()
assert content == str(info) + '\n'
# Send to it again.
pipe = L.file_sink(tf.name, append=True, suffix='\n')
pipe.send(info)
del pipe
with open(tf.name) as fp:
content = fp.read()
assert content == str(info) + '\n' + str(info) + '\n', 'content not as expected'
# Delete file.
os.remove(tf.name)
def test_exclude_tags():
lst = []
pipe = L.list_sink(lst)
pipe = L.exclude_tags(pipe, 'fun')
pipe1 = L.taggify(pipe, 'warn')
pipe2 = L.taggify(pipe, 'fun')
pipe1.send({'message': 'take care'})
pipe2.send({'message': 'haahaa'})
assert len(lst) == 1, 'wrong number of messages got through'
assert lst[0]['message'] == 'take care', 'wrong message got through'
def test_keep():
lst = []
pipe = L.list_sink(lst)
pipe = L.keep(pipe, ['message'])
pipe.send({'message': 'take care', 'blabla': 'blubb'})
assert len(lst) == 1, 'wrong number of messages got through'
assert lst[0] == {'message': 'take care'}, 'wrong keys got through'
def test_dontkeep():
lst = []
pipe = L.list_sink(lst)
pipe = L.dontkeep(pipe, ['blabla'])
pipe.send({'message': 'take care', 'blabla': 'blubb'})
assert len(lst) == 1, 'wrong number of messages got through'
assert lst[0] == {'message': 'take care'}, 'wrong keys got through'
def test_uniquify():
lst = []
pipe = L.list_sink(lst)
pipe = L.uniquify(pipe)
pipe.send({'message': 'take care', 'blabla': 'blubb'})
assert len(lst) == 1, 'wrong number of messages got through'
assert 'uuid' in lst[0], 'did not get a uuid'
assert len(lst[0]['uuid']) == 36, 'not a uuid'
def test_addkeyvalue():
lst = []
pipe = L.list_sink(lst)
pipe = L.add_keyvalue(pipe, 'yo', 'no')
pipe.send({'message': 'take care', 'blabla': 'blubb'})
assert len(lst) == 1, 'wrong number of messages got through'
assert 'yo' in lst[0], 'did not get field'
assert lst[0]['yo'] == 'no', 'field has not the right value'