Skip to content

Commit 53a24da

Browse files
committed
Use decorator in test_updater_with_simulator
Reuse the decorator defined in tests/utils.py in order to receive more helpful messages when an assertion fails in test_tragets(). Signed-off-by: Martin Vrachev <[email protected]>
1 parent 6ae4995 commit 53a24da

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

tests/test_updater_with_simulator.py

Lines changed: 41 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, NamedTuple, Optional, Tuple
1414
from tuf.exceptions import UnsignedMetadataError
1515
import unittest
1616

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

22+
class TargetData(NamedTuple):
23+
targetpath: str
24+
content: str
25+
encoded_path: str
26+
27+
DataSet = Dict[str, TargetData]
28+
2229
class TestUpdater(unittest.TestCase):
2330
# set dump_dir to trigger repository state dumps
2431
dump_dir:Optional[str] = None
@@ -79,43 +86,45 @@ def test_refresh(self):
7986

8087
self._run_refresh()
8188

82-
def test_targets(self):
83-
targets = {
84-
"targetpath": b"content",
85-
"åäö": b"more content",
86-
"dir/targetpath": b"dir target content"
87-
}
89+
targets: DataSet = {
90+
"standard case": ("targetpath", b"content", "targetpath"),
91+
"non-asci case": ("åäö", b"more content", "%C3%A5%C3%A4%C3%B6"),
92+
"subdirectory case": ("a/b/c/targetpath", b"dir target content", "a%2Fb%2Fc%2Ftargetpath"),
93+
}
94+
95+
@utils.run_sub_tests_with_dataset(targets)
96+
def test_targets(self, test_case_data: TargetData):
97+
targetpath, content, encoded_path = test_case_data
98+
# target does not exist yet
99+
updater = self._run_refresh()
100+
self.assertIsNone(updater.get_one_valid_targetinfo(targetpath))
88101

89102
# Add targets to repository
90103
self.sim.targets.version += 1
91-
for targetpath, content in targets.items():
92-
self.sim.add_target("targets", content, targetpath)
104+
self.sim.add_target("targets", content, targetpath)
93105
self.sim.update_snapshot()
94106

95107
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)
108+
# target now exists, is not in cache yet
109+
file_info = updater.get_one_valid_targetinfo(targetpath)
110+
self.assertIsNotNone(file_info)
111+
self.assertEqual(
112+
updater.updated_targets([file_info], self.targets_dir),
113+
[file_info]
114+
)
115+
116+
# download target, assert it is in cache and content is correct
117+
local_path = updater.download_target(file_info, self.targets_dir)
118+
self.assertEqual(
119+
updater.updated_targets([file_info], self.targets_dir), []
120+
)
121+
self.assertTrue(local_path.startswith(self.targets_dir))
122+
with open(local_path, "rb") as f:
123+
self.assertEqual(f.read(), content)
124+
125+
# Assert that the targetpath was URL encoded as expected.
126+
encoded_absolute_path = os.path.join(self.targets_dir, encoded_path)
127+
self.assertEqual(local_path, encoded_absolute_path)
119128

120129

121130

tests/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
# (where N is number of items in dataset), feeding the actual test
5050
# function one test case at a time
5151
def run_sub_tests_with_dataset(dataset: DataSet):
52-
def real_decorator(function: Callable[["unittest.TestCase", Any], None]):
53-
def wrapper(test_cls: "unittest.TestCase"):
52+
def real_decorator(function: Callable[[unittest.TestCase, Any], None]):
53+
def wrapper(test_cls: unittest.TestCase):
5454
for case, data in dataset.items():
5555
with test_cls.subTest(case=case):
5656
function(test_cls, data)

0 commit comments

Comments
 (0)