forked from benweatherman/python-ship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
344 lines (299 loc) · 13.6 KB
/
test.py
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
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/python
# -*- coding: utf-8 -*-
try:
from test_config import UPSConfig, EndiciaConfig
from test_config import FedexConfigDebug as FedexConfig
except:
print 'HELLO THERE! test_config not found. If you want to run this test, you need to setup test_config.py with your account information.'
raise
import os, tempfile
from shipping import Package, Address, Product
import sys
sys.path.append('../')
import ups
import endicia
import fedex
shipper = Address('Adobe', "345 Park Avenue", 'San Jose', 'CA', 95110, 'US', phone='5122901212', email='[email protected]')
recipient = Address('Apple', "1 Infinite Loop", 'Cupertino', 'CA', 95014, 'US', phone='5122901212', email='[email protected]')
bad_recipient = Address('Apple', "1 Park Place.", 'San Diego', 'CA', 95014, 'US', phone='5122901212', email='[email protected]', address2='#502')
recipient_intl = Address('Apple', "1 Infinite Loop", 'Cupertino', 'CA', 95014, 'CA', phone='5122901212', email='[email protected]')
recipient_intl2 = Address('Bazaarvoice', 'One Lyric Square', 'London', '', 'W6 0NB', 'GB', phone='+44 (0)208 080', email='[email protected]')
# recipient_intl3 = Address('Bazaarvoice', '111 Rue Francis de Pressensé', 'villeurbanne', '', '69100', 'FR', phone='+44 (0)208 080', email='[email protected]')
recipient_intl4 = Address('Some Recipient', '24 Tennant st', 'EDINBURGH', 'Midlothian', 'EH6 5ND', 'GB', phone='+44 (0)208 080', email='[email protected]')
# recipient_intl5 = Address('Bazaarvoice', 'Flat 3 Gainsborough Court', 'London', '', 'W6 0NB', 'GB', phone='+44 (0)208 080', email='[email protected]')
# recipient_intl6 = Address('Bazaarvoice', 'One Lyric Square', 'London', '', 'W6 0NB', 'GB', phone='+44 (0)208 080', email='[email protected]')
def _show_file(extension, data):
with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as temp_file:
temp_file.write(data)
os.system('open %s' % temp_file.name)
class P(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def TestUPS():
packages = [ Package(20.0 * 16, 12, 12, 12, value=1000, require_signature=3, reference='a12302b') ]
intl_packages = [ Package(2.0 * 16, 12, 12, 12, value=100, require_signature=2, reference='a12302b') ]
products = [
Product(total=10, value=2, quantity=5, description='It\'s just a bunch of widgets', country = 'CA'),
Product(total=10, value=2, quantity=5, description='It\'s just a bunch of widgets', country = 'CA')
]
u = ups.UPS(UPSConfig, debug=True)
for r in ( recipient_intl2, recipient_intl4, ):
try:
validate = True
r.is_residence = True
response = u.label(intl_packages, shipper, r, ups.SERVICES[9][0], ups.PACKAGES[5][0], validate, [ '[email protected] '], create_commercial_invoice=True, customs_info=products)
status = response['status']
print 'Status: %s' % status,
for info in response['shipments']:
print 'tracking: %s, cost: %s' % (info['tracking_number'], info['cost'])
# _show_file(extension='.gif', data=info['label'])
# _show_file(extension='.pdf', data=response['international_document']['pdf'])
except ups.UPSError as e:
print e
for r in ( recipient, shipper, bad_recipient, recipient_intl ):
try:
print u.validate(r)
except ups.UPSError as e:
print e
try:
validate = False
r.is_residence = True
response = u.label(packages, shipper, r, ups.SERVICES[2][0], ups.PACKAGES[7][0], validate, [ '[email protected] '])
status = response['status']
print 'Status: %s' % status,
for info in response['shipments']:
print 'tracking: %s, cost: %s' % (info['tracking_number'], info['cost'])
# _show_file(extension='.gif', data=info['label'])
except ups.UPSError as e:
print e
return
for package_type in ups.PACKAGES:
package_code = package_type[0]
try:
response = u.rate(packages, package_code, shipper, r)
print response
except ups.UPSError as e:
print e
def TestEndiciaLabel():
package = endicia.Package(endicia.Package.shipment_types[0], 20, endicia.Package.shapes[1], 10, 10, 10)
package_intl = endicia.Package(endicia.Package.international_shipment_types[0], 20, endicia.Package.shapes[3], 10, 10, 10)
customs = [ endicia.Customs('hello', 1, 2, 100, 'Bermuda'), endicia.Customs('Thingy', 10, 16, 80, 'Bahamas') ]
debug = True
req0 = endicia.LabelRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, package_intl, shipper, recipient_intl, contents_type='Merchandise', customs_info=customs, debug=debug)
req1 = endicia.LabelRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, package, shipper, recipient, debug=debug)
req2 = endicia.LabelRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, package, shipper, recipient, stealth=False, debug=debug)
req3 = endicia.LabelRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, package, shipper, recipient, insurance='ENDICIA', insurance_amount=1.0, debug=debug)
req4 = endicia.LabelRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, package, shipper, recipient, customs_form='Form2976A', customs_info=customs, contents_type='Merchandise', debug=debug)
for request in [ req0, req1, req2, req3, req4 ]:
response = request.send()
print response
if not isinstance(response, endicia.Error):
_show_file(extension='.png', data=response.label)
return response
def TestEndicia():
debug = True
# TestEndiciaLabel()
# Rate
packages = [ Package(20.0 * 16, 12, 12, 12, value=100) ]
en = endicia.Endicia(EndiciaConfig)
for shape in endicia.Package.shapes:
try:
response = en.rate(packages, shape, shipper, recipient)
print response
except endicia.EndiciaError as e:
print e
# # Account Status
# request = endicia.AccountStatusRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, debug=debug)
# response = request.send()
# print response
# # Recredit
# request = endicia.RecreditRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, 10.00, debug=debug)
# response = request.send()
# print response
# Change Password
# request = endicia.ChangePasswordRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, 'ord7oro', debug=debug)
# response = request.send()
# print response
# Refund
# request = endicia.RefundRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, tracking_number, debug=debug)
# response = request.send()
# print response
def TestFedexGroundCertification():
test_cases = [
(
Address('TC #4', '20 FedEx Parkway', 'Kansas City', 'MO', 64112, 'US', address2='2nd Floor Suite 2001', phone=5152961616, is_residence=False),
fedex.Package(20.0 * 16, 12, 12, 12),
fedex.SERVICES[0],
fedex.PACKAGES[-1],
False, # Email alert
False, # Evening
),
(
Address('TC #20', '456 Roswell Rd', 'Atlanta', 'GA', 30328, 'US', phone=5152961616),
fedex.Package(20.0 * 16, 5, 5, 108),
fedex.SERVICES[1],
fedex.PACKAGES[-1],
False, # Email alert
False, # Evening
),
(
Address('TC #21', '987 Main St', 'Boston', 'MA', '02129', 'US', phone=5152961616),
fedex.Package(20.0 * 16, 5, 5, 108, require_signature=True),
fedex.SERVICES[1],
fedex.PACKAGES[-1],
False, # Email alert
False, # Evening
),
(
Address('TC #23', '321 Jay St', 'New York', 'NY', '11201', 'US', phone=5152961616),
fedex.Package(20.0 * 16, 5, 5, 108, require_signature=True),
fedex.SERVICES[1],
fedex.PACKAGES[-1],
False, # Email alert
True, # Evening
),
(
Address('TC #24', '456 Roswell Rd', 'Atlanta', 'GA', 30328, 'US', phone=5152961616),
fedex.Package(20.0 * 16, 5, 5, 108, require_signature=True),
fedex.SERVICES[1],
fedex.PACKAGES[-1],
False, # Email alert
True, # Evening
),
]
f = fedex.Fedex(FedexConfig)
for case in test_cases:
try:
print case
recipient, package, service, package_type, email, evening = case
packages = [ package ]
response = f.label(packages, package_type, service, shipper, recipient, email, evening=evening)
for info in response['info']:
_show_file(extension='.png', data=info['label'])
except fedex.FedexError as e:
print e
def TestFedexExpressCertification():
test_cases = [
(
Address('TC #1', '123 Bishop Rd', 'Honolulu', 'HI', 96819, 'US', phone=5152961616),
fedex.Package(1.0 * 16, 12, 12, 12),
fedex.SERVICES[5],
fedex.PACKAGES[0],
False, # Email alert
False, # Evening
),
(
Address('TC #4', '789 Davies', 'Englewood', 'CO', 80112, 'US', phone=5152961616),
fedex.Package(1.0 * 16, 12, 12, 12),
fedex.SERVICES[4],
fedex.PACKAGES[0],
False, # Email alert
False, # Evening
),
(
Address('TC #7', '6050 Rockwell Ave', 'Anchorage', 'AK', 99501, 'US', phone=5152961616),
fedex.Package(5.0 * 16, 12, 12, 12),
fedex.SERVICES[3],
fedex.PACKAGES[1],
False, # Email alert
False, # Evening
),
(
Address('TC #8', '44 Binney St', 'Boston', 'MA', 02115, 'US', phone=5152961616),
fedex.Package(1.0 * 16, 12, 12, 12),
fedex.SERVICES[6],
fedex.PACKAGES[0],
False, # Email alert
False, # Evening
),
(
Address('TC #9', '16 Court St', 'New York', 'NY', 10211, 'US', phone=5152961616),
fedex.Package(8.0 * 16, 7, 10, 15),
fedex.SERVICES[6],
fedex.PACKAGES[3],
False, # Email alert
False, # Evening
),
(
Address('TC #10', 'SW 129th St', 'Miami', 'FL', 33156, 'US', phone=5152961616),
fedex.Package(1.0 * 16, 12, 12, 12),
fedex.SERVICES[2],
fedex.PACKAGES[0],
False, # Email alert
False, # Evening
),
(
Address('TC #11', '36 Charles Lane', 'Baltimore', 'MD', 21201, 'US', phone=5152961616),
fedex.Package(150 * 16, 10, 10, 15),
fedex.SERVICES[2],
fedex.PACKAGES[3],
False, # Email alert
False, # Evening
),
]
f = fedex.Fedex(FedexConfig)
for case in test_cases:
try:
print case
recipient, package, service, package_type, email, evening = case
packages = [ package ]
response = f.label(packages, package_type, service, shipper, recipient, email, evening=evening)
for info in response['info']:
_show_file(extension='.png', data=info['label'])
except fedex.FedexError as e:
print e
def TestFedexProd():
test_cases = [
(
Address('TC #4', '20 FedEx Parkway', 'Kansas City', 'MO', 64112, 'US', address2='2nd Floor Suite 2001', phone=5152961616, is_residence=False),
fedex.Package(20.0 * 16, 12, 12, 12),
fedex.SERVICES[0],
fedex.PACKAGES[-1],
False, # Email alert
False, # Evening
),
]
from test_config import FedexConfigProd
f = fedex.Fedex(FedexConfigProd, debug=False)
for case in test_cases:
try:
print case
recipient, package, service, package_type, email, evening = case
packages = [ package ]
response = f.label(packages, package_type, service, shipper, recipient, email, evening=evening)
for info in response['info']:
_show_file(extension='.png', data=info['label'])
except fedex.FedexError as e:
print e
def TestFedex():
f = fedex.Fedex(FedexConfig)
packages = [
fedex.Package(100, 12, 12, 12, 100.0, dry_ice_weight_in_ozs=54.27),
]
for service in fedex.SERVICES:
for package_type in fedex.PACKAGES:
try:
print service, package_type,
response = f.label(packages, package_type, service, shipper, recipient, True)
status = response['status']
print 'Status: %s' % status,
for shipment_info in response['shipments']:
print 'tracking: %s, cost: %s' % (shipment_info['tracking_number'], shipment_info['cost'])
# _show_file(extension='.png', data=info['label'])
except fedex.FedexError as e:
print e
for package_type in fedex.PACKAGES:
try:
response = f.rate(packages, package_type, shipper, recipient)
print response
except fedex.FedexError as e:
print e
if __name__ == '__main__':
TestUPS()
#TestUSPS()
# TestEndicia()
#TestFedex()
#TestFedexGroundCertification()
#TestFedexExpressCertification()
#TestFedexProd()