Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/io/test_utils.py
6939 views
1
from __future__ import annotations
2
3
from typing import TYPE_CHECKING, Any
4
5
import pytest
6
7
import polars as pl
8
from polars.io._utils import looks_like_url, parse_columns_arg, parse_row_index_args
9
from polars.io.cloud._utils import _get_path_scheme
10
11
if TYPE_CHECKING:
12
from collections.abc import Sequence
13
14
15
@pytest.mark.parametrize(
16
("columns", "expected"),
17
[
18
(["a", "b"], (None, ["a", "b"])),
19
((1, 2), ((1, 2), None)),
20
("foo", (None, ["foo"])),
21
(3, ([3], None)),
22
(None, (None, None)),
23
],
24
)
25
def test_parse_columns_arg(
26
columns: Sequence[str] | Sequence[int] | str | int | None,
27
expected: tuple[Sequence[int] | None, Sequence[str] | None],
28
) -> None:
29
assert parse_columns_arg(columns) == expected
30
31
32
def test_parse_columns_arg_mixed_types() -> None:
33
with pytest.raises(TypeError):
34
parse_columns_arg(["a", 1]) # type: ignore[arg-type]
35
36
37
@pytest.mark.parametrize("columns", [["a", "a"], [1, 1, 2]])
38
def test_parse_columns_arg_duplicates(columns: Sequence[str] | Sequence[int]) -> None:
39
with pytest.raises(ValueError):
40
parse_columns_arg(columns)
41
42
43
def test_parse_row_index_args() -> None:
44
assert parse_row_index_args("idx", 5) == ("idx", 5)
45
assert parse_row_index_args(None, 5) is None
46
47
48
@pytest.mark.parametrize(
49
("url", "result"),
50
[
51
("HTTPS://pola.rs/data.csv", True),
52
("http://pola.rs/data.csv", True),
53
("ftps://pola.rs/data.csv", True),
54
("FTP://pola.rs/data.csv", True),
55
("htp://pola.rs/data.csv", False),
56
("fttp://pola.rs/data.csv", False),
57
("http_not_a_url", False),
58
("ftp_not_a_url", False),
59
("/mnt/data.csv", False),
60
("file://mnt/data.csv", False),
61
],
62
)
63
def test_looks_like_url(url: str, result: bool) -> None:
64
assert looks_like_url(url) == result
65
66
67
@pytest.mark.parametrize(
68
"scan", [pl.scan_csv, pl.scan_parquet, pl.scan_ndjson, pl.scan_ipc]
69
)
70
def test_filename_in_err(scan: Any) -> None:
71
with pytest.raises(FileNotFoundError, match=r".*does not exist"):
72
scan("does not exist").collect()
73
74
75
def test_get_path_scheme() -> None:
76
assert _get_path_scheme("") is None
77
assert _get_path_scheme("A") is None
78
assert _get_path_scheme("scheme://") == "scheme"
79
assert _get_path_scheme("://") == ""
80
assert _get_path_scheme("://...") == ""
81
82