|
2 | 2 | from api import Api
|
3 | 3 | from attribute import Attribute
|
4 | 4 | from attribute_manager import AttributeManager
|
5 |
| -from httmock import HTTMock, all_requests |
| 5 | +from httmock import HTTMock, all_requests, urlmatch |
6 | 6 | from requests import HTTPError
|
7 | 7 |
|
8 | 8 |
|
9 | 9 | class ContactManagerTest(unittest.TestCase):
|
10 | 10 | def setUp(self):
|
11 | 11 | gan_token = 'h027MapNNujPH0gV+sXAdmzZTDffHOpJEHaBtrD3NXtNqI4dT3NLXhyTwiZr7PUOGZJNSGv/b9xVyaguX0nDrONGhudPkxtl5EoXrM4SOZHswebpSy2ehh0edrGVF7dVJVZLIlRwgViY3n3/2hMQ5Njp9JFywnOy7gMeaoKw0hYLRbd+wVqvl2oOnspXwGTTcZ9Y+cdP8jIhUUoXOieXst0IXVclAHXa+K1d15gKLcpmXzK+jx14wGEmb4t8MSU'
|
12 | 12 | self.api = Api(token=gan_token)
|
| 13 | + self.api.batch_size = 2 |
13 | 14 | self.attribute_manager = AttributeManager(self.api)
|
14 | 15 | self.start_path = '/v3'
|
15 | 16 |
|
@@ -122,3 +123,44 @@ def test_get_paginated_attributes(self):
|
122 | 123 | self.assertEqual(len(paginated_result_set.entities), 6)
|
123 | 124 |
|
124 | 125 | self.assertRaises(StopIteration, paginated_result_set.next)
|
| 126 | + |
| 127 | + @all_requests |
| 128 | + def get_all_attr_mock(self, url, request): |
| 129 | + status_code = 200 |
| 130 | + if url.query == 'paginate_by=2': |
| 131 | + content = '{"count":8,"next":"https://api.getanewsletter.com/v3/attributes/?page=2&paginate_by=2","previous":null,"results":[{"url":"https://api.getanewsletter.com/v3/attributes/attribute/","name":"attr0","code":"attribute","usage_count":0},{"url":"https://api.getanewsletter.com/v3/attributes/attribute2/","name":"attr1","code":"attribute2","usage_count":0}]}' |
| 132 | + elif url.query in ['page=2&paginate_by=2'] : |
| 133 | + content = '{"count":8,"next":"https://api.getanewsletter.com/v3/attributes/?page=3&paginate_by=2","previous":"https://api.getanewsletter.com/v3/attributes/?paginate_by=2","results":[{"url":"https://api.getanewsletter.com/v3/attributes/bu/","name":"attr2","code":"bu","usage_count":0},{"url":"https://api.getanewsletter.com/v3/attributes/bu1/","name":"attr3","code":"bu1","usage_count":0}]}' |
| 134 | + |
| 135 | + elif url.query == 'page=3&paginate_by=2' or url.query == 'paginate_by=2&page=3': |
| 136 | + content = '{"count":8,"next":"https://api.getanewsletter.com/v3/attributes/?page=4&paginate_by=2","previous":"https://api.getanewsletter.com/v3/attributes/?page=2&paginate_by=2","results":[{"url":"https://api.getanewsletter.com/v3/attributes/bu2/","name":"attr4","code":"bu2","usage_count":0},{"url":"https://api.getanewsletter.com/v3/attributes/bu3/","name":"bu3","code":"bu3","usage_count":0}]}' |
| 137 | + |
| 138 | + elif url.query == 'page=4&paginate_by=2': |
| 139 | + content = '{"count":8,"next":"https://api.getanewsletter.com/v3/attributes/?page=5&paginate_by=2","previous":"https://api.getanewsletter.com/v3/attributes/?page=3&paginate_by=2","results":[{"url":"https://api.getanewsletter.com/v3/attributes/bu4/","name":"bu4","code":"bu4","usage_count":0},{"url":"https://api.getanewsletter.com/v3/attributes/bu5/","name":"bu5","code":"bu5","usage_count":0}]}' |
| 140 | + else: |
| 141 | + return {'status_code': 404} |
| 142 | + |
| 143 | + return {'content': content, |
| 144 | + 'status_code': status_code} |
| 145 | + |
| 146 | + def test_get_all_attributes(self): |
| 147 | + with HTTMock(self.get_all_attr_mock): |
| 148 | + all_attributes = self.attribute_manager.all() |
| 149 | + attrs = [attr for attr in all_attributes] |
| 150 | + self.assertEqual(len(attrs), 8) |
| 151 | + |
| 152 | + def test_get_between_attributes(self): |
| 153 | + with HTTMock(self.get_all_attr_mock): |
| 154 | + self.assertEqual(self.api.batch_size, 2) |
| 155 | + # should get entities from page 3 and 4 |
| 156 | + attributes_between = self.attribute_manager.all(start=4, stop=7) |
| 157 | + attrs = [attr for attr in attributes_between] |
| 158 | + self.assertEqual(len(attrs), 4) |
| 159 | + self.assertEqual(attrs[0].name, 'attr4') |
| 160 | + self.assertTrue(isinstance(attrs[0], Attribute)) |
| 161 | + |
| 162 | + def test_get_too_high_start(self): |
| 163 | + with HTTMock(self.get_all_attr_mock): |
| 164 | + attributes_between = self.attribute_manager.all(start=9) |
| 165 | + attrs = [attr for attr in attributes_between] |
| 166 | + self.assertEqual(len(attrs), 0) |
0 commit comments