|
2 | 2 | from email_validator import EmailSyntaxError, EmailUndeliverableError, \
|
3 | 3 | validate_email, validate_email_deliverability, \
|
4 | 4 | ValidatedEmail
|
| 5 | +# Let's test main but rename it to be clear |
| 6 | +from email_validator import main as validator_main |
5 | 7 |
|
6 | 8 |
|
7 | 9 | @pytest.mark.parametrize(
|
@@ -284,3 +286,61 @@ def test_deliverability_dns_timeout():
|
284 | 286 | assert response.get("unknown-deliverability") == "timeout"
|
285 | 287 | validate_email('test@gmail.com')
|
286 | 288 | del validate_email_deliverability.TEST_CHECK_TIMEOUT
|
| 289 | + |
| 290 | + |
| 291 | +def test_main_single_good_input(monkeypatch, capsys): |
| 292 | + import json |
| 293 | + test_email = "test@example.com" |
| 294 | + monkeypatch.setattr('sys.argv', ['email_validator', test_email]) |
| 295 | + validator_main() |
| 296 | + stdout, _ = capsys.readouterr() |
| 297 | + output = json.loads(str(stdout)) |
| 298 | + assert isinstance(output, dict) |
| 299 | + assert validate_email(test_email).original_email == output["original_email"] |
| 300 | + |
| 301 | + |
| 302 | +def test_main_single_bad_input(monkeypatch, capsys): |
| 303 | + bad_email = 'test@..com' |
| 304 | + monkeypatch.setattr('sys.argv', ['email_validator', bad_email]) |
| 305 | + validator_main() |
| 306 | + stdout, _ = capsys.readouterr() |
| 307 | + assert stdout == 'An email address cannot have a period immediately after the @-sign.\n' |
| 308 | + |
| 309 | + |
| 310 | +def test_main_multi_input(monkeypatch, capsys): |
| 311 | + import io |
| 312 | + test_cases = ["test@example.com", "test2@example.com", "test@.com", "test3@.com"] |
| 313 | + test_input = io.StringIO("\n".join(test_cases)) |
| 314 | + monkeypatch.setattr('sys.stdin', test_input) |
| 315 | + monkeypatch.setattr('sys.argv', ['email_validator']) |
| 316 | + validator_main() |
| 317 | + stdout, _ = capsys.readouterr() |
| 318 | + assert test_cases[0] not in stdout |
| 319 | + assert test_cases[1] not in stdout |
| 320 | + assert test_cases[2] in stdout |
| 321 | + assert test_cases[3] in stdout |
| 322 | + |
| 323 | + |
| 324 | +def test_main_input_shim(monkeypatch, capsys): |
| 325 | + import json |
| 326 | + monkeypatch.setattr('sys.version_info', (2, 7)) |
| 327 | + test_email = b"test@example.com" |
| 328 | + monkeypatch.setattr('sys.argv', ['email_validator', test_email]) |
| 329 | + validator_main() |
| 330 | + stdout, _ = capsys.readouterr() |
| 331 | + output = json.loads(str(stdout)) |
| 332 | + assert isinstance(output, dict) |
| 333 | + assert validate_email(test_email).original_email == output["original_email"] |
| 334 | + |
| 335 | + |
| 336 | +def test_main_output_shim(monkeypatch, capsys): |
| 337 | + monkeypatch.setattr('sys.version_info', (2, 7)) |
| 338 | + test_email = b"test@.com" |
| 339 | + monkeypatch.setattr('sys.argv', ['email_validator', test_email]) |
| 340 | + validator_main() |
| 341 | + stdout, _ = capsys.readouterr() |
| 342 | + |
| 343 | + # This looks bad but it has to do with the way python 2.7 prints vs py3 |
| 344 | + # The \n is part of the print statement, not part of the string, which is what the b'...' is |
| 345 | + # Since we're mocking py 2.7 here instead of actually using 2.7, this was the closest I could get |
| 346 | + assert stdout == "b'An email address cannot have a period immediately after the @-sign.'\n" |
0 commit comments