test_datatable.py 3.5 KB

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