Skip to content

Commit 83bf2c2

Browse files
committed
maint: improves ip_address module
- uses Python's powerful inbuilt `ipaddress` library - CIDR validation is now default via func argument - compiles ip address test cases into a single module - fixes bad `poetry.lock` file **Related items** *Issues* - Closes #152 *PRs* - Closes #91 - Closes #158
1 parent 49dfec2 commit 83bf2c2

File tree

8 files changed

+171
-252
lines changed

8 files changed

+171
-252
lines changed

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_ip_address.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""Test IP Address."""
2+
# -*- coding: utf-8 -*-
3+
4+
# external
5+
import pytest
6+
7+
# local
8+
from validators import ipv4, ipv6, ValidationFailure
9+
10+
11+
@pytest.mark.parametrize(
12+
("address",),
13+
[
14+
("127.0.0.1",),
15+
("123.5.77.88",),
16+
("12.12.12.12",),
17+
# w/ cidr
18+
("127.0.0.1/0",),
19+
("123.5.77.88/8",),
20+
("12.12.12.12/32",),
21+
],
22+
)
23+
def test_returns_true_on_valid_ipv4_address(address: str):
24+
"""Test returns true on valid ipv4 address."""
25+
assert ipv4(address)
26+
assert not ipv6(address)
27+
28+
29+
@pytest.mark.parametrize(
30+
("address",),
31+
[
32+
# leading zeroes error-out from Python 3.9.5
33+
# ("100.100.033.033",),
34+
("900.200.100.75",),
35+
("0127.0.0.1",),
36+
("abc.0.0.1",),
37+
# w/ cidr
38+
("1.1.1.1/-1",),
39+
("1.1.1.1/33",),
40+
("1.1.1.1/foo",),
41+
],
42+
)
43+
def test_returns_failed_validation_on_invalid_ipv4_address(address: str):
44+
"""Test returns failed validation on invalid ipv4 address."""
45+
assert isinstance(ipv4(address), ValidationFailure)
46+
47+
48+
@pytest.mark.parametrize(
49+
("address",),
50+
[
51+
("::",),
52+
("::1",),
53+
("1::",),
54+
("dead:beef:0:0:0:0000:42:1",),
55+
("abcd:ef::42:1",),
56+
("0:0:0:0:0:ffff:1.2.3.4",),
57+
("::192.168.30.2",),
58+
("0000:0000:0000:0000:0000::",),
59+
("0:a:b:c:d:e:f::",),
60+
# w/ cidr
61+
("::1/128",),
62+
("::1/0",),
63+
("dead:beef:0:0:0:0:42:1/8",),
64+
("abcd:ef::42:1/32",),
65+
("0:0:0:0:0:ffff:1.2.3.4/16",),
66+
("2001:0db8:85a3:0000:0000:8a2e:0370:7334/64",),
67+
("::192.168.30.2/128",),
68+
],
69+
)
70+
def test_returns_true_on_valid_ipv6_address(address: str):
71+
"""Test returns true on valid ipv6 address."""
72+
assert ipv6(address)
73+
assert not ipv4(address)
74+
75+
76+
@pytest.mark.parametrize(
77+
("address",),
78+
[
79+
("abc.0.0.1",),
80+
("abcd:1234::123::1",),
81+
("1:2:3:4:5:6:7:8:9",),
82+
("1:2:3:4:5:6:7:8::",),
83+
("1:2:3:4:5:6:7::8:9",),
84+
("abcd::1ffff",),
85+
("1111:",),
86+
(":8888",),
87+
(":1.2.3.4",),
88+
("18:05",),
89+
(":",),
90+
(":1:2:",),
91+
(":1:2::",),
92+
("::1:2::",),
93+
("8::1:2::9",),
94+
("02001:0000:1234:0000:0000:C1C0:ABCD:0876",),
95+
# w/ cidr
96+
("::1/129",),
97+
("::1/-1",),
98+
("::1/foo",),
99+
],
100+
)
101+
def test_returns_failed_validation_on_invalid_ipv6_address(address: str):
102+
"""Test returns failed validation on invalid ipv6 address."""
103+
assert isinstance(ipv6(address), ValidationFailure)

tests/test_ipv4.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/test_ipv4_cidr.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/test_ipv6.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

tests/test_ipv6_cidr.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

validators/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .hashes import md5, sha1, sha224, sha256, sha512
1010
from .i18n import fi_business_id, fi_ssn
1111
from .iban import iban
12-
from .ip_address import ipv4, ipv4_cidr, ipv6, ipv6_cidr
12+
from .ip_address import ipv4, ipv6
1313
from .length import length
1414
from .mac_address import mac_address
1515
from .slug import slug
@@ -29,9 +29,7 @@
2929
"fi_business_id",
3030
"fi_ssn",
3131
"iban",
32-
"ipv4_cidr",
3332
"ipv4",
34-
"ipv6_cidr",
3533
"ipv6",
3634
"jcb",
3735
"length",

0 commit comments

Comments
 (0)