test_datatable.py 3.0 KB

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