1
0

test_datatable.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os
  2. import pandas as pd
  3. import pytest
  4. import pynecone as pc
  5. from pynecone import utils
  6. from pynecone.components import data_table
  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 utils.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)