11
11
from sasctl .core import PageIterator , RestObj
12
12
13
13
14
+ @pytest .fixture (params = [(6 , 2 , 2 ), (6 , 1 , 4 ), (6 , 5 , 4 ), (6 , 6 , 2 ), (100 , 10 , 20 )])
15
+ def paging (request ):
16
+ """Create a RestObj designed to page through a collection of items and the
17
+ collection itself.
18
+
19
+ Returns
20
+ -------
21
+ RestObj : initial RestObj that can be used to initialize a paging iterator
22
+ List[dict] : List of items being used as the "server-side" source
23
+ MagicMock : Mock of sasctl.request for performing additional validation
24
+
25
+ """
26
+ import math
27
+ import re
28
+
29
+ num_items , start , limit = request .param
30
+
31
+ with mock .patch ("sasctl.core.request" ) as req :
32
+ items = [{"name" : str (i )} for i in range (num_items )]
33
+
34
+ obj = RestObj (
35
+ items = items [:start ],
36
+ count = len (items ),
37
+ links = [
38
+ {
39
+ "rel" : "next" ,
40
+ "href" : "/moaritems?start=%d&limit=%d" % (start , limit ),
41
+ }
42
+ ],
43
+ )
44
+
45
+ def side_effect (_ , link , ** kwargs ):
46
+ assert "limit=%d" % limit in link
47
+ start = int (re .search (r"(?<=start=)[\d]+" , link ).group ())
48
+ return RestObj (items = items [start : start + limit ])
49
+
50
+ req .side_effect = side_effect
51
+ yield obj , items [:], req
52
+
53
+ # Enough requests should have been made to retrieve all the data.
54
+ # Additional requests may have been made by workers to non-existent pages.
55
+ call_count = (num_items - start ) / float (limit )
56
+ assert req .call_count >= math .ceil (call_count )
57
+
58
+
14
59
@pytest .mark .incremental
15
- class TestPageIterator () :
60
+ class TestPageIterator :
16
61
def test_no_paging_required (self ):
17
62
"""If "next" link not present, current items should be included."""
18
63
@@ -35,47 +80,6 @@ def test_no_paging_required(self):
35
80
f"method_calls={ request .mock_calls } call_args={ request .call_args_list } "
36
81
)
37
82
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
83
def test_paging_required (self , paging ):
80
84
"""Requests should be made to retrieve additional pages."""
81
85
obj , items , _ = paging
0 commit comments