test_datatable.py 2.9 KB

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