|
1 | 1 | """Tests related to pagination.""" |
2 | | -import unittest |
3 | | -from random import randint, sample |
| 2 | +import pytest |
4 | 3 |
|
5 | | -from pulp_smash import api, config |
6 | 4 | from pulp_smash.pulp3.bindings import monitor_task |
7 | | -from pulp_smash.pulp3.utils import gen_repo, get_versions, modify_repo |
8 | 5 |
|
9 | | -from pulp_file.tests.functional.utils import populate_pulp |
10 | | -from .constants import ( |
11 | | - FILE_CONTENT_PATH, |
12 | | - FILE_MANY_FIXTURE_COUNT, |
13 | | - FILE_MANY_FIXTURE_MANIFEST_URL, |
14 | | - FILE_REPO_PATH, |
15 | | -) |
16 | 6 |
|
17 | | - |
18 | | -class RepoVersionPaginationTestCase(unittest.TestCase): |
19 | | - """Test pagination of the core RepositoryVersion endpoints. |
20 | | -
|
21 | | - This test case assumes that Pulp returns 100 elements in each page of |
22 | | - results. This is configurable, but the current default set by all known |
23 | | - Pulp installers. |
24 | | - """ |
25 | | - |
26 | | - @classmethod |
27 | | - def setUpClass(cls): |
28 | | - """Create class-wide variables.""" |
29 | | - cls.cfg = config.get_config() |
30 | | - cls.client = api.Client(cls.cfg, api.page_handler) |
31 | | - |
32 | | - def test_file_content(self): |
33 | | - """Test pagination for repository versions.""" |
34 | | - # Add content to Pulp, create a repo, and add content to repo. We |
35 | | - # sample 21 contents, because with page_size set to 10, this produces 3 |
36 | | - # pages, where the three three pages have unique combinations of values |
37 | | - # for the "previous" and "next" links. |
38 | | - populate_pulp(self.cfg, url=FILE_MANY_FIXTURE_MANIFEST_URL) |
39 | | - sample_size = min(FILE_MANY_FIXTURE_COUNT, 21) |
40 | | - contents = sample(self.client.get(FILE_CONTENT_PATH), sample_size) |
41 | | - repo = self.client.post(FILE_REPO_PATH, gen_repo()) |
42 | | - self.addCleanup(self.client.delete, repo["pulp_href"]) |
43 | | - |
44 | | - for content in contents: |
45 | | - modify_repo(self.cfg, repo, add_units=[content]) |
46 | | - |
47 | | - # Verify pagination works for getting repo versions. |
48 | | - repo = self.client.get(repo["pulp_href"]) |
49 | | - repo_versions = get_versions(repo, {"page_size": 10}) |
50 | | - self.assertEqual(len(repo_versions), sample_size + 1, repo_versions) |
51 | | - |
52 | | - |
53 | | -class PaginationTestCase(unittest.TestCase): |
54 | | - """Test pagination assuming that Pulp returns 100 elements in each page of results.""" |
55 | | - |
56 | | - @classmethod |
57 | | - def setUpClass(cls): |
58 | | - """Create class-wide variables.""" |
59 | | - cls.cfg = config.get_config() |
60 | | - cls.client = api.Client(cls.cfg, api.json_handler) |
61 | | - |
62 | | - def setUp(self): |
63 | | - self.repos = [] |
64 | | - self.number_to_create = 21 |
65 | | - |
66 | | - # Perform a sanity check. |
67 | | - repos = self.client.using_handler(api.page_handler).get(FILE_REPO_PATH) |
68 | | - assert len(repos) == 0, repos # AssertEqual not available here yet |
69 | | - |
70 | | - # Create repos |
71 | | - for _ in range(self.number_to_create): |
72 | | - repo = self.client.post(FILE_REPO_PATH, gen_repo()) |
73 | | - self.repos.append(repo) |
74 | | - |
75 | | - def tearDown(self): |
76 | | - responses = [] |
77 | | - for repo in self.repos: |
78 | | - responses.append(self.client.delete(repo["pulp_href"])) |
79 | | - for response in responses: |
80 | | - monitor_task(response["task"]) |
81 | | - |
82 | | - def test_pagination_workflow(self): |
83 | | - self._raw_pagination() |
84 | | - self._page_handler_pagination() |
85 | | - |
86 | | - def _raw_pagination(self): |
87 | | - """Assert content can be paginated page by page. |
88 | | -
|
89 | | - Do the following: |
90 | | -
|
91 | | - 1. Without using page_handler request content |
92 | | - 2. Save collected_results and assert it is equal the per_page param |
93 | | - 3. Assert there is a next link but not a previous link |
94 | | - 4. Loop pages "number_to_create / per_page" (3) |
95 | | - 5. For each page request next link and assert length equals per_page |
96 | | - 6. For each page assert the presence of next and previous links |
97 | | - 7. Assert last page is reached |
98 | | - 8. Assert the final count equals number_to_create |
99 | | - """ |
100 | | - |
101 | | - per_page = 7 # will result in 3 pages |
102 | | - resp = self.client.get(FILE_REPO_PATH, params={"limit": per_page}) |
103 | | - collected_results = resp["results"] |
104 | | - # First call returns 7 results |
105 | | - self.assertEqual(len(collected_results), per_page, collected_results) |
106 | | - # no previous but there is a next |
107 | | - self.assertIsNone(resp["previous"], resp["previous"]) |
108 | | - self.assertIsNotNone(resp["next"], resp["next"]) |
109 | | - |
110 | | - # paginate pages 2 and 3 |
111 | | - for page in range(int(self.number_to_create / per_page)): # [0, 1, 2] |
112 | | - if page == 1: |
113 | | - # there is a previous and a next |
114 | | - self.assertIsNotNone(resp["previous"], resp["previous"]) |
115 | | - self.assertIsNotNone(resp["next"], resp["next"]) |
116 | | - # must have twice the size |
117 | | - self.assertEqual(len(collected_results), per_page * 2, collected_results) |
118 | | - if page == 2: |
119 | | - # last page there is no next but there is a previous |
120 | | - self.assertIsNone(resp["next"], resp["next"]) |
121 | | - self.assertIsNotNone(resp["previous"], resp["previous"]) |
122 | | - # must have 3 x the size |
123 | | - self.assertEqual(len(collected_results), per_page * 3, collected_results) |
124 | | - break # last page reached |
125 | | - resp = self.client.get(resp["next"]) |
126 | | - page_results = resp["results"] |
127 | | - self.assertEqual(len(page_results), per_page, page_results) |
128 | | - collected_results.extend(page_results) |
129 | | - |
130 | | - # Assert the final count |
131 | | - self.assertEqual(len(collected_results), self.number_to_create, collected_results) |
132 | | - |
133 | | - def _page_handler_pagination(self): |
134 | | - """ |
135 | | - Assert page handler returns all items independent of page_size. |
136 | | -
|
137 | | - This test asserts that pulp-smash page_handler will collect results from all pages and |
138 | | - return it in the same call independent of the page_size provided. |
139 | | - """ |
140 | | - repos = self.client.using_handler(api.page_handler).get( |
141 | | - FILE_REPO_PATH, params={"page_size": randint(2, 11)} |
| 7 | +@pytest.mark.parallel |
| 8 | +def test_repo_version_pagination( |
| 9 | + file_content_unit_with_name_factory, file_repo_api_client, file_repo_ver_api_client, file_repo |
| 10 | +): |
| 11 | + # Create 20 new repository versions (21 in total) |
| 12 | + for i in range(20): |
| 13 | + content_unit = file_content_unit_with_name_factory(f"{i}.iso") |
| 14 | + monitor_task( |
| 15 | + file_repo_api_client.modify( |
| 16 | + file_repo.pulp_href, {"add_content_units": [content_unit.pulp_href]} |
| 17 | + ).task |
142 | 18 | ) |
143 | | - self.assertEqual(len(repos), self.number_to_create, repos) |
| 19 | + |
| 20 | + # Assert that the limit of 10 items per page of results is respected. |
| 21 | + first_page = file_repo_ver_api_client.list(file_repo.pulp_href, limit=10, offset=0) |
| 22 | + assert len(first_page.results) == 10 |
| 23 | + assert first_page.previous is None |
| 24 | + assert first_page.next is not None |
| 25 | + |
| 26 | + # Assert that a limit and an offset are respected. |
| 27 | + second_page = file_repo_ver_api_client.list(file_repo.pulp_href, limit=10, offset=10) |
| 28 | + assert len(second_page.results) == 10 |
| 29 | + assert second_page.previous is not None |
| 30 | + assert second_page.next is not None |
| 31 | + |
| 32 | + # Assert that the limit and offset are respected for the last page of results. |
| 33 | + third_page = file_repo_ver_api_client.list(file_repo.pulp_href, limit=10, offset=20) |
| 34 | + assert len(third_page.results) == 1 |
| 35 | + assert third_page.previous is not None |
| 36 | + assert third_page.next is None |
0 commit comments