forked from snowplow/snowplow-python-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_integration.py
More file actions
332 lines (287 loc) · 16.4 KB
/
test_integration.py
File metadata and controls
332 lines (287 loc) · 16.4 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""
test_integration.py
Copyright (c) 2013-2014 Snowplow Analytics Ltd. All rights reserved.
This program is licensed to you under the Apache License Version 2.0,
and you may not use this file except in compliance with the Apache License
Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing,
software distributed under the Apache License Version 2.0 is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the Apache License Version 2.0 for the specific
language governing permissions and limitations there under.
Authors: Anuj More, Alex Dean, Fred Blundun
Copyright: Copyright (c) 2013-2014 Snowplow Analytics Ltd
License: Apache License Version 2.0
"""
import unittest
import re
import json
import base64
try:
from urllib.parse import unquote_plus # Python 3
except ImportError:
from urllib import unquote_plus # Python 2
import redis
from httmock import all_requests, HTTMock
from freezegun import freeze_time
from snowplow_tracker import tracker, _version, emitters, subject
from snowplow_tracker.timestamp import DeviceTimestamp, TrueTimestamp
from snowplow_tracker.self_describing_json import SelfDescribingJson
querystrings = [""]
default_emitter = emitters.Emitter("localhost", protocol="http", port=80)
post_emitter = emitters.Emitter("localhost", protocol="http", port=80, method='post', buffer_size=1)
default_subject = subject.Subject()
def from_querystring(field, url):
pattern = re.compile("^[^#]*[?&]" + field + "=([^&#]*)")
match = pattern.match(url)
if match:
return match.groups()[0]
@all_requests
def pass_response_content(url, request):
querystrings.append(request.url)
return {
"url": request.url,
"status_code": 200
}
@all_requests
def pass_post_response_content(url, request):
querystrings.append(json.loads(request.body))
return {
"url": request.url,
"status_code": 200
}
@all_requests
def fail_response_content(url, request):
return {
"url": request.url,
"status_code": 501
}
class IntegrationTest(unittest.TestCase):
def test_integration_page_view(self):
t = tracker.Tracker([default_emitter], default_subject)
with HTTMock(pass_response_content):
t.track_page_view("http://savethearctic.org", "Save The Arctic", "http://referrer.com")
expected_fields = {"e": "pv", "page": "Save+The+Arctic", "url": "http%3A%2F%2Fsavethearctic.org", "refr": "http%3A%2F%2Freferrer.com"}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-1]), expected_fields[key])
def test_integration_ecommerce_transaction_item(self):
t = tracker.Tracker([default_emitter], default_subject)
with HTTMock(pass_response_content):
t.track_ecommerce_transaction_item("12345", "pbz0025", 7.99, 2, "black-tarot", "tarot", currency="GBP")
expected_fields = {"ti_ca": "tarot", "ti_id": "12345", "ti_qu": "2", "ti_sk": "pbz0025", "e": "ti", "ti_nm": "black-tarot", "ti_pr": "7.99", "ti_cu": "GBP"}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-1]), expected_fields[key])
def test_integration_ecommerce_transaction(self):
t = tracker.Tracker([default_emitter], default_subject)
with HTTMock(pass_response_content):
t.track_ecommerce_transaction("6a8078be", 35, city="London", currency="GBP", items=
[{
"sku": "pbz0026",
"price": 20,
"quantity": 1
},
{
"sku": "pbz0038",
"price": 15,
"quantity": 1
}])
expected_fields = {"e": "tr", "tr_id": "6a8078be", "tr_tt": "35", "tr_ci": "London", "tr_cu": "GBP"}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-3]), expected_fields[key])
expected_fields = {"e": "ti", "ti_id": "6a8078be", "ti_sk": "pbz0026", "ti_pr": "20", "ti_cu": "GBP"}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-2]), expected_fields[key])
expected_fields = {"e": "ti", "ti_id": "6a8078be", "ti_sk": "pbz0038", "ti_pr": "15", "ti_cu": "GBP"}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-1]), expected_fields[key])
self.assertEquals(from_querystring("dtm", querystrings[-3]), from_querystring("dtm", querystrings[-2]))
def test_integration_screen_view(self):
t = tracker.Tracker([default_emitter], default_subject, encode_base64=False)
with HTTMock(pass_response_content):
t.track_screen_view("Game HUD 2", id_="534")
expected_fields = {"e": "ue"}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-1]), expected_fields[key])
envelope_string = from_querystring("ue_pr", querystrings[-1])
envelope = json.loads(unquote_plus(envelope_string))
self.assertEquals(envelope, {
"schema": "iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0",
"data": {"schema": "iglu:com.snowplowanalytics.snowplow/screen_view/jsonschema/1-0-0",
"data": {
"name": "Game HUD 2",
"id": "534"
}
}
})
def test_integration_struct_event(self):
t = tracker.Tracker([default_emitter], default_subject)
with HTTMock(pass_response_content):
t.track_struct_event("Ecomm", "add-to-basket", "dog-skateboarding-video", "hd", 13.99)
expected_fields = {"se_ca": "Ecomm", "se_pr": "hd", "se_la": "dog-skateboarding-video", "se_va": "13.99", "se_ac": "add-to-basket", "e": "se"}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-1]), expected_fields[key])
def test_integration_unstruct_event_non_base64(self):
t = tracker.Tracker([default_emitter], default_subject, encode_base64=False)
with HTTMock(pass_response_content):
t.track_unstruct_event(SelfDescribingJson("iglu:com.acme/viewed_product/jsonschema/2-0-2", {"product_id": "ASO01043", "price$flt": 49.95, "walrus$tms": 1000}))
expected_fields = {"e": "ue"}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-1]), expected_fields[key])
envelope_string = from_querystring("ue_pr", querystrings[-1])
envelope = json.loads(unquote_plus(envelope_string))
self.assertEquals(envelope, {
"schema": "iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0",
"data": {"schema": "iglu:com.acme/viewed_product/jsonschema/2-0-2", "data": {"product_id": "ASO01043", "price$flt": 49.95, "walrus$tms": 1000}}
})
def test_integration_unstruct_event_base64(self):
t = tracker.Tracker([default_emitter], default_subject, encode_base64=True)
with HTTMock(pass_response_content):
t.track_unstruct_event(SelfDescribingJson("iglu:com.acme/viewed_product/jsonschema/2-0-2", {"product_id": "ASO01043", "price$flt": 49.95, "walrus$tms": 1000}))
expected_fields = {"e": "ue"}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-1]), expected_fields[key])
envelope_string = unquote_plus(from_querystring("ue_px", querystrings[-1]))
envelope = json.loads((base64.urlsafe_b64decode(bytearray(envelope_string, "utf-8"))).decode("utf-8"))
self.assertEquals(envelope, {
"schema": "iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0",
"data": {"schema": "iglu:com.acme/viewed_product/jsonschema/2-0-2", "data": {"product_id": "ASO01043", "price$flt": 49.95, "walrus$tms": 1000}}
})
def test_integration_context_non_base64(self):
t = tracker.Tracker([default_emitter], default_subject, encode_base64=False)
with HTTMock(pass_response_content):
t.track_page_view("localhost", "local host", None, [SelfDescribingJson("iglu:com.example/user/jsonschema/2-0-3", {"user_type": "tester"})])
envelope_string = from_querystring("co", querystrings[-1])
envelope = json.loads(unquote_plus(envelope_string))
self.assertEquals(envelope, {
"schema": "iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-1",
"data":[{"schema": "iglu:com.example/user/jsonschema/2-0-3", "data": {"user_type": "tester"}}]
})
def test_integration_context_base64(self):
t = tracker.Tracker([default_emitter], default_subject, encode_base64=True)
with HTTMock(pass_response_content):
t.track_page_view("localhost", "local host", None, [SelfDescribingJson("iglu:com.example/user/jsonschema/2-0-3", {"user_type": "tester"})])
envelope_string = unquote_plus(from_querystring("cx", querystrings[-1]))
envelope = json.loads((base64.urlsafe_b64decode(bytearray(envelope_string, "utf-8"))).decode("utf-8"))
self.assertEquals(envelope, {
"schema": "iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-1",
"data":[{"schema": "iglu:com.example/user/jsonschema/2-0-3", "data": {"user_type": "tester"}}]
})
def test_integration_standard_nv_pairs(self):
s = subject.Subject()
s.set_platform("mob")
s.set_user_id("user12345")
s.set_screen_resolution(100, 200)
s.set_color_depth(24)
s.set_timezone("Europe London")
s.set_lang("en")
t = tracker.Tracker([emitters.Emitter("localhost")], s, "cf", app_id="angry-birds-android")
with HTTMock(pass_response_content):
t.track_page_view("localhost", "local host")
expected_fields = {"tna": "cf", "res": "100x200",
"lang": "en", "aid": "angry-birds-android", "cd": "24", "tz": "Europe+London",
"p": "mob", "tv": "py-" + _version.__version__}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-1]), expected_fields[key])
self.assertIsNotNone(from_querystring("eid", querystrings[-1]))
self.assertIsNotNone(from_querystring("dtm", querystrings[-1]))
def test_integration_identification_methods(self):
s = subject.Subject()
s.set_domain_user_id("4616bfb38f872d16")
s.set_ip_address("255.255.255.255")
s.set_useragent("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)")
s.set_network_user_id("fbc6c76c-bce5-43ce-8d5a-31c5")
t = tracker.Tracker([emitters.Emitter("localhost")], s, "cf", app_id="angry-birds-android")
with HTTMock(pass_response_content):
t.track_page_view("localhost", "local host")
expected_fields = {
"duid": "4616bfb38f872d16",
"ip": "255.255.255.255",
"ua": "Mozilla%2F5.0+%28compatible%3B+MSIE+9.0%3B+Windows+NT+6.0%3B+Trident%2F5.0%29",
"tnuid": "fbc6c76c-bce5-43ce-8d5a-31c5"
}
for key in expected_fields:
self.assertEquals(from_querystring(key, querystrings[-1]), expected_fields[key])
def test_integration_redis_default(self):
r = redis.StrictRedis()
t = tracker.Tracker([emitters.RedisEmitter()], default_subject)
t.track_page_view("http://www.example.com")
event_string = r.rpop("snowplow")
event_dict = json.loads(event_string.decode("utf-8"))
self.assertEquals(event_dict["e"], "pv")
def test_integration_redis_custom(self):
r = redis.StrictRedis(db=1)
t = tracker.Tracker([emitters.RedisEmitter(rdb=r, key="custom_key")], default_subject)
t.track_page_view("http://www.example.com")
event_string = r.rpop("custom_key")
event_dict = json.loads(event_string.decode("utf-8"))
self.assertEquals(event_dict["e"], "pv")
def test_integration_success_callback(self):
callback_success_queue = []
callback_failure_queue = []
callback_emitter = emitters.Emitter("localhost", on_success=lambda x: callback_success_queue.append(x),
on_failure=lambda x, y:callback_failure_queue.append(x))
t = tracker.Tracker([callback_emitter], default_subject)
with HTTMock(pass_response_content):
t.track_page_view("http://www.example.com")
self.assertEquals(callback_success_queue[0], 1)
self.assertEquals(callback_failure_queue, [])
def test_integration_failure_callback(self):
callback_success_queue = []
callback_failure_queue = []
callback_emitter = emitters.Emitter("localhost", on_success=lambda x: callback_success_queue.append(x),
on_failure=lambda x, y:callback_failure_queue.append(x))
t = tracker.Tracker([callback_emitter], default_subject)
with HTTMock(fail_response_content):
t.track_page_view("http://www.example.com")
self.assertEquals(callback_success_queue, [])
self.assertEquals(callback_failure_queue[0], 0)
def test_post_page_view(self):
t = tracker.Tracker([post_emitter], default_subject)
with HTTMock(pass_post_response_content):
t.track_page_view("localhost", "local host", None)
expected_fields = {"e": "pv", "page": "local host", "url": "localhost"}
request = querystrings[-1]
self.assertEquals(request["schema"], "iglu:com.snowplowanalytics.snowplow/payload_data/jsonschema/1-0-4")
for key in expected_fields:
self.assertEquals(request["data"][0][key], expected_fields[key])
def test_post_batched(self):
post_emitter = emitters.Emitter("localhost", protocol="http", port=80, method='post', buffer_size=2)
t = tracker.Tracker(post_emitter, default_subject)
with HTTMock(pass_post_response_content):
t.track_struct_event("Test", "A")
t.track_struct_event("Test", "B")
self.assertEquals(querystrings[-1]["data"][0]["se_ac"], "A")
self.assertEquals(querystrings[-1]["data"][1]["se_ac"], "B")
def test_timestamps(self):
emitter = emitters.Emitter("localhost", protocol="http", port=80, method='post', buffer_size=4)
t = tracker.Tracker([emitter], default_subject)
with HTTMock(pass_post_response_content):
with freeze_time("2013-01-14 03:21:34"):
t.track_page_view("localhost", "stamp0", None, tstamp=None)
t.track_page_view("localhost", "stamp1", None, tstamp=1358933694000)
with freeze_time("2013-01-14 03:22:36"):
t.track_page_view("localhost", "stamp2", None, tstamp=DeviceTimestamp(1458133694000))
t.track_page_view("localhost", "stamp3", None, tstamp=TrueTimestamp(1458033694000))
expected_timestamps = [
{"dtm": "1358133694000", "ttm": None, "stm": "1358133756000"},
{"dtm": "1358933694000", "ttm": None, "stm": "1358133756000"},
{"dtm": "1458133694000", "ttm": None, "stm": "1358133756000"},
{"dtm": None, "ttm": "1458033694000", "stm": "1358133756000"},
]
request = querystrings[-1]
for i, event in enumerate(expected_timestamps):
self.assertEquals(request["data"][i].get("dtm"), expected_timestamps[i]["dtm"])
self.assertEquals(request["data"][i].get("ttm"), expected_timestamps[i]["ttm"])
self.assertEquals(request["data"][i].get("stm"), expected_timestamps[i]["stm"])
self.assertEquals(request["data"][i].get("page"), "stamp" + str(i))
def test_bytelimit(self):
post_emitter = emitters.Emitter("localhost", protocol="http", port=80, method='post', buffer_size=5, byte_limit=420)
t = tracker.Tracker(post_emitter, default_subject)
with HTTMock(pass_post_response_content):
t.track_struct_event("Test", "A") # 140 bytes
t.track_struct_event("Test", "A") # 280 bytes
t.track_struct_event("Test", "A") # 420 bytes. Send
t.track_struct_event("Test", "AA") # 141
self.assertEquals(len(querystrings[-1]["data"]), 3)
self.assertEqual(post_emitter.bytes_queued, 141)