|
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 |
|
26 | 26 | from httmock import all_requests, HTTMock |
27 | 27 |
|
28 | 28 | def from_querystring(field, url): |
29 | | - pattern = re.compile('^[^#]*[?&]' + field + '=([^&#]*)') |
| 29 | + pattern = re.compile("^[^#]*[?&]" + field + "=([^&#]*)") |
30 | 30 | match = pattern.match(url) |
31 | 31 | if match: |
32 | 32 | return match.groups()[0] |
33 | 33 |
|
34 | 34 | @all_requests |
35 | 35 | def pass_response_content(url, request): |
36 | 36 | return { |
37 | | - 'url': request.url, |
38 | | - 'status_code': 200 |
| 37 | + "url": request.url, |
| 38 | + "status_code": 200 |
39 | 39 | } |
40 | 40 |
|
41 | 41 | @all_requests |
42 | 42 | def fail_response_content(url, request): |
43 | | - return 'HTTP status code [501] is a server error' |
| 43 | + return "HTTP status code [501] is a server error" |
44 | 44 |
|
45 | 45 |
|
46 | 46 | class IntegrationTest(unittest.TestCase): |
47 | 47 |
|
48 | 48 | def test_integration_page_view(self): |
49 | | - t = tracker.Tracker('localhost') |
| 49 | + t = tracker.Tracker("localhost") |
50 | 50 | with HTTMock(pass_response_content): |
51 | | - val = t.track_page_view('http://savethearctic.org', 'Save The Arctic', None) |
52 | | - self.assertEquals(from_querystring('page', val), 'Save+The+Arctic') |
| 51 | + val = t.track_page_view("http://savethearctic.org", "Save The Arctic", None) |
| 52 | + self.assertEquals(from_querystring("page", val), "Save+The+Arctic") |
53 | 53 |
|
54 | 54 | def test_integration_ecommerce_transaction(self): |
55 | | - t = tracker.Tracker('localhost') |
| 55 | + t = tracker.Tracker("localhost") |
56 | 56 | with HTTMock(pass_response_content): |
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'} |
| 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 | 59 | for key in assertion_array: |
60 | 60 | self.assertEquals(from_querystring(key, val), assertion_array[key]) |
61 | 61 |
|
62 | 62 | def test_integration_ecommerce_transaction_item(self): |
63 | | - t = tracker.Tracker('localhost') |
| 63 | + t = tracker.Tracker("localhost") |
64 | 64 | with HTTMock(pass_response_content): |
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'} |
| 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 | 67 | for key in assertion_array: |
68 | 68 | self.assertEquals(from_querystring(key, val), assertion_array[key]) |
69 | 69 |
|
70 | 70 | def test_integration_screen_view(self): |
71 | | - t = tracker.Tracker('localhost') |
| 71 | + t = tracker.Tracker("localhost") |
72 | 72 | with HTTMock(pass_response_content): |
73 | | - val = t.track_screen_view('Game HUD 2', 'Hello!') |
74 | | - assertion_array = {'e': 'ue', 'ue_na': 'screen_view'} |
| 73 | + val = t.track_screen_view("Game HUD 2", "Hello!") |
| 74 | + assertion_array = {"e": "ue", "ue_na": "screen_view"} |
75 | 75 | for key in assertion_array: |
76 | 76 | self.assertEquals(from_querystring(key, val), assertion_array[key]) |
77 | 77 |
|
78 | 78 | def test_integration_struct_event(self): |
79 | | - t = tracker.Tracker('localhost') |
| 79 | + t = tracker.Tracker("localhost") |
80 | 80 | with HTTMock(pass_response_content): |
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'} |
| 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 | 83 | for key in assertion_array: |
84 | 84 | self.assertEquals(from_querystring(key, val), assertion_array[key]) |
85 | 85 |
|
86 | 86 |
|
87 | 87 | def test_integration_unstruct_event_non_base64(self): |
88 | | - t = tracker.Tracker('localhost') |
89 | | - t.config['encode_base64'] = False |
| 88 | + t = tracker.Tracker("localhost") |
| 89 | + t.config["encode_base64"] = False |
90 | 90 | with HTTMock(pass_response_content): |
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'} |
| 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 | 93 | for key in assertion_array: |
94 | 94 | self.assertEquals(from_querystring(key, val), assertion_array[key]) |
95 | 95 |
|
96 | 96 | def test_integration_unstruct_event_base64(self): |
97 | | - t = tracker.Tracker('localhost') |
| 97 | + t = tracker.Tracker("localhost") |
98 | 98 | with HTTMock(pass_response_content): |
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'} |
| 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 | 101 | for key in assertion_array: |
102 | 102 | self.assertEquals(from_querystring(key, val), assertion_array[key]) |
103 | 103 |
|
104 | 104 | def test_integration_unstruct_event_non_base64_error(self): |
105 | | - t = tracker.Tracker('localhost') |
106 | | - t.config['encode_base64'] = False |
| 105 | + t = tracker.Tracker("localhost") |
| 106 | + t.config["encode_base64"] = False |
107 | 107 | try: |
108 | | - val = t.track_unstruct_event('viewed_product', |
| 108 | + val = t.track_unstruct_event("viewed_product", |
109 | 109 | { |
110 | | - 'product_id': 'ASO01043', |
111 | | - 'price$flt': 49, # ERROR |
112 | | - 'walrus$tms': int(time.time() * 1000), |
| 110 | + "product_id": "ASO01043", |
| 111 | + "price$flt": 49, # ERROR |
| 112 | + "walrus$tms": int(time.time() * 1000), |
113 | 113 | }) |
114 | 114 | except RuntimeError as e: |
115 | 115 | self.assertEquals("price$flt in dict is not a flt", str(e)) |
116 | 116 |
|
117 | 117 |
|
118 | 118 | def test_integration_unstruct_event_base64_error(self): |
119 | | - t = tracker.Tracker('localhost') |
| 119 | + t = tracker.Tracker("localhost") |
120 | 120 | try: |
121 | | - val = t.track_unstruct_event('viewed_product', |
| 121 | + val = t.track_unstruct_event("viewed_product", |
122 | 122 | { |
123 | | - 'product_id': 'ASO01043', |
124 | | - 'price$flt': 49.95, |
125 | | - 'walrus$tms': 'hello', # ERROR |
| 123 | + "product_id": "ASO01043", |
| 124 | + "price$flt": 49.95, |
| 125 | + "walrus$tms": "hello", # ERROR |
126 | 126 | }) |
127 | 127 | except RuntimeError as e: |
128 | 128 | self.assertEquals("walrus$tms in dict is not a tms", str(e)) |
129 | 129 |
|
130 | 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') |
| 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 | 135 | t.set_screen_resolution(100, 200) |
136 | 136 | t.set_color_depth(24) |
137 | | - t.set_timezone('Europe London') |
138 | | - t.set_lang('en') |
| 137 | + t.set_timezone("Europe London") |
| 138 | + t.set_lang("en") |
139 | 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-0.1.0'} |
| 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-0.1.0"} |
142 | 142 | for key in assertion_array: |
143 | 143 | self.assertEquals(from_querystring(key, val), assertion_array[key]) |
0 commit comments