Bug in Test of Dependencies
In !89 (merged) I have realised that even though some dependencies where missing in the attribute
ViscoelasticFluid._k_f all tests were always passed.
The reason is a quite subtle bug we have in our tests.
Our tests look like this
class TestClass: def randomly_change_parameters(self) -> None: self.a = self.randomly_change_number(self.a) self.b = self.randomly_change_number(self.b) def assign_parameters(self) -> None: self.cls.a = self.a self.cls.b = self.b def change_and_assign(self) -> None: self.randomly_change_parameters() self.assign_parameters() def c(self) -> None: for _ in self.range_runs: self.change_and_assign() self.assertEqual( self._compute_a(), self.cls.a)
I have omitted a few methods here that do not contribute to the bug.
Let's assume we have a class that is tested that looks like this:
class MyClass: def __init__(self, a, b): self._a = a self._b = b self._c = ActiveVariable(self._compute_c) self._c.is_computed_by(self._a) # self._b missing @property def a(self): return self._a.value @property def b(self): return self._b.value @property def c(self): return self._c.value def _compute_c(self): return self.a * self.b
This code contains a bug.
self._b is missing in the dependencies of
self._c. However, this class would still pass the test.
The reason is that in every loop of the test the value of
self._c.value is recomputed even if
self._b are set to a new value. The problem is that
assign_parameters is called every time even if the value of a variable has not changed. If no change to the variable has happened then the value is simply set again to the same value.
self._a is reset even though it has not changed it will trigger the recomputation of
self._c.value. If now
self._b has changed
self._c.value will be correctly recomputed even though the dependency of
self._b is missing in the code.
We therefore have to make sure that this assignment only happens if a variable has indeed changed. Otherwise dependencies are not tested.