test_datatable.py 3.7 KB

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