-
Notifications
You must be signed in to change notification settings - Fork 32
Unit test refactor #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
.PHONY: test | ||
test: | ||
python -m pytest --version | ||
python -m pytest test/ | ||
python -m pytest -v test/ | ||
|
||
|
||
.PHONY: lint | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,52 +18,74 @@ def approxeq(x, y, *, err=0.0001): | |
"viscm/examples/sample_diverging_continuous.jscm", | ||
], | ||
) | ||
@pytest.mark.xfail(reason="Test very old; intent unclear") | ||
def test_editor_loads_native(colormap_file): | ||
with open(colormap_file) as f: | ||
data = json.loads(f.read()) | ||
cm = Colormap(None, "CatmulClark", "CAM02-UCS") | ||
cm.load(colormap_file) | ||
viscm = viscm_editor( | ||
uniform_space=cm.uniform_space, | ||
cmtype=cm.cmtype, | ||
method=cm.method, | ||
**cm.params, | ||
) | ||
assert viscm.name == data["name"] | ||
|
||
extensions = data["extensions"]["https://matplotlib.org/viscm"] | ||
xp, yp, fixed = viscm.control_point_model.get_control_points() | ||
|
||
assert extensions["fixed"] == fixed | ||
assert len(extensions["xp"]) == len(xp) | ||
assert len(extensions["yp"]) == len(yp) | ||
assert len(xp) == len(yp) | ||
for i in range(len(xp)): | ||
assert extensions["xp"][i] == xp[i] | ||
assert extensions["yp"][i] == yp[i] | ||
assert extensions["min_Jp"] == viscm.min_Jp | ||
assert extensions["max_Jp"] == viscm.max_Jp | ||
assert extensions["filter_k"] == viscm.cmap_model.filter_k | ||
assert extensions["cmtype"] == viscm.cmtype | ||
|
||
# Decode hexadecimal-encoded colormap string (grouped in units of 3 pairs of | ||
# two-character (0-255) values) to 3-tuples of floats (0-1). | ||
colors_hex = data["colors"] | ||
colors_hex = [colors_hex[i : i + 6] for i in range(0, len(colors_hex), 6)] | ||
colors = [ | ||
[int(c[i : i + 2], 16) / 255 for i in range(0, len(c), 2)] for c in colors_hex | ||
] | ||
|
||
editor_colors = viscm.cmap_model.get_sRGB(num=256)[0].tolist() | ||
|
||
for i in range(len(colors)): | ||
for z in range(3): | ||
# FIXME: The right-hand side of this comparison will always be 0. | ||
# https://github.com/matplotlib/viscm/pull/66#discussion_r1213818015 | ||
assert colors[i][z] == np.rint(editor_colors[i][z] / 256) | ||
# Should the test look more like this? | ||
# assert approxeq(colors[i][z], editor_colors[i][z], err=0.005) | ||
class TestEditorLoad: | ||
def expected(self, colormap_file): | ||
with open(colormap_file) as f: | ||
exp = json.loads(f.read()) | ||
return exp | ||
|
||
def actual(self, colormap_file): | ||
cm = Colormap(None, "CatmulClark", "CAM02-UCS") | ||
cm.load(colormap_file) | ||
act = viscm_editor( | ||
uniform_space=cm.uniform_space, | ||
cmtype=cm.cmtype, | ||
method=cm.method, | ||
**cm.params, | ||
) | ||
return act | ||
|
||
def test_editor_loads_jscm_parameters_match(self, colormap_file): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This gives us a set of 3 passing tests which just check attributes are as expected, and 3 xfail tests which check actual values loaded from a |
||
expected = self.expected(colormap_file) | ||
actual = self.actual(colormap_file) | ||
|
||
assert actual.name == expected["name"] | ||
|
||
extensions = expected["extensions"]["https://matplotlib.org/viscm"] | ||
xp, yp, fixed = actual.control_point_model.get_control_points() | ||
|
||
assert extensions["fixed"] == fixed | ||
assert len(extensions["xp"]) == len(xp) | ||
assert len(extensions["yp"]) == len(yp) | ||
assert len(xp) == len(yp) | ||
for i in range(len(xp)): | ||
assert extensions["xp"][i] == xp[i] | ||
assert extensions["yp"][i] == yp[i] | ||
assert extensions["min_Jp"] == actual.min_Jp | ||
assert extensions["max_Jp"] == actual.max_Jp | ||
assert extensions["filter_k"] == actual.cmap_model.filter_k | ||
assert extensions["cmtype"] == actual.cmtype | ||
|
||
@pytest.mark.xfail(reason="Test very old; intent unclear") | ||
def test_editor_loads_jscm_data_match(self, colormap_file): | ||
expected = self.expected(colormap_file) | ||
actual = self.actual(colormap_file) | ||
|
||
# Decode hexadecimal-encoded colormap string (grouped in units of 3 pairs of | ||
# two-character [00-ff / 0-255] values) to 3-tuples of floats (0-1). | ||
expected_colors_hex = expected["colors"] | ||
expected_colors_hex = [ | ||
expected_colors_hex[i : i + 6] | ||
for i in range(0, len(expected_colors_hex), 6) | ||
] | ||
expected_colors = [ | ||
[int(c[i : i + 2], 16) / 255 for i in range(0, len(c), 2)] | ||
for c in expected_colors_hex | ||
] | ||
|
||
actual_colors = actual.cmap_model.get_sRGB(num=256)[0].tolist() | ||
|
||
for i in range(len(expected_colors)): | ||
for z in range(3): | ||
# FIXME: The right-hand side of this comparison will always be 0. | ||
# https://github.com/matplotlib/viscm/pull/66#discussion_r1213818015 | ||
assert actual_colors[i][z] == np.rint(expected_colors[i][z] / 256) | ||
# Should the test look more like this? | ||
# assert approxeq( | ||
# expected_colors[i][z], | ||
# actual_colors[i][z], | ||
# err=0.005, | ||
# ) | ||
|
||
|
||
# import matplotlib as mpl | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a test that ensures that the editor can load a
.jscm
data file and interpret its values correctly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The refactoring looks good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got that; I'm referring to this assertion here when I say "intent unclear":
Right now I'm not sure why this was ever working :)