|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright The Lance Authors |
| 3 | + |
| 4 | +""" |
| 5 | +Unit tests for Hive2 list-tables pagination (Java PageUtil parity). |
| 6 | +
|
| 7 | +Loaded via file path so the suite does not import ``lance_namespace_impls`` package |
| 8 | +``__init__`` (which pulls in Lance / JVM-dependent modules). |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import importlib.util |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +def _load_hive2_page_util(): |
| 18 | + root = Path(__file__).resolve().parents[1] |
| 19 | + path = root / "src/lance_namespace_impls/hive2_page_util.py" |
| 20 | + spec = importlib.util.spec_from_file_location("hive2_page_util", path) |
| 21 | + assert spec is not None and spec.loader is not None |
| 22 | + mod = importlib.util.module_from_spec(spec) |
| 23 | + spec.loader.exec_module(mod) |
| 24 | + return mod |
| 25 | + |
| 26 | + |
| 27 | +pu = _load_hive2_page_util() |
| 28 | + |
| 29 | + |
| 30 | +class TestHive2PageUtil: |
| 31 | + def test_normalize_page_size(self): |
| 32 | + assert pu.normalize_list_tables_page_size(None) == pu.DEFAULT_LIST_TABLES_PAGE_SIZE |
| 33 | + assert pu.normalize_list_tables_page_size(0) == pu.DEFAULT_LIST_TABLES_PAGE_SIZE |
| 34 | + assert pu.normalize_list_tables_page_size(-1) == pu.DEFAULT_LIST_TABLES_PAGE_SIZE |
| 35 | + assert pu.normalize_list_tables_page_size(50) == 50 |
| 36 | + |
| 37 | + def test_split_page_first_and_next_token(self): |
| 38 | + items = ["a", "b", "c", "d"] |
| 39 | + page, token = pu.split_list_tables_page(items, None, 2) |
| 40 | + assert page == ["a", "b"] |
| 41 | + assert token == "2" |
| 42 | + |
| 43 | + page2, token2 = pu.split_list_tables_page(items, "2", 2) |
| 44 | + assert page2 == ["c", "d"] |
| 45 | + assert token2 is None |
| 46 | + |
| 47 | + def test_split_page_invalid_token_resets_to_start(self): |
| 48 | + items = ["x", "y"] |
| 49 | + page, token = pu.split_list_tables_page(items, "not-an-int", 1) |
| 50 | + assert page == ["x"] |
| 51 | + assert token == "1" |
| 52 | + |
| 53 | + def test_split_page_start_beyond_end(self): |
| 54 | + page, token = pu.split_list_tables_page(["only"], "99", 10) |
| 55 | + assert page == [] |
| 56 | + assert token is None |
| 57 | + |
| 58 | + def test_apply_pagination_multi_page(self): |
| 59 | + names = [f"t{i:02d}" for i in range(5)] |
| 60 | + r1 = pu.apply_list_tables_pagination(names, None, 2) |
| 61 | + assert r1 == (["t00", "t01"], "2") |
| 62 | + |
| 63 | + r2 = pu.apply_list_tables_pagination(names, "2", 2) |
| 64 | + assert r2 == (["t02", "t03"], "4") |
| 65 | + |
| 66 | + r3 = pu.apply_list_tables_pagination(names, "4", 2) |
| 67 | + assert r3 == (["t04"], None) |
| 68 | + |
| 69 | + def test_default_limit_full_single_page(self): |
| 70 | + names = ["a", "b"] |
| 71 | + page, token = pu.apply_list_tables_pagination(names, None, None) |
| 72 | + assert page == names |
| 73 | + assert token is None |
0 commit comments