Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit de10885

Browse files
add full unit tests
1 parent a7cd91e commit de10885

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

tests/unit/test_col.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import operator
16+
import pathlib
17+
from typing import Generator
18+
19+
import pandas as pd
20+
import pytest
21+
22+
import bigframes
23+
import bigframes.pandas as bpd
24+
from bigframes.testing.utils import assert_frame_equal, convert_pandas_dtypes
25+
26+
pytest.importorskip("polars")
27+
pytest.importorskip("pandas", minversion="3.0.0")
28+
29+
30+
CURRENT_DIR = pathlib.Path(__file__).parent
31+
DATA_DIR = CURRENT_DIR.parent / "data"
32+
33+
34+
@pytest.fixture(scope="module", autouse=True)
35+
def session() -> Generator[bigframes.Session, None, None]:
36+
import bigframes.core.global_session
37+
from bigframes.testing import polars_session
38+
39+
session = polars_session.TestSession()
40+
with bigframes.core.global_session._GlobalSessionContext(session):
41+
yield session
42+
43+
44+
@pytest.fixture(scope="module")
45+
def scalars_pandas_df_index() -> pd.DataFrame:
46+
"""pd.DataFrame pointing at test data."""
47+
48+
df = pd.read_json(
49+
DATA_DIR / "scalars.jsonl",
50+
lines=True,
51+
)
52+
convert_pandas_dtypes(df, bytes_col=True)
53+
54+
df = df.set_index("rowindex", drop=False)
55+
df.index.name = None
56+
return df.set_index("rowindex").sort_index()
57+
58+
59+
@pytest.fixture(scope="module")
60+
def scalars_df_index(
61+
session: bigframes.Session, scalars_pandas_df_index
62+
) -> bpd.DataFrame:
63+
return session.read_pandas(scalars_pandas_df_index)
64+
65+
66+
@pytest.fixture(scope="module")
67+
def scalars_df_2_index(
68+
session: bigframes.Session, scalars_pandas_df_index
69+
) -> bpd.DataFrame:
70+
return session.read_pandas(scalars_pandas_df_index)
71+
72+
73+
@pytest.fixture(scope="module")
74+
def scalars_dfs(
75+
scalars_df_index,
76+
scalars_pandas_df_index,
77+
):
78+
return scalars_df_index, scalars_pandas_df_index
79+
80+
81+
@pytest.mark.parametrize(
82+
("op",),
83+
[
84+
(operator.invert,),
85+
],
86+
)
87+
def test_pd_col_unary_operators(scalars_dfs, op):
88+
scalars_df, scalars_pandas_df = scalars_dfs
89+
bf_kwargs = {
90+
"result": op(bpd.col("float64_col")),
91+
}
92+
pd_kwargs = {
93+
"result": op(pd.col("float64_col")), # type: ignore
94+
}
95+
df = scalars_df.assign(**bf_kwargs)
96+
bf_result = df.to_pandas()
97+
pd_result = scalars_pandas_df.assign(**pd_kwargs)
98+
99+
assert_frame_equal(bf_result, pd_result)
100+
101+
102+
@pytest.mark.parametrize(
103+
("op",),
104+
[
105+
(operator.add,),
106+
(operator.sub,),
107+
(operator.mul,),
108+
(operator.truediv,),
109+
(operator.floordiv,),
110+
(operator.gt,),
111+
(operator.lt,),
112+
(operator.ge,),
113+
(operator.le,),
114+
(operator.eq,),
115+
(operator.mod,),
116+
],
117+
)
118+
def test_pd_col_binary_operators(scalars_dfs, op):
119+
scalars_df, scalars_pandas_df = scalars_dfs
120+
bf_kwargs = {
121+
"result": op(bpd.col("float64_col"), 2.4),
122+
"reverse_result": op(2.4, bpd.col("float64_col")),
123+
}
124+
pd_kwargs = {
125+
"result": op(pd.col("float64_col"), 2.4), # type: ignore
126+
"reverse_result": op(2.4, pd.col("float64_col")), # type: ignore
127+
}
128+
df = scalars_df.assign(**bf_kwargs)
129+
bf_result = df.to_pandas()
130+
pd_result = scalars_pandas_df.assign(**pd_kwargs)
131+
132+
assert_frame_equal(bf_result, pd_result)
133+
134+
135+
@pytest.mark.parametrize(
136+
("op",),
137+
[
138+
(operator.and_,),
139+
(operator.or_,),
140+
(operator.xor,),
141+
],
142+
)
143+
def test_pd_col_binary_bool_operators(scalars_dfs, op):
144+
scalars_df, scalars_pandas_df = scalars_dfs
145+
bf_kwargs = {
146+
"result": op(bpd.col("bool_col"), True),
147+
"reverse_result": op(False, bpd.col("bool_col")),
148+
}
149+
pd_kwargs = {
150+
"result": op(pd.col("bool_col"), True), # type: ignore
151+
"reverse_result": op(False, pd.col("bool_col")), # type: ignore
152+
}
153+
df = scalars_df.assign(**bf_kwargs)
154+
bf_result = df.to_pandas()
155+
pd_result = scalars_pandas_df.assign(**pd_kwargs)
156+
157+
assert_frame_equal(bf_result, pd_result)

0 commit comments

Comments
 (0)