Skip to content

Commit 254839b

Browse files
committed
Removed tests that didn't work, changed to double quotes for consistency, and added author
1 parent bf070a3 commit 254839b

5 files changed

Lines changed: 80 additions & 102 deletions

File tree

snowplow_tracker/payload.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
express or implied. See the Apache License Version 2.0 for the specific
1515
language governing permissions and limitations there under.
1616
17-
Authors: Anuj More, Alex Dean
17+
Authors: Anuj More, Alex Dean, Fred Blundun
1818
Copyright: Copyright (c) 2013-2014 Snowplow Analytics Ltd
1919
License: Apache License Version 2.0
2020
"""
@@ -36,7 +36,7 @@ def __init__(self, tstamp=None, dict_=None):
3636

3737
self.context = {}
3838
# Set transaction for every event
39-
self.context['tid'] = Payload.set_transaction_id()
39+
self.context["tid"] = Payload.set_transaction_id()
4040
# Set timestamp for every event
4141
self.set_timestamp(tstamp)
4242
if dict_ is not None:
@@ -67,7 +67,7 @@ def set_timestamp(self, tstamp=None):
6767
value = int(tstamp * 1000)
6868
else:
6969
value = tstamp
70-
self.context['dtm'] = value
70+
self.context["dtm"] = value
7171

7272
"""
7373
Payload creators
@@ -77,7 +77,7 @@ def add(self, name, value):
7777
"""
7878
Add a name value pair to the Payload object
7979
"""
80-
if not (value == '' or value is None):
80+
if not (value == "" or value is None):
8181
self.context[name] = value
8282

