Skip to content

Commit 1e698f7

Browse files
committed
[tests] improve testing process by catching warning
and explain it in doc #36
1 parent e1aa83c commit 1e698f7

16 files changed

Lines changed: 98 additions & 48 deletions

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ src/tests/outputs/**
1818
# Doxygen output
1919
doxygen/*
2020

21-
2221
# Virtual environment
2322
.venv/
2423
*.egg-info/
@@ -29,4 +28,7 @@ dist/
2928
# Tox
3029
.tox/
3130
.coverage
32-
coverage.xml
31+
coverage.xml
32+
33+
# Mkdocs site
34+
site/**

docs/develop/contributing.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ You can also submit a pull request with the desired feature or the correction of
1010

1111
## Development
1212

13-
The actual code maybe quite messy (is with your contribution you manage to imrpove it, I thank you in advance !).
13+
The actual code maybe quite messy (is with your contribution you manage to improve it, I thank you in advance !).
1414
The source code of the package are present in the directory `src/tikzplotly`, in which each file is dedicated to a specific feature of the library.
1515

1616
Some external packages are necessary to make tikzplotly work, that are specified in the `requirements.txt` file.
@@ -40,13 +40,19 @@ tox -- --cov tikzplotly --cov-report html --cov-report term
4040

4141
The code coverage is available in the directory `htmlcov`.
4242

43-
!!! info "Note about coverage"
43+
!!! Tip "Note about coverage"
4444
The coverage CI is quite strict, so you need to cover all the modification to pass it.
45-
I found that tedious at first, but actually making it pass make me realize that there was some bogs in the code!
45+
I found that tedious at first, but actually making it pass make me realize that there were some bogs in the code!
46+
47+
??? Info "Pytest"
48+
Some warnings are ignored by `pytest`, as they are intended for the user in some specific cases, that are not relevant in general test cases.
49+
This corresponds to:
50+
- the warning raised when [heat maps](../plot/supported/#heat-maps) PNG file is not resized (cf [issue comment](https://github.com/thomas-saigre/tikzplotly/issues/6#issuecomment-2106180586)),
51+
- warning for features that are not yet implemented (such as text templates, or `barpolar` plots).
4652

4753

4854
## Documentation
4955

50-
Feel free to add some comments on this page, espacially if there are some notable differences between plotly and pgfplots (see [this page](../plot/NB.md)).
51-
This pages are written in Markdown and are present in the directory `docs`.
56+
Feel free to add some comments on this page, especially if there are some notable differences between plotly and pgfplots (see [this page](../plot/NB.md)).
57+
These pages are written in Markdown and are present in the directory `docs`.
5258
The site is build using [Mkdocs-materials](https://squidfunk.github.io/mkdocs-material/).

docs/develop/tests.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Local tests
44

5-
These tests are for development purpose, to ensure that the features that are developped are working as expected.
5+
These tests are for development purpose, to ensure that the features that are developed are working as expected.
66
They are present in the directory `src/tests` and can be run with the following command, from the `src` directory.
77

88
```bash

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ dev = ["pandas"]
3636
[project.urls]
3737
Code = "https://github.com/thomas-saigre/tikzplotly"
3838
Issues = "https://github.com/thomas-saigre/tikzplotly/issues"
39+
40+
41+
[tool.pytest.ini_options]
42+
filterwarnings = [
43+
"ignore:png image has not been reduced.*:UserWarning",
44+
"ignore:Text template is not supported yet.:UserWarning",
45+
]

requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
numpy
22
plotly
33
pillow
4-
kaleido<1.0.0
4+
kaleido
55
tox
66
pandas
77
mkdocs-material[recommended]
88
mkdocs-git-revision-date-localized-plugin
9+
pytest
10+
pytest-cov
11+
pytest-codeblocks

src/tikzplotly/_histogram.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ def draw_histogram(trace, axis: Axis, colors_set, row_sep="\\\\"):
9292
axis.add_option("x filter/.expression", "rawy")
9393
axis.add_option("y filter/.expression", "rawx")
9494
hist_options["handler/.style"] = "{xbar interval}"
95+
else: # trace.x and trace.y are both empty
96+
warn("Normally, we should reach this line")
97+
data_str = ""
9598

9699

97100

tests/test_colors.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
22
import pathlib
3+
from contextlib import nullcontext
34

45
import numpy
56
import plotly.graph_objects as go
7+
import plotly.express as px
68
import pytest
79

810
from .helpers import assert_equality
@@ -17,6 +19,12 @@ def plot_color(color_scheme):
1719
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16], marker_color=color_scheme))
1820
return fig
1921

22+
def plot_transparent_background():
23+
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
24+
fig.update_layout(plot_bgcolor='rgba(255, 182, 193, .5)')
25+
26+
return fig
27+
2028

2129
@pytest.mark.parametrize(
2230
"color, warning_match",
@@ -35,7 +43,11 @@ def plot_color(color_scheme):
3543
def test_color(color, warning_match, request):
3644
id = request.node.callspec.id
3745
if warning_match is None:
38-
assert_equality(plot_color(color), os.path.join(this_dir, test_name, f"{test_name}_{id}_reference.tex"))
46+
context = nullcontext()
3947
else:
40-
with pytest.warns(UserWarning, match=warning_match):
41-
assert_equality(plot_color(color), os.path.join(this_dir, test_name, f"{test_name}_{id}_reference.tex"))
48+
context = pytest.warns(UserWarning, match=warning_match)
49+
with context:
50+
assert_equality(plot_color(color), os.path.join(this_dir, test_name, f"{test_name}_{id}_reference.tex"))
51+
52+
def test_transparent_background():
53+
assert_equality(plot_transparent_background(), os.path.join(this_dir, test_name, test_name + "_transparent_background_reference.tex"))

tests/test_specific/test_specific_transparent_background_reference.tex renamed to tests/test_colors/test_colors_transparent_background_reference.tex

File renamed without changes.

tests/test_heatmap.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import plotly.express as px
22
import plotly.graph_objects as go
33
import numpy as np
4+
import pytest
45
import os
56
from .helpers import assert_equality
67
import pathlib
@@ -22,6 +23,7 @@ def plot_2():
2223
return fig
2324

2425
def plot_3():
26+
# A plot with no colorscale
2527
fig = go.Figure(data=go.Heatmap(
2628
z=[[1, None, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]],
2729
x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
@@ -35,7 +37,7 @@ def plot_4():
3537
programmers = ['Alex','Nicole','Sara','Etienne','Chelsea','Jody','Marianne']
3638

3739
base = datetime.datetime(2021, 7, 20, 19, 30, 0)
38-
dates = base - np.arange(180) * datetime.timedelta(days=1)
40+
dates = [base - datetime.timedelta(days=int(i)) for i in np.arange(180)]
3941
np.random.seed(43)
4042
z = np.random.poisson(size=(len(programmers), len(dates)))
4143

@@ -51,7 +53,7 @@ def plot_4():
5153

5254
return fig
5355

54-
def plot_5():
56+
def plot_empty_trace():
5557
fig = px.imshow([[1, 20, 30],
5658
[20, 1, 60],
5759
[30, 60, 1]])
@@ -65,10 +67,12 @@ def test_2():
6567
assert_equality(plot_2(), os.path.join(this_dir, test_name, test_name + "_2_reference.tex"), img_name="/tmp/tikzplotly/fig2.png")
6668

6769
def test_3():
68-
assert_equality(plot_3(), os.path.join(this_dir, test_name, test_name + "_3_reference.tex"), img_name="/tmp/tikzplotly/fig3.png")
70+
with pytest.warns(UserWarning, match="No colorscale found, using default"):
71+
assert_equality(plot_3(), os.path.join(this_dir, test_name, test_name + "_3_reference.tex"), img_name="/tmp/tikzplotly/fig3.png")
6972

7073
def test_4():
7174
assert_equality(plot_4(), os.path.join(this_dir, test_name, test_name + "_4_reference.tex"), img_name="/tmp/tikzplotly/fig4.png")
7275

73-
def test_5():
74-
assert_equality(plot_5(), os.path.join(this_dir, test_name, test_name + "_5_reference.tex"))
76+
def test_empty_trace():
77+
with pytest.warns(UserWarning, match="Adding empty trace."):
78+
assert_equality(plot_empty_trace(), os.path.join(this_dir, test_name, test_name + "_5_reference.tex"))

tests/test_histograms.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import plotly.graph_objects as go
33
import numpy as np
44
import os
5+
from contextlib import nullcontext
56
from .helpers import assert_equality
67
import pathlib
78
import pytest
@@ -77,7 +78,12 @@ def test_3():
7778

7879
@pytest.mark.parametrize("histnorm", ["percent", "probability", "density", "probability density"])
7980
def test_4(histnorm):
80-
assert_equality(plot_4(histnorm), os.path.join(this_dir, test_name, test_name + "_4_reference.tex"))
81+
if histnorm in ["percent", "probability", "density"]:
82+
context = pytest.warns(UserWarning, match=r"Sorry, I did not find an equivalent for histnorm='\w+' in TikZ*")
83+
else:
84+
context = nullcontext()
85+
with context:
86+
assert_equality(plot_4(histnorm), os.path.join(this_dir, test_name, test_name + "_4_reference.tex"))
8187

8288
def test_5():
8389
assert_equality(plot_5(), os.path.join(this_dir, test_name, test_name + "_5_reference.tex"))
@@ -86,7 +92,8 @@ def test_6():
8692
assert_equality(plot_6(), os.path.join(this_dir, test_name, test_name + "_6_reference.tex"))
8793

8894
def test_7():
89-
assert_equality(plot_7(), os.path.join(this_dir, test_name, test_name + "_7_reference.tex"))
95+
with pytest.warns(UserWarning, match="To the best of our knowledge, other aggregate function than 'count' are not supported in pgfplots.*"):
96+
assert_equality(plot_7(), os.path.join(this_dir, test_name, test_name + "_7_reference.tex"))
9097

9198
def test_8():
9299
assert_equality(plot_8(), os.path.join(this_dir, test_name, test_name + "_8_reference.tex"))

0 commit comments

Comments
 (0)