Mercurial > p > roundup > code
comparison test/test_rest.py @ 5592:adcb5cbe82bd REST-rebased
Add unittest for pagination and filtering
committer: Ralf Schlatterbeck <rsc@runtux.com>
| author | Chau Nguyen <dangchau1991@yahoo.com> |
|---|---|
| date | Wed, 30 Jan 2019 10:26:35 +0100 |
| parents | a25d79e874cb |
| children | 65caddd54da2 |
comparison
equal
deleted
inserted
replaced
| 5591:a25d79e874cb | 5592:adcb5cbe82bd |
|---|---|
| 6 from roundup.cgi.exceptions import * | 6 from roundup.cgi.exceptions import * |
| 7 from roundup import password, hyperdb | 7 from roundup import password, hyperdb |
| 8 from roundup.rest import RestfulInstance | 8 from roundup.rest import RestfulInstance |
| 9 from roundup.backends import list_backends | 9 from roundup.backends import list_backends |
| 10 from roundup.cgi import client | 10 from roundup.cgi import client |
| 11 import random | |
| 11 | 12 |
| 12 import db_test_base | 13 import db_test_base |
| 13 | 14 |
| 14 NEEDS_INSTANCE = 1 | 15 NEEDS_INSTANCE = 1 |
| 15 | 16 |
| 91 'user', self.joeid, 'username', self.empty_form | 92 'user', self.joeid, 'username', self.empty_form |
| 92 ) | 93 ) |
| 93 self.assertEqual(self.dummy_client.response_code, 200) | 94 self.assertEqual(self.dummy_client.response_code, 200) |
| 94 self.assertEqual(results['data']['data'], 'joe') | 95 self.assertEqual(results['data']['data'], 'joe') |
| 95 | 96 |
| 97 def testFilter(self): | |
| 98 """ | |
| 99 Retrieve all three users | |
| 100 obtain data for 'joe' | |
| 101 """ | |
| 102 # create sample data | |
| 103 try: | |
| 104 self.db.status.create(name='open') | |
| 105 except ValueError: | |
| 106 pass | |
| 107 try: | |
| 108 self.db.status.create(name='closed') | |
| 109 except ValueError: | |
| 110 pass | |
| 111 try: | |
| 112 self.db.priority.create(name='normal') | |
| 113 except ValueError: | |
| 114 pass | |
| 115 try: | |
| 116 self.db.priority.create(name='critical') | |
| 117 except ValueError: | |
| 118 pass | |
| 119 self.db.issue.create( | |
| 120 title='foo4', | |
| 121 status=self.db.status.lookup('closed'), | |
| 122 priority=self.db.priority.lookup('critical') | |
| 123 ) | |
| 124 self.db.issue.create( | |
| 125 title='foo1', | |
| 126 status=self.db.status.lookup('open'), | |
| 127 priority=self.db.priority.lookup('normal') | |
| 128 ) | |
| 129 issue_open_norm = self.db.issue.create( | |
| 130 title='foo2', | |
| 131 status=self.db.status.lookup('open'), | |
| 132 priority=self.db.priority.lookup('normal') | |
| 133 ) | |
| 134 issue_closed_norm = self.db.issue.create( | |
| 135 title='foo3', | |
| 136 status=self.db.status.lookup('closed'), | |
| 137 priority=self.db.priority.lookup('normal') | |
| 138 ) | |
| 139 issue_closed_crit = self.db.issue.create( | |
| 140 title='foo4', | |
| 141 status=self.db.status.lookup('closed'), | |
| 142 priority=self.db.priority.lookup('critical') | |
| 143 ) | |
| 144 issue_open_crit = self.db.issue.create( | |
| 145 title='foo5', | |
| 146 status=self.db.status.lookup('open'), | |
| 147 priority=self.db.priority.lookup('critical') | |
| 148 ) | |
| 149 base_path = self.dummy_client.env['PATH_INFO'] + 'issue' | |
| 150 | |
| 151 # Retrieve all issue status=open | |
| 152 form = cgi.FieldStorage() | |
| 153 form.list = [ | |
| 154 cgi.MiniFieldStorage('where_status', 'open') | |
| 155 ] | |
| 156 results = self.server.get_collection('issue', form) | |
| 157 self.assertEqual(self.dummy_client.response_code, 200) | |
| 158 self.assertIn(get_obj(base_path, issue_open_norm), results['data']) | |
| 159 self.assertIn(get_obj(base_path, issue_open_crit), results['data']) | |
| 160 self.assertNotIn( | |
| 161 get_obj(base_path, issue_closed_norm), results['data'] | |
| 162 ) | |
| 163 | |
| 164 # Retrieve all issue status=closed and priority=critical | |
| 165 form = cgi.FieldStorage() | |
| 166 form.list = [ | |
| 167 cgi.MiniFieldStorage('where_status', 'closed'), | |
| 168 cgi.MiniFieldStorage('where_priority', 'critical') | |
| 169 ] | |
| 170 results = self.server.get_collection('issue', form) | |
| 171 self.assertEqual(self.dummy_client.response_code, 200) | |
| 172 self.assertIn(get_obj(base_path, issue_closed_crit), results['data']) | |
| 173 self.assertNotIn(get_obj(base_path, issue_open_crit), results['data']) | |
| 174 self.assertNotIn( | |
| 175 get_obj(base_path, issue_closed_norm), results['data'] | |
| 176 ) | |
| 177 | |
| 178 # Retrieve all issue status=closed and priority=normal,critical | |
| 179 form = cgi.FieldStorage() | |
| 180 form.list = [ | |
| 181 cgi.MiniFieldStorage('where_status', 'closed'), | |
| 182 cgi.MiniFieldStorage('where_priority', 'normal,critical') | |
| 183 ] | |
| 184 results = self.server.get_collection('issue', form) | |
| 185 self.assertEqual(self.dummy_client.response_code, 200) | |
| 186 self.assertIn(get_obj(base_path, issue_closed_crit), results['data']) | |
| 187 self.assertIn(get_obj(base_path, issue_closed_norm), results['data']) | |
| 188 self.assertNotIn(get_obj(base_path, issue_open_crit), results['data']) | |
| 189 self.assertNotIn(get_obj(base_path, issue_open_norm), results['data']) | |
| 190 | |
| 191 def testPagination(self): | |
| 192 """ | |
| 193 Retrieve all three users | |
| 194 obtain data for 'joe' | |
| 195 """ | |
| 196 # create sample data | |
| 197 for i in range(0, random.randint(5, 10)): | |
| 198 self.db.issue.create(title='foo' + str(i)) | |
| 199 | |
| 200 # Retrieving all the issues | |
| 201 results = self.server.get_collection('issue', self.empty_form) | |
| 202 self.assertEqual(self.dummy_client.response_code, 200) | |
| 203 total_length = len(results['data']) | |
| 204 | |
| 205 # Pagination will be 70% of the total result | |
| 206 page_size = total_length * 70 // 100 | |
| 207 page_zero_expected = page_size | |
| 208 page_one_expected = total_length - page_zero_expected | |
| 209 | |
| 210 # Retrieve page 0 | |
| 211 form = cgi.FieldStorage() | |
| 212 form.list = [ | |
| 213 cgi.MiniFieldStorage('page_size', page_size), | |
| 214 cgi.MiniFieldStorage('page_index', 0) | |
| 215 ] | |
| 216 results = self.server.get_collection('issue', form) | |
| 217 self.assertEqual(self.dummy_client.response_code, 200) | |
| 218 self.assertEqual(len(results['data']), page_zero_expected) | |
| 219 | |
| 220 # Retrieve page 1 | |
| 221 form = cgi.FieldStorage() | |
| 222 form.list = [ | |
| 223 cgi.MiniFieldStorage('page_size', page_size), | |
| 224 cgi.MiniFieldStorage('page_index', 1) | |
| 225 ] | |
| 226 results = self.server.get_collection('issue', form) | |
| 227 self.assertEqual(self.dummy_client.response_code, 200) | |
| 228 self.assertEqual(len(results['data']), page_one_expected) | |
| 229 | |
| 230 # Retrieve page 2 | |
| 231 form = cgi.FieldStorage() | |
| 232 form.list = [ | |
| 233 cgi.MiniFieldStorage('page_size', page_size), | |
| 234 cgi.MiniFieldStorage('page_index', 2) | |
| 235 ] | |
| 236 results = self.server.get_collection('issue', form) | |
| 237 self.assertEqual(self.dummy_client.response_code, 200) | |
| 238 self.assertEqual(len(results['data']), 0) | |
| 239 | |
| 96 def testPut(self): | 240 def testPut(self): |
| 97 """ | 241 """ |
| 98 Change joe's 'realname' | 242 Change joe's 'realname' |
| 99 Check if we can't change admin's detail | 243 Check if we can't change admin's detail |
| 100 """ | 244 """ |
| 314 results = results['data'] | 458 results = results['data'] |
| 315 self.assertEqual(self.dummy_client.response_code, 200) | 459 self.assertEqual(self.dummy_client.response_code, 200) |
| 316 self.assertEqual(results['attributes']['title'], None) | 460 self.assertEqual(results['attributes']['title'], None) |
| 317 self.assertEqual(len(results['attributes']['nosy']), 0) | 461 self.assertEqual(len(results['attributes']['nosy']), 0) |
| 318 self.assertEqual(results['attributes']['nosy'], []) | 462 self.assertEqual(results['attributes']['nosy'], []) |
| 463 | |
| 464 | |
| 465 def get_obj(path, id): | |
| 466 return { | |
| 467 'id': id, | |
| 468 'link': path + id | |
| 469 } | |
| 319 | 470 |
| 320 | 471 |
| 321 def test_suite(): | 472 def test_suite(): |
| 322 suite = unittest.TestSuite() | 473 suite = unittest.TestSuite() |
| 323 for l in list_backends(): | 474 for l in list_backends(): |
