forked from aweber/AWeber-API-Python-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_aweber_entry.py
More file actions
296 lines (210 loc) · 10.2 KB
/
test_aweber_entry.py
File metadata and controls
296 lines (210 loc) · 10.2 KB
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
import re
from unittest import TestCase
from urllib import urlencode
from aweber_api import AWeberAPI, AWeberCollection, AWeberEntry
from aweber_api.base import APIException
from mock_adapter import MockAdapter
class TestAWeberEntry(TestCase):
def setUp(self):
self.aweber = AWeberAPI('1', '2')
self.aweber.adapter = MockAdapter()
self.list = self.aweber.load_from_url('/accounts/1/lists/303449')
def test_should_be_an_entry(self):
self.assertEqual(type(self.list), AWeberEntry)
self.assertEqual(self.list.type, 'list')
def test_should_have_id(self):
self.assertEqual(self.list.id, 303449)
def test_should_have_other_properties(self):
self.assertEqual(self.list.name, 'default303449')
def test_should_have_child_collections(self):
campaigns = self.list.campaigns
self.assertEqual(type(campaigns), AWeberCollection)
def test_findSubscribers_should_handle_errors(self):
account = self.aweber.load_from_url('/accounts/1')
self.assertRaises(APIException, account.findSubscribers, name='bob')
class AccountTestCase(TestCase):
def setUp(self):
self.aweber = AWeberAPI('1', '2')
self.aweber.adapter = MockAdapter()
self.account = self.aweber.load_from_url('/accounts/1')
class TestAWeberAccountEntry(AccountTestCase):
def test_should_be_an_entry(self):
self.assertEqual(type(self.account), AWeberEntry)
self.assertEqual(self.account.type, 'account')
class TestAccountGetWebForms(AccountTestCase):
def setUp(self):
super(TestAccountGetWebForms, self).setUp()
self.forms = self.account.get_web_forms()
def test_should_be_a_list(self):
self.assertEqual(type(self.forms), list)
def test_should_have_181_web_forms(self):
self.assertEqual(len(self.forms), 181)
def test_each_should_be_entry(self):
for entry in self.forms:
self.assertEqual(type(entry), AWeberEntry)
self.assertEqual(entry.type, 'web_form')
def test_each_should_have_correct_url(self):
url_regex = '/accounts\/1\/lists\/\d*/web_forms/\d*'
for entry in self.forms:
self.assertTrue(re.match(url_regex, entry.url))
class TestAccountGetWebFormSplitTests(AccountTestCase):
def setUp(self):
super(TestAccountGetWebFormSplitTests, self).setUp()
self.forms = self.account.get_web_form_split_tests()
def test_should_be_a_list(self):
self.assertEqual(type(self.forms), list)
def test_should_have_10_split_tests(self):
self.assertEqual(len(self.forms), 10)
def test_each_should_be_entry(self):
for entry in self.forms:
self.assertEqual(type(entry), AWeberEntry)
self.assertEqual(entry.type, 'web_form_split_test')
def test_each_should_have_correct_url(self):
url_regex = '/accounts\/1\/lists\/\d*/web_form_split_tests/\d*'
for entry in self.forms:
self.assertTrue(re.match(url_regex, entry.url))
class TestAccountFindSubscribers(AccountTestCase):
def test_should_support_find_method(self):
base_url = '/accounts/1'
account = self.aweber.load_from_url(base_url)
self.aweber.adapter.requests = []
request = self.aweber.adapter.requests[0]
assert subscribers != False
assert isinstance(subscribers, AWeberCollection)
assert len(subscribers) == 1
assert subscribers[0].self_link == \
'https://api.aweber.com/1.0/accounts/1/lists/303449/subscribers/1'
class SubscriberTestCase(TestCase):
def setUp(self):
self.aweber = AWeberAPI('1', '2')
self.aweber.adapter = MockAdapter()
sub_url = '/accounts/1/lists/303449/subscribers/1'
self.subscriber = self.aweber.load_from_url(sub_url)
class TestGetAndSetData(SubscriberTestCase):
def test_get_name(self):
self.assertEqual(self.subscriber.name, 'Joe Jones')
def test_set_name(self):
self.subscriber.name = 'Randy Rhodes'
self.assertEqual(self.subscriber.name, 'Randy Rhodes')
def test_get_custom_fields(self):
fields = self.subscriber.custom_fields
self.assertEqual(fields['Color'], 'blue')
def test_set_custom_fields(self):
self.subscriber.custom_fields['Color'] = 'Red'
self.assertEqual(self.subscriber._data['custom_fields']['Color'], 'Red')
fields = self.subscriber.custom_fields
self.assertEqual(fields['Color'], 'Red')
def test_should_be_able_get_activity(self):
activity = self.subscriber.get_activity()
class TestMovingSubscribers(TestCase):
def setUp(self):
self.aweber = AWeberAPI('1', '2')
self.aweber.adapter = MockAdapter()
subscriber_url = '/accounts/1/lists/303449/subscribers/1'
new_list_url = '/accounts/1/lists/505454'
self.subscriber = self.aweber.load_from_url(subscriber_url)
self.subscriber._diff['name'] = 'Joe Schmoe'
self.list = self.aweber.load_from_url(new_list_url)
self.move_subscriber()
def move_subscriber(self, **kwargs):
self.aweber.adapter.requests = []
self.resp = self.subscriber.move(self.list, **kwargs)
self.move_req = self.aweber.adapter.requests[0]
self.get_req = self.aweber.adapter.requests[1]
def test_returned_true(self):
self.assertTrue(self.resp)
def test_should_have_requested_move_with_post(self):
self.assertEqual(self.move_req['method'], 'POST')
def test_should_have_requested_move_on_subscriber(self):
self.assertEqual(self.move_req['url'] , self.subscriber.url)
def test_should_have_requested_move_with_correct_parameters(self):
expected_params = {'ws.op': 'move', 'list_link': self.list.self_link}
self.assertEqual(self.move_req['data'], expected_params)
def test_should_make_two_requests(self):
self.assertEqual(len(self.aweber.adapter.requests), 2)
def test_should_refresh_subscriber_resource(self):
self.assertEqual(self.get_req['method'], 'GET')
self.assertEqual(self.get_req['url'] ,
'/accounts/1/lists/505454/subscribers/3')
def test_should_reset_diff(self):
self.assertEqual(self.subscriber._diff, {})
def test_should_accept_last_followup_message_number_sent(self):
self.move_subscriber(last_followup_message_number_sent=999)
expected_params = {'ws.op': 'move', 'list_link': self.list.self_link,
'last_followup_message_number_sent': 999}
self.assertEqual(self.move_req['data'], expected_params)
class TestSavingSubscriberData(SubscriberTestCase):
def setUp(self):
super(TestSavingSubscriberData, self).setUp()
self.aweber.adapter.requests = []
self.subscriber.name = 'Gary Oldman'
self.subscriber.custom_fields['Color'] = 'Red'
self.resp = self.subscriber.save()
self.req = self.aweber.adapter.requests[0]
def test_returned_true(self):
self.assertTrue(self.resp)
def test_should_make_request(self):
self.assertEqual(len(self.aweber.adapter.requests), 1)
def test_should_have_requested_resource_url(self):
self.assertEqual(self.req['url'] , self.subscriber.url)
def test_should_have_requested_with_patch(self):
self.assertEqual(self.req['method'], 'PATCH')
def test_should_have_supplied_data(self):
self.assertEqual(self.req['data']['name'], 'Gary Oldman')
def test_should_not_include_unchanged_data(self):
self.assertFalse('email' in self.req['data'])
def test_should_given_all_custom_fields(self):
# Make changed, Model did not
self.assertEqual(self.req['data']['custom_fields']['Color'], 'Red')
self.assertEqual(self.req['data']['custom_fields']['Walruses'], '')
class TestSavingInvalidSubscriberData(TestCase):
def setUp(self):
self.aweber = AWeberAPI('1', '2')
self.aweber.adapter = MockAdapter()
sub_url = '/accounts/1/lists/303449/subscribers/2'
self.subscriber = self.aweber.load_from_url(sub_url)
self.subscriber.name = 'Gary Oldman'
self.subscriber.custom_fields['New Custom Field'] = 'Cookies'
def test_save_failed(self):
self.assertRaises(APIException, self.subscriber.save)
class TestDeletingSubscriberData(SubscriberTestCase):
def setUp(self):
super(TestDeletingSubscriberData, self).setUp()
self.aweber.adapter.requests = []
self.response = self.subscriber.delete()
self.req = self.aweber.adapter.requests[0]
def test_should_be_deleted(self):
self.assertTrue(self.response)
def test_should_have_made_request(self):
self.assertEqual(len(self.aweber.adapter.requests), 1)
def test_should_have_made_delete(self):
self.assertEqual(self.req['method'], 'DELETE')
class TestFailedSubscriberDelete(TestCase):
def setUp(self):
self.aweber = AWeberAPI('1', '2')
self.aweber.adapter = MockAdapter()
sub_url = '/accounts/1/lists/303449/subscribers/2'
self.subscriber = self.aweber.load_from_url(sub_url)
def test_should_raise_exception_when_failing(self):
self.assertRaises(APIException, self.subscriber.delete)
class TestGettingParentEntry(TestCase):
def setUp(self):
self.aweber = AWeberAPI('1', '2')
self.aweber.adapter = MockAdapter()
self.list = self.aweber.load_from_url('/accounts/1/lists/303449')
self.account = self.aweber.load_from_url('/accounts/1')
self.custom_field = self.aweber.load_from_url('/accounts/1/lists/303449/custom_fields/1')
def test_should_be_able_get_parent_entry(self):
entry = self.list.get_parent_entry()
def test_list_parent_should_be_account(self):
entry = self.list.get_parent_entry()
self.assertEqual(type(entry), AWeberEntry)
self.assertEqual(entry.type, 'account')
def test_custom_field_parent_should_be_list(self):
entry = self.custom_field.get_parent_entry()
self.assertEqual(type(entry), AWeberEntry)
self.assertEqual(entry.type, 'list')
def test_account_parent_should_be_none(self):
entry = self.account.get_parent_entry()
self.assertEqual(entry, None)