Skip to content

Commit 0f1e717

Browse files
committed
Added a few more tests to improve code coverage
1 parent 6bc4589 commit 0f1e717

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

tests/mocked-dns-answers.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
"type": "A",
130130
"class": "IN"
131131
},
132-
"answer": []
132+
"answer": ["127.0.0.1"]
133133
},
134134
{
135135
"query": {
@@ -139,6 +139,30 @@
139139
},
140140
"answer": []
141141
},
142+
{
143+
"query": {
144+
"name": "ipv6only.joshdata.me",
145+
"type": "MX",
146+
"class": "IN"
147+
},
148+
"answer": []
149+
},
150+
{
151+
"query": {
152+
"name": "ipv6only.joshdata.me",
153+
"type": "A",
154+
"class": "IN"
155+
},
156+
"answer": []
157+
},
158+
{
159+
"query": {
160+
"name": "ipv6only.joshdata.me",
161+
"type": "AAAA",
162+
"class": "IN"
163+
},
164+
"answer": ["::1"]
165+
},
142166
{
143167
"query": {
144168
"name": "mail.example",

tests/test_deliverability.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_deliverability_found(domain: str, expected_response: str) -> None:
3535
# No MX or A/AAAA records, but some other DNS records must
3636
# exist such that the response is NOANSWER instead of NXDOMAIN.
3737
('justtxt.joshdata.me', 'The domain name {domain} does not accept email'),
38+
('ipv6only.joshdata.me', 'The domain name {domain} does not accept email'),
3839
],
3940
)
4041
def test_deliverability_fails(domain: str, error: str) -> None:
@@ -65,6 +66,11 @@ def test_deliverability_dns_timeout() -> None:
6566
assert response.get("unknown-deliverability") == "timeout"
6667

6768

69+
def test_timeout_and_resolver() -> None:
70+
with pytest.raises(ValueError, match="It's not valid to pass both timeout and dns_resolver."):
71+
validate_email_deliverability('timeout.com', 'timeout.com', timeout=1, dns_resolver=RESOLVER)
72+
73+
6874
@pytest.mark.network
6975
def test_caching_dns_resolver() -> None:
7076
class TestCache:

tests/test_main.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ def test_dict_accessor() -> None:
1616
assert valid_email.as_dict()["original"] == input_email
1717

1818

19+
def test_dict_accessor_with_domain_address() -> None:
20+
input_email = "me@[127.0.0.1]"
21+
valid_email = validate_email(input_email, check_deliverability=False, allow_domain_literal=True)
22+
assert valid_email.domain == "[127.0.0.1]"
23+
assert isinstance(valid_email.as_dict(), dict)
24+
assert valid_email.as_dict()["domain_address"] == '"IPv4Address(\'127.0.0.1\')"'
25+
26+
1927
def test_main_single_good_input(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
2028
import json
2129
test_email = "google@google.com"
@@ -65,3 +73,21 @@ def test_deprecation() -> None:
6573
valid_email = validate_email(input_email, check_deliverability=False)
6674
with pytest.deprecated_call():
6775
assert valid_email.email is not None
76+
77+
78+
@pytest.mark.parametrize('invalid_email', [
79+
None,
80+
12345,
81+
[],
82+
{},
83+
lambda x: x,
84+
])
85+
def test_invalid_type(invalid_email) -> None:
86+
with pytest.raises(TypeError, match="email must be str or bytes"):
87+
validate_email(invalid_email, check_deliverability=False)
88+
89+
90+
def test_invalid_ascii() -> None:
91+
invalid_email = b'\xd0\xba\xd0\xb2\xd1\x96\xd1\x82\xd0\xbe\xd1\x87\xd0\xba\xd0\xb0@\xd0\xbf\xd0\xbe\xd1\x88\xd1\x82\xd0\xb0.test'
92+
with pytest.raises(EmailSyntaxError, match="The email address is not valid ASCII."):
93+
validate_email(invalid_email, check_deliverability=False)

0 commit comments

Comments
 (0)