-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_printfulClient.py
159 lines (124 loc) · 5.01 KB
/
test_printfulClient.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
import unittest
from printful import Printful, \
PrintfulAPIException, PrintfulException
KEY = ''
SAMPLE_ORDER = { 'recipient': {
'name': 'John Doe',
'address1': '172 W Providencia Ave #105',
'city': 'Burbank',
'state_code': 'CA',
'country_code': 'US',
'zip': '91502'
},
'items': [
{
'variant_id': 1, #Small poster
'name': 'Niagara Falls poster', #Display name
'retail_price': '19.99', #Retail price for packing slip
'quantity': 1,
'files': [
{'url': 'http://example.com/files/posters/poster_1.jpg'}
]
},
{
'variant_id': 1118,
'quantity': 2,
'name': 'Grand Canyon T-Shirt', #Display name
'retail_price': '29.99', #Retail price for packing slip
'files': [
{'url': 'http://example.com/files/tshirts/shirt_front.ai'}, #Front print
{'type': 'back', 'url': 'http://example.com/files/tshirts/shirt_back.ai'}, #Back print
{'type': 'preview', 'url': 'http://example.com/files/tshirts/shirt_mockup.jpg'} #Mockup image
],
'options': [ #Additional options
{'id': 'remove_labels', 'value': True}
]
}
]
}
SHIPPING_RATES = { 'recipient': {
'country_code': 'US',
'state_code': 'CA'
},
'items': [
{'variant_id': 1, 'quantity': 1},
{'variant_id': 1118, 'quantity': 2}
]
}
class TestPrintfulClient(unittest.TestCase):
def setUp(self):
self.pf = Printful(KEY)
def test_get_store_info(self):
p = self.pf.get('store')
self.assertEqual(p['code'], 200)
self.assertTrue('result' in p)
def test_get_product_list(self):
p = self.pf.get('products')
self.assertEqual(p['code'], 200)
self.assertTrue('result' in p)
def test_get_variants(self):
"""Get variants for product 10
"""
prod = [('brand','American Apparel'),
('model', '2007 Unisex Fine Jersey Long Sleeve T-Shirt'),
('variant_count', 35)]
p = self.pf.get('products/10')['result']['product']
for k, v in prod:
self.assertEqual(p[k], v)
def test_get_variant_info_1007(self):
"""Get information about Variant 1007
"""
prod = [('brand','American Apparel'),
('type', 'T-SHIRT'),
('variant_count', 35)]
p = self.pf.get('products/variant/1007')['result']['product']
for k, v in prod:
self.assertEqual(p[k], v)
def test_get_last_10_orders(self):
"""Select 10 latest orders and get the total number of orders
"""
p = self.pf.get('orders', params={'offset': 5, 'limit':10})
self.assertEqual(p['code'], 200)
self.assertEqual(p['paging']['limit'], 10)
self.assertEqual(p['paging']['offset'], 5)
def test_get_order_info(self):
"""Select order with ID 12345 (Replace with your order's ID)
"""
p = self.pf.get('orders/12345')
pass
def test_get_order_info_external(self):
"""Select order with External ID 9900999 (Replace with your order's External ID)
"""
p = self.pf.get('orders/@9900999')
pass
def test_confirm_order(self):
"""Confirm order with ID 12345 (Replace with your order's ID)
"""
p = self.pf.post('orders/12345/confirm')
pass
def test_shipping_rates(self):
exp = {'currency': 'USD',
'id': 'STANDARD',
'name': 'Flat Rate (3-8 business days after fulfillment)',
'rate': '14.20'}
p = self.pf.post('shipping/rates', json=SHIPPING_RATES)
self.assertEqual(p['code'], 200)
for k in exp.keys():
self.assertEqual(exp[k], p['result'][0][k])
def test_create_order_and_delete(self):
"""This will create a dummy order and delete afterwards.
"""
# create
p = self.pf.post('orders', json=SAMPLE_ORDER)
self.assertEqual(p['code'], 200)
# delete
order_id = p['result']['id']
p = self.pf.delete('orders/{}'.format(order_id))
def test_raise_bad_request(self):
self.assertRaises(PrintfulAPIException, self.pf.get, 'fakeurl')
def test_item_count(self):
pf = self.pf.get('orders')
item_count = self.pf.item_count()
self.assertTrue(item_count > 0)
if __name__ == '__main__':
unittest.main()