Skip to content

Commit

Permalink
Add additional tests (#1900)
Browse files Browse the repository at this point in the history
Plugging some more holes in the coverage.
  • Loading branch information
zundertj committed Nov 26, 2021
1 parent fc7dad8 commit fcaa6f3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 25 deletions.
10 changes: 5 additions & 5 deletions py-polars/polars/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,21 @@ class Categorical(DataType):
def dtype_to_ctype(dtype: Type[DataType]) -> Type[_SimpleCData]:
try:
return _DTYPE_TO_CTYPE[dtype]
except KeyError:
except KeyError: # pragma: no cover
raise NotImplementedError


def dtype_to_ffiname(dtype: Type[DataType]) -> str:
try:
return _DTYPE_TO_FFINAME[dtype]
except KeyError:
except KeyError: # pragma: no cover
raise NotImplementedError


def dtype_to_py_type(dtype: Type[DataType]) -> Type:
try:
return _DTYPE_TO_PY_TYPE[dtype]
except KeyError:
except KeyError: # pragma: no cover
raise NotImplementedError


Expand All @@ -207,7 +207,7 @@ def py_type_to_dtype(data_type: Type[Any]) -> Type[DataType]:

try:
return _PY_TYPE_TO_DTYPE[data_type]
except KeyError:
except KeyError: # pragma: no cover
raise NotImplementedError


Expand All @@ -217,7 +217,7 @@ def py_type_to_arrow_type(dtype: Type[Any]) -> "pa.lib.DataType":
"""
try:
return _PY_TYPE_TO_ARROW_TYPE[dtype]
except KeyError:
except KeyError: # pragma: no cover
raise ValueError(f"Cannot parse dtype {dtype} into Arrow dtype.")


Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/datatypes_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def polars_type_to_constructor(
"""
try:
return _POLARS_TYPE_TO_CONSTRUCTOR[dtype]
except KeyError:
except KeyError: # pragma: no cover
raise ValueError(f"Cannot construct PySeries for type {dtype}.")


Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def sequence_to_pyseries(

try:
nested_arrow_dtype = py_type_to_arrow_type(nested_dtype)
except ValueError as e:
except ValueError as e: # pragma: no cover
raise ValueError(
f"Cannot construct Series from sequence of {nested_dtype}."
) from e
Expand Down
3 changes: 0 additions & 3 deletions py-polars/polars/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,4 @@ def raise_assert_detail(
[left]: {left}
[right]: {right}"""

if diff is not None:
msg += f"\n[diff left]: {diff}"

raise AssertionError(msg)
5 changes: 5 additions & 0 deletions py-polars/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ def fruits_cars() -> pl.DataFrame:
"cars": ["beetle", "audi", "beetle", "beetle", "beetle"],
}
)


@pytest.fixture
def series() -> pl.Series:
return pl.Series("a", [1, 2])
9 changes: 9 additions & 0 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def test_init_ndarray() -> None:
truth = pl.DataFrame({"a": [1, 2, 3]})
assert df.frame_equal(truth)

# 2D array - default to column orientation
df = pl.DataFrame(np.array([[1, 2], [3, 4]]), orient="column")
truth = pl.DataFrame({"column_0": [1, 2], "column_1": [3, 4]})
assert df.frame_equal(truth)

df = pl.DataFrame(np.array([[1, 2], [3, 4]]), orient="row")
truth = pl.DataFrame({"column_0": [1, 3], "column_1": [2, 4]})
assert df.frame_equal(truth)

# TODO: Uncomment tests below when removing deprecation warning
# # 2D array - default to column orientation
# df = pl.DataFrame(np.array([[1, 2], [3, 4]]))
Expand Down
37 changes: 22 additions & 15 deletions py-polars/tests/test_series.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from datetime import date, datetime, timedelta

import numpy as np
import pandas as pd
import pyarrow as pa
import pytest

import polars as pl
from polars import testing


def create_series() -> pl.Series:
def series() -> pl.Series:
return pl.Series("a", [1, 2])


Expand Down Expand Up @@ -36,7 +37,7 @@ def test_init_inputs() -> None:
assert pl.Series(values=np.array(["foo", "bar"])).dtype == pl.Utf8
assert pl.Series(values=["foo", "bar"]).dtype == pl.Utf8
assert pl.Series("a", [pl.Series([1, 2, 4]), pl.Series([3, 2, 1])]).dtype == pl.List

assert pl.Series(pd.Series([1, 2])).dtype == pl.Int64
# 2d numpy array
res = pl.Series(name="a", values=np.array([[1, 2], [3, 4]]))
assert all(res[0] == np.array([1, 2]))
Expand All @@ -59,8 +60,8 @@ def test_concat() -> None:
assert s.len() == 3


def test_to_frame() -> None:
assert create_series().to_frame().shape == (2, 1)
def test_to_frame(series: pl.Series) -> None:
assert series.to_frame().shape == (2, 1)


def test_bitwise_ops() -> None:
Expand All @@ -71,8 +72,8 @@ def test_bitwise_ops() -> None:
assert ~a == [False, True, False]


def test_equality() -> None:
a = create_series()
def test_equality(series: pl.Series) -> None:
a = series
b = a

cmp = a == b
Expand All @@ -90,15 +91,14 @@ def test_equality() -> None:
assert (a == "ham").to_list() == [True, False, False]


def test_agg() -> None:
a = create_series()
assert a.mean() == 1.5
assert a.min() == 1
assert a.max() == 2
def test_agg(series: pl.Series) -> None:
assert series.mean() == 1.5
assert series.min() == 1
assert series.max() == 2


def test_arithmetic() -> None:
a = create_series()
def test_arithmetic(series: pl.Series) -> None:
a = series
b = a

assert ((a * b) == [1, 4]).sum() == 2
Expand Down Expand Up @@ -129,8 +129,8 @@ def test_arithmetic() -> None:
assert ((1.0 % a) == [0, 1]).sum() == 2


def test_various() -> None:
a = create_series()
def test_various(series: pl.Series) -> None:
a = series

assert a.is_null().sum() == 0
assert a.name == "a"
Expand Down Expand Up @@ -948,6 +948,13 @@ def test_compare_series_value_mismatch() -> None:
testing.assert_series_equal(srs1, srs2)


def test_compare_series_type_mismatch() -> None:
srs1 = pl.Series([1, 2, 3])
srs2 = pl.DataFrame({"col1": [2, 3, 4]})
with pytest.raises(AssertionError, match="Series are different\n\nType mismatch"):
testing.assert_series_equal(srs1, srs2) # type: ignore


def test_compare_series_name_mismatch() -> None:
srs1 = pl.Series(values=[1, 2, 3], name="srs1")
srs2 = pl.Series(values=[1, 2, 3], name="srs2")
Expand Down

0 comments on commit fcaa6f3

Please sign in to comment.