reflex/tests/components/datadisplay/test_datatable.py
Elijah Ahianyo e8387c8e26
Fix Event chaining in the on_load event handler return not working (#773)
* Fix Event chaining in the on_load event handler return not working

* added async tests

* addressed comments
2023-04-06 22:26:43 -07:00

105 lines
2.9 KiB
Python

import os
import pandas as pd
import pytest
import pynecone as pc
from pynecone.components import data_table
from pynecone.utils import types
@pytest.mark.parametrize(
"data_table_state,expected",
[
pytest.param(
{
"data": pd.DataFrame(
[["foo", "bar"], ["foo1", "bar1"]], columns=["column1", "column2"]
)
},
"data_table_state.data",
),
pytest.param({"data": ["foo", "bar"]}, "data_table_state"),
pytest.param({"data": [["foo", "bar"], ["foo1", "bar1"]]}, "data_table_state"),
],
indirect=["data_table_state"],
)
def test_validate_data_table(data_table_state: pc.Var, expected):
"""Test the str/render function.
Args:
data_table_state: The state fixture.
expected: expected var name.
"""
props = {"data": data_table_state.data}
if not types.is_dataframe(data_table_state.data.type_):
props["columns"] = data_table_state.columns
data_table_component = data_table(**props)
assert (
str(data_table_component)
== f"<DataTableGrid columns={{{expected}.columns}}{os.linesep}data={{"
f"{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):
data_table(**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:
data_table(data=request.getfixturevalue(fixture).data)
else:
data_table(
data=request.getfixturevalue(fixture).data,
columns=request.getfixturevalue(fixture).columns,
)
assert err.value.args[0] == err_msg