|
8 | 8 | from roundup.rest import RestfulInstance |
9 | 9 | from roundup.backends import list_backends |
10 | 10 | from roundup.cgi import client |
| 11 | +import random |
11 | 12 |
|
12 | 13 | import db_test_base |
13 | 14 |
|
@@ -93,6 +94,149 @@ def testGet(self): |
93 | 94 | self.assertEqual(self.dummy_client.response_code, 200) |
94 | 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 | 240 | def testPut(self): |
97 | 241 | """ |
98 | 242 | Change joe's 'realname' |
@@ -318,6 +462,13 @@ def testPatchRemoveAll(self): |
318 | 462 | self.assertEqual(results['attributes']['nosy'], []) |
319 | 463 |
|
320 | 464 |
|
| 465 | +def get_obj(path, id): |
| 466 | + return { |
| 467 | + 'id': id, |
| 468 | + 'link': path + id |
| 469 | + } |
| 470 | + |
| 471 | + |
321 | 472 | def test_suite(): |
322 | 473 | suite = unittest.TestSuite() |
323 | 474 | for l in list_backends(): |
|
0 commit comments