Refactorings

This commit is contained in:
László Károlyi 2019-03-01 20:37:52 +01:00
parent 66a4c81e0e
commit db8a24b5d4
Signed by: karolyi
GPG key ID: 2DCAF25E55735BFE
9 changed files with 79 additions and 78 deletions

3
.gitignore vendored
View file

@ -2,4 +2,5 @@
build
dist
MANIFEST
*.egg-info
*.egg-info
venv

View file

@ -9,7 +9,7 @@ python:
- "3.7-dev" # 3.7 development branch
# command to install dependencies
install:
- pip install -r requirements.txt
- pip install -e .
# command to run tests
script:
- nosetests

View file

@ -1,3 +1,3 @@
from pyemailval.regex_check import regex_check
from pyemailval.mx_check import mx_check
from pyemailval.validate_email import validate_email
validate_email

View file

@ -1,55 +0,0 @@
from .regex_check import regex_check
VALID_EMAIL_ADDRESS_EXAMPLES = [
"email@domain.com", # basic valid email
"firstname.lastname@domain.com", # dot in address field
"email@subdomain.domain.com", # dot in subdomain
"firstname+lastname@domain.com", # + in address field
"email@123.123.123.123", # domain address is IP address
"email@[123.123.123.123]", # square brackets around IP address
"\"email\"@domain.com", # quote marks in address fields
"1234567890@domain.com", # numbers in address field
"email@domain-one.com", # dash in subdomain
"_______@domain.com", # underscore in address field
"email@domain.name", # .name top level domain name
"email@domain.co.jp", # dot in top level domain
"firstname-lastname@domain.com" # dash in address field
]
INVALID_EMAIL_ADDRESS_EXAMPLES = [
"plainaddress", # missing @ sign and domain
"#@%^%#$@#$@#.com", # garbage
"@domain.com", # missing username
"Joe Smith <email@domain.com>", # encoded html within email is invalid
"email.domain.com", # missing @
"email@domain@domain.com", # two @ sign
".email@domain.com", # leading dot in address is not allowed
"email.@domain.com", # trailing dot in address is not allowed
"email..email@domain.com", # multiple dots
"あいうえお@domain.com", # unicode char as address
"email@domain.com (Joe Smith)", # text followed email is not allowed
"email@domain", # missing top level domain (.com/.net/.org/etc)
"email@-domain.com", # leading dash in front of domain is invalid
"email@domain.web", # .web is not a valid top level domain
"email@111.222.333.44444", # invalid IP format
"email@domain..com", # multiple dot in the domain portion is invalid
]
def test_valid_email_structure_regex():
for index, valid_email_address in enumerate(VALID_EMAIL_ADDRESS_EXAMPLES):
try:
assert regex_check(valid_email_address) is True
except AssertionError:
raise AssertionError(
"{} should be valid ({}th email address in the list)"
.format(valid_email_address, index))
def test_invalid_email_structure_regex():
for index, invalid_email_address in enumerate(INVALID_EMAIL_ADDRESS_EXAMPLES):
try:
assert regex_check(invalid_email_address) is False
except AssertionError:
raise AssertionError(
"{} should be invalid ({}th email address in the list)"
.format(invalid_email_address, index))

View file

@ -3,10 +3,7 @@ from .mx_check import mx_check
def validate_email(
email_address,
check_regex=True,
check_mx=True,
smtp_timeout=10):
email_address, check_regex=True, check_mx=True, smtp_timeout=10):
if check_regex and not regex_check(email_address):
return False

View file

