Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 369 lines (320 sloc) 15.482 kB
1b2e0f3 @3ft9 Initial implementation.
3ft9 authored
1 import unittest, sys, os, json
2 from datetime import datetime
3 import testdata
4 sys.path[0:0] = [os.path.join(os.path.dirname(__file__), ".."),]
5 import datasift
6
7 class TestDefinition(unittest.TestCase):
8
9 user = None
10 mock_api_client = None
11
12 def setUp(self):
13 self.user = datasift.User(testdata.username, testdata.api_key)
14 self.mock_api_client = datasift.mockapiclient.MockApiClient()
15 self.user.set_api_client(self.mock_api_client)
16
17 def test_construction(self):
18 definition = datasift.Definition(self.user)
19 self.assertEqual(definition.get(), '', 'Default definition CSDL is not empty')
20
21 def test_construction_with_definition(self):
22 definition = datasift.Definition(self.user, testdata.definition)
23 self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
24
25 def test_construction_invalid_user(self):
26 try:
27 definition = datasift.Definition(testdata.username)
28 self.fail('Expected InvalidDataError exception not thrown')
29 except datasift.InvalidDataError:
30 # Expected exception
31 pass
32
33 def test_construction_invalid_definition(self):
34 try:
35 definition = datasift.Definition(self.user, 1234)
36 self.fail('Expected InvalidDataError exception not thrown')
37 except datasift.InvalidDataError:
38 # Expected exception
39 pass
40
41 def test_set_and_get(self):
42 definition = datasift.Definition(self.user)
43 self.assertEqual(definition.get(), '', 'Default definition CSDL is not empty')
44 definition.set(testdata.definition)
45 self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
46
47 def test_compile_success(self):
48 response = {
49 'response_code': 200,
50 'data': {
51 'hash': testdata.definition_hash,
52 'created_at': '2011-12-13 14:15:16',
53 'dpu': 10,
54 },
55 'rate_limit': 200,
56 'rate_limit_remaining': 150,
57 }
58 self.mock_api_client.set_response(response)
59
60 definition = datasift.Definition(self.user, testdata.definition)
61 self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
62
63 try:
64 definition.compile()
65 except datasift.InvalidDataError as e:
66 self.fail('InvalidDataError: %s' % e)
67 except datasift.CompileFailedError as e:
68 self.fail('CompileFailedError: %s' % e)
69 except datasift.APIError as (e, c):
70 self.fail('APIError: %s' % e)
71
72 self.assertEqual(self.user.get_rate_limit(), response['rate_limit'], 'Incorrect rate limit')
73 self.assertEqual(self.user.get_rate_limit_remaining(), response['rate_limit_remaining'], 'Incorrect rate limit remaining')
74 self.assertEqual(definition.get_hash(), response['data']['hash'], 'Incorrect hash')
75 self.assertEqual(definition.get_created_at(), datetime.strptime(response['data']['created_at'], '%Y-%m-%d %H:%M:%S'), 'Incorrect created at date')
76 self.assertEqual(definition.get_total_dpu(), response['data']['dpu'], 'Incorrect total DPU')
77
78 def test_compile_failure(self):
79 response = {
80 'response_code': 400,
81 'data': {
82 'error': 'The target interactin.content does not exist',
83 },
84 'rate_limit': 200,
85 'rate_limit_remaining': 150,
86 }
87 self.mock_api_client.set_response(response)
88
89 definition = datasift.Definition(self.user, testdata.invalid_definition)
90 self.assertEqual(definition.get(), testdata.invalid_definition, 'Definition CSDL not set correctly')
91
92 try:
93 definition.compile()
94 self.fail('Expected CompileFailedError not thrown')
95 except datasift.InvalidDataError as e:
96 self.fail('InvalidDataError: %s' % e)
97 except datasift.CompileFailedError as e:
98 self.assertEqual(e.__str__(), response['data']['error'])
99 except datasift.APIError as (e, c):
100 self.fail('APIError: %s' % e)
101
102 def test_compile_success_then_failure(self):
103 response = {
104 'response_code': 200,
105 'data': {
106 'hash': testdata.definition_hash,
107 'created_at': '2011-12-13 14:15:16',
108 'dpu': 10,
109 },
110 'rate_limit': 200,
111 'rate_limit_remaining': 150,
112 }
113 self.mock_api_client.set_response(response)
114
115 definition = datasift.Definition(self.user, testdata.definition)
116 self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
117
118 try:
119 definition.compile()
120 except datasift.InvalidDataError as e:
121 self.fail('InvalidDataError: %s' % e)
122 except datasift.CompileFailedError as e:
123 self.fail('CompileFailedError: %s' % e)
124 except datasift.APIError as (e, c):
125 self.fail('APIError: %s' % e)
126
127 self.assertEqual(self.user.get_rate_limit(), response['rate_limit'], 'Incorrect rate limit')
128 self.assertEqual(self.user.get_rate_limit_remaining(), response['rate_limit_remaining'], 'Incorrect rate limit remaining')
129 self.assertEqual(definition.get_hash(), response['data']['hash'], 'Incorrect hash')
130 self.assertEqual(definition.get_created_at(), datetime.strptime(response['data']['created_at'], '%Y-%m-%d %H:%M:%S'), 'Incorrect created at date')
131 self.assertEqual(definition.get_total_dpu(), response['data']['dpu'], 'Incorrect total DPU')
132
133 definition.set(testdata.invalid_definition)
134 self.assertEqual(definition.get(), testdata.invalid_definition, 'Definition CSDL not set correctly')
135
136 response = {
137 'response_code': 400,
138 'data': {
139 'error': 'The target interactin.content does not exist',
140 },
141 'rate_limit': 200,
142 'rate_limit_remaining': 150,
143 }
144 self.mock_api_client.set_response(response)
145
146 try:
147 definition.compile()
148 self.fail('Expected CompileFailedError not thrown')
149 except datasift.InvalidDataError as e:
150 self.fail('InvalidDataError: %s' % e)
151 except datasift.CompileFailedError as e:
152 self.assertEqual(e.__str__(), response['data']['error'])
153 except datasift.APIError as (e, c):
154 self.fail('APIError: %s' % e)
155
156 def test_get_created_at(self):
157 response = {
158 'response_code': 200,
159 'data': {
160 'created_at': '2011-12-13 14:15:16',
161 'dpu': 10,
162 },
163 'rate_limit': 200,
164 'rate_limit_remaining': 150,
165 }
166 self.mock_api_client.set_response(response)
167
168 definition = datasift.Definition(self.user, testdata.definition)
169 self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
170
171 self.assertEqual(definition.get_created_at(), datetime.strptime(response['data']['created_at'], '%Y-%m-%d %H:%M:%S'), 'Incorrect created at date')
172
173 def test_get_total_dpu(self):
174 response = {
175 'response_code': 200,
176 'data': {
177 'created_at': '2011-12-13 14:15:16',
178 'dpu': 10,
179 },
180 'rate_limit': 200,
181 'rate_limit_remaining': 150,
182 }
183 self.mock_api_client.set_response(response)
184
185 definition = datasift.Definition(self.user, testdata.definition)
186 self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
187
188 self.assertEqual(definition.get_total_dpu(), response['data']['dpu'], 'Incorrect total DPU')
189
190 def test_get_dpu_breakdown(self):
191 response = {
192 'response_code': 200,
193 'data': {
194 'hash': testdata.definition_hash,
195 'created_at': '2011-12-13 14:15:16',
196 'dpu': 4,
197 },
198 'rate_limit': 200,
199 'rate_limit_remaining': 150,
200 }
201 self.mock_api_client.set_response(response)
202
203 definition = datasift.Definition(self.user, testdata.definition)
204 self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
205
206 self.assertEqual(definition.get_hash(), response['data']['hash'], 'Incorrect hash')
207
208 response = {
209 'response_code': 200,
210 'data': {
f6add7b @3ft9 Fixed a couple of minor bugs.
3ft9 authored
211 'detail': {
1b2e0f3 @3ft9 Initial implementation.
3ft9 authored
212 'contains': {
213 'count': 1,
214 'dpu': 4,
215 'targets': {
216 'interaction.content': {
217 'count': 1,
218 'dpu': 4,
219 },
220 },
221 },
222 },
223 'dpu': 4,
224 },
225 'rate_limit': 200,
226 'rate_limit_remaining': 150,
227 }
228 self.mock_api_client.set_response(response)
229
230 dpu = definition.get_dpu_breakdown()
231
232 self.assertEqual(dpu, response['data'], 'The DPU breakdown is not as expected')
233 self.assertEqual(definition.get_total_dpu(), response['data']['dpu'], 'The total DPU is incorrect')
234
235 def test_get_dpu_breakdown_on_invalid_definition(self):
236 definition = datasift.Definition(self.user, testdata.invalid_definition)
237 self.assertEqual(definition.get(), testdata.invalid_definition, 'Definition CSDL not set correctly')
238
239 response = {
240 'response_code': 400,
241 'data': {
242 'error': 'The target interactin.content does not exist',
243 },
244 'rate_limit': 200,
245 'rate_limit_remaining': 150,
246 }
247 self.mock_api_client.set_response(response)
248
249 try:
250 definition.get_dpu_breakdown()
251 self.fail('Expected CompileFailedError not thrown')
252 except datasift.InvalidDataError as e:
253 self.fail('InvalidDataError: %s' % e)
254 except datasift.CompileFailedError as e:
255 self.assertEqual(e.__str__(), response['data']['error'])
256 except datasift.APIError as (e, c):
257 self.fail('APIError: %s' % e)
258
259 def test_get_buffered(self):
260 response = {
261 'response_code': 200,
262 'data': {
263 'hash': testdata.definition_hash,
264 'created_at': '2011-12-13 14:15:16',
265 'dpu': 4,
266 },
267 'rate_limit': 200,
268 'rate_limit_remaining': 150,
269 }
270 self.mock_api_client.set_response(response)
271
272 definition = datasift.Definition(self.user, testdata.definition)
273 self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
274
275 self.assertEqual(definition.get_hash(), response['data']['hash'], 'Incorrect hash')
276
277 response = {
278 'response_code': 200,
279 'data': {
280 'stream': {
281 0: {
282 'interaction': {
283 'source': 'Snaptu',
284 'author': {
285 'username': 'nittolexia',
286 'name' : 'nittosoetreznoe',
287 'id' : 172192091,
288 'avatar' : 'http://a0.twimg.com/profile_images/1429378181/gendowor_normal.jpg',
289 'link' : 'http://twitter.com/nittolexia',
290 },
291 'type' : 'twitter',
292 'link' : 'http://twitter.com/nittolexia/statuses/89571192838684672',
293 'created_at': 'Sat, 09 Jul 2011 05:46:51 +0000',
294 'content' : 'RT @ayyuchadel: Haha RT @nittolexia: Mending gak ush maen twitter dehh..RT @sansan_arie:',
295 'id' : '1e0a9eedc207acc0e074ea8aecb2c5ea',
296 },
297 'twitter': {
298 'user': {
299 'name' : 'nittosoetreznoe',
300 'description' : 'fuck all',
301 'location' : 'denpasar, bali',
302 'statuses_count' : 6830,
303 'followers_count': 88,
304 'friends_count' : 111,
305 'screen_name' : 'nittolexia',
306 'lang' : 'en',
307 'time_zone' : 'Alaska',
308 'id' : 172192091,
309 'geo_enabled' : True,
310 },
311 'mentions': {
312 0: 'ayyuchadel',
313 1: 'nittolexia',
314 2: 'sansan_arie',
315 },
316 'id' : '89571192838684672',
317 'text' : 'RT @ayyuchadel: Haha RT @nittolexia: Mending gak ush maen twitter dehh..RT @sansan_arie:',
318 'source' : '<a href="http://www.snaptu.com" rel="nofollow">Snaptu</a>',
319 'created_at': 'Sat, 09 Jul 2011 05:46:51 +0000',
320 },
321 'klout': {
322 'score' : 45,
323 'network' : 55,
324 'amplification': 17,
325 'true_reach' : 31,
326 'slope' : 0,
327 'class' : 'Networker',
328 },
329 'peerindex': {
330 'score': 30,
331 },
332 'language': {
333 'tag': 'da',
334 },
335 },
336 },
337 },
338 'rate_limit': 200,
339 'rate_limit_remaining': 150,
340 }
341 self.mock_api_client.set_response(response)
342
343 interactions = definition.get_buffered()
344
345 self.assertEqual(interactions, response['data']['stream'], 'Buffered interactions are not as expected')
346
347 def test_get_consumer(self):
348 response = {
349 'response_code': 200,
350 'data': {
351 'hash': testdata.definition_hash,
352 'created_at': '2011-12-13 14:15:16',
353 'dpu': 4,
354 },
355 'rate_limit': 200,
356 'rate_limit_remaining': 150,
357 }
358 self.mock_api_client.set_response(response)
359
360 definition = datasift.Definition(self.user, testdata.definition)
361 self.assertEqual(definition.get(), testdata.definition, 'Definition CSDL not set correctly')
362
363 self.assertEqual(definition.get_hash(), response['data']['hash'], 'Incorrect hash')
364
365 consumer = definition.get_consumer(datasift.StreamConsumerEventHandler())
366
367 if __name__ == '__main__':
368 unittest.main()
Something went wrong with that request. Please try again.