Skip to content

Commit

Permalink
ci: don't modify test parametrization during test
Browse files Browse the repository at this point in the history
Previously, the test parametrization was using a mutable type, which got modified by the test itself. This results in the test failing every run after the first. By changing it to an immutable type, we are forced to first convert it into a modifiable list explicitly in the test (copying its contents) making sure we don't modify the source data in the parametrization.
  • Loading branch information
JoepVanlier committed Sep 18, 2024
1 parent 300b7e0 commit f77c083
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lumicks/pylake/tests/test_scalebar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import numpy as np
import pytest
import matplotlib.pyplot as plt
Expand All @@ -25,7 +23,7 @@ def _validate_elements(ref_elements, item):
else:
ref_elements.pop(idx)
else:
raise AssertionError("Missing element")
raise AssertionError(f"Missing element {item}")


@pytest.mark.parametrize(
Expand All @@ -35,32 +33,32 @@ def _validate_elements(ref_elements, item):
# All entries
(
1, 2, "x", "y", "upper right", "white", 2, 2, 16,
["Text(0, 0, 'y')", "Text(0, 0, 'x')", "Rectangle(xy=(0, 2), width=1, height=0, angle=0)", "Rectangle(xy=(0, 0), width=0, height=2, angle=0)", "Text(0, 0, 'x')"]
("Text(0, 0, 'y')", "Text(0, 0, 'x')", "Rectangle(xy=(0, 2), width=1, height=0, angle=0)", "Rectangle(xy=(0, 0), width=0, height=2, angle=0)", "Text(0, 0, 'x')"),
),
# No label for x
(
1, 2, None, "y", "upper right", "white", 2, 2, 16,
["Text(0, 0, 'y')", "Rectangle(xy=(0, 2), width=1, height=0, angle=0)", "Rectangle(xy=(0, 0), width=0, height=2, angle=0)"]
("Text(0, 0, 'y')", "Rectangle(xy=(0, 2), width=1, height=0, angle=0)", "Rectangle(xy=(0, 0), width=0, height=2, angle=0)"),
),
# No label for y
(
1, 2, "x", None, "upper right", "white", 2, 2, 16,
["Rectangle(xy=(0, 2), width=1, height=0, angle=0)", "Rectangle(xy=(0, 0), width=0, height=2, angle=0)", "Text(0, 0, 'x')"]
("Rectangle(xy=(0, 2), width=1, height=0, angle=0)", "Rectangle(xy=(0, 0), width=0, height=2, angle=0)", "Text(0, 0, 'x')"),
),
# No labels
(
1, 2, None, None, "upper right", "white", 2, 2, 16,
["Rectangle(xy=(0, 2), width=1, height=0, angle=0)", "Rectangle(xy=(0, 0), width=0, height=2, angle=0)"]
("Rectangle(xy=(0, 2), width=1, height=0, angle=0)", "Rectangle(xy=(0, 0), width=0, height=2, angle=0)"),
),
# Only one axis
(
1, 0, "x", "y", "upper right", "white", 2, 2, 16,
["Rectangle(xy=(0, 0), width=1, height=0, angle=0)", "Text(0, 0, 'x')"],
("Rectangle(xy=(0, 0), width=1, height=0, angle=0)", "Text(0, 0, 'x')"),
),
# Only one axis
(
0, 2, "x", "y", "upper right", "white", 2, 2, 16,
["Text(0, 0, 'y')", "Rectangle(xy=(0, 0), width=0, height=2, angle=0)"],
("Text(0, 0, 'y')", "Rectangle(xy=(0, 0), width=0, height=2, angle=0)"),
),
# fmt:on
],
Expand All @@ -75,9 +73,12 @@ def test_scalebar(
axes.transData, size_x, size_y, label_x, label_y, loc, color, separation, barwidth, fontsize
)

_validate_elements(reference, box)
if reference:
raise AssertionError(f"Not all elements were accounted for: {reference}")
# Note that _validate_elements pops items from this list (hence we copy to prevent mutating the
# input parametrization)
reference_list = list(reference)
_validate_elements(reference_list, box)
if reference_list:
raise AssertionError(f"Not all elements were accounted for: {reference_list}")


def validate_args(refs):
Expand Down

0 comments on commit f77c083

Please sign in to comment.