Skip to content

Commit ce11102

Browse files
committed
Tests: restore objects to initial state after test
Inside test_metadata_eq_.py we test the __eq__ implementations of all classes. In order to do this, we change the attribute of the object and then compare them to the unchanged version of those objects. Usually, we do it in the following steps: 1. create an initial version "a" 2. create a copy of "a" called "b" 3. iterate all attributes inside "b" and change them to a given value 4. check that "a" and "b" are different We do however forget to restore the object `b` to its initial state which means we don't check the `__eq__` correctly as we stop on the first, the found difference which could be of an older attribute changed in one of the past iterations. Signed-off-by: Martin Vrachev <[email protected]>
1 parent c47121b commit ce11102

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/test_metadata_eq_.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def test_metadata_eq_(self) -> None:
6161
for attr, value in [("signed", None), ("signatures", None)]:
6262
setattr(md_2, attr, value)
6363
self.assertNotEqual(md, md_2, f"Failed case: {attr}")
64+
# Restore the old value of the attribute.
65+
setattr(md_2, attr, getattr(md, attr))
6466

6567
def test_md_eq_signatures_reversed_order(self) -> None:
6668
# Test comparing objects with same signatures but different order.
@@ -103,6 +105,8 @@ def test_signed_eq_(self) -> None:
103105
for attr, value in [("version", -1), ("spec_version", "0.0.0")]:
104106
setattr(md_2.signed, attr, value)
105107
self.assertNotEqual(md.signed, md_2.signed, f"Failed case: {attr}")
108+
# Restore the old value of the attribute.
109+
setattr(md_2.signed, attr, getattr(md.signed, attr))
106110

107111
def test_key_eq_(self) -> None:
108112
key_dict = {
@@ -120,6 +124,8 @@ def test_key_eq_(self) -> None:
120124
]:
121125
setattr(key_2, attr, value)
122126
self.assertNotEqual(key, key_2, f"Failed case: {attr}")
127+
# Restore the old value of the attribute.
128+
setattr(key_2, attr, getattr(key, attr))
123129

124130
def test_role_eq_(self) -> None:
125131
role_dict = {
@@ -132,6 +138,8 @@ def test_role_eq_(self) -> None:
132138
for attr, value in [("keyids", []), ("threshold", 10)]:
133139
setattr(role_2, attr, value)
134140
self.assertNotEqual(role, role_2, f"Failed case: {attr}")
141+
# Restore the old value of the attribute.
142+
setattr(role_2, attr, getattr(role, attr))
135143

136144
def test_root_eq_(self) -> None:
137145
md = Metadata.from_bytes(self.metadata["root"])
@@ -147,6 +155,8 @@ def test_root_eq_(self) -> None:
147155

148156
setattr(signed_copy, attr, value)
149157
self.assertNotEqual(md.signed, signed_copy, f"Failed case: {attr}")
158+
# Restore the old value of the attribute.
159+
setattr(signed_copy, attr, getattr(md.signed, attr))
150160

151161
def test_metafile_eq_(self) -> None:
152162
metafile_dict = {
@@ -165,6 +175,8 @@ def test_metafile_eq_(self) -> None:
165175
]:
166176
setattr(metafile_2, attr, value)
167177
self.assertNotEqual(metafile, metafile_2, f"Failed case: {attr}")
178+
# Restore the old value of the attribute.
179+
setattr(metafile_2, attr, getattr(metafile, attr))
168180

169181
def test_timestamp_eq_(self) -> None:
170182
md = Metadata.from_bytes(self.metadata["timestamp"])
@@ -205,6 +217,8 @@ def test_delegated_role_eq_(self) -> None:
205217
setattr(delegated_role_2, attr, value)
206218
msg = f"Failed case: {attr}"
207219
self.assertNotEqual(delegated_role, delegated_role_2, msg)
220+
# Restore the old value of the attribute.
221+
setattr(delegated_role_2, attr, getattr(delegated_role, attr))
208222

209223
def test_delegations_eq_(self) -> None:
210224
delegations_dict = {
@@ -232,6 +246,8 @@ def test_delegations_eq_(self) -> None:
232246
setattr(delegations_2, attr, value)
233247
msg = f"Failed case: {attr}"
234248
self.assertNotEqual(delegations, delegations_2, msg)
249+
# Restore the old value of the attribute.
250+
setattr(delegations_2, attr, getattr(delegations, attr))
235251

236252
def test_targetfile_eq_(self) -> None:
237253
targetfile_dict = {
@@ -296,6 +312,8 @@ def test_targets_eq_(self) -> None:
296312
for attr, value in [("targets", {}), ("delegations", [])]: # type: ignore
297313
setattr(signed_copy, attr, value)
298314
self.assertNotEqual(md.signed, signed_copy, f"Failed case: {attr}")
315+
# Restore the old value of the attribute.
316+
setattr(signed_copy, attr, getattr(md.signed, attr))
299317

300318

301319
# Run unit test.

0 commit comments

Comments
 (0)