Skip to content

Port the matplotlylib tests from plotly.py - #1

Open
robertoffmoura wants to merge 23 commits into
mainfrom
rm/port-plotly-tests
Open

robertoffmoura wants to merge 23 commits into
mainfrom
rm/port-plotly-tests

Conversation

@robertoffmoura

Copy link
Copy Markdown
Collaborator

plotly.py has 8 matplotlylib test modules in tests/test_optional/test_matplotlylib/ that haven't run since 2022, when all their tests were marked skip. This PR brings them over, gets them running on current matplotlib, and rewrites them as targeted assertions like the ones in tests/test_renderer.py.

Changes

  • Copy the 8 modules from plotly.py and remove the skip marks.
  • Clean up the port:
    • remove unused helpers, the nose setup_package hook (pytest never calls it), redundant matplotlib.use("Agg") calls and debugging prints
    • call mpl_to_plotly directly
    • convert test_date_times to plain pytest functions
    • move the tests from tests/test_optional/test_matplotlylib/ to tests/
  • Update calls to matplotlib APIs that changed since 2014: bar(left=…), barh(bottom=…), set_xticks(ticks, True) (the True meant minor) and plot_date.
  • Replace the full-figure snapshots in tests/data/ with assertions on just the properties each test is about. The snapshots compared margins, axis ranges and fonts, so any styling change broke them. Their comparison helper also passed when the output was missing expected keys or points, and test_blank_subplots never asserted on its traces.
  • Add an autouse fixture in tests/conftest.py that closes matplotlib figures after each test.
  • Add pandas to the dev extras for the date tests.

Copy tests/test_optional/test_matplotlylib (8 test modules and their
expected data) from plotly.py, together with the helpers they use,
tests/utils.py and tests/test_optional/optional_utils.py.

Changes from the plotly.py versions:
- Import matplotlylib and matplotlib directly. plotly.py looked them up
  through plotly.optional_imports because they were optional there; here
  they are always installed.
- Call matplotlylib.mpl_to_plotly in test_date_times instead of
  plotly.tools.mpl_to_plotly, which runs plotly's own copy of
  matplotlylib.
- Remove the @pytest.mark.skip marks, which plotly.py added in 2022, and
  the pytest imports left unused by that.

Add pandas to the dev extras, since test_date_times imports it.

12 of the 16 ported tests fail and are fixed in later commits.
Nothing uses NumpyTestUtilsMixin or its assert_fig_equal and
assert_dict_equal methods. Remove it, the imports only it used, and the
references to it in the strip_dict_params docstring.
Nothing uses TestCaseNoTemplate. Remove it and the imports only it used.
setup_package is a nose hook from when plotly.py ran its tests with
nose. pytest does not call it (checked with pytest 7.4.4 and 9.2.0), so
it has not silenced any warnings since plotly.py switched to pytest.
Keep the file empty, since it still marks the directory as a package.
tests/__init__.py already selects the Agg backend, and Python runs it
before importing anything under tests/, so the matplotlib.use("Agg")
calls in tests/test_optional/__init__.py and optional_utils.py have no
effect. The try/except around the first one also guarded against
matplotlib being missing, which was possible in plotly.py but not here.
Keep the now-empty __init__.py, since it still marks the directory as a
package.
- print(exporter) in run_fig, which only printed the object's default
  repr
- print calls in test_scatter and test_date_times. pytest already shows
  the differing values when the assertions after them fail.
- A commented-out assertion in test_axis_scales
- The unused `_ =` assignments of set_xticks and set_yticks in
  test_axis_scales. The calls themselves stay, since they set up the
  figure under test.
The ported tests converted figures with run_fig, a test helper that
builds a PlotlyRenderer, runs an Exporter on it and returns the renderer.
mpl_to_plotly does the same with its default arguments and returns the
figure, so call it directly, as test_renderer does, and remove
optional_utils.py, which only held run_fig.
Turn the TestDateTimes unittest.TestCase methods into module-level test
functions and their self.assertEqual calls into plain asserts, as in
the rest of the test suite.
Replace the comp_nums and comp_num_list helpers with pytest.approx,
keeping the same absolute tolerance. comp_num_list compared lists with
zip, so a list shorter than the expected one passed; pytest.approx also
checks that the lengths match.
The test only checks the x values, but drew its y values from
random.randint, so each run plotted different data. Use fixed values
instead.
Use the figure of the axes that DataFrame.plot returns instead of
plt.gcf(), so the test does not depend on which figure is current.
mpl_to_plotly closes the figure it converts, but a test that fails
before calling it leaves its figure open for the rest of the run. Add an
autouse fixture that closes all figures after each test in tests/.
plotly.py kept these tests in tests/test_optional/test_matplotlylib
because they need matplotlib, an optional dependency there, and its CI
runs tests/test_optional separately with the optional extras installed.
Here matplotlib is required and all tests run together, so move the
test modules and their expected data next to test_renderer.py and
simplify their relative imports.
Five ported tests crashed before converting their figures because they
used matplotlib APIs that have changed since 2014:
- bar(left=...) and barh(bottom=...): these arguments are now x and y
- set_xticks(ticks, True) and set_yticks(ticks, True): the second
  positional argument used to be minor and is now labels, so pass
  minor=True
