Skip to content

Commit 52f2ab7

Browse files
committed
Improve code coverage
1 parent d40f708 commit 52f2ab7

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

tests/test_deliverability.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import re
33

44
from email_validator import EmailUndeliverableError, \
5-
validate_email
5+
validate_email, caching_resolver
66
from email_validator.deliverability import validate_email_deliverability
77

88
from mocked_dns_response import MockedDnsResponseData, MockedDnsResponseDataCleanup # noqa: F401
@@ -53,3 +53,24 @@ def test_deliverability_dns_timeout():
5353
response = validate_email_deliverability('timeout.com', 'timeout.com', dns_resolver=RESOLVER)
5454
assert "mx" not in response
5555
assert response.get("unknown-deliverability") == "timeout"
56+
57+
58+
@pytest.mark.network
59+
def test_caching_dns_resolver():
60+
class TestCache:
61+
def __init__(self):
62+
self.cache = {}
63+
64+
def get(self, key):
65+
return self.cache.get(key)
66+
67+
def put(self, key, value):
68+
self.cache[key] = value
69+
70+
cache = TestCache()
71+
resolver = caching_resolver(timeout=1, cache=cache)
72+
validate_email("test@gmail.com", dns_resolver=resolver)
73+
assert len(cache.cache) == 1
74+
75+
validate_email("test@gmail.com", dns_resolver=resolver)
76+
assert len(cache.cache) == 1

tests/test_main.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from email_validator import validate_email
1+
import pytest
2+
3+
from email_validator import validate_email, EmailSyntaxError
24
# Let's test main but rename it to be clear
35
from email_validator.__main__ import main as validator_command_line_tool
46

@@ -45,3 +47,14 @@ def test_main_multi_input(monkeypatch, capsys):
4547
assert test_cases[1] not in stdout
4648
assert test_cases[2] in stdout
4749
assert test_cases[3] in stdout
50+
51+
52+
def test_bytes_input():
53+
input_email = b"testaddr@example.tld"
54+
valid_email = validate_email(input_email, check_deliverability=False)
55+
assert isinstance(valid_email.as_dict(), dict)
56+
assert valid_email.as_dict()["email"] == input_email.decode("utf8")
57+
58+
input_email = "testaddr中example.tld".encode("utf32")
59+
with pytest.raises(EmailSyntaxError):
60+
validate_email(input_email, check_deliverability=False)

tests/test_syntax.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,18 @@ def test_email_valid(email_input, output):
239239
('111111111122222222223333333333444444444455555555556666666666777777@example.com', 'The email address is too long before the @-sign (2 characters too many).'),
240240
('me@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.111111111122222222223333333333444444444455555555556.com', 'The email address is too long (4 characters too many).'),
241241
('me@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555566.com', 'The email address is too long after the @-sign (1 character too many).'),
242+
('me@中1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555566.com', 'The email address is too long after the @-sign.'),
242243
('my.long.address@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.11111111112222222222333333333344444.info', 'The email address is too long (2 characters too many).'),
243244
('my.long.address@λ111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.11111111112222222222333333.info', 'The email address is too long (when converted to IDNA ASCII).'),
244245
('my.long.address@λ111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444.info', 'The email address is too long (at least 1 character too many).'),
245246
('my.λong.address@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.111111111122222222223333333333444.info', 'The email address is too long (when encoded in bytes).'),
246247
('my.λong.address@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444.info', 'The email address is too long (at least 1 character too many).'),
248+
('me@bad-tld-1', 'The part after the @-sign is not valid. It should have a period.'),
249+
('me@bad.tld-2', 'The part after the @-sign is not valid. It is not within a valid top-level domain.'),
250+
('me@-', 'The part after the @-sign contains invalid characters (Label must not start or end with a hyphen).'),
251+
('me@x!', 'The part after the @-sign contains invalid characters (Codepoint U+0021 at position 2 of \'x!\' not allowed).'),
252+
('me@xn--', 'The part after the @-sign contains invalid characters (Malformed A-label, no Punycode eligible content found).'),
253+
('me@yy--', 'The part after the @-sign contains invalid characters (Label has disallowed hyphens in 3rd and 4th position).'),
247254
],
248255
)
249256
def test_email_invalid_syntax(email_input, error_msg):
@@ -287,6 +294,7 @@ def test_email_invalid_reserved_domain(email_input):
287294
('\uD800@test'), # surrogate (Cs)
288295
('\uE000@test'), # private use (Co)
289296
('\uFDEF@test'), # unassigned (Cn)
297+
('\u0300@test'), # grave accent (M)
290298
],
291299
)
292300
def test_email_unsafe_character(email_input):

0 commit comments

Comments
 (0)