test_datatable.py 2.9 KB

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