|
11 | 11 | from sasctl.core import PageIterator, RestObj
|
12 | 12 |
|
13 | 13 |
|
14 |
| -def test_no_paging_required(): |
15 |
| - """If "next" link not present, current items should be included.""" |
| 14 | +@pytest.mark.incremental |
| 15 | +class TestPageIterator(): |
| 16 | + def test_no_paging_required(self): |
| 17 | + """If "next" link not present, current items should be included.""" |
| 18 | + |
| 19 | + items = [{"name": "a"}, {"name": "b"}, {"name": "c"}] |
| 20 | + obj = RestObj(items=items, count=len(items)) |
| 21 | + |
| 22 | + with mock.patch("sasctl.core.request") as request: |
| 23 | + pager = PageIterator(obj) |
| 24 | + |
| 25 | + # Returned page of items should preserve item order |
| 26 | + items = next(pager) |
| 27 | + for idx, item in enumerate(items): |
| 28 | + assert item.name == RestObj(items[idx]).name |
| 29 | + |
| 30 | + # No request should have been made to retrieve additional data. |
| 31 | + try: |
| 32 | + request.assert_not_called() |
| 33 | + except AssertionError as e: |
| 34 | + raise AssertionError( |
| 35 | + f"method_calls={request.mock_calls} call_args={request.call_args_list}" |
| 36 | + ) |
| 37 | + |
| 38 | + @pytest.fixture(params=[(6, 2, 2), (6, 1, 4), (6, 5, 4), (6, 6, 2), (100, 10, 20)]) |
| 39 | + def paging(self, request): |
| 40 | + """Create a RestObj designed to page through a collection of items and the |
| 41 | + collection itself. |
| 42 | +
|
| 43 | + Returns |
| 44 | + ------- |
| 45 | + RestObj : initial RestObj that can be used to initialize a paging iterator |
| 46 | + List[dict] : List of items being used as the "server-side" source |
| 47 | + MagicMock : Mock of sasctl.request for performing additional validation |
| 48 | +
|
| 49 | + """ |
| 50 | + import math |
| 51 | + import re |
| 52 | + |
| 53 | + num_items, start, limit = request.param |
| 54 | + |
| 55 | + with mock.patch("sasctl.core.request") as req: |
| 56 | + items = [{"name": str(i)} for i in range(num_items)] |
| 57 | + |
| 58 | + obj = RestObj( |
| 59 | + items=items[:start], |
| 60 | + count=len(items), |
| 61 | + links=[ |
| 62 | + {"rel": "next", "href": "/moaritems?start=%d&limit=%d" % (start, limit)} |
| 63 | + ], |
| 64 | + ) |
| 65 | + |
| 66 | + def side_effect(_, link, **kwargs): |
| 67 | + assert "limit=%d" % limit in link |
| 68 | + start = int(re.search(r"(?<=start=)[\d]+", link).group()) |
| 69 | + return RestObj(items=items[start : start + limit]) |
| 70 | + |
| 71 | + req.side_effect = side_effect |
| 72 | + yield obj, items[:], req |
| 73 | + |
| 74 | + # Enough requests should have been made to retrieve all the data. |
| 75 | + # Additional requests may have been made by workers to non-existent pages. |
| 76 | + call_count = (num_items - start) / float(limit) |
| 77 | + assert req.call_count >= math.ceil(call_count) |
| 78 | + |
| 79 | + def test_paging_required(self, paging): |
| 80 | + """Requests should be made to retrieve additional pages.""" |
| 81 | + obj, items, _ = paging |
16 | 82 |
|
17 |
| - items = [{"name": "a"}, {"name": "b"}, {"name": "c"}] |
18 |
| - obj = RestObj(items=items, count=len(items)) |
19 |
| - |
20 |
| - with mock.patch("sasctl.core.request") as request: |
21 | 83 | pager = PageIterator(obj)
|
22 |
| - |
23 |
| - # Returned page of items should preserve item order |
24 |
| - items = next(pager) |
25 |
| - for idx, item in enumerate(items): |
26 |
| - assert item.name == RestObj(items[idx]).name |
27 |
| - |
28 |
| - # No request should have been made to retrieve additional data. |
29 |
| - try: |
30 |
| - request.assert_not_called() |
31 |
| - except AssertionError as e: |
32 |
| - raise AssertionError( |
33 |
| - f"method_calls={request.mock_calls} call_args={request.call_args_list}" |
34 |
| - ) |
35 |
| - |
36 |
| - |
37 |
| -@pytest.fixture(params=[(6, 2, 2), (6, 1, 4), (6, 5, 4), (6, 6, 2), (100, 10, 20)]) |
38 |
| -def paging(request): |
39 |
| - """Create a RestObj designed to page through a collection of items and the |
40 |
| - collection itself. |
41 |
| -
|
42 |
| - Returns |
43 |
| - ------- |
44 |
| - RestObj : initial RestObj that can be used to initialize a paging iterator |
45 |
| - List[dict] : List of items being used as the "server-side" source |
46 |
| - MagicMock : Mock of sasctl.request for performing additional validation |
47 |
| -
|
48 |
| - """ |
49 |
| - import math |
50 |
| - import re |
51 |
| - |
52 |
| - num_items, start, limit = request.param |
53 |
| - |
54 |
| - with mock.patch("sasctl.core.request") as req: |
55 |
| - items = [{"name": str(i)} for i in range(num_items)] |
56 |
| - |
57 |
| - obj = RestObj( |
58 |
| - items=items[:start], |
59 |
| - count=len(items), |
60 |
| - links=[ |
61 |
| - {"rel": "next", "href": "/moaritems?start=%d&limit=%d" % (start, limit)} |
62 |
| - ], |
63 |
| - ) |
64 |
| - |
65 |
| - def side_effect(_, link, **kwargs): |
66 |
| - assert "limit=%d" % limit in link |
67 |
| - start = int(re.search(r"(?<=start=)[\d]+", link).group()) |
68 |
| - return RestObj(items=items[start : start + limit]) |
69 |
| - |
70 |
| - req.side_effect = side_effect |
71 |
| - yield obj, items[:], req |
72 |
| - |
73 |
| - # Enough requests should have been made to retrieve all the data. |
74 |
| - # Additional requests may have been made by workers to non-existent pages. |
75 |
| - call_count = (num_items - start) / float(limit) |
76 |
| - assert req.call_count >= math.ceil(call_count) |
77 |
| - |
78 |
| - |
79 |
| -def test_paging_required(paging): |
80 |
| - """Requests should be made to retrieve additional pages.""" |
81 |
| - obj, items, _ = paging |
82 |
| - |
83 |
| - pager = PageIterator(obj) |
84 |
| - init_count = pager._start |
85 |
| - |
86 |
| - for i, page in enumerate(pager): |
87 |
| - for j, item in enumerate(page): |
88 |
| - if i == 0: |
89 |
| - item_idx = j |
90 |
| - else: |
91 |
| - # Account for initial page size not necessarily being same size |
92 |
| - # as additional pages |
93 |
| - item_idx = init_count + (i - 1) * pager._limit + j |
94 |
| - target = RestObj(items[item_idx]) |
95 |
| - assert item.name == target.name |
| 84 | + init_count = pager._start |
| 85 | + |
| 86 | + for i, page in enumerate(pager): |
| 87 | + for j, item in enumerate(page): |
| 88 | + if i == 0: |
| 89 | + item_idx = j |
| 90 | + else: |
| 91 | + # Account for initial page size not necessarily being same size |
| 92 | + # as additional pages |
| 93 | + item_idx = init_count + (i - 1) * pager._limit + j |
| 94 | + target = RestObj(items[item_idx]) |
| 95 | + assert item.name == target.name |
0 commit comments