-
Notifications
You must be signed in to change notification settings - Fork 189
#764 - Support cursor-based pagination from NetBox 4.6 #787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ab97506
#764 - Support cursor-based pagination from NetBox 4.6
arthanson ba795cf
review comments from claude
arthanson 7429226
review comments from claude
arthanson c12bde4
fix merge
arthanson e87a5db
fix review comments
arthanson 30c0295
review comments
arthanson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import pytest | ||
| from packaging import version | ||
|
|
||
| import pynetbox | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def cursor_api(docker_netbox_service): | ||
| """An Api instance configured for cursor pagination (NetBox 4.6+).""" | ||
| nb = pynetbox.api(docker_netbox_service["url"], pagination="cursor") | ||
| nb.create_token("admin", "admin") | ||
| return nb | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def prefixes(api): | ||
| """Create enough prefixes to span more than one cursor page.""" | ||
| created = [ | ||
| api.ipam.prefixes.create(prefix="198.51.100.{}/32".format(i)) | ||
| for i in range(5) | ||
| ] | ||
| yield created | ||
| for prefix in created: | ||
| prefix.delete() | ||
|
|
||
|
|
||
| class TestCursorPagination: | ||
|
arthanson marked this conversation as resolved.
|
||
| def test_cursor_pagination_returns_all_objects( | ||
| self, cursor_api, nb_version, prefixes | ||
| ): | ||
| """Cursor pagination walks every page and yields all objects. | ||
|
|
||
| Gated to NetBox 4.6+, where cursor pagination is available. On older | ||
| versions the client transparently falls back to offset pagination, so | ||
| this end-to-end check of the ``start`` semantics and ``next`` link | ||
| following is only meaningful from 4.6 onward. | ||
| """ | ||
| if nb_version < version.parse("4.6"): | ||
|
arthanson marked this conversation as resolved.
|
||
| pytest.skip("cursor pagination requires NetBox 4.6+") | ||
|
|
||
| assert cursor_api._effective_pagination() == "cursor" | ||
|
|
||
| # Force a small page size so the result set spans multiple cursor | ||
| # pages and the next-link following is actually exercised. | ||
| results = list(cursor_api.ipam.prefixes.filter(limit=2)) | ||
| returned = {str(p.prefix) for p in results} | ||
| for prefix in prefixes: | ||
| assert str(prefix.prefix) in returned | ||
|
|
||
| def test_cursor_pagination_count(self, cursor_api, nb_version, prefixes): | ||
| """len() on a cursor RecordSet refetches the count NetBox omits.""" | ||
| if nb_version < version.parse("4.6"): | ||
| pytest.skip("cursor pagination requires NetBox 4.6+") | ||
|
|
||
| record_set = cursor_api.ipam.prefixes.filter(limit=2) | ||
| assert len(record_set) >= len(prefixes) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.