Skip to content

Commit 6a488fc

Browse files
authored
Add pytest coverage for length, between, uuid, MAC, IBAN, cron, encodings and card brands (220 tests)
1 parent a5e84d4 commit 6a488fc

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
"""Extended coverage for uuid, mac_address and iban.
2+
3+
Existing tests for these modules are thin (uuid: 39 lines, mac: 35,
4+
iban: 33). All values below were verified against the cloned source
5+
with a probe script; well-known IBAN examples (DE/GB/FR/NL/CH) were
6+
checked by the mod-97 code path itself, not copied from documentation.
7+
8+
Quirks pinned as-is:
9+
- ``uuid`` accepts *any* UUID version and even brace/URN spellings,
10+
despite the docstring saying "UUID-v4".
11+
- ``uuid(123)`` raises ``AttributeError`` (the decorator only converts
12+
ValueError/TypeError/UnicodeError).
13+
"""
14+
15+
from uuid import UUID, uuid1, uuid4
16+
17+
import pytest
18+
19+
from validators import ValidationError, iban, mac_address, uuid
20+
21+
22+
# ---------------------------------------------------------------- uuid
23+
24+
25+
@pytest.mark.parametrize(
26+
"value",
27+
[
28+
"2bc1c94f-0deb-43e9-92a1-4775189ec9f8", # plain v4
29+
"2BC1C94F-0DEB-43E9-92A1-4775189EC9F8", # upper case hex
30+
"888256d7c49341f19fa33f29d3f820d7", # 32 hex digits, no dashes
31+
"{2bc1c94f-0deb-43e9-92a1-4775189ec9f8}", # braces accepted
32+
"urn:uuid:2bc1c94f-0deb-43e9-92a1-4775189ec9f8", # URN accepted
33+
str(uuid1()), # version 1 accepted despite the "v4" docstring
34+
"2bc1c94f-0deb-13e9-92a1-4775189ec9f8", # version nibble 1
35+
uuid4(), # UUID objects are always accepted
36+
uuid1(),
37+
UUID("2bc1c94f-0deb-43e9-92a1-4775189ec9f8"),
38+
],
39+
)
40+
def test_uuid_returns_true_on_valid_values(value):
41+
assert uuid(value) is True
42+
43+
44+
@pytest.mark.parametrize(
45+
"value",
46+
[
47+
"",
48+
None, # falsy input short-circuits to a failure
49+
"2bc1c94f-deb-43e9-92a1-4775189ec9f8", # short second group
50+
"2bc1c94f-0deb-43e9-92a1-4775189ec9f", # one hex digit short
51+
"gbc1c94f-0deb-43e9-92a1-4775189ec9f8", # 'g' is not hex
52+
"2bc1c94f 0deb-43e9-92a1-4775189ec9f8", # space separator
53+
"2bc1c94f-0deb-43e9-92a1-4775189ec9f8-extra",
54+
"2bc1c94f-0deb-43e9-92a1", # truncated
55+
],
56+
)
57+
def test_uuid_returns_failed_validation_on_invalid_values(value):
58+
assert isinstance(uuid(value), ValidationError)
59+
60+
61+
def test_uuid_non_string_non_uuid_raises_attribute_error():
62+
# Not converted by @validator: AttributeError is not in its catch list.
63+
with pytest.raises(AttributeError):
64+
uuid(123)
65+
66+
67+
def test_uuid_failure_carries_the_function_name():
68+
result = uuid("not-a-uuid")
69+
assert isinstance(result, ValidationError)
70+
assert result.func.__name__ == "uuid"
71+
assert result.value == "not-a-uuid"
72+
73+
74+
# ---------------------------------------------------------------- mac_address
75+
76+
77+
@pytest.mark.parametrize(
78+
"value",
79+
[
80+
"01:23:45:67:ab:CD", # mixed case, colon separated
81+
"01-23-45-67-AB-CD", # dash separated
82+
"00:00:00:00:00:00", # all zeros is still a well-formed address
83+
"FF:FF:FF:FF:FF:FF", # broadcast
84+
"aB:cD:eF:01:23:45",
85+
],
86+
)
87+
def test_mac_address_returns_true_on_valid_values(value):
88+
assert mac_address(value) is True
89+
90+
91+
@pytest.mark.parametrize(
92+
"value",
93+
[
94+
"", # empty
95+
"00:00:00:00:00", # only five groups
96+
"01:23:45:67:AB:CD:EF", # seven groups
97+
"01:23-45:67:AB:CD", # mixed separators are rejected explicitly
98+
"1:23:45:67:AB:CD", # single-digit group
99+
"012:23:45:67:AB:CD", # three-digit group
100+
"01:23:45:67:AB:CD:", # trailing separator
101+
":01:23:45:67:AB:CD", # leading separator
102+
"gg:23:45:67:AB:CD", # non-hex
103+
"01.23.45.67.AB.CD", # dots are not a separator here
104+
"01:23:45:67:AB:CD ", # trailing space breaks the full match
105+
],
106+
)
107+
def test_mac_address_returns_failed_validation_on_invalid_values(value):
108+
assert isinstance(mac_address(value), ValidationError)
109+
110+
111+
# ---------------------------------------------------------------- iban
112+
113+
114+
@pytest.mark.parametrize(
115+
"value",
116+
[
117+
"DE29100500001061045672",
118+
"de29100500001061045672", # country code is case-insensitive
119+
"GB29NWBK60161331926819",
120+
"FR1420041010050500013M02606", # letters inside the BBAN
121+
"NL91ABNA0417164300",
122+
"CH9300762011623852957",
123+
],
124+
)
125+
def test_iban_returns_true_on_valid_values(value):
126+
assert iban(value) is True
127+
128+
129+
@pytest.mark.parametrize(
130+
"value",
131+
[
132+
"",
133+
"123456", # no country code at all
134+
"DE29", # far too short
135+
"DE29" + "1" * 31, # 35 chars: over the 34-char maximum
136+
"DE29 1005 0000 1061 0456 72", # spaces are not stripped
137+
"DE29100500001061045673", # one check digit off: mod-97 fails
138+
"DE2910050000106104567", # too short to be a real IBAN
139+
"XX29100500001061045672", # unknown country still fails mod-97
140+
],
141+
)
142+
def test_iban_returns_failed_validation_on_invalid_values(value):
143+
assert isinstance(iban(value), ValidationError)

0 commit comments

Comments
 (0)