"""Unit tests for pagination helpers in ``depicts.pager``. Covers page counting, slicing, page iteration, URL building, and Jinja init. """ from __future__ import annotations from depicts.pager import Pagination, init_pager, url_for_other_page from flask import Flask def _make_app() -> Flask: """Create a minimal Flask app with a paging route.""" app = Flask(__name__) @app.get("/items/") def items(category_id: int) -> str: # noqa: ARG001 - used via request # Return a URL for a different page while preserving args. return url_for_other_page(5) return app def test_pagination_basic_properties() -> None: """Compute total pages and prev/next boundaries.""" p = Pagination(page=1, per_page=10, total_count=95) assert p.pages == 10 assert p.has_prev is False assert p.has_next is True p2 = Pagination(page=10, per_page=10, total_count=95) assert p2.pages == 10 assert p2.has_prev is True assert p2.has_next is False def test_pagination_slice() -> None: """Return the correct slice for a page window.""" items = list(range(25)) p = Pagination(page=2, per_page=10, total_count=len(items)) assert p.slice(items) == list(range(10, 20)) def test_iter_pages_first_middle_last() -> None: """Iterate pages with ellipses represented by ``None`` when skipping.""" p = Pagination(page=1, per_page=10, total_count=100) pages = list(p.iter_pages()) # First page shows early pages, ellipsis, then tail pages assert pages == [1, 2, 3, 4, 5, 6, None, 9, 10] mid = Pagination(page=5, per_page=10, total_count=100) # In the middle, defaults show all pages without gaps assert list(mid.iter_pages()) == list(range(1, 11)) last = Pagination(page=10, per_page=10, total_count=100) # Near the end, elide the early middle (3), keep a window around current assert list(last.iter_pages()) == [1, 2, None, 4, 5, 6, 7, 8, 9, 10] def test_url_for_other_page_preserves_args_and_view_args() -> None: """Build a URL for another page preserving args.""" app = _make_app() with app.test_client() as client: resp = client.get("/items/42?q=abc&page=3") assert resp.status_code == 200 # The route returns the URL generated for page=5 assert resp.get_data(as_text=True) == "/items/42?q=abc&page=5" def test_init_pager_registers_jinja_helper() -> None: """Register the helper in the Jinja environment globals.""" app = Flask(__name__) init_pager(app) assert app.jinja_env.globals["url_for_other_page"] is url_for_other_page