- plot_date, removed in matplotlib 3.11: with its default arguments it
  called xaxis_date() and then plot(x, y, "o"), so do that directly

The tests now get as far as comparing the figures with their expected
data, which dates from 2014 and is updated in later commits.
The tests compared each trace's x and y with the input through zip, so
they passed when the output dropped points, and one assertion message
reported the wrong trace. Check the number of traces and compare x and
y as whole lists instead, including the bar positions that the bar
tests did not check. Move the input data into the test module and
delete tests/data/data.py.
Compare only the bar properties each test is about: orientation, the
bar positions and lengths, colour, opacity and bargap. The full-figure
snapshots also compared margins, axis ranges and fonts, so they failed
on unrelated styling changes.

The snapshot for test_h_and_v_bars expected bargap=1, the raw gap in
data units. Since 7cfc307 bargap is the gap as a fraction of the bar
spacing, so the horizontal bars (14 high at spacing 15) give 1/15.

Move the input data into the tests and delete tests/data/bars.py.
Compare only the marker properties each test is about: mode, the
points, marker symbol, size and colour. The full-figure snapshots also
compared the layout, so they failed on unrelated styling changes. All
of these properties still match the 2014 snapshots.

Move the input data into the tests and delete tests/data/scatter.py.
Compare only the properties each test is about: mode, name, the points,
and the line colour, dash and width or the marker symbol, size, colour
and opacity. The full-figure snapshots also compared the layout, so
they failed on unrelated styling changes.

The snapshots wrote line colours as "rgba (r, g, b, a)". Since efb8905
the converter writes them without the space, so the tests expect that.

Move the input data into the tests and delete tests/data/lines.py.
Check that each of the four corner texts becomes an annotation in paper
coordinates, with the expected anchors and alignment and a position
within 0.005 of the input. The full-figure snapshot also compared the
plotted lines and the layout, so it failed on unrelated changes, and
compared positions to 12 digits.

The annotations still match the 2014 snapshot.

Delete tests/data/annotations.py.
Check the axis properties the test is about: both axes are linear, their
ranges follow the view limits that the minor ticks widen, and, since
matplotlib places the major ticks automatically, plotly gets their
number rather than explicit tick values or a tick spacing. The
full-figure snapshot also compared the plotted line and the rest of the
layout, so it failed on unrelated changes.

The axes still match the 2014 snapshot.

Delete tests/data/axis_scales.py.
Check that each of the eight GridSpec subplots becomes a pair of plotly
axes whose domains match its cells and which anchor each other, and
that the one line is drawn on the first subplot. The domains are worked
out from the GridSpec geometry instead of copied as 12-digit numbers.

The full-figure snapshot also compared each axis's styling, so it
failed on unrelated changes, and it never asserted on the result of
comparing the traces. The domains and anchors still match the 2014
snapshot.

Delete tests/data/subplots.py and tests/data/__init__.py, since nothing
uses the tests/data package any more.
test_normal_mpl_dates also checked matplotlib's internal day number for
the first date, which counted from year 1 when the test was written and
now counts from 1970. That says nothing about the conversion, so drop
it and check the date axis, the marker mode and the x and y values.

test_pandas_time_series_date_formatter finds a real bug: for a pandas
time series plot, the converter sets a date axis but keeps matplotlib's
day numbers as the x values, which plotly reads as milliseconds since
1970. plotly 7.2.0's built-in copy of matplotlylib has the same bug.
Mark the test as a strict xfail until the bug is fixed, and drop its
check of matplotlib's internal day number.
compare_dict, strip_dict_params and is_num_list were only used by the
full-figure snapshot comparisons, which the ported tests no longer make.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant