1
0

test_datatable.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import pandas as pd
  2. import pytest
  3. import reflex as rx
  4. from reflex.components.gridjs.datatable import DataTable
  5. from reflex.utils import types
  6. from reflex.utils.serializers import serialize, serialize_dataframe
  7. @pytest.mark.parametrize(
  8. "data_table_state,expected",
  9. [
  10. pytest.param(
  11. {
  12. "data": pd.DataFrame(
  13. [["foo", "bar"], ["foo1", "bar1"]], columns=["column1", "column2"]
  14. )
  15. },
  16. "data",
  17. ),
  18. pytest.param({"data": ["foo", "bar"]}, ""),
  19. pytest.param({"data": [["foo", "bar"], ["foo1", "bar1"]]}, ""),
  20. ],
  21. indirect=["data_table_state"],
  22. )
  23. def test_validate_data_table(data_table_state: rx.State, expected):
  24. """Test the str/render function.
  25. Args:
  26. data_table_state: The state fixture.
  27. expected: expected var name.
  28. """
  29. if not types.is_dataframe(data_table_state.data._var_type):
  30. data_table_component = DataTable.create(
  31. data=data_table_state.data, columns=data_table_state.columns
  32. )
  33. else:
  34. data_table_component = DataTable.create(data=data_table_state.data)
  35. data_table_dict = data_table_component.render()
  36. # prefix expected with state name
  37. state_name = data_table_state.get_name()
  38. expected = f"{state_name}.{expected}" if expected else state_name
  39. assert data_table_dict["props"] == [
  40. f"columns={{{expected}.columns}}",
  41. f"data={{{expected}.data}}",
  42. ]
  43. @pytest.mark.parametrize(
  44. "props",
  45. [
  46. {"data": [["foo", "bar"], ["foo1", "bar1"]]},
  47. {
  48. "data": pd.DataFrame([["foo", "bar"], ["foo1", "bar1"]]),
  49. "columns": ["column1", "column2"],
  50. },
  51. ],
  52. )
  53. def test_invalid_props(props):
  54. """Test if value error is thrown when invalid props are passed.
  55. Args:
  56. props: props to pass in component.
  57. """
  58. with pytest.raises(ValueError):
  59. DataTable.create(**props)
  60. @pytest.mark.parametrize(
  61. "fixture, err_msg, is_data_frame",
  62. [
  63. (
  64. "data_table_state2",
  65. "Annotation of the computed var assigned to the data field should be provided.",
  66. True,
  67. ),
  68. (
  69. "data_table_state3",
  70. "Annotation of the computed var assigned to the column field should be provided.",
  71. False,
  72. ),
  73. (
  74. "data_table_state4",
  75. "Annotation of the computed var assigned to the data field should be provided.",
  76. False,
  77. ),
  78. ],
  79. )
  80. def test_computed_var_without_annotation(fixture, request, err_msg, is_data_frame):
  81. """Test if value error is thrown when the computed var assigned to the data/column prop is not annotated.
  82. Args:
  83. fixture: the state.
  84. request: fixture request.
  85. err_msg: expected error message.
  86. is_data_frame: whether data field is a pandas dataframe.
  87. """
  88. with pytest.raises(ValueError) as err:
  89. if is_data_frame:
  90. DataTable.create(data=request.getfixturevalue(fixture).data)
  91. else:
  92. DataTable.create(
  93. data=request.getfixturevalue(fixture).data,
  94. columns=request.getfixturevalue(fixture).columns,
  95. )
  96. assert err.value.args[0] == err_msg
  97. def test_serialize_dataframe():
  98. """Test if dataframe is serialized correctly."""
  99. df = pd.DataFrame(
  100. [["foo", "bar"], ["foo1", "bar1"]], columns=["column1", "column2"]
  101. )
  102. value = serialize(df)
  103. assert value == serialize_dataframe(df)
  104. assert isinstance(value, dict)
  105. assert tuple(value) == ("columns", "data")