Skip to content

Commit 6b35100

Browse files
authored
Merge pull request #66 from mfisher87/unit-test
Fixup original unit test, set up tests to run in GitHub Actions
2 parents f9dd86d + beb1a97 commit 6b35100

File tree

5 files changed

+123
-71
lines changed

5 files changed

+123
-71
lines changed

.github/workflows/test.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: "Unit test"
2+
on:
3+
- "push"
4+
- "pull_request"
5+
6+
jobs:
7+
8+
unit-test-with-pytest:
9+
runs-on: "ubuntu-latest"
10+
strategy:
11+
matrix:
12+
python-version:
13+
- "3.11"
14+
- "3.10"
15+
- "3.9"
16+
- "3.8"
17+
pyqt-dependency:
18+
- "PyQt5"
19+
# - "PyQt6"
20+
21+
steps:
22+
- uses: "actions/checkout@v3"
23+
24+
- name: "Set up Python ${{ matrix.python-version }}"
25+
uses: "actions/setup-python@v4"
26+
with:
27+
python-version: "${{ matrix.python-version }}"
28+
29+
- name: "Install dependencies in pyproject.toml"
30+
run: |
31+
pip install .
32+
pip install pytest pytest-cov ${{ matrix.pyqt-dependency }}
33+
34+
- name: "Run tests"
35+
run: "make test"

.travis.yml

-20
This file was deleted.

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
.PHONY: test
2+
test:
3+
python -m pytest --version
4+
python -m pytest test/
5+
6+
17
.PHONY: lint
28
lint:
9+
pre-commit --version
310
pre-commit run --all-files --show-diff-on-failure --color always
11+
12+
13+
.PHONY: ci
14+
ci: lint test

environment.yml

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ dependencies:
1212

1313
# Development
1414
- "pre-commit"
15+
- "pytest"
16+
- "pytest-cov"
17+
- "pytest-qt"
18+
- "pytest-xvfb"
1519

1620
- pip:
1721
- "build ~=0.10"

tests.py test/test_editor_loads_native.py

+73-51
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,83 @@
1+
import json
2+
13
import numpy as np
4+
import pytest
25

3-
from viscm.bezierbuilder import json
46
from viscm.gui import Colormap, viscm_editor
57

6-
cms = {
7-
"viscm/examples/sample_linear.jscm",
8-
"viscm/examples/sample_diverging.jscm",
9-
"viscm/examples/sample_diverging_continuous.jscm",
10-
}
11-
12-
13-
def test_editor_loads_native():
14-
for k in cms:
15-
with open(k) as f:
16-
data = json.loads(f.read())
17-
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
18-
cm.load(k)
19-
viscm = viscm_editor(
20-
uniform_space=cm.uniform_space,
21-
cmtype=cm.cmtype,
22-
method=cm.method,
23-
**cm.params,
24-
)
25-
assert viscm.name == data["name"]
26-
27-
extensions = data["extensions"]["https://matplotlib.org/viscm"]
28-
xp, yp, fixed = viscm.control_point_model.get_control_points()
29-
30-
assert extensions["fixed"] == fixed
31-
assert len(extensions["xp"]) == len(xp)
32-
assert len(extensions["yp"]) == len(yp)
33-
assert len(xp) == len(yp)
34-
for i in range(len(xp)):
35-
assert extensions["xp"][i] == xp[i]
36-
assert extensions["yp"][i] == yp[i]
37-
assert extensions["min_Jp"] == viscm.min_Jp
38-
assert extensions["max_Jp"] == viscm.max_Jp
39-
assert extensions["filter_k"] == viscm.filter_k
40-
assert extensions["cmtype"] == viscm.cmtype
41-
42-
colors = data["colors"]
43-
colors = [
44-
[int(c[i : i + 2], 16) / 256 for i in range(0, 6, 2)]
45-
for c in [colors[i : i + 6] for i in range(0, len(colors), 6)]
46-
]
47-
editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist()
48-
for i in range(len(colors)):
49-
for z in range(3):
50-
assert colors[i][z] == np.rint(editor_colors[i][z] / 256)
8+
9+
def approxeq(x, y, *, err=0.0001):
10+
return abs(y - x) < err
11+
12+
13+
@pytest.mark.parametrize(
14+
"colormap_file",
15+
[
16+
"viscm/examples/sample_linear.jscm",
17+
"viscm/examples/sample_diverging.jscm",
18+
"viscm/examples/sample_diverging_continuous.jscm",
19+
],
20+
)
21+
@pytest.mark.xfail(reason="Test very old; intent unclear")
22+
def test_editor_loads_native(colormap_file):
23+
with open(colormap_file) as f:
24+
data = json.loads(f.read())
25+
cm = Colormap(None, "CatmulClark", "CAM02-UCS")
26+
cm.load(colormap_file)
27+
viscm = viscm_editor(
28+
uniform_space=cm.uniform_space,
29+
cmtype=cm.cmtype,
30+
method=cm.method,
31+
**cm.params,
32+
)
33+
assert viscm.name == data["name"]
34+
35+
extensions = data["extensions"]["https://matplotlib.org/viscm"]
36+
xp, yp, fixed = viscm.control_point_model.get_control_points()
37+
38+
assert extensions["fixed"] == fixed
39+
assert len(extensions["xp"]) == len(xp)
40+
assert len(extensions["yp"]) == len(yp)
41+
assert len(xp) == len(yp)
42+
for i in range(len(xp)):
43+
assert extensions["xp"][i] == xp[i]
44+
assert extensions["yp"][i] == yp[i]
45+
assert extensions["min_Jp"] == viscm.min_Jp
46+
assert extensions["max_Jp"] == viscm.max_Jp
47+
assert extensions["filter_k"] == viscm.cmap_model.filter_k
48+
assert extensions["cmtype"] == viscm.cmtype
49+
50+
# Decode hexadecimal-encoded colormap string (grouped in units of 3 pairs of
51+
# two-character (0-255) values) to 3-tuples of floats (0-1).
52+
colors_hex = data["colors"]
53+
colors_hex = [colors_hex[i : i + 6] for i in range(0, len(colors_hex), 6)]
54+
colors = [
55+
[int(c[i : i + 2], 16) / 255 for i in range(0, len(c), 2)] for c in colors_hex
56+
]
57+
58+
editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist()
59+
60+
for i in range(len(colors)):
61+
for z in range(3):
62+
# FIXME: The right-hand side of this comparison will always be 0.
63+
# https://github.com/matplotlib/viscm/pull/66#discussion_r1213818015
64+
assert colors[i][z] == np.rint(editor_colors[i][z] / 256)
65+
# Should the test look more like this?
66+
# assert approxeq(colors[i][z], editor_colors[i][z], err=0.005)
5167

5268

5369
# import matplotlib as mpl
54-
# from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
70+
# try:
71+
# from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
72+
# except ImportError:
73+
# try:
74+
# from matplotlib.backends.backend_qt5agg import (
75+
# FigureCanvasQTAgg as FigureCanvas
76+
# )
77+
# except ImportError:
78+
# from matplotlib.backends.backend_qt4agg import (
79+
# FigureCanvasQTAgg as FigureCanvas
80+
# )
5581
# from matplotlib.backends.qt_compat import QtCore, QtGui
5682
#
5783
# def test_editor_add_point():
@@ -144,7 +170,3 @@ def test_editor_loads_native():
144170

145171
# print(linear.control_point_model.get_control_points())
146172
# # print(linear.cmap_model.get_Jpapbp(3))
147-
148-
149-
def approxeq(x, y, err=0.0001):
150-
return abs(y - x) < err

0 commit comments

Comments
 (0)