reflex/tests/components/datadisplay/test_datatable.py
benedikt-bartscher 3039b54a75
add module prefix to generated state names (#3214)
* add module prefix to state names

* fix state names in test_app

* update state names in test_state

* fix state names in test_var

* fix state name in test_component

* fix state names in test_format

* fix state names in test_foreach

* fix state names in test_cond

* fix state names in test_datatable

* fix state names in test_colors

* fix state names in test_script

* fix state names in test_match

* fix state name in event1 fixture

* fix pyright and darglint

* fix state names in state_tree

* fix state names in redis only test

* fix state names in test_client_storage

* fix state name in js template

* add `get_state_name` and `get_full_state_name` helpers for `AppHarness`

* fix state names in test_dynamic_routes

* use new state name helpers in test_client_storage

* fix state names in test_event_actions

* fix state names in test_event_chain

* fix state names in test_upload

* fix state name in test_login_flow

* fix state names in test_input

* fix state names in test_form_submit

* ruff

* validate state module names

* wtf is going on here?

* remove comments leftover from refactoring

* adjust new test_add_style_embedded_vars

* fix state name in state.js

* fix integration/test_client_state.py

new SessionStorage feature was added with more full state names that need to be formatted in

* fix pre-commit issues in test_client_storage.py

* adjust test_computed_vars

* adjust safe-guards

* fix redis tests with new exception state

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
2024-07-11 11:13:57 -07:00

122 lines
3.6 KiB
Python

import pandas as pd
import pytest
import reflex as rx
from reflex.components.gridjs.datatable import DataTable
from reflex.utils import types
from reflex.utils.serializers import serialize, serialize_dataframe
@pytest.mark.parametrize(
"data_table_state,expected",
[
pytest.param(
{
"data": pd.DataFrame(
[["foo", "bar"], ["foo1", "bar1"]], columns=["column1", "column2"]
)
},
"data",
),
pytest.param({"data": ["foo", "bar"]}, ""),
pytest.param({"data": [["foo", "bar"], ["foo1", "bar1"]]}, ""),
],
indirect=["data_table_state"],
)
def test_validate_data_table(data_table_state: rx.State, expected):
"""Test the str/render function.
Args:
data_table_state: The state fixture.
expected: expected var name.
"""
if not types.is_dataframe(data_table_state.data._var_type):
data_table_component = DataTable.create(
data=data_table_state.data, columns=data_table_state.columns
)
else:
data_table_component = DataTable.create(data=data_table_state.data)
data_table_dict = data_table_component.render()
# prefix expected with state name
state_name = data_table_state.get_name()
expected = f"{state_name}.{expected}" if expected else state_name
assert data_table_dict["props"] == [
f"columns={{{expected}.columns}}",
f"data={{{expected}.data}}",
]
@pytest.mark.parametrize(
"props",
[
{"data": [["foo", "bar"], ["foo1", "bar1"]]},
{
"data": pd.DataFrame([["foo", "bar"], ["foo1", "bar1"]]),
"columns": ["column1", "column2"],
},
],
)
def test_invalid_props(props):
"""Test if value error is thrown when invalid props are passed.
Args:
props: props to pass in component.
"""
with pytest.raises(ValueError):
DataTable.create(**props)
@pytest.mark.parametrize(
"fixture, err_msg, is_data_frame",
[
(
"data_table_state2",
"Annotation of the computed var assigned to the data field should be provided.",
True,
),
(
"data_table_state3",
"Annotation of the computed var assigned to the column field should be provided.",
False,
),
(
"data_table_state4",
"Annotation of the computed var assigned to the data field should be provided.",
False,
),
],
)
def test_computed_var_without_annotation(fixture, request, err_msg, is_data_frame):
"""Test if value error is thrown when the computed var assigned to the data/column prop is not annotated.
Args:
fixture: the state.
request: fixture request.
err_msg: expected error message.
is_data_frame: whether data field is a pandas dataframe.
"""
with pytest.raises(ValueError) as err:
if is_data_frame:
DataTable.create(data=request.getfixturevalue(fixture).data)
else:
DataTable.create(
data=request.getfixturevalue(fixture).data,
columns=request.getfixturevalue(fixture).columns,
)
assert err.value.args[0] == err_msg
def test_serialize_dataframe():
"""Test if dataframe is serialized correctly."""
df = pd.DataFrame(
[["foo", "bar"], ["foo1", "bar1"]], columns=["column1", "column2"]
)
value = serialize(df)
assert value == serialize_dataframe(df)
assert isinstance(value, dict)
assert tuple(value) == ("columns", "data")