@ -1,16 +1,16 @@
from setuptools import setup, find_packages
setup(
name="pyemailval",
version="0.6",
py_modules=("pyemailval",),
name='pyemailval',
version='0.6',
py_modules=('pyemailval',),
install_requires=['dnspython', 'nose'],
author="Ben Baert",
author_email="ben_b@gmx.com",
description="pyemailval verifies if an email address really exists.",
long_description=open("README.rst").read(),
keywords="email validation verification mx verify",
url="http://github.com/ben-baert/pyemailval",
download_url="http://github.com/ben-baert/pyemailval/archive/0.1.tar.gz",
license="LGPL",
author='Ben Baert',
author_email='laszlo@karolyi.hu',
description='pyemailval verifies if an email address really exists.',
long_description=open('README.rst').read(),
keywords='email validation verification mx verify',
url='http://github.com/karolyi/pyemailval',
download_url='http://github.com/karolyi/pyemailval/archive/0.1.tar.gz',
license='LGPL',
)

0
tests/__init__.py Normal file
View file

View file

@ -1,8 +1,8 @@
from .mx_check import _get_domain_from_email_address
from pyemailval.regex_check import _get_domain_from_email_address
DOMAINS = {
"email@domain.com": "domain.com",
"email@domain.com": "domain.com",
"email@subdomain.domain.com": "subdomain.domain.com",
"email@123.123.123.123": "123.123.123.123",
"email@[123.123.123.123]": "123.123.123.123",
@ -14,9 +14,10 @@ DOMAINS = {
def test_domain_from_email_address():
for email_address, domain in DOMAINS.items():
try:
domain_from_function = _get_domain_from_email_address(email_address)
domain_from_function = _get_domain_from_email_address(
email_address)
assert domain_from_function == domain
except AssertionError:
raise AssertionError(
"Email address {} should result in domain {} but resulted in domain {}"
.format(email_address, domain, domain_from_function))
.format(email_address, domain, domain_from_function))

57
tests/test_regex_check.py Normal file
View file

@ -0,0 +1,57 @@
from pyemailval.regex_check import regex_check
VALID_EMAIL_ADDRESS_EXAMPLES = [
'email@domain.com', # basic valid email
'firstname.lastname@domain.com', # dot in address field
'email@subdomain.domain.com', # dot in subdomain
'firstname+lastname@domain.com', # + in address field
'email@123.123.123.123', # domain address is IP address
'email@[123.123.123.123]', # square brackets around IP address
'\'email\'@domain.com', # quote marks in address fields
'1234567890@domain.com', # numbers in address field
'email@domain-one.com', # dash in subdomain
'_______@domain.com', # underscore in address field
'email@domain.name', # .name top level domain name
'email@domain.co.jp', # dot in top level domain
'firstname-lastname@domain.com' # dash in address field
]
INVALID_EMAIL_ADDRESS_EXAMPLES = [
'plainaddress', # missing @ sign and domain
'#@%^%#$@#$@#.com', # garbage
'@domain.com', # missing username
'Joe Smith <email@domain.com>', # encoded html within email is invalid
'email.domain.com', # missing @
'email@domain@domain.com', # two @ sign
'.email@domain.com', # leading dot in address is not allowed
'email.@domain.com', # trailing dot in address is not allowed
'email..email@domain.com', # multiple dots
'あいうえお@domain.com', # unicode char as address
'email@domain.com (Joe Smith)', # text followed email is not allowed
'email@domain', # missing top level domain (.com/.net/.org/etc)
'email@-domain.com', # leading dash in front of domain is invalid
'email@domain.web', # .web is not a valid top level domain
'email@111.222.333.44444', # invalid IP format
'email@domain..com', # multiple dot in the domain portion is invalid
]
def test_valid_email_structure_regex():
for index, valid_email_address in enumerate(VALID_EMAIL_ADDRESS_EXAMPLES):
try:
assert regex_check(valid_email_address) is True
except AssertionError:
raise AssertionError(
'{} should be valid ({}th email address in the list)'
.format(valid_email_address, index))
def test_invalid_email_structure_regex():
for index, invalid_email_address in \
enumerate(INVALID_EMAIL_ADDRESS_EXAMPLES):
try:
assert regex_check(invalid_email_address) is False
except AssertionError:
raise AssertionError(
'{} should be invalid ({}th email address in the list)'
.format(invalid_email_address, index))