test_datatable.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_table_state.data",
  17. ),
  18. pytest.param({"data": ["foo", "bar"]}, "data_table_state"),
  19. pytest.param({"data": [["foo", "bar"], ["foo1", "bar1"]]}, "data_table_state"),
  20. ],
  21. indirect=["data_table_state"],
  22. )
  23. def test_validate_data_table(data_table_state: rx.Var, 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. assert data_table_dict["props"] == [
  37. f"columns={{{expected}.columns}}",
  38. f"data={{{expected}.data}}",
  39. ]
  40. @pytest.mark.parametrize(
  41. "props",
  42. [
  43. {"data": [["foo", "bar"], ["foo1", "bar1"]]},
  44. {
  45. "data": pd.DataFrame([["foo", "bar"], ["foo1", "bar1"]]),
  46. "columns": ["column1", "column2"],
  47. },
  48. ],
  49. )
  50. def test_invalid_props(props):
  51. """Test if value error is thrown when invalid props are passed.
  52. Args:
  53. props: props to pass in component.
  54. """
  55. with pytest.raises(ValueError):
  56. DataTable.create(**props)
  57. @pytest.mark.parametrize(
  58. "fixture, err_msg, is_data_frame",
  59. [
  60. (
  61. "data_table_state2",
  62. "Annotation of the computed var assigned to the data field should be provided.",
  63. True,
  64. ),
  65. (
  66. "data_table_state3",
  67. "Annotation of the computed var assigned to the column field should be provided.",
  68. False,
  69. ),
  70. (
  71. "data_table_state4",
  72. "Annotation of the computed var assigned to the data field should be provided.",
  73. False,
  74. ),
  75. ],
  76. )
  77. def test_computed_var_without_annotation(fixture, request, err_msg, is_data_frame):
  78. """Test if value error is thrown when the computed var assigned to the data/column prop is not annotated.
  79. Args:
  80. fixture: the state.
  81. request: fixture request.
  82. err_msg: expected error message.
  83. is_data_frame: whether data field is a pandas dataframe.
  84. """
  85. with pytest.raises(ValueError) as err:
  86. if is_data_frame:
  87. DataTable.create(data=request.getfixturevalue(fixture).data)
  88. else:
  89. DataTable.create(
  90. data=request.getfixturevalue(fixture).data,
  91. columns=request.getfixturevalue(fixture).columns,
  92. )
  93. assert err.value.args[0] == err_msg
  94. def test_serialize_dataframe():
  95. """Test if dataframe is serialized correctly."""
  96. df = pd.DataFrame(
  97. [["foo", "bar"], ["foo1", "bar1"]], columns=["column1", "column2"]
  98. )
  99. value = serialize(df)
  100. assert value == serialize_dataframe(df)
  101. assert isinstance(value, dict)
  102. assert tuple(value) == ("columns", "data")