Skip to content

Commit 7620478

Browse files
committed
Use decorator in test_updater_with_simulator
Apply the same decorator we used inside test_metadata_serialization.py in order to receive more helpful messages with a use case fails in test_targets. Signed-off-by: Martin Vrachev <[email protected]>
1 parent 75f6b00 commit 7620478

File tree

1 file changed

+53
-32
lines changed

1 file changed

+53
-32
lines changed

tests/test_updater_with_simulator.py

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import os
1111
import sys
1212
import tempfile
13-
from typing import Optional
13+
from typing import Dict, Callable, Optional, Tuple
1414
from tuf.exceptions import UnsignedMetadataError
1515
import unittest
1616

@@ -19,6 +19,23 @@
1919
from tests import utils
2020
from tests.repository_simulator import RepositorySimulator
2121

22+
# DataSet is only here so type hints can be used:
23+
# It is a dict of name to test dict
24+
DataSet = Dict[str, str]
25+
26+
# Test runner decorator: Runs the test as a set of N SubTests,
27+
# (where N is number of items in dataset), feeding the actual test
28+
# function one test case at a time
29+
def run_sub_tests_with_dataset(dataset: DataSet):
30+
def real_decorator(function: Callable[["TestUpdater", str], None]):
31+
def wrapper(test_cls: "TestUpdater"):
32+
for case, data in dataset.items():
33+
with test_cls.subTest(case=case):
34+
function(test_cls, data)
35+
return wrapper
36+
return real_decorator
37+
38+
2239
class TestUpdater(unittest.TestCase):
2340
# set dump_dir to trigger repository state dumps
2441
dump_dir:Optional[str] = None
@@ -79,43 +96,47 @@ def test_refresh(self):
7996

8097
self._run_refresh()
8198

82-
def test_targets(self):
83-
targets = {
84-
"targetpath": b"content",
85-
"åäö": b"more content",
86-
"dir/targetpath": b"dir target content"
87-
}
99+
targets: DataSet = {
100+
"standard case": ("targetpath", b"content"),
101+
"non-asci case": ("åäö", b"more content"),
102+
"subdirectory case": ("dir/targetpath", b"dir target content"),
103+
}
104+
105+
@run_sub_tests_with_dataset(targets)
106+
def test_targets(self, test_case_data: Tuple[str, str]):
107+
targetpath, content = test_case_data
108+
# target does not exist yet
109+
updater = self._run_refresh()
110+
self.assertIsNone(updater.get_one_valid_targetinfo(targetpath))
88111

89112
# Add targets to repository
90113
self.sim.targets.version += 1
91-
for targetpath, content in targets.items():
92-
self.sim.add_target("targets", content, targetpath)
114+
self.sim.add_target("targets", content, targetpath)
93115
self.sim.update_snapshot()
94116

95117
updater = self._run_refresh()
96-
for targetpath, content in targets.items():
97-
# target now exists, is not in cache yet
98-
file_info = updater.get_one_valid_targetinfo(targetpath)
99-
self.assertIsNotNone(file_info)
100-
self.assertEqual(
101-
updater.updated_targets([file_info], self.targets_dir),
102-
[file_info]
103-
)
104-
105-
# download target, assert it is in cache and content is correct
106-
local_path = updater.download_target(file_info, self.targets_dir)
107-
self.assertEqual(
108-
updater.updated_targets([file_info], self.targets_dir), []
109-
)
110-
self.assertTrue(local_path.startswith(self.targets_dir))
111-
with open(local_path, "rb") as f:
112-
self.assertEqual(f.read(), content)
113-
114-
if "/" in targetpath:
115-
# assert local_path != targetpath because of the URL encoding
116-
# make target_path absolute as local_path
117-
target_path = os.path.join(self.targets_dir, targetpath)
118-
self.assertNotEqual(target_path, local_path)
118+
# target now exists, is not in cache yet
119+
file_info = updater.get_one_valid_targetinfo(targetpath)
120+
self.assertIsNotNone(file_info)
121+
self.assertEqual(
122+
updater.updated_targets([file_info], self.targets_dir),
123+
[file_info]
124+
)
125+
126+
# download target, assert it is in cache and content is correct
127+
local_path = updater.download_target(file_info, self.targets_dir)
128+
self.assertEqual(
129+
updater.updated_targets([file_info], self.targets_dir), []
130+
)
131+
self.assertTrue(local_path.startswith(self.targets_dir))
132+
with open(local_path, "rb") as f:
133+
self.assertEqual(f.read(), content)
134+
135+
if "/" in targetpath:
136+
# assert local_path != targetpath because of the URL encoding
137+
# make target_path absolute as local_path
138+
target_path = os.path.join(self.targets_dir, targetpath)
139+
self.assertNotEqual(target_path, local_path)
119140

120141

121142

0 commit comments

Comments
 (0)