| 1 |
import math |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
import pandas as pd |
| 5 |
import pytest |
| 6 |
from bqplot import DateScale, LinearScale, LogScale |
| 7 |
|
| 8 |
from ipydatagrid import BarRenderer, DataGrid, TextRenderer |
| 9 |
from ipydatagrid.datagrid import _widgets_dict_serialization |
| 10 |
|
| 11 |
|
| 12 |
@pytest.fixture() |
| 13 |
def dataframe() -> pd.DataFrame: |
| 14 |
return pd.DataFrame( |
| 15 |
data={"A": [1, 2, 3], "B": [4, 5, 6]}, index=["One", "Two", "Three"] |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
@pytest.fixture() |
| 20 |
def dataframe_with_datetime(dataframe) -> pd.DataFrame: |
| 21 |
return dataframe.assign( |
| 22 |
dates=pd.to_datetime(["2024-03-05", "2024-03-06", "2024-03-07"]) |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture() |
| 27 |
def datagrid(dataframe) -> DataGrid: |
| 28 |
return DataGrid(dataframe) |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture() |
| 32 |
def data_object(dataframe) -> None: |
| 33 |
return DataGrid.generate_data_object(dataframe, "ipydguuid", "key") |
| 34 |
|
| 35 |
|
| 36 |
@pytest.mark.parametrize("clear", [True, False]) |
| 37 |
def test_selections(clear: bool, dataframe: pd.DataFrame) -> None: |
| 38 |
grid = DataGrid(dataframe, selection_mode="cell", editable=True) |
| 39 |
grid.select(1, 0, 2, 1) # Select 1A to 2B |
| 40 |
if clear: |
| 41 |
grid.clear_selection() |
| 42 |
assert grid.selected_cells == [] |
| 43 |
else: |
| 44 |
assert grid.selected_cells == [ |
| 45 |
{"c": 0, "r": 1}, |
| 46 |
{"c": 1, "r": 1}, |
| 47 |
{"c": 0, "r": 2}, |
| 48 |
{"c": 1, "r": 2}, |
| 49 |
] |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.parametrize("drop_rows", [True, False]) |
| 53 |
def test_data_getter(drop_rows: bool, dataframe: pd.DataFrame) -> None: |
| 54 |
"""Testing data getter of DataGrid to check it can be called when there is |
| 55 |
data and when there isn't data (i.e. all rows have been deleted in the |
| 56 |
dataframe). |
| 57 |
|
| 58 |
Args: |
| 59 |
drop_rows (bool): boolean determining whether to drop rows in dataframe. |
| 60 |
dataframe (pd.DataFrame): initial dataframe passed. |
| 61 |
""" |
| 62 |
grid = DataGrid(dataframe) |
| 63 |
if drop_rows: |
| 64 |
grid.data = grid.data.drop(["One", "Two", "Three"]) # Drop all rows |
| 65 |
grid_data = grid.data # calling data getter |
| 66 |
assert list(grid_data.columns) == ["A", "B"] |
| 67 |
assert grid_data.index.name == "key" |
| 68 |
assert list(grid_data.values) == [] |
| 69 |
else: |
| 70 |
grid_data = grid.data |
| 71 |
assert grid_data.equals(dataframe) |
| 72 |
|
| 73 |
|
| 74 |
def test_data_setter(dataframe) -> None: |
| 75 |
grid = DataGrid(dataframe) |
| 76 |
new_df = pd.DataFrame(data={"A": [0, 0, 0], "B": [4, 5, 6]}) |
| 77 |
grid.data = new_df |
| 78 |
assert grid.data.equals(new_df) |
| 79 |
|
| 80 |
|
| 81 |
def test_get_cell_value(datagrid: DataGrid) -> None: |
| 82 |
cell = datagrid.get_cell_value("B", "Three") |
| 83 |
assert cell == [6] |
| 84 |
|
| 85 |
|
| 86 |
def test_set_cell_value() -> None: |
| 87 |
"""Cannot be tested without a running front end""" |
| 88 |
pass |
| 89 |
|
| 90 |
|
| 91 |
def get_cell_value_by_index(datagrid: DataGrid) -> None: |
| 92 |
cell = datagrid.get_cell_value_by_index("B", 1) |
| 93 |
assert cell == [5] |
| 94 |
|
| 95 |
|
| 96 |
def test_set_cell_value_by_index() -> None: |
| 97 |
"""Cannot be tested without a running front end""" |
| 98 |
pass |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.parametrize("invalid_index", [True, False]) |
| 102 |
def test_column_name_to_index(invalid_index: bool, datagrid: DataGrid) -> None: |
| 103 |
if invalid_index: |
| 104 |
assert datagrid._column_name_to_index("Z") is None |
| 105 |
else: |
| 106 |
assert datagrid._column_name_to_index("A") == 0 |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.parametrize("invalid_index", [True, False]) |
| 110 |
def test_column_index_to_name(invalid_index: bool, data_object: dict) -> None: |
| 111 |
if invalid_index: |
| 112 |
assert DataGrid._column_index_to_name(data_object, 4) is None |
| 113 |
else: |
| 114 |
assert DataGrid._column_index_to_name(data_object, 1) == "B" |
| 115 |
|
| 116 |
|
| 117 |
def test_get_col_headers(data_object) -> None: |
| 118 |
assert DataGrid._get_col_headers(data_object) == ["A", "B"] |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.parametrize("invalid_prim_key", [True, False]) |
| 122 |
def test_get_row_index_of_primary_key( |
| 123 |
invalid_prim_key: bool, datagrid: DataGrid |
| 124 |
) -> None: |
| 125 |
if invalid_prim_key: |
| 126 |
assert datagrid._get_row_index_of_primary_key("Nay") == [] |
| 127 |
else: |
| 128 |
assert datagrid._get_row_index_of_primary_key("Two") == [1] |
| 129 |
|
| 130 |
|
| 131 |
@pytest.mark.parametrize("invalid_coords", [True, False]) |
| 132 |
def test_get_cell_value_by_numerical_index( |
| 133 |
invalid_coords: bool, data_object: dict |
| 134 |
) -> None: |
| 135 |
if invalid_coords: |
| 136 |
assert ( |
| 137 |
DataGrid._get_cell_value_by_numerical_index(data_object, 2, 2) |
| 138 |
is None |
| 139 |
) |
| 140 |
else: |
| 141 |
assert ( |
| 142 |
DataGrid._get_cell_value_by_numerical_index(data_object, 1, 0) == 4 |
| 143 |
) |
| 144 |
|
| 145 |
|
| 146 |
def test_data_object_generation(dataframe: pd.DataFrame) -> None: |
| 147 |
data_object = DataGrid.generate_data_object(dataframe, "ipydguuid", "key") |
| 148 |
expected_output = { |
| 149 |
"data": pd.DataFrame( |
| 150 |
data={ |
| 151 |
"key": ["One", "Two", "Three"], |
| 152 |
"A": [1, 2, 3], |
| 153 |
"B": [4, 5, 6], |
| 154 |
"ipydguuid": [0, 1, 2], |
| 155 |
} |
| 156 |
), |
| 157 |
"schema": { |
| 158 |
"fields": [ |
| 159 |
{"name": "key", "type": "string"}, |
| 160 |
{"name": "A", "type": "integer"}, |
| 161 |
{"name": "B", "type": "integer"}, |
| 162 |
{"name": "ipydguuid", "type": "integer"}, |
| 163 |
], |
| 164 |
"primaryKey": ["key", "ipydguuid"], |
| 165 |
"pandas_version": "0.20.0", |
| 166 |
"primaryKeyUuid": "ipydguuid", |
| 167 |
}, |
| 168 |
"fields": [ |
| 169 |
{"key": None}, |
| 170 |
{"A": None}, |
| 171 |
{"B": None}, |
| 172 |
{"ipydguuid": None}, |
| 173 |
], |
| 174 |
} |
| 175 |
|
| 176 |
# Pin pandas version to avoid potential errors due to differing versions |
| 177 |
data_object["schema"]["pandas_version"] = "0.20.0" |
| 178 |
|
| 179 |
assert data_object["data"].equals(expected_output["data"]) |
| 180 |
assert data_object["schema"] == expected_output["schema"] |
| 181 |
assert data_object["fields"] == expected_output["fields"] |
| 182 |
|
| 183 |
|
| 184 |
def test_selected_cell_values(monkeypatch, datagrid, dataframe): |
| 185 |
# Mocking data returned from front-end |
| 186 |
def mock_get_visible_data(): |
| 187 |
return dataframe |
| 188 |
|
| 189 |
monkeypatch.setattr(datagrid, "get_visible_data", mock_get_visible_data) |
| 190 |
datagrid.select(1, 0, 2, 1) # Select 1A to 2B |
| 191 |
|
| 192 |
assert datagrid.selected_cell_values == [2, 5, 3, 6] |
| 193 |
|
| 194 |
|
| 195 |
def test_dataframe_index_name(dataframe): |
| 196 |
# Setting a custom index name |
| 197 |
dataframe.index.name = "custom_index" |
| 198 |
grid = DataGrid(dataframe) |
| 199 |
|
| 200 |
# Making sure no value is set for index_name |
| 201 |
assert grid._index_name is None |
| 202 |
|
| 203 |
# Checking index name matches DataFrame retrieved from grid |
| 204 |
data = grid.get_visible_data() |
| 205 |
assert data.index.name == "custom_index" |
| 206 |
|
| 207 |
|
| 208 |
def test_user_defined_index_name(dataframe): |
| 209 |
# Setting a custom index name |
| 210 |
dataframe.index.name = "unused_index" |
| 211 |
grid = DataGrid(dataframe, index_name="custom_index") |
| 212 |
|
| 213 |
# Making sure index_name is set |
| 214 |
assert grid._index_name is not None |
| 215 |
|
| 216 |
# Checking index name matches DataFrame retrieved from grid |
| 217 |
index_key = grid.get_dataframe_index(dataframe) |
| 218 |
data_obj = grid.generate_data_object(dataframe, "ipydguuid", index_key) |
| 219 |
|
| 220 |
# Default and unused keys should not be in schema |
| 221 |
assert "key" not in data_obj["schema"]["primaryKey"] |
| 222 |
assert "unused_index" not in data_obj["schema"]["primaryKey"] |
| 223 |
|
| 224 |
# User defined key should be in primary key schema |
| 225 |
assert "custom_index" in data_obj["schema"]["primaryKey"] |
| 226 |
|
| 227 |
|
| 228 |
def test_named_dataframe_index(dataframe): |
| 229 |
# Setting a custom index name |
| 230 |
dataframe.index.name = "my_used_index" |
| 231 |
grid = DataGrid(dataframe) |
| 232 |
|
| 233 |
# Making sure index_name is set |
| 234 |
assert grid._index_name is None |
| 235 |
|
| 236 |
# Generate primary key schema object |
| 237 |
index_key = grid.get_dataframe_index(dataframe) |
| 238 |
data_obj = grid.generate_data_object(dataframe, "ipydguuid", index_key) |
| 239 |
|
| 240 |
# Default and unused keys should not be in schema |
| 241 |
assert "key" not in data_obj["schema"]["primaryKey"] |
| 242 |
|
| 243 |
# User named dataframe index should be in schema |
| 244 |
assert "my_used_index" in data_obj["schema"]["primaryKey"] |
| 245 |
|
| 246 |
|
| 247 |
def test_default_dataframe_index(dataframe): |
| 248 |
# Setting a custom index name |
| 249 |
grid = DataGrid(dataframe) |
| 250 |
|
| 251 |
# Making sure index_name is set |
| 252 |
assert grid._index_name is None |
| 253 |
|
| 254 |
# Generate primary key schema object |
| 255 |
index_key = grid.get_dataframe_index(dataframe) |
| 256 |
data_obj = grid.generate_data_object(dataframe, "ipydguuid", index_key) |
| 257 |
|
| 258 |
# Default and unused keys should not be in schema |
| 259 |
assert "key" in data_obj["schema"]["primaryKey"] |
| 260 |
|
| 261 |
|
| 262 |
def test_setting_column_widths_on_multiindex_grid(): |
| 263 |
import pandas as pd |
| 264 |
|
| 265 |
from ipydatagrid import DataGrid |
| 266 |
|
| 267 |
col_names = [("Parent Column", "SubCol 1"), ("Parent Column", "SubCol 2")] |
| 268 |
df = pd.DataFrame(data={i: [1, 2, 3] for i in col_names}, columns=col_names) |
| 269 |
|
| 270 |
grid = DataGrid(df, layout={"height": "200px"}) |
| 271 |
grid |
| 272 |
|
| 273 |
col_widths = {("Parent Column", "SubCol 1"): 100} |
| 274 |
|
| 275 |
grid.column_widths = col_widths |
| 276 |
|
| 277 |
|
| 278 |
def test_renderers_serialization(): |
| 279 |
col_names = [("Parent Column", "SubCol 1"), ("Parent Column", "SubCol 2")] |
| 280 |
df = pd.DataFrame(data={i: [1, 2, 3] for i in col_names}, columns=col_names) |
| 281 |
|
| 282 |
grid = DataGrid( |
| 283 |
df, |
| 284 |
layout={"height": "200px"}, |
| 285 |
renderers={("Parent Column", "SubCol 1"): TextRenderer()}, |
| 286 |
) |
| 287 |
|
| 288 |
serialized_renderers = _widgets_dict_serialization["to_json"]( |
| 289 |
grid.renderers, grid |
| 290 |
) |
| 291 |
|
| 292 |
assert isinstance(list(serialized_renderers.keys())[0], str) |
| 293 |
|
| 294 |
|
| 295 |
def test_serialization(): |
| 296 |
import datetime |
| 297 |
from decimal import Decimal |
| 298 |
|
| 299 |
df = pd.DataFrame( |
| 300 |
{ |
| 301 |
"text_col": [ |
| 302 |
"a", |
| 303 |
1, |
| 304 |
-2, |
| 305 |
0.23, |
| 306 |
"11", |
| 307 |
math.nan, |
| 308 |
pd.NA, |
| 309 |
Decimal(0.00), |
| 310 |
datetime.date(2022, 8, 19), |
| 311 |
datetime.datetime.now(), |
| 312 |
[1, [2, 3], {4: 5}], |
| 313 |
np.full((2, 3), {"foo": "bar"}), |
| 314 |
np.full((3, 4, 5), 1.0), |
| 315 |
] |
| 316 |
} |
| 317 |
) |
| 318 |
|
| 319 |
# Should not raise an error |
| 320 |
dg = DataGrid(df) |
| 321 |
|
| 322 |
assert dg.data.to_json() == df.to_json() |
| 323 |
|
| 324 |
|
| 325 |
@pytest.mark.parametrize( |
| 326 |
("column", "bar_value", "expected_min", "expected_max"), |
| 327 |
[ |
| 328 |
("B", None, 4.0, 6.0), |
| 329 |
("B", LinearScale(), 4.0, 6.0), |
| 330 |
("B", LinearScale(min=2.2), 2.2, 6.0), |
| 331 |
("B", LinearScale(max=4.4), 4.0, 4.4), |
| 332 |
("B", LogScale(), 4.0, 6.0), |
| 333 |
( |
| 334 |
"dates", |
| 335 |
DateScale(), |
| 336 |
pd.Timestamp("2024-03-05"), |
| 337 |
pd.Timestamp("2024-03-07"), |
| 338 |
), |
| 339 |
], |
| 340 |
) |
| 341 |
def test_bar_renderer_defaults( |
| 342 |
dataframe_with_datetime, |
| 343 |
column, |
| 344 |
bar_value, |
| 345 |
expected_min, |
| 346 |
expected_max, |
| 347 |
): |
| 348 |
expected_type = type(bar_value) if bar_value else LinearScale |
| 349 |
|
| 350 |
grid = DataGrid( |
| 351 |
dataframe_with_datetime, |
| 352 |
renderers={column: BarRenderer(bar_value=bar_value)}, |
| 353 |
) |
| 354 |
|
| 355 |
actual_bar_value = grid.renderers[column].bar_value |
| 356 |
assert isinstance(actual_bar_value, expected_type) |
| 357 |
assert actual_bar_value.min == expected_min |
| 358 |
assert actual_bar_value.max == expected_max |