|
1 | | -''' |
| 1 | +""" |
2 | 2 | test_integration.py |
3 | 3 |
|
4 | 4 | Copyright (c) 2013-2014 Snowplow Analytics Ltd. All rights reserved. |
|
10 | 10 |
|
11 | 11 | Unless required by applicable law or agreed to in writing, |
12 | 12 | software distributed under the Apache License Version 2.0 is distributed on |
13 | | - an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 13 | + an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
14 | 14 | express or implied. See the Apache License Version 2.0 for the specific |
15 | 15 | language governing permissions and limitations there under. |
16 | 16 |
|
17 | | - Authors: Anuj More, Alex Dean |
| 17 | + Authors: Anuj More, Alex Dean, Fred Blundun |
18 | 18 | Copyright: Copyright (c) 2013-2014 Snowplow Analytics Ltd |
19 | 19 | License: Apache License Version 2.0 |
20 | | -''' |
| 20 | +""" |
21 | 21 |
|
22 | 22 | import unittest |
23 | 23 | import time |
24 | | -from snowplow_tracker import tracker |
| 24 | +import re |
| 25 | +from snowplow_tracker import tracker, _version |
25 | 26 | from httmock import all_requests, HTTMock |
26 | 27 |
|
| 28 | +def from_querystring(field, url): |
| 29 | + pattern = re.compile("^[^#]*[?&]" + field + "=([^&#]*)") |
| 30 | + match = pattern.match(url) |
| 31 | + if match: |
| 32 | + return match.groups()[0] |
27 | 33 |
|
28 | 34 | @all_requests |
29 | 35 | def pass_response_content(url, request): |
30 | | - return 'True' |
31 | | - |
| 36 | + return { |
| 37 | + "url": request.url, |
| 38 | + "status_code": 200 |
| 39 | + } |
32 | 40 |
|
33 | 41 | @all_requests |
34 | 42 | def fail_response_content(url, request): |
35 | | - return 'HTTP status code [501] is a server error' |
| 43 | + return "HTTP status code [501] is a server error" |
36 | 44 |
|
37 | 45 |
|
38 | 46 | class IntegrationTest(unittest.TestCase): |
39 | 47 |
|
40 | 48 | def test_integration_page_view(self): |
41 | | - t = tracker.Tracker.hostname('localhost') |
| 49 | + t = tracker.Tracker("localhost") |
42 | 50 | with HTTMock(pass_response_content): |
43 | | - val = t.track_page_view('http://savethearctic.org', 'Save The Arctic', None) |
44 | | - self.assertTrue(val) |
| 51 | + val = t.track_page_view("http://savethearctic.org", "Save The Arctic", None) |
| 52 | + self.assertEquals(from_querystring("page", val), "Save+The+Arctic") |
45 | 53 |
|
46 | 54 | def test_integration_ecommerce_transaction(self): |
47 | | - t = tracker.Tracker.hostname('localhost') |
| 55 | + t = tracker.Tracker("localhost") |
48 | 56 | with HTTMock(pass_response_content): |
49 | | - val = t.track_ecommerce_transaction('12345', 'Web', 9.99, 1.98, 3.00, 'London', 'Denver', 'Greenland') |
50 | | - self.assertTrue(val) |
| 57 | + val = t.track_ecommerce_transaction("12345", 9.99, "Web", 1.98, 3.05, "London", "Denver", "Greenland") |
| 58 | + assertion_array = {"tr_tt": "9.99", "e": "tr", "tr_id": "12345", "tr_sh": "3.05", "tr_st": "Denver", "tr_af": "Web", "tr_co": "Greenland", "tr_tx": "1.98", "tr_ci": "London"} |
| 59 | + for key in assertion_array: |
| 60 | + self.assertEquals(from_querystring(key, val), assertion_array[key]) |
51 | 61 |
|
52 | 62 | def test_integration_ecommerce_transaction_item(self): |
53 | | - t = tracker.Tracker.hostname('localhost') |
| 63 | + t = tracker.Tracker("localhost") |
54 | 64 | with HTTMock(pass_response_content): |
55 | | - val = t.track_ecommerce_transaction_item('12345', 'pbz0025', 'black-tarot', 'tarot', 7.99, 2) |
56 | | - self.assertTrue(val) |
| 65 | + val = t.track_ecommerce_transaction_item("12345", "pbz0025", 7.99, 2, "black-tarot", "tarot") |
| 66 | + assertion_array = {"ti_ca": "tarot", "ti_id": "12345", "ti_qu": "2", "ti_sk": "pbz0025", "e": "ti", "ti_nm": "black-tarot", "ti_pr": "7.99"} |
| 67 | + for key in assertion_array: |
| 68 | + self.assertEquals(from_querystring(key, val), assertion_array[key]) |
57 | 69 |
|
58 | 70 | def test_integration_screen_view(self): |
59 | | - t = tracker.Tracker.hostname('localhost') |
| 71 | + t = tracker.Tracker("localhost") |
60 | 72 | with HTTMock(pass_response_content): |
61 | | - val = t.track_screen_view('Game HUD 2', 'Hello!') |
62 | | - self.assertTrue(val) |
| 73 | + val = t.track_screen_view("Game HUD 2", "Hello!") |
| 74 | + assertion_array = {"e": "ue", "ue_na": "screen_view"} |
| 75 | + for key in assertion_array: |
| 76 | + self.assertEquals(from_querystring(key, val), assertion_array[key]) |
63 | 77 |
|
64 | 78 | def test_integration_struct_event(self): |
65 | | - t = tracker.Tracker.hostname('localhost') |
| 79 | + t = tracker.Tracker("localhost") |
66 | 80 | with HTTMock(pass_response_content): |
67 | | - val = t.track_struct_event('Ecomm', 'add-to-basket', 'dog-skateboarding-video', 'hd', 13.99) |
68 | | - self.assertTrue(val) |
| 81 | + val = t.track_struct_event("Ecomm", "add-to-basket", "dog-skateboarding-video", "hd", 13.99) |
| 82 | + assertion_array = {"se_ca": "Ecomm", "se_pr": "hd", "se_la": "dog-skateboarding-video", "se_va": "13.99", "se_ac": "add-to-basket", "e": "se"} |
| 83 | + for key in assertion_array: |
| 84 | + self.assertEquals(from_querystring(key, val), assertion_array[key]) |
| 85 | + |
69 | 86 |
|
70 | 87 | def test_integration_unstruct_event_non_base64(self): |
71 | | - t = tracker.Tracker.hostname('localhost') |
72 | | - t.config['encode_base64'] = False |
| 88 | + t = tracker.Tracker("localhost") |
| 89 | + t.config["encode_base64"] = False |
73 | 90 | with HTTMock(pass_response_content): |
74 | | - val = t.track_unstruct_event('viewed_product', {'product_id': 'ASO01043', 'price$flt': 49.95, 'walrus$tms': int(time.time() * 1000),}) |
75 | | - self.assertTrue(val) |
| 91 | + val = t.track_unstruct_event("viewed_product", {"product_id": "ASO01043", "price$flt": 49.95, "walrus$tms": int(time.time() * 1000)}) |
| 92 | + assertion_array = {"e": "ue", "ue_na": "viewed_product"} |
| 93 | + for key in assertion_array: |
| 94 | + self.assertEquals(from_querystring(key, val), assertion_array[key]) |
76 | 95 |
|
77 | 96 | def test_integration_unstruct_event_base64(self): |
78 | | - t = tracker.Tracker.hostname('localhost') |
| 97 | + t = tracker.Tracker("localhost") |
79 | 98 | with HTTMock(pass_response_content): |
80 | | - val = t.track_unstruct_event('viewed_product', {'product_id': 'ASO01043', 'price$flt': 49.95, 'walrus$tms': int(time.time() * 1000),}) |
81 | | - self.assertTrue(val) |
| 99 | + val = t.track_unstruct_event("viewed_product", {"product_id": "ASO01043", "price$flt": 49.95, "walrus$tms": int(time.time() * 1000)}) |
| 100 | + assertion_array = {"e": "ue", "ue_na": "viewed_product"} |
| 101 | + for key in assertion_array: |
| 102 | + self.assertEquals(from_querystring(key, val), assertion_array[key]) |
82 | 103 |
|
83 | 104 | def test_integration_unstruct_event_non_base64_error(self): |
84 | | - t = tracker.Tracker.hostname('localhost') |
85 | | - t.config['encode_base64'] = False |
| 105 | + t = tracker.Tracker("localhost") |
| 106 | + t.config["encode_base64"] = False |
86 | 107 | try: |
87 | | - val = t.track_unstruct_event('viewed_product', |
| 108 | + val = t.track_unstruct_event("viewed_product", |
88 | 109 | { |
89 | | - 'product_id': 'ASO01043', |
90 | | - 'price$flt': 49, # ERROR |
91 | | - 'walrus$tms': int(time.time() * 1000), |
| 110 | + "product_id": "ASO01043", |
| 111 | + "price$flt": 49, # ERROR |
| 112 | + "walrus$tms": int(time.time() * 1000), |
92 | 113 | }) |
93 | 114 | except RuntimeError as e: |
94 | 115 | self.assertEquals("price$flt in dict is not a flt", str(e)) |
95 | 116 |
|
96 | 117 |
|
97 | 118 | def test_integration_unstruct_event_base64_error(self): |
98 | | - t = tracker.Tracker.hostname('localhost') |
| 119 | + t = tracker.Tracker("localhost") |
99 | 120 | try: |
100 | | - val = t.track_unstruct_event('viewed_product', |
| 121 | + val = t.track_unstruct_event("viewed_product", |
101 | 122 | { |
102 | | - 'product_id': 'ASO01043', |
103 | | - 'price$flt': 49.95, |
104 | | - 'walrus$tms': 'hello', # ERROR |
| 123 | + "product_id": "ASO01043", |
| 124 | + "price$flt": 49.95, |
| 125 | + "walrus$tms": "hello", # ERROR |
105 | 126 | }) |
106 | 127 | except RuntimeError as e: |
107 | 128 | self.assertEquals("walrus$tms in dict is not a tms", str(e)) |
| 129 | + |
| 130 | + def test_integration_standard_nv_pairs(self): |
| 131 | + t = tracker.Tracker("localhost", "cf") |
| 132 | + t.set_platform("mob") |
| 133 | + t.set_user_id("user12345") |
| 134 | + t.set_app_id("angry-birds-android") |
| 135 | + t.set_screen_resolution(100, 200) |
| 136 | + t.set_color_depth(24) |
| 137 | + t.set_timezone("Europe London") |
| 138 | + t.set_lang("en") |
| 139 | + with HTTMock(pass_response_content): |
| 140 | + val = t.track_page_view("localhost", "local host", None) |
| 141 | + assertion_array = {"tna": "cf", "evn": "com.snowplowanalytics", "res": "100x200", "lang": "en", "aid": "angry-birds-android", "cd": "24", "tz": "Europe+London", "p": "mob", "tv": "py-" + _version.__version__} |
| 142 | + for key in assertion_array: |
| 143 | + self.assertEquals(from_querystring(key, val), assertion_array[key]) |
0 commit comments