Skip to content

Commit 130092b

Browse files
committed
feat: add support to regex on assertResponseMatch
1 parent 868870a commit 130092b

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

drf_kit/tests.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ def assertNoPendingMigration(self, app_name): # pylint: disable=invalid-name
2727
message = f"Missing migration. Run python manage.py makemigrations {app_name}"
2828
self.assertIn("No changes", out.getvalue(), msg=message)
2929

30-
def assertUUIDFilePath(self, prefix, name, extension, pk, file): # pylint: disable=invalid-name
31-
expected_path = r"^{prefix}/{pk}/{name}_{uuid}\.{extension}$".format(
32-
prefix=prefix,
33-
pk=pk,
34-
name=name,
35-
extension=extension,
36-
uuid=r"[0-9a-f]{8}\-[0-9a-f]{4}\-4[0-9a-f]{3}\-[89ab][0-9a-f]{3}\-[0-9a-f]{12}",
30+
def uuid_file_path_regex(self, prefix, pk, name, extension):
31+
uuid_regex = r"[0-9a-f]{8}\-[0-9a-f]{4}\-4[0-9a-f]{3}\-[89ab][0-9a-f]{3}\-[0-9a-f]{12}"
32+
return re.compile(
33+
r"^{prefix}/{pk}/{name}_{uuid}\.{extension}$".format(
34+
prefix=prefix,
35+
pk=pk,
36+
name=name,
37+
extension=extension,
38+
uuid=uuid_regex,
39+
)
3740
)
38-
self.assertTrue(re.match(expected_path, str(file)))
41+
42+
def assertUUIDFilePath(self, prefix, name, extension, pk, file): # pylint: disable=invalid-name
43+
pattern = self.uuid_file_path_regex(prefix=prefix, pk=pk, name=name, extension=extension)
44+
self.assertTrue(pattern.match(str(file)))
3945

4046
def assertResponseMatch(self, expected, received): # pylint: disable=invalid-name
4147
def _assert_dict(expected_item, received_item, idx=None):
@@ -69,13 +75,23 @@ def _compare(expected_item, received_item):
6975
if expected_item is ANY:
7076
return {}
7177

78+
if isinstance(expected_item, re.Pattern):
79+
if not expected_item.match(received_item):
80+
msg = f"Received `{received_item}`, but expected to match `{expected_item.pattern}`"
81+
return {"__match__": msg}
82+
return {}
83+
7284
if isinstance(expected_item, list) and isinstance(received_item, list):
7385
if len(expected_item) != len(received_item):
7486
msg = f"Received {len(received_item)} items and it was expected to have {len(expected_item)}"
7587
return {"__len__": msg}
7688

89+
all_errors = {}
7790
for _idx, (_expected_item, _received_item) in enumerate(zip(expected_item, received_item)):
78-
return _assert_dict(idx=_idx, expected_item=_expected_item, received_item=_received_item)
91+
inner_errors = _assert_dict(idx=_idx, expected_item=_expected_item, received_item=_received_item)
92+
for inner_key, inner_error in inner_errors.items():
93+
all_errors[f"[#{_idx}] {inner_key}"] = inner_error
94+
return all_errors
7995
elif isinstance(expected_item, dict) and isinstance(received_item, dict):
8096
return _assert_dict(expected_item=expected_item, received_item=received_item)
8197
else:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "drf-kit"
3-
version = "1.4.1"
3+
version = "1.4.2"
44
description = "DRF Toolkit"
55
authors = ["eduK <[email protected]>"]
66
packages = [

0 commit comments

Comments
 (0)