Mercurial > p > roundup > code
comparison test/rest_common.py @ 5865:04deafac71ab
Implement sorting of collections in REST API
| author | Ralf Schlatterbeck <rsc@runtux.com> |
|---|---|
| date | Mon, 26 Aug 2019 09:56:20 +0200 |
| parents | 9c6617857032 |
| children | 1b91e3df3fd0 |
comparison
equal
deleted
inserted
replaced
| 5864:5e8e160fe2a0 | 5865:04deafac71ab |
|---|---|
| 116 try: | 116 try: |
| 117 return self.headers[header.lower()] | 117 return self.headers[header.lower()] |
| 118 except (AttributeError, KeyError, TypeError): | 118 except (AttributeError, KeyError, TypeError): |
| 119 return not_found | 119 return not_found |
| 120 | 120 |
| 121 def testGet(self): | 121 def create_stati(self): |
| 122 """ | |
| 123 Retrieve all three users | |
| 124 obtain data for 'joe' | |
| 125 """ | |
| 126 # Retrieve all three users. | |
| 127 results = self.server.get_collection('user', self.empty_form) | |
| 128 self.assertEqual(self.dummy_client.response_code, 200) | |
| 129 self.assertEqual(len(results['data']['collection']), 3) | |
| 130 self.assertEqual(results['data']['@total_size'], 3) | |
| 131 print(self.dummy_client.additional_headers["X-Count-Total"]) | |
| 132 self.assertEqual( | |
| 133 self.dummy_client.additional_headers["X-Count-Total"], | |
| 134 "3" | |
| 135 ) | |
| 136 | |
| 137 # Obtain data for 'joe'. | |
| 138 results = self.server.get_element('user', self.joeid, self.empty_form) | |
| 139 results = results['data'] | |
| 140 self.assertEqual(self.dummy_client.response_code, 200) | |
| 141 self.assertEqual(results['attributes']['username'], 'joe') | |
| 142 self.assertEqual(results['attributes']['realname'], 'Joe Random') | |
| 143 | |
| 144 # Obtain data for 'joe' via username lookup. | |
| 145 results = self.server.get_element('user', 'joe', self.empty_form) | |
| 146 results = results['data'] | |
| 147 self.assertEqual(self.dummy_client.response_code, 200) | |
| 148 self.assertEqual(results['attributes']['username'], 'joe') | |
| 149 self.assertEqual(results['attributes']['realname'], 'Joe Random') | |
| 150 | |
| 151 # Obtain data for 'joe' via username lookup (long form). | |
| 152 key = 'username=joe' | |
| 153 results = self.server.get_element('user', key, self.empty_form) | |
| 154 results = results['data'] | |
| 155 self.assertEqual(self.dummy_client.response_code, 200) | |
| 156 self.assertEqual(results['attributes']['username'], 'joe') | |
| 157 self.assertEqual(results['attributes']['realname'], 'Joe Random') | |
| 158 | |
| 159 # Obtain data for 'joe'. | |
| 160 results = self.server.get_attribute( | |
| 161 'user', self.joeid, 'username', self.empty_form | |
| 162 ) | |
| 163 self.assertEqual(self.dummy_client.response_code, 200) | |
| 164 self.assertEqual(results['data']['data'], 'joe') | |
| 165 | |
| 166 def testOutputFormat(self): | |
| 167 """ test of @fields and @verbose implementation """ | |
| 168 | |
| 169 self.maxDiff = 4000 | |
| 170 # create sample data | |
| 171 try: | 122 try: |
| 172 self.db.status.create(name='open') | 123 self.db.status.create(name='open', order='9') |
| 173 except ValueError: | 124 except ValueError: |
| 174 pass | 125 pass |
| 175 try: | 126 try: |
| 176 self.db.status.create(name='closed') | 127 self.db.status.create(name='closed', order='91') |
| 177 except ValueError: | 128 except ValueError: |
| 178 pass | 129 pass |
| 179 try: | 130 try: |
| 180 self.db.priority.create(name='normal') | 131 self.db.priority.create(name='normal') |
| 181 except ValueError: | 132 except ValueError: |
| 182 pass | 133 pass |
| 183 try: | 134 try: |
| 184 self.db.priority.create(name='critical') | 135 self.db.priority.create(name='critical') |
| 185 except ValueError: | 136 except ValueError: |
| 186 pass | 137 pass |
| 138 | |
| 139 def create_sampledata(self): | |
| 140 """ Create sample data common to some test cases | |
| 141 """ | |
| 142 self.create_stati() | |
| 187 self.db.issue.create( | 143 self.db.issue.create( |
| 188 title='foo1', | 144 title='foo1', |
| 189 status=self.db.status.lookup('open'), | 145 status=self.db.status.lookup('open'), |
| 190 priority=self.db.priority.lookup('normal'), | 146 priority=self.db.priority.lookup('normal'), |
| 191 nosy = [ "1", "2" ] | 147 nosy = [ "1", "2" ] |
| 199 issue_open_crit = self.db.issue.create( | 155 issue_open_crit = self.db.issue.create( |
| 200 title='foo5', | 156 title='foo5', |
| 201 status=self.db.status.lookup('open'), | 157 status=self.db.status.lookup('open'), |
| 202 priority=self.db.priority.lookup('critical') | 158 priority=self.db.priority.lookup('critical') |
| 203 ) | 159 ) |
| 160 | |
| 161 def testGet(self): | |
| 162 """ | |
| 163 Retrieve all three users | |
| 164 obtain data for 'joe' | |
| 165 """ | |
| 166 # Retrieve all three users. | |
| 167 results = self.server.get_collection('user', self.empty_form) | |
| 168 self.assertEqual(self.dummy_client.response_code, 200) | |
| 169 self.assertEqual(len(results['data']['collection']), 3) | |
| 170 self.assertEqual(results['data']['@total_size'], 3) | |
| 171 print(self.dummy_client.additional_headers["X-Count-Total"]) | |
| 172 self.assertEqual( | |
| 173 self.dummy_client.additional_headers["X-Count-Total"], | |
| 174 "3" | |
| 175 ) | |
| 176 | |
| 177 # Obtain data for 'joe'. | |
| 178 results = self.server.get_element('user', self.joeid, self.empty_form) | |
| 179 results = results['data'] | |
| 180 self.assertEqual(self.dummy_client.response_code, 200) | |
| 181 self.assertEqual(results['attributes']['username'], 'joe') | |
| 182 self.assertEqual(results['attributes']['realname'], 'Joe Random') | |
| 183 | |
| 184 # Obtain data for 'joe' via username lookup. | |
| 185 results = self.server.get_element('user', 'joe', self.empty_form) | |
| 186 results = results['data'] | |
| 187 self.assertEqual(self.dummy_client.response_code, 200) | |
| 188 self.assertEqual(results['attributes']['username'], 'joe') | |
| 189 self.assertEqual(results['attributes']['realname'], 'Joe Random') | |
| 190 | |
| 191 # Obtain data for 'joe' via username lookup (long form). | |
| 192 key = 'username=joe' | |
| 193 results = self.server.get_element('user', key, self.empty_form) | |
| 194 results = results['data'] | |
| 195 self.assertEqual(self.dummy_client.response_code, 200) | |
| 196 self.assertEqual(results['attributes']['username'], 'joe') | |
| 197 self.assertEqual(results['attributes']['realname'], 'Joe Random') | |
| 198 | |
| 199 # Obtain data for 'joe'. | |
| 200 results = self.server.get_attribute( | |
| 201 'user', self.joeid, 'username', self.empty_form | |
| 202 ) | |
| 203 self.assertEqual(self.dummy_client.response_code, 200) | |
| 204 self.assertEqual(results['data']['data'], 'joe') | |
| 205 | |
| 206 def testOutputFormat(self): | |
| 207 """ test of @fields and @verbose implementation """ | |
| 208 | |
| 209 self.maxDiff = 4000 | |
| 210 self.create_sampledata() | |
| 204 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/issue/' | 211 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/issue/' |
| 205 | 212 |
| 206 | 213 |
| 207 # Check formating for issues status=open; @fields and verbose tests | 214 # Check formating for issues status=open; @fields and verbose tests |
| 208 form = cgi.FieldStorage() | 215 form = cgi.FieldStorage() |
| 423 | 430 |
| 424 results = self.server.get_element('issue', "3", form) | 431 results = self.server.get_element('issue', "3", form) |
| 425 results['data']['@etag'] = '' # etag depends on date, set to empty | 432 results['data']['@etag'] = '' # etag depends on date, set to empty |
| 426 self.assertDictEqual(expected,results) | 433 self.assertDictEqual(expected,results) |
| 427 | 434 |
| 435 def testSorting(self): | |
| 436 self.maxDiff = 4000 | |
| 437 self.create_sampledata() | |
| 438 self.db.issue.set('1', status='7') | |
| 439 self.db.issue.set('2', status='2') | |
| 440 self.db.issue.set('3', status='2') | |
| 441 self.db.commit() | |
| 442 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/issue/' | |
| 443 # change some data for sorting on later | |
| 444 form = cgi.FieldStorage() | |
| 445 form.list = [ | |
| 446 cgi.MiniFieldStorage('@fields', 'status'), | |
| 447 cgi.MiniFieldStorage('@sort', 'status,-id'), | |
| 448 cgi.MiniFieldStorage('@verbose', '0') | |
| 449 ] | |
| 450 | |
| 451 # status is sorted by orderprop (property 'order') | |
| 452 # which provides the same ordering as the status ID | |
| 453 expected={'data': { | |
| 454 '@total_size': 3, | |
| 455 'collection': [ | |
| 456 {'link': base_path + '3', 'status': '2', 'id': '3'}, | |
| 457 {'link': base_path + '2', 'status': '2', 'id': '2'}, | |
| 458 {'link': base_path + '1', 'status': '7', 'id': '1'}]}} | |
| 459 | |
| 460 results = self.server.get_collection('issue', form) | |
| 461 self.assertDictEqual(expected, results) | |
| 462 | |
| 428 def testFilter(self): | 463 def testFilter(self): |
| 429 """ | 464 """ |
| 430 Retrieve all three users | 465 Retrieve all three users |
| 431 obtain data for 'joe' | 466 obtain data for 'joe' |
| 432 """ | 467 """ |
| 433 # create sample data | 468 # create sample data |
| 434 try: | 469 self.create_stati() |
| 435 self.db.status.create(name='open') | |
| 436 except ValueError: | |
| 437 pass | |
| 438 try: | |
| 439 self.db.status.create(name='closed') | |
| 440 except ValueError: | |
| 441 pass | |
| 442 try: | |
| 443 self.db.priority.create(name='normal') | |
| 444 except ValueError: | |
| 445 pass | |
| 446 try: | |
| 447 self.db.priority.create(name='critical') | |
| 448 except ValueError: | |
| 449 pass | |
| 450 self.db.issue.create( | 470 self.db.issue.create( |
| 451 title='foo4', | 471 title='foo4', |
| 452 status=self.db.status.lookup('closed'), | 472 status=self.db.status.lookup('closed'), |
| 453 priority=self.db.priority.lookup('critical') | 473 priority=self.db.priority.lookup('critical') |
| 454 ) | 474 ) |
