|
| 1 | +import io |
| 2 | + |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import pytest |
| 5 | + |
| 6 | +import mplotutils as mpu |
| 7 | +from mplotutils.tests.test_colorbar import create_fig_aspect |
| 8 | + |
| 9 | +from . import figure_context |
| 10 | + |
| 11 | + |
| 12 | +def test_autodraw_orig_func(): |
| 13 | + |
| 14 | + # NOTE plt.Figure.savefig is the overwritten one |
| 15 | + assert mpu._savefig.savefig_orig is not plt.Figure.savefig |
| 16 | + |
| 17 | + with mpu.autodraw(False): |
| 18 | + assert mpu._savefig.savefig_orig is plt.Figure.savefig |
| 19 | + |
| 20 | + with mpu.autodraw(False): |
| 21 | + with mpu.autodraw(True): |
| 22 | + assert mpu._savefig.savefig_orig is not plt.Figure.savefig |
| 23 | + |
| 24 | + with mpu.autodraw(False): |
| 25 | + savefig_autodraw = plt.Figure.savefig |
| 26 | + |
| 27 | + with mpu.autodraw(True): |
| 28 | + savefig_no_autodraw = plt.Figure.savefig |
| 29 | + |
| 30 | + assert savefig_no_autodraw is not savefig_autodraw |
| 31 | + |
| 32 | + mpu.autodraw(True) |
| 33 | + assert mpu._savefig.savefig_orig is not plt.Figure.savefig |
| 34 | + |
| 35 | + mpu.autodraw(False) |
| 36 | + assert mpu._savefig.savefig_orig is plt.Figure.savefig |
| 37 | + |
| 38 | + # restore to default |
| 39 | + mpu.autodraw(True) |
| 40 | + |
| 41 | + |
| 42 | +def test_ensure_draw_method_called(monkeypatch): |
| 43 | + # this is a pseudo-mock test (I think the actual backend would need to be mocked) |
| 44 | + |
| 45 | + class DrawMethodCalled(Exception): |
| 46 | + pass |
| 47 | + |
| 48 | + def draw(): |
| 49 | + raise DrawMethodCalled() |
| 50 | + |
| 51 | + with figure_context() as f: |
| 52 | + |
| 53 | + monkeypatch.setattr(f.canvas, "draw", draw) |
| 54 | + |
| 55 | + # not called when not autodrawing |
| 56 | + with mpu.autodraw(False): |
| 57 | + f.savefig(io.BytesIO()) |
| 58 | + |
| 59 | + # called when autodrawing |
| 60 | + with pytest.raises(DrawMethodCalled): |
| 61 | + f.savefig(io.BytesIO()) |
| 62 | + |
| 63 | + |
| 64 | +def test_saved_figure_not_the_same_vertical(): |
| 65 | + |
| 66 | + with figure_context() as f: |
| 67 | + create_fig_aspect(aspect=0.5, orientation="vertical") |
| 68 | + |
| 69 | + file_no_autodraw = io.BytesIO() |
| 70 | + |
| 71 | + with mpu.autodraw(False): |
| 72 | + f.savefig(file_no_autodraw) |
| 73 | + |
| 74 | + file_autodraw = io.BytesIO() |
| 75 | + with mpu.autodraw(True): |
| 76 | + f.savefig(file_autodraw) |
| 77 | + |
| 78 | + assert file_no_autodraw.getvalue() != file_autodraw.getvalue() |
| 79 | + |
| 80 | + |
| 81 | +def test_saved_figure_not_the_same_horizontal(): |
| 82 | + |
| 83 | + with figure_context() as f: |
| 84 | + create_fig_aspect(aspect=2, orientation="horizontal") |
| 85 | + # ensure the colorbar is actually on the figure |
| 86 | + f.subplots_adjust(bottom=0.5) |
| 87 | + |
| 88 | + file_no_autodraw = io.BytesIO() |
| 89 | + with mpu.autodraw(False): |
| 90 | + f.savefig(file_no_autodraw) |
| 91 | + |
| 92 | + file_autodraw = io.BytesIO() |
| 93 | + with mpu.autodraw(True): |
| 94 | + f.savefig(file_autodraw) |
| 95 | + |
| 96 | + assert file_no_autodraw.getvalue() != file_autodraw.getvalue() |
0 commit comments