Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdlineluser committed Aug 1, 2023
1 parent 11a67b8 commit c0995e3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions py-polars/tests/unit/namespaces/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,39 @@ def test_extract_all_many() -> None:
assert df["foo"].str.extract_all(df["re"]).to_list() == [["a"], ["bc"], ["abc"]]


def test_extract_groups() -> None:
def _named_groups_builder(pattern: str, groups: dict[str, str]) -> str:
return pattern.format(
**{name: f"(?<{name}>{value})" for name, value in groups.items()}
)

expected = {
"authority": ["ISO", "ISO/IEC/IEEE"],
"spec_num": ["80000", "29148"],
"part_num": ["1", None],
"revision_year": ["2009", "2018"],
}

pattern = _named_groups_builder(
r"{authority}\s{spec_num}(?:-{part_num})?(?::{revision_year})",
{
"authority": r"^ISO(?:/[A-Z]+)*",
"spec_num": r"\d+",
"part_num": r"\d+",
"revision_year": r"\d{4}",
},
)

df = pl.DataFrame({"iso_code": ["ISO 80000-1:2009", "ISO/IEC/IEEE 29148:2018"]})

assert (
df.select(pl.col("iso_code").str.extract_groups(pattern))
.unnest("iso_code")
.to_dict(False)
== expected
)


def test_zfill() -> None:
df = pl.DataFrame(
{
Expand Down

0 comments on commit c0995e3

Please sign in to comment.