test_plotly.py 793 B

123456789101112131415161718192021222324252627282930313233
  1. import numpy as np
  2. import plotly.graph_objects as go
  3. import pytest
  4. from reflex.utils.serializers import serialize, serialize_figure
  5. @pytest.fixture
  6. def plotly_fig() -> go.Figure:
  7. """Get a plotly figure.
  8. Returns:
  9. A random plotly figure.
  10. """
  11. # Generate random data.
  12. data = np.random.randint(0, 10, size=(10, 4))
  13. trace = go.Scatter(
  14. x=list(range(len(data))), y=data[:, 0], mode="lines", name="Trace 1"
  15. )
  16. # Create a graph.
  17. return go.Figure(data=[trace])
  18. def test_serialize_plotly(plotly_fig: go.Figure):
  19. """Test that serializing a plotly figure works.
  20. Args:
  21. plotly_fig: The figure to serialize.
  22. """
  23. value = serialize(plotly_fig)
  24. assert isinstance(value, list)
  25. assert value == serialize_figure(plotly_fig)