From f77c083b83b154d6572aa383b0017e75d45183ae Mon Sep 17 00:00:00 2001 From: Joep Vanlier Date: Sat, 17 Aug 2024 21:50:46 +0200 Subject: [PATCH] ci: don't modify test parametrization during test 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. --- lumicks/pylake/tests/test_scalebar.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lumicks/pylake/tests/test_scalebar.py b/lumicks/pylake/tests/test_scalebar.py index 650f1e157..3b356a352 100644 --- a/lumicks/pylake/tests/test_scalebar.py +++ b/lumicks/pylake/tests/test_scalebar.py @@ -1,5 +1,3 @@ -import json - import numpy as np import pytest import matplotlib.pyplot as plt @@ -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( @@ -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 ], @@ -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):