|
5 | 5 | import os
|
6 | 6 | import itertools
|
7 | 7 | import warnings
|
8 |
| - |
| 8 | +import pytest |
9 | 9 |
|
10 | 10 | import _plotly_utils.exceptions
|
11 | 11 | from chart_studio.plotly import plotly as py
|
12 |
| -from chart_studio.tests.utils import PlotlyTestCase |
13 |
| - |
14 |
| - |
15 |
| -class TestImage(PlotlyTestCase): |
16 |
| - def setUp(self): |
17 |
| - super(TestImage, self).setUp() |
18 |
| - py.sign_in("PlotlyImageTest", "786r5mecv0") |
19 |
| - self.data = [{"x": [1, 2, 3], "y": [3, 1, 6]}] |
20 |
| - |
21 |
| - |
22 |
| -def _generate_image_get_returns_valid_image_test(image_format, width, height, scale): |
23 |
| - def test(self): |
24 |
| - # TODO: better understand why this intermittently fails. See #649 |
25 |
| - num_attempts = 5 |
26 |
| - for i in range(num_attempts): |
27 |
| - if i > 0: |
28 |
| - warnings.warn("image test intermittently failed, retrying...") |
29 |
| - try: |
30 |
| - image = py.image.get(self.data, image_format, width, height, scale) |
31 |
| - if image_format in ["png", "jpeg"]: |
32 |
| - assert imghdr.what("", image) == image_format |
33 |
| - return |
34 |
| - except (KeyError, _plotly_utils.exceptions.PlotlyError): |
35 |
| - if i == num_attempts - 1: |
36 |
| - raise |
37 |
| - |
38 |
| - return test |
39 |
| - |
40 |
| - |
41 |
| -def _generate_image_save_as_saves_valid_image(image_format, width, height, scale): |
42 |
| - def _test(self): |
43 |
| - f, filename = tempfile.mkstemp(".{}".format(image_format)) |
44 |
| - py.image.save_as( |
45 |
| - self.data, |
46 |
| - filename, |
47 |
| - format=image_format, |
48 |
| - width=width, |
49 |
| - height=height, |
50 |
| - scale=scale, |
51 |
| - ) |
52 |
| - if image_format in ["png", "jpeg"]: |
53 |
| - assert imghdr.what(filename) == image_format |
54 |
| - else: |
55 |
| - assert os.path.getsize(filename) > 0 |
56 |
| - |
57 |
| - os.remove(filename) |
58 |
| - |
59 |
| - return _test |
60 |
| - |
61 |
| - |
62 |
| -kwargs = { |
63 |
| - "format": ["png", "jpeg", "pdf", "svg", "emf"], |
64 |
| - "width": [None, 300], |
65 |
| - "height": [None, 300], |
66 |
| - "scale": [None, 5], |
67 |
| -} |
68 | 12 |
|
69 |
| -for args in itertools.product( |
70 |
| - kwargs["format"], kwargs["width"], kwargs["height"], kwargs["scale"] |
71 |
| -): |
72 |
| - for test_generator in [ |
73 |
| - _generate_image_get_returns_valid_image_test, |
74 |
| - _generate_image_save_as_saves_valid_image, |
75 |
| - ]: |
| 13 | +py.sign_in("PlotlyImageTest", "786r5mecv0") |
| 14 | +data = [{"x": [1, 2, 3], "y": [3, 1, 6]}] |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize('image_format', ("png", "jpeg", "pdf", "svg", "emf")) |
| 18 | +@pytest.mark.parametrize('width', (None, 300)) |
| 19 | +@pytest.mark.parametrize('height', (None, 300)) |
| 20 | +@pytest.mark.parametrize('scale', (None, 5)) |
| 21 | +def test_image_get_returns_valid_image_test(image_format, width, height, scale): |
| 22 | + # TODO: better understand why this intermittently fails. See #649 |
| 23 | + num_attempts = 5 |
| 24 | + for i in range(num_attempts): |
| 25 | + if i > 0: |
| 26 | + warnings.warn("image test intermittently failed, retrying...") |
| 27 | + try: |
| 28 | + image = py.image.get(data, image_format, width, height, scale) |
| 29 | + if image_format in ["png", "jpeg"]: |
| 30 | + assert imghdr.what("", image) == image_format |
| 31 | + return |
| 32 | + except (KeyError, _plotly_utils.exceptions.PlotlyError): |
| 33 | + if i == num_attempts - 1: |
| 34 | + raise |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize('image_format', ("png", "jpeg", "pdf", "svg", "emf")) |
| 38 | +@pytest.mark.parametrize('width', (None, 300)) |
| 39 | +@pytest.mark.parametrize('height', (None, 300)) |
| 40 | +@pytest.mark.parametrize('scale', (None, 5)) |
| 41 | +def test_image_save_as_saves_valid_image(image_format, width, height, scale): |
| 42 | + f, filename = tempfile.mkstemp(".{}".format(image_format)) |
| 43 | + py.image.save_as( |
| 44 | + data, |
| 45 | + filename, |
| 46 | + format=image_format, |
| 47 | + width=width, |
| 48 | + height=height, |
| 49 | + scale=scale, |
| 50 | + ) |
| 51 | + if image_format in ["png", "jpeg"]: |
| 52 | + assert imghdr.what(filename) == image_format |
| 53 | + else: |
| 54 | + assert os.path.getsize(filename) > 0 |
| 55 | + |
| 56 | + os.remove(filename) |
76 | 57 |
|
77 |
| - _test = test_generator(*args) |
78 |
| - arg_string = ", ".join([str(a) for a in args]) |
79 |
| - test_name = test_generator.__name__.replace("_generate", "test") |
80 |
| - test_name += "({})".format(arg_string) |
81 |
| - setattr(TestImage, test_name, _test) |
0 commit comments