Skip to content

Commit 9e2c471

Browse files
committed
fix: Restore deleted test file
1 parent 3ce375d commit 9e2c471

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

tests/unit/display/test_html.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# Copyright 2024 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 datetime
16+
17+
import pandas as pd
18+
import pyarrow as pa
19+
import pytest
20+
21+
import bigframes as bf
22+
from bigframes.display.html import _flatten_nested_data
23+
import bigframes.display.html as bf_html
24+
25+
26+
@pytest.mark.parametrize(
27+
("data", "expected_alignments", "expected_strings"),
28+
[
29+
pytest.param(
30+
{
31+
"string_col": ["a", "b", "c"],
32+
"int_col": [1, 2, 3],
33+
"float_col": [1.1, 2.2, 3.3],
34+
"bool_col": [True, False, True],
35+
},
36+
{
37+
"string_col": "left",
38+
"int_col": "right",
39+
"float_col": "right",
40+
"bool_col": "left",
41+
},
42+
["1.100000", "2.200000", "3.300000"],
43+
id="scalars",
44+
),
45+
pytest.param(
46+
{
47+
"timestamp_col": pa.array(
48+
[
49+
datetime.datetime.fromisoformat(value)
50+
for value in [
51+
"2024-01-01 00:00:00",
52+
"2024-01-01 00:00:01",
53+
"2024-01-01 00:00:02",
54+
]
55+
],
56+
pa.timestamp("us", tz="UTC"),
57+
),
58+
"datetime_col": pa.array(
59+
[
60+
datetime.datetime.fromisoformat(value)
61+
for value in [
62+
"2027-06-05 04:03:02.001",
63+
"2027-01-01 00:00:01",
64+
"2027-01-01 00:00:02",
65+
]
66+
],
67+
pa.timestamp("us"),
68+
),
69+
"date_col": pa.array(
70+
[
71+
datetime.date(1999, 1, 1),
72+
datetime.date(1999, 1, 2),
73+
datetime.date(1999, 1, 3),
74+
],
75+
pa.date32(),
76+
),
77+
"time_col": pa.array(
78+
[
79+
datetime.time(11, 11, 0),
80+
datetime.time(11, 11, 1),
81+
datetime.time(11, 11, 2),
82+
],
83+
pa.time64("us"),
84+
),
85+
},
86+
{
87+
"timestamp_col": "left",
88+
"datetime_col": "left",
89+
"date_col": "left",
90+
"time_col": "left",
91+
},
92+
[
93+
"2024-01-01 00:00:00",
94+
"2027-06-05 04:03:02.001",
95+
"1999-01-01",
96+
"11:11:01",
97+
],
98+
id="datetimes",
99+
),
100+
pytest.param(
101+
{
102+
"array_col": pd.Series(
103+
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
104+
dtype=pd.ArrowDtype(pa.list_(pa.int64())),
105+
),
106+
},
107+
{
108+
"array_col": "left",
109+
},
110+
["[1, 2, 3]", "[4, 5, 6]", "[7, 8, 9]"],
111+
id="array",
112+
),
113+
pytest.param(
114+
{
115+
"struct_col": pd.Series(
116+
[{"v": 1}, {"v": 2}, {"v": 3}],
117+
dtype=pd.ArrowDtype(pa.struct([("v", pa.int64())])),
118+
),
119+
},
120+
{
121+
"struct_col": "left",
122+
},
123+
["{'v': 1}", "{'v': 2}", "{'v': 3}"],
124+
id="struct",
125+
),
126+
],
127+
)
128+
def test_render_html_alignment_and_precision(
129+
data, expected_alignments, expected_strings
130+
):
131+
df = pd.DataFrame(data)
132+
html = bf_html.render_html(dataframe=df, table_id="test-table")
133+
134+
for _, align in expected_alignments.items():
135+
assert 'th style="text-align: left;"' in html
136+
assert f'<td style="text-align: {align};' in html
137+
138+
for expected_string in expected_strings:
139+
assert expected_string in html
140+
141+
142+
def test_render_html_precision():
143+
data = {"float_col": [3.14159265]}
144+
df = pd.DataFrame(data)
145+
146+
with bf.option_context("display.precision", 4):
147+
html = bf_html.render_html(dataframe=df, table_id="test-table")
148+
assert "3.1416" in html
149+
150+
# Make sure we reset to default
151+
html = bf_html.render_html(dataframe=df, table_id="test-table")
152+
assert "3.141593" in html
153+
154+
155+
def test_flatten_nested_data():
156+
# Test STRUCT flattening
157+
struct_data = pd.DataFrame(
158+
{
159+
"id": [1, 2],
160+
"struct_col": pd.Series(
161+
[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}],
162+
dtype=pd.ArrowDtype(
163+
pa.struct([("name", pa.string()), ("age", pa.int64())])
164+
),
165+
),
166+
}
167+
)
168+
169+
flattened, _, _ = _flatten_nested_data(struct_data)
170+
assert "struct_col.name" in flattened.columns
171+
assert "struct_col.age" in flattened.columns
172+
assert flattened["struct_col.name"].tolist() == ["Alice", "Bob"]
173+
174+
175+
def test_array_explode():
176+
# Test ARRAY explosion
177+
array_data = pd.DataFrame(
178+
{
179+
"id": [1, 2],
180+
"array_col": pd.Series(
181+
[[10, 20, 30], [40, 50]], dtype=pd.ArrowDtype(pa.list_(pa.int64()))
182+
),
183+
}
184+
)
185+
186+
flattened, groups, _ = _flatten_nested_data(array_data)
187+
assert len(flattened) == 6 # 3 + 2 array elements, padded to 3*2
188+
assert "0" in groups # First original row
189+
assert len(groups["0"]) == 3 # Three array elements
190+
assert "1" in groups
191+
assert len(groups["1"]) == 3

0 commit comments

Comments
 (0)