8383
@contract
@@ -110,7 +110,7 @@ def add_unstruct(self, dict_, encode_base64,
110110
:type type_when_not_encoded: str
111111
"""
112112
def raise_error(f, type_):
113-
raise RuntimeError(''.join([f, " in dict is not a ", type_]))
113+
raise RuntimeError("".join([f, " in dict is not a ", type_]))
114114

115115
types = ["int", "flt", "geo", "dt", "ts", "tms"]
116116

@@ -128,7 +128,7 @@ def raise_error(f, type_):
128128
json_dict = json.dumps(dict_)
129129

130130
if encode_base64:
131-
self.add(type_when_encoded, base64.urlsafe_b64encode(json_dict.encode('ascii')))
131+
self.add(type_when_encoded, base64.urlsafe_b64encode(json_dict.encode("ascii")))
132132
else:
133133
self.add(type_when_not_encoded, json_dict)
134134

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'''
1+
"""
22
test_integration.py
33
44
Copyright (c) 2013-2014 Snowplow Analytics Ltd. All rights reserved.
@@ -10,14 +10,14 @@
1010
1111
Unless required by applicable law or agreed to in writing,
1212
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
1414
express or implied. See the Apache License Version 2.0 for the specific
1515
language governing permissions and limitations there under.
1616
17-
Authors: Anuj More, Alex Dean
17+
Authors: Anuj More, Alex Dean, Fred Blundun
1818
Copyright: Copyright (c) 2013-2014 Snowplow Analytics Ltd
1919
License: Apache License Version 2.0
20-
'''
20+
"""
2121

2222
import unittest
2323
import time
@@ -26,118 +26,118 @@
2626
from httmock import all_requests, HTTMock
2727

2828
def from_querystring(field, url):
29-
pattern = re.compile('^[^#]*[?&]' + field + '=([^&#]*)')
29+
pattern = re.compile("^[^#]*[?&]" + field + "=([^&#]*)")
3030
match = pattern.match(url)
3131
if match:
3232
return match.groups()[0]
3333

3434
@all_requests
3535
def pass_response_content(url, request):
3636
return {
37-
'url': request.url,
38-
'status_code': 200
37+
"url": request.url,
38+
"status_code": 200
3939
}
4040

4141
@all_requests
4242
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"
4444

4545

4646
class IntegrationTest(unittest.TestCase):
4747

4848
def test_integration_page_view(self):
49-
t = tracker.Tracker('localhost')
49+
t = tracker.Tracker("localhost")
5050
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")
5353

5454
def test_integration_ecommerce_transaction(self):
55-
t = tracker.Tracker('localhost')
55+
t = tracker.Tracker("localhost")
5656
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"}
5959
for key in assertion_array:
6060
self.assertEquals(from_querystring(key, val), assertion_array[key])
6161

6262
def test_integration_ecommerce_transaction_item(self):
63-
t = tracker.Tracker('localhost')
63+
t = tracker.Tracker("localhost")
6464
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"}
6767
for key in assertion_array:
6868
self.assertEquals(from_querystring(key, val), assertion_array[key])
6969

7070
def test_integration_screen_view(self):
71-
t = tracker.Tracker('localhost')
71+
t = tracker.Tracker("localhost")
7272
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"}
7575
for key in assertion_array:
7676
self.assertEquals(from_querystring(key, val), assertion_array[key])
7777

7878
def test_integration_struct_event(self):
79-
t = tracker.Tracker('localhost')
79+
t = tracker.Tracker("localhost")
8080
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"}
8383
for key in assertion_array:
8484
self.assertEquals(from_querystring(key, val), assertion_array[key])
8585

8686

8787
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
9090
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"}
9393
for key in assertion_array:
9494
self.assertEquals(from_querystring(key, val), assertion_array[key])
9595

9696
def test_integration_unstruct_event_base64(self):
97-
t = tracker.Tracker('localhost')
97+
t = tracker.Tracker("localhost")
9898
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"}
101101
for key in assertion_array:
102102
self.assertEquals(from_querystring(key, val), assertion_array[key])
103103

104104
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
107107
try:
108-
val = t.track_unstruct_event('viewed_product',
108+
val = t.track_unstruct_event("viewed_product",
109109
{
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),
113113
})
114114
except RuntimeError as e:
115115
self.assertEquals("price$flt in dict is not a flt", str(e))
116116

117117

118118
def test_integration_unstruct_event_base64_error(self):
119-
t = tracker.Tracker('localhost')
119+
t = tracker.Tracker("localhost")
120120
try:
121-
val = t.track_unstruct_event('viewed_product',
121+
val = t.track_unstruct_event("viewed_product",
122122
{
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
126126
})
127127
except RuntimeError as e:
128128
self.assertEquals("walrus$tms in dict is not a tms", str(e))
129129

130130
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")
135135
t.set_screen_resolution(100, 200)
136136
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")
139139
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"}
142142
for key in assertion_array:
143143
self.assertEquals(from_querystring(key, val), assertion_array[key])

snowplow_tracker/test/unit/test_payload.py

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
express or implied. See the Apache License Version 2.0 for the specific
1515
language governing permissions and limitations there under.
1616
17-
Authors: Anuj More, Alex Dean
17+
Authors: Anuj More, Alex Dean, Fred Blundun
1818
Copyright: Copyright (c) 2013-2014 Snowplow Analytics Ltd
1919
License: Apache License Version 2.0
2020
"""
@@ -52,8 +52,8 @@ def test_object_generation(self):
5252
self.assertTrue(is_subset({}, p.context))
5353

5454
def test_object_generation_2(self):
55-
p = payload.Payload(None, {'test1': 'result1', 'test2': 'result2', })
56-
output = {'test1': 'result1', 'test2': 'result2'}
55+
p = payload.Payload(None, {"test1": "result1", "test2": "result2", })
56+
output = {"test1": "result1", "test2": "result2"}
5757
self.assertTrue(is_subset(output, p.context))
5858

5959
def test_add(self):
@@ -71,62 +71,40 @@ def test_add_dict(self):
7171

7272
def test_get_transaction_id(self):
7373
p = payload.Payload()
74-
self.assertTrue(p.context['tid'] >= 100000 and
75-
p.context['tid'] <= 999999)
74+
self.assertTrue(p.context["tid"] >= 100000 and
75+
p.context["tid"] <= 999999)
7676

7777
@freeze_time("1970-01-01 00:00:01")
7878
def test_get_timestamp(self):
7979
p = payload.Payload()
80-
self.assertTrue(p.context['dtm'] == 1000) # 1970-01-01 00:00:01 in ms
80+
self.assertTrue(p.context["dtm"] == 1000) # 1970-01-01 00:00:01 in ms
8181

8282
def test_set_timestamp(self):
8383
p = payload.Payload()
8484
p.set_timestamp(0)
85-
self.assertEquals(p.context['dtm'], 0)
85+
self.assertEquals(p.context["dtm"], 0)
8686

8787
def test_set_timestamp(self):
8888
p = payload.Payload()
8989
p.set_timestamp(12345654321)
90-
self.assertEquals(p.context['dtm'], 12345654321000)
90+
self.assertEquals(p.context["dtm"], 12345654321000)
9191

9292
def test_add_unstruct_1(self):
9393
p = payload.Payload()
9494
try:
95-
p.add_unstruct({'product_id': 'ASO01043',
96-
'price$flt': 33, # ERROR
97-
'walrus$tms': int(time.time() * 1000),
95+
p.add_unstruct({"product_id": "ASO01043",
96+
"price$flt": 33, # ERROR
97+
"walrus$tms": int(time.time() * 1000),
9898
}, False, "ue_px", "ue_pe")
9999
except RuntimeError as e:
100100
self.assertEquals("price$flt in dict is not a flt", str(e))
101101

102102
def test_add_unstruct_2(self):
103103
p = payload.Payload()
104104
try:
105-
p.add_unstruct({'product_id': 'ASO01043',
106-
'price$flt': 33.3,
107-
'walrus$tms': 'hello world!', # ERROR
105+
p.add_unstruct({"product_id": "ASO01043",
106+
"price$flt": 33.3,
107+
"walrus$tms": "hello world!", # ERROR
108108
}, True, "ue_px", "ue_pe")
109109
except RuntimeError as e:
110110
self.assertEquals("walrus$tms in dict is not a tms", str(e))
111-
112-
"""
113-
DO NOT TRUST THESE TESTS. THE OUTPUT DEPENDS ON THE ORDERING OF THE
114-
ELEMENTS IN THE DICT!
115-
def test_add_unstruct_3(self):
116-
p = payload.Payload()
117-
p.add_unstruct({'product_id': 'ASO01043',
118-
'price$flt': 33.3,
119-
'walrus$tms': 1388494669990,
120-
}, True, "ue_px", "ue_pe")
121-
self.assertTrue(p.context['ue_px'], b'eyJwcmljZSRmbHQiOiAzMy4zLCAicHJvZHVjdF9pZCI6ICJBU08wMTA0MyIsICJ3YWxydXMkdG1zIjogMTM4ODQ5NDI2MDAxOH0=')
122-
123-
def test_add_unstruct_4(self):
124-
p = payload.Payload()
125-
p.add_unstruct({'product_id': 'ASO01043',
126-
'price$flt': 33.3,
127-
'walrus$tms': 1388494669990,
128-
}, False, "ue_px", "ue_pe")
129-
print(p.context['ue_pe'])
130-
self.assertEquals(p.context['ue_pe'],
131-
'{"walrus$tms": 1388494669990, "price$flt": 33.3, "product_id": "ASO01043"}')
132-
"""

snowplow_tracker/test/unit/test_tracker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
express or implied. See the Apache License Version 2.0 for the specific
1515
language governing permissions and limitations there under.
1616
17-
Authors: Anuj More, Alex Dean
17+
Authors: Anuj More, Alex Dean, Fred Blundun
1818
Copyright: Copyright (c) 2013-2014 Snowplow Analytics Ltd
1919
License: Apache License Version 2.0
2020
"""

0 commit comments

Comments
 (0)