diff --git a/.isort.cfg b/.isort.cfg deleted file mode 100644 index 52591e45..00000000 --- a/.isort.cfg +++ /dev/null @@ -1,6 +0,0 @@ -[settings] -known_first_party=sqlalchemy_utils,tests -line_length=79 -multi_line_output=3 -not_skip=__init__.py -order_by_type=false diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..a9421a8f --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,19 @@ +repos: +- repo: https://gitlab.com/pycqa/flake8 + rev: 3.7.9 + hooks: + - id: flake8 + +- repo: https://github.com/pre-commit/mirrors-isort + rev: v4.3.21 + hooks: + - id: isort + + +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.4.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files diff --git a/.travis.yml b/.travis.yml index 977df9ef..5e70ebcd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: python matrix: include: - - python: 3.4 - python: 3.5 - python: 3.6 - python: 3.7 @@ -9,8 +8,7 @@ matrix: sudo: true - python: pypy3 install: - - pip install -e ".[test]" + - make install script: - - isort --recursive --diff validators tests && isort --recursive --check-only validators tests - - flake8 validators tests - - py.test --doctest-glob="*.rst" --doctest-modules --ignore=setup.py + - make lint + - make test diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..026c22d1 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +lint: + isort --diff validators tests + isort --check-only validators tests + + flake8 validators tests + +install: + pip install -e ".[test]" + +test: + py.test --doctest-glob="*.rst" --doctest-modules --ignore=setup.py diff --git a/README.rst b/README.rst index 32f57ca4..7eaab956 100644 --- a/README.rst +++ b/README.rst @@ -18,6 +18,29 @@ schema. True +Development +----------- + +To install the package for development use the following commands, + +.. code-block:: bash + + make install # For installing the package + + make test # For running tests + + make lint # For checking lint + + + +Pre-commit hooks are run as soon as you commit something. + +To run pre-commit hooks manually + +.. code-block:: bash + + pre-commit run --all-files + Resources --------- diff --git a/docs/conf.py b/docs/conf.py index 43717706..ec0f32aa 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,14 +12,15 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys import os +import sys + +from validators import __version__ # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. sys.path.insert(0, os.path.abspath('..')) -from validators import __version__ # -- General configuration ------------------------------------------------ diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 00000000..c5686b21 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,17 @@ +[isort] +known_first_party=sqlalchemy_utils,tests +line_length=88 +multi_line_output=3 +order_by_type=false +include_trailing_comma=True + +[flake8] +ignore = D10,E203,E501,W503,E231,E265,W504,E122,E121 +max-line-length = 88 +select = A,B,C4,D,E,F,M,Q,T,W,ABS,BLK +inline-quotes = " +docstring-convention = google +exclude = + .git, + __pycache__, + src/validators/libs diff --git a/setup.py b/setup.py index c1a5b80f..e0be793e 100644 --- a/setup.py +++ b/setup.py @@ -6,11 +6,11 @@ Python Data Validation for Humans™. """ -from setuptools import setup, find_packages import os import re import sys +from setuptools import find_packages, setup PY3 = sys.version_info[0] == 3 HERE = os.path.dirname(os.path.abspath(__file__)) @@ -23,13 +23,8 @@ def get_version(): pattern = r"^__version__ = '(.*?)'$" return re.search(pattern, contents, re.MULTILINE).group(1) - extras_require = { - 'test': [ - 'pytest>=2.2.3', - 'flake8>=2.4.0', - 'isort>=4.2.2' - ], + 'test': ['pytest>=2.2.3', 'flake8>=2.4.0', 'isort>=4.2.2', 'pre-commit',], } install_requires = [ @@ -67,7 +62,7 @@ def get_version(): 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', - 'Topic :: Software Development :: Libraries :: Python Modules' + 'Topic :: Software Development :: Libraries :: Python Modules', ], - python_requires='>=3.4' + python_requires='>=3.4', ) diff --git a/tests/i18n/test_es.py b/tests/i18n/test_es.py index 3d955c7b..168c9e9f 100644 --- a/tests/i18n/test_es.py +++ b/tests/i18n/test_es.py @@ -5,101 +5,106 @@ from validators.i18n.es import es_cif, es_doi, es_nie, es_nif -@pytest.mark.parametrize(('value',), [ - ('B25162520',), - ('U4839822F',), - ('B96817697',), - ('P7067074J',), - ('Q7899705C',), - ('C75098681',), - ('G76061860',), - ('C71345375',), - ('G20558169',), - ('U5021960I',), -]) +@pytest.mark.parametrize( + ('value',), + [ + ('B25162520',), + ('U4839822F',), + ('B96817697',), + ('P7067074J',), + ('Q7899705C',), + ('C75098681',), + ('G76061860',), + ('C71345375',), + ('G20558169',), + ('U5021960I',), + ], +) def test_returns_true_on_valid_cif(value): assert es_cif(value) -@pytest.mark.parametrize(('value',), [ - ('12345',), - ('ABCDEFGHI',), - ('Z5021960I',), -]) +@pytest.mark.parametrize(('value',), [('12345',), ('ABCDEFGHI',), ('Z5021960I',),]) def test_returns_false_on_invalid_cif(value): result = es_cif(value) assert isinstance(result, ValidationFailure) -@pytest.mark.parametrize(('value',), [ - ('X0095892M',), - ('X8868108K',), - ('X2911154K',), - ('Y2584969J',), - ('X7536157T',), - ('Y5840388N',), - ('Z2915723H',), - ('Y4002236C',), - ('X7750702R',), - ('Y0408759V',), -]) +@pytest.mark.parametrize( + ('value',), + [ + ('X0095892M',), + ('X8868108K',), + ('X2911154K',), + ('Y2584969J',), + ('X7536157T',), + ('Y5840388N',), + ('Z2915723H',), + ('Y4002236C',), + ('X7750702R',), + ('Y0408759V',), + ], +) def test_returns_true_on_valid_nie(value): assert es_nie(value) -@pytest.mark.parametrize(('value',), [ - ('K0000023T',), - ('L0000024R',), - ('M0000025W',), - ('00000026A',), - ('00000027G',), - ('00000028M',), - ('00000029Y',), - ('00000030F',), - ('00000031P',), - ('00000032D',), - ('00000033X',), - ('00000034B',), - ('00000035N',), - ('00000036J',), - ('00000037Z',), - ('00000038S',), - ('00000039Q',), - ('00000040V',), - ('00000041H',), - ('00000042L',), - ('00000043C',), - ('00000044K',), - ('00000045E',), -]) +@pytest.mark.parametrize( + ('value',), + [ + ('K0000023T',), + ('L0000024R',), + ('M0000025W',), + ('00000026A',), + ('00000027G',), + ('00000028M',), + ('00000029Y',), + ('00000030F',), + ('00000031P',), + ('00000032D',), + ('00000033X',), + ('00000034B',), + ('00000035N',), + ('00000036J',), + ('00000037Z',), + ('00000038S',), + ('00000039Q',), + ('00000040V',), + ('00000041H',), + ('00000042L',), + ('00000043C',), + ('00000044K',), + ('00000045E',), + ], +) def test_returns_true_on_valid_nif(value): assert es_nif(value) -@pytest.mark.parametrize(('value',), [ - ('12345',), - ('X0000000T',), - ('00000000T',), - ('00000001R',), -]) +@pytest.mark.parametrize( + ('value',), [('12345',), ('X0000000T',), ('00000000T',), ('00000001R',),] +) def test_returns_false_on_invalid_nif(value): result = es_nif(value) assert isinstance(result, ValidationFailure) -@pytest.mark.parametrize(('value',), [ - # CIFs - ('B25162520',), - ('U4839822F',), - ('B96817697',), - # NIEs - ('X0095892M',), - ('X8868108K',), - ('X2911154K',), - # NIFs - ('26643189N',), - ('07060225F',), - ('49166693F',), -]) +@pytest.mark.parametrize( + ('value',), + [ + # CIFs + ('B25162520',), + ('U4839822F',), + ('B96817697',), + # NIEs + ('X0095892M',), + ('X8868108K',), + ('X2911154K',), + # NIFs + ('26643189N',), + ('07060225F',), + ('49166693F',), + ], +) def test_returns_true_on_valid_doi(value): assert es_doi(value) diff --git a/tests/i18n/test_fi.py b/tests/i18n/test_fi.py index b900bc4e..0557a614 100644 --- a/tests/i18n/test_fi.py +++ b/tests/i18n/test_fi.py @@ -5,56 +5,60 @@ from validators.i18n.fi import fi_business_id, fi_ssn -@pytest.mark.parametrize(('value',), [ - ('2336509-6',), # Supercell - ('0112038-9',), # Fast Monkeys - ('2417581-7',), # Nokia -]) +@pytest.mark.parametrize( + ('value',), + [ + ('2336509-6',), # Supercell + ('0112038-9',), # Fast Monkeys + ('2417581-7',), # Nokia + ], +) def test_returns_true_on_valid_business_id(value): assert fi_business_id(value) -@pytest.mark.parametrize(('value',), [ - (None,), - ('',), - ('1233312312',), - ('1333333-8',), - ('1231233-9',), -]) +@pytest.mark.parametrize( + ('value',), [(None,), ('',), ('1233312312',), ('1333333-8',), ('1231233-9',),] +) def test_returns_failed_validation_on_invalid_business_id(value): assert isinstance(fi_business_id(value), ValidationFailure) -@pytest.mark.parametrize(('value',), [ - ('010190-002R',), - ('010101-0101',), - ('010101+0101',), - ('010101A0101',), - ('010190-900P',), -]) +@pytest.mark.parametrize( + ('value',), + [ + ('010190-002R',), + ('010101-0101',), + ('010101+0101',), + ('010101A0101',), + ('010190-900P',), + ], +) def test_returns_true_on_valid_ssn(value): assert fi_ssn(value) -@pytest.mark.parametrize(('value',), [ - (None,), - ('',), - ('010190-001P',), # Too low serial - ('010190-000N',), # Too low serial - ('000190-0023',), # Invalid day - ('010090-002X',), # Invalid month - ('010190-002r',), # Invalid checksum - ('101010-0102',), - ('10a010-0101',), - ('101010-0\xe401',), - ('101010b0101',) -]) +@pytest.mark.parametrize( + ('value',), + [ + (None,), + ('',), + ('010190-001P',), # Too low serial + ('010190-000N',), # Too low serial + ('000190-0023',), # Invalid day + ('010090-002X',), # Invalid month + ('010190-002r',), # Invalid checksum + ('101010-0102',), + ('10a010-0101',), + ('101010-0\xe401',), + ('101010b0101',), + ], +) def test_returns_failed_validation_on_invalid_ssn(value): assert isinstance(fi_ssn(value), ValidationFailure) def test_returns_failed_validation_on_temporal_ssn_when_not_allowed(): assert isinstance( - fi_ssn('010190-900P', allow_temporal_ssn=False), - ValidationFailure + fi_ssn('010190-900P', allow_temporal_ssn=False), ValidationFailure ) diff --git a/tests/test_between.py b/tests/test_between.py index 45f0eeeb..ee10f9b8 100644 --- a/tests/test_between.py +++ b/tests/test_between.py @@ -4,30 +4,23 @@ import validators -@pytest.mark.parametrize(('value', 'min', 'max'), [ - (12, 11, 13), - (12, None, 14), - (12, 11, None), - (12, 12, 12) -]) +@pytest.mark.parametrize( + ('value', 'min', 'max'), + [(12, 11, 13), (12, None, 14), (12, 11, None), (12, 12, 12)], +) def test_returns_true_on_valid_range(value, min, max): assert validators.between(value, min=min, max=max) -@pytest.mark.parametrize(('value', 'min', 'max'), [ - (12, 13, 12), - (12, None, None), -]) +@pytest.mark.parametrize(('value', 'min', 'max'), [(12, 13, 12), (12, None, None),]) def test_raises_assertion_error_for_invalid_args(value, min, max): with pytest.raises(AssertionError): assert validators.between(value, min=min, max=max) -@pytest.mark.parametrize(('value', 'min', 'max'), [ - (12, 13, 14), - (12, None, 11), - (12, 13, None) -]) +@pytest.mark.parametrize( + ('value', 'min', 'max'), [(12, 13, 14), (12, None, 11), (12, 13, None)] +) def test_returns_failed_validation_on_invalid_range(value, min, max): result = validators.between(value, min=min, max=max) assert isinstance(result, validators.ValidationFailure) diff --git a/tests/test_card.py b/tests/test_card.py index d76dbcf2..378658c3 100644 --- a/tests/test_card.py +++ b/tests/test_card.py @@ -10,39 +10,20 @@ mastercard, unionpay, ValidationFailure, - visa + visa, ) -visa_cards = [ - '4242424242424242', - '4000002760003184' -] -mastercard_cards = [ - '5555555555554444', - '2223003122003222' -] -amex_cards = [ - '378282246310005', - '371449635398431' -] -unionpay_cards = [ - '6200000000000005' -] -diners_cards = [ - '3056930009020004', - '36227206271667' -] -jcb_cards = [ - '3566002020360505' -] -discover_cards = [ - '6011111111111117', - '6011000990139424' -] +visa_cards = ['4242424242424242', '4000002760003184'] +mastercard_cards = ['5555555555554444', '2223003122003222'] +amex_cards = ['378282246310005', '371449635398431'] +unionpay_cards = ['6200000000000005'] +diners_cards = ['3056930009020004', '36227206271667'] +jcb_cards = ['3566002020360505'] +discover_cards = ['6011111111111117', '6011000990139424'] @pytest.mark.parametrize( - "value", + 'value', visa_cards + mastercard_cards + amex_cards @@ -55,11 +36,9 @@ def test_returns_true_on_valid_card_number(value): assert card_number(value) -@pytest.mark.parametrize('value', [ - '4242424242424240', - '4000002760003180', - '400000276000318X' -]) +@pytest.mark.parametrize( + 'value', ['4242424242424240', '4000002760003180', '400000276000318X'] +) def test_returns_failed_on_valid_card_number(value): assert isinstance(card_number(value), ValidationFailure) @@ -70,7 +49,7 @@ def test_returns_true_on_valid_visa(value): @pytest.mark.parametrize( - "value", + 'value', mastercard_cards + amex_cards + unionpay_cards @@ -88,7 +67,7 @@ def test_returns_true_on_valid_mastercard(value): @pytest.mark.parametrize( - "value", + 'value', visa_cards + amex_cards + unionpay_cards @@ -106,7 +85,7 @@ def test_returns_true_on_valid_amex(value): @pytest.mark.parametrize( - "value", + 'value', visa_cards + mastercard_cards + unionpay_cards @@ -124,7 +103,7 @@ def test_returns_true_on_valid_unionpay(value): @pytest.mark.parametrize( - "value", + 'value', visa_cards + mastercard_cards + amex_cards @@ -142,7 +121,7 @@ def test_returns_true_on_valid_diners(value): @pytest.mark.parametrize( - "value", + 'value', visa_cards + mastercard_cards + amex_cards @@ -160,7 +139,7 @@ def test_returns_true_on_valid_jcb(value): @pytest.mark.parametrize( - "value", + 'value', visa_cards + mastercard_cards + amex_cards @@ -178,7 +157,7 @@ def test_returns_true_on_valid_discover(value): @pytest.mark.parametrize( - "value", + 'value', visa_cards + mastercard_cards + amex_cards diff --git a/tests/test_domain.py b/tests/test_domain.py index f8fe35b3..eefc8784 100644 --- a/tests/test_domain.py +++ b/tests/test_domain.py @@ -4,39 +4,45 @@ from validators import domain, ValidationFailure -@pytest.mark.parametrize('value', [ - 'example.com', - 'xn----gtbspbbmkef.xn--p1ai', - 'underscore_subdomain.example.com', - 'something.versicherung', - 'someThing.versicherung', - '11.com', - '3.cn', - 'a.cn', - 'sub1.sub2.sample.co.uk', - 'somerandomexample.xn--fiqs8s', - 'kräuter.com', - 'über.com' -]) +@pytest.mark.parametrize( + 'value', + [ + 'example.com', + 'xn----gtbspbbmkef.xn--p1ai', + 'underscore_subdomain.example.com', + 'something.versicherung', + 'someThing.versicherung', + '11.com', + '3.cn', + 'a.cn', + 'sub1.sub2.sample.co.uk', + 'somerandomexample.xn--fiqs8s', + 'kräuter.com', + 'über.com', + ], +) def test_returns_true_on_valid_domain(value): assert domain(value) -@pytest.mark.parametrize('value', [ - 'example.com/', - 'example.com:4444', - 'example.-com', - 'example.', - '-example.com', - 'example-.com', - '_example.com', - 'example_.com', - 'example', - 'a......b.com', - 'a.123', - '123.123', - '123.123.123', - '123.123.123.123' -]) +@pytest.mark.parametrize( + 'value', + [ + 'example.com/', + 'example.com:4444', + 'example.-com', + 'example.', + '-example.com', + 'example-.com', + '_example.com', + 'example_.com', + 'example', + 'a......b.com', + 'a.123', + '123.123', + '123.123.123', + '123.123.123.123', + ], +) def test_returns_failed_validation_on_invalid_domain(value): assert isinstance(domain(value), ValidationFailure) diff --git a/tests/test_email.py b/tests/test_email.py index 0b7f4e27..8cfe576f 100644 --- a/tests/test_email.py +++ b/tests/test_email.py @@ -4,42 +4,48 @@ from validators import email, ValidationFailure -@pytest.mark.parametrize(('value', 'whitelist'), [ - ('email@here.com', None), - ('weirder-email@here.and.there.com', None), - ('email@[127.0.0.1]', None), - ('example@valid-----hyphens.com', None), - ('example@valid-with-hyphens.com', None), - ('test@domain.with.idn.tld.उदाहरण.परीक्षा', None), - ('email@localhost', None), - ('email@localdomain', ['localdomain']), - ('"test@test"@example.com', None), - ('"\\\011"@here.com', None), -]) +@pytest.mark.parametrize( + ('value', 'whitelist'), + [ + ('email@here.com', None), + ('weirder-email@here.and.there.com', None), + ('email@[127.0.0.1]', None), + ('example@valid-----hyphens.com', None), + ('example@valid-with-hyphens.com', None), + ('test@domain.with.idn.tld.उदाहरण.परीक्षा', None), + ('email@localhost', None), + ('email@localdomain', ['localdomain']), + ('"test@test"@example.com', None), + ('"\\\011"@here.com', None), + ], +) def test_returns_true_on_valid_email(value, whitelist): assert email(value, whitelist=whitelist) -@pytest.mark.parametrize(('value',), [ - (None,), - ('',), - ('abc',), - ('abc@',), - ('abc@bar',), - ('a @x.cz',), - ('abc@.com',), - ('something@@somewhere.com',), - ('email@127.0.0.1',), - ('example@invalid-.com',), - ('example@-invalid.com',), - ('example@inv-.alid-.com',), - ('example@inv-.-alid.com',), - ( - 'john56789.john56789.john56789.john56789.john56789.john56789.john5' - '@example.com', - ), - # Quoted-string format (CR not allowed) - ('"\\\012"@here.com',), -]) +@pytest.mark.parametrize( + ('value',), + [ + (None,), + ('',), + ('abc',), + ('abc@',), + ('abc@bar',), + ('a @x.cz',), + ('abc@.com',), + ('something@@somewhere.com',), + ('email@127.0.0.1',), + ('example@invalid-.com',), + ('example@-invalid.com',), + ('example@inv-.alid-.com',), + ('example@inv-.-alid.com',), + ( + 'john56789.john56789.john56789.john56789.john56789.john56789.john5' + '@example.com', + ), + # Quoted-string format (CR not allowed) + ('"\\\012"@here.com',), + ], +) def test_returns_failed_validation_on_invalid_email(value): assert isinstance(email(value), ValidationFailure) diff --git a/tests/test_extremes.py b/tests/test_extremes.py index d9f5023c..5bc1e0e3 100644 --- a/tests/test_extremes.py +++ b/tests/test_extremes.py @@ -4,12 +4,7 @@ from validators import Max, Min -@pytest.mark.parametrize(('value',), [ - (None,), - ('',), - (12,), - (Min,), -]) +@pytest.mark.parametrize(('value',), [(None,), ('',), (12,), (Min,),]) def test_max_is_greater_than_every_other_value(value): assert value < Max assert Max > value @@ -25,12 +20,7 @@ def test_other_comparison_methods_for_max(): assert not (Max != Max) -@pytest.mark.parametrize(('value',), [ - (None,), - ('',), - (12,), - (Max,), -]) +@pytest.mark.parametrize(('value',), [(None,), ('',), (12,), (Max,),]) def test_min_is_smaller_than_every_other_value(value): assert value > Min diff --git a/tests/test_iban.py b/tests/test_iban.py index 2dcca652..26aa2557 100644 --- a/tests/test_iban.py +++ b/tests/test_iban.py @@ -4,18 +4,12 @@ import validators -@pytest.mark.parametrize('value', [ - 'GB82WEST12345698765432', - 'NO9386011117947' -]) +@pytest.mark.parametrize('value', ['GB82WEST12345698765432', 'NO9386011117947']) def test_returns_true_on_valid_iban(value): assert validators.iban(value) -@pytest.mark.parametrize('value', [ - 'GB81WEST12345698765432', - 'NO9186011117947' -]) +@pytest.mark.parametrize('value', ['GB81WEST12345698765432', 'NO9186011117947']) def test_returns_failed_validation_on_invalid_iban(value): result = validators.iban(value) assert isinstance(result, validators.ValidationFailure) diff --git a/tests/test_ipv4.py b/tests/test_ipv4.py index 3dac98d8..829cdc1e 100644 --- a/tests/test_ipv4.py +++ b/tests/test_ipv4.py @@ -4,21 +4,17 @@ from validators import ipv4, ipv6, ValidationFailure -@pytest.mark.parametrize(('address',), [ - ('127.0.0.1',), - ('123.5.77.88',), - ('12.12.12.12',), -]) +@pytest.mark.parametrize( + ('address',), [('127.0.0.1',), ('123.5.77.88',), ('12.12.12.12',),] +) def test_returns_true_on_valid_ipv4_address(address): assert ipv4(address) assert not ipv6(address) -@pytest.mark.parametrize(('address',), [ - ('abc.0.0.1',), - ('1278.0.0.1',), - ('127.0.0.abc',), - ('900.200.100.75',), -]) +@pytest.mark.parametrize( + ('address',), + [('abc.0.0.1',), ('1278.0.0.1',), ('127.0.0.abc',), ('900.200.100.75',),], +) def test_returns_failed_validation_on_invalid_ipv4_address(address): assert isinstance(ipv4(address), ValidationFailure) diff --git a/tests/test_ipv4_cidr.py b/tests/test_ipv4_cidr.py index 3216a17a..eb130baa 100644 --- a/tests/test_ipv4_cidr.py +++ b/tests/test_ipv4_cidr.py @@ -4,22 +4,17 @@ from validators import ipv4_cidr, ipv6_cidr, ValidationFailure -@pytest.mark.parametrize(('cidr',), [ - ('127.0.0.1/0',), - ('123.5.77.88/8',), - ('12.12.12.12/32',), -]) +@pytest.mark.parametrize( + ('cidr',), [('127.0.0.1/0',), ('123.5.77.88/8',), ('12.12.12.12/32',),] +) def test_returns_true_on_valid_ipv4_cidr(cidr): assert ipv4_cidr(cidr) assert not ipv6_cidr(cidr) -@pytest.mark.parametrize(('cidr',), [ - ('abc.0.0.1',), - ('1.1.1.1',), - ('1.1.1.1/-1',), - ('1.1.1.1/33',), - ('1.1.1.1/foo',), -]) +@pytest.mark.parametrize( + ('cidr',), + [('abc.0.0.1',), ('1.1.1.1',), ('1.1.1.1/-1',), ('1.1.1.1/33',), ('1.1.1.1/foo',),], +) def test_returns_failed_validation_on_invalid_ipv4_cidr(cidr): assert isinstance(ipv4_cidr(cidr), ValidationFailure) diff --git a/tests/test_ipv6.py b/tests/test_ipv6.py index 3f321404..4c650e9c 100644 --- a/tests/test_ipv6.py +++ b/tests/test_ipv6.py @@ -4,23 +4,24 @@ from validators import ipv4, ipv6, ValidationFailure -@pytest.mark.parametrize(('address',), [ - ('::1',), - ('dead:beef:0:0:0:0:42:1',), - ('abcd:ef::42:1',), - ('0:0:0:0:0:ffff:1.2.3.4',), - ('::192.168.30.2',), -]) +@pytest.mark.parametrize( + ('address',), + [ + ('::1',), + ('dead:beef:0:0:0:0:42:1',), + ('abcd:ef::42:1',), + ('0:0:0:0:0:ffff:1.2.3.4',), + ('::192.168.30.2',), + ], +) def test_returns_true_on_valid_ipv6_address(address): assert ipv6(address) assert not ipv4(address) -@pytest.mark.parametrize(('address',), [ - ('abc.0.0.1',), - ('abcd:1234::123::1',), - ('1:2:3:4:5:6:7:8:9',), - ('abcd::1ffff',), -]) +@pytest.mark.parametrize( + ('address',), + [('abc.0.0.1',), ('abcd:1234::123::1',), ('1:2:3:4:5:6:7:8:9',), ('abcd::1ffff',),], +) def test_returns_failed_validation_on_invalid_ipv6_address(address): assert isinstance(ipv6(address), ValidationFailure) diff --git a/tests/test_ipv6_cidr.py b/tests/test_ipv6_cidr.py index 308390a9..cd28206d 100644 --- a/tests/test_ipv6_cidr.py +++ b/tests/test_ipv6_cidr.py @@ -4,28 +4,34 @@ from validators import ipv4_cidr, ipv6_cidr, ValidationFailure -@pytest.mark.parametrize(('cidr',), [ - ('::1/0',), - ('dead:beef:0:0:0:0:42:1/8',), - ('abcd:ef::42:1/32',), - ('0:0:0:0:0:ffff:1.2.3.4/64',), - ('::192.168.30.2/128',), -]) +@pytest.mark.parametrize( + ('cidr',), + [ + ('::1/0',), + ('dead:beef:0:0:0:0:42:1/8',), + ('abcd:ef::42:1/32',), + ('0:0:0:0:0:ffff:1.2.3.4/64',), + ('::192.168.30.2/128',), + ], +) def test_returns_true_on_valid_ipv6_cidr(cidr): assert ipv6_cidr(cidr) assert not ipv4_cidr(cidr) -@pytest.mark.parametrize(('cidr',), [ - ('abc.0.0.1',), - ('abcd:1234::123::1',), - ('1:2:3:4:5:6:7:8:9',), - ('abcd::1ffff',), - ('1.1.1.1',), - ('::1',), - ('::1/129',), - ('::1/-1',), - ('::1/foo',), -]) +@pytest.mark.parametrize( + ('cidr',), + [ + ('abc.0.0.1',), + ('abcd:1234::123::1',), + ('1:2:3:4:5:6:7:8:9',), + ('abcd::1ffff',), + ('1.1.1.1',), + ('::1',), + ('::1/129',), + ('::1/-1',), + ('::1/foo',), + ], +) def test_returns_failed_validation_on_invalid_ipv6_cidr(cidr): assert isinstance(ipv6_cidr(cidr), ValidationFailure) diff --git a/tests/test_length.py b/tests/test_length.py index 86342f1e..04a04438 100644 --- a/tests/test_length.py +++ b/tests/test_length.py @@ -4,34 +4,38 @@ import validators -@pytest.mark.parametrize(('value', 'min', 'max'), [ - ('password', 2, 10), - ('password', None, 10), - ('password', 2, None), - ('password', 8, 8) -]) +@pytest.mark.parametrize( + ('value', 'min', 'max'), + [ + ('password', 2, 10), + ('password', None, 10), + ('password', 2, None), + ('password', 8, 8), + ], +) def test_returns_true_on_valid_length(value, min, max): assert validators.length(value, min=min, max=max) -@pytest.mark.parametrize(('value', 'min', 'max'), [ - ('something', 13, 12), - ('something', -1, None), - ('something', -1, None), - ('something', -3, -2) -]) +@pytest.mark.parametrize( + ('value', 'min', 'max'), + [ + ('something', 13, 12), + ('something', -1, None), + ('something', -1, None), + ('something', -3, -2), + ], +) def test_raises_assertion_error_for_invalid_args(value, min, max): with pytest.raises(AssertionError): assert validators.length(value, min=min, max=max) -@pytest.mark.parametrize(('value', 'min', 'max'), [ - ('something', 13, 14), - ('something', None, 6), - ('something', 13, None) -]) +@pytest.mark.parametrize( + ('value', 'min', 'max'), + [('something', 13, 14), ('something', None, 6), ('something', 13, None)], +) def test_returns_failed_validation_on_invalid_range(value, min, max): assert isinstance( - validators.length(value, min=min, max=max), - validators.ValidationFailure + validators.length(value, min=min, max=max), validators.ValidationFailure ) diff --git a/tests/test_mac_address.py b/tests/test_mac_address.py index 756fa3ec..3eaeaf0d 100644 --- a/tests/test_mac_address.py +++ b/tests/test_mac_address.py @@ -4,18 +4,19 @@ from validators import mac_address, ValidationFailure -@pytest.mark.parametrize(('address',), [ - ('01:23:45:67:ab:CD',), -]) +@pytest.mark.parametrize(('address',), [('01:23:45:67:ab:CD',),]) def test_returns_true_on_valid_mac_address(address): assert mac_address(address) -@pytest.mark.parametrize(('address',), [ - ('00:00:00:00:00',), - ('01:23:45:67:89:',), - ('01:23:45:67:89:gh',), - ('123:23:45:67:89:00',), -]) +@pytest.mark.parametrize( + ('address',), + [ + ('00:00:00:00:00',), + ('01:23:45:67:89:',), + ('01:23:45:67:89:gh',), + ('123:23:45:67:89:00',), + ], +) def test_returns_failed_validation_on_invalid_mac_address(address): assert isinstance(mac_address(address), ValidationFailure) diff --git a/tests/test_md5.py b/tests/test_md5.py index 3efb1d69..0a5fd689 100644 --- a/tests/test_md5.py +++ b/tests/test_md5.py @@ -4,19 +4,21 @@ import validators -@pytest.mark.parametrize('value', [ - 'd41d8cd98f00b204e9800998ecf8427e', - 'D41D8CD98F00B204E9800998ECF8427E' -]) +@pytest.mark.parametrize( + 'value', ['d41d8cd98f00b204e9800998ecf8427e', 'D41D8CD98F00B204E9800998ECF8427E'] +) def test_returns_true_on_valid_md5(value): assert validators.md5(value) -@pytest.mark.parametrize('value', [ - 'z41d8cd98f00b204e9800998ecf8427e', - 'z8cd98f00b204e9800998ecf8427e', - 'z4aaaa1d8cd98f00b204e9800998ecf8427e' -]) +@pytest.mark.parametrize( + 'value', + [ + 'z41d8cd98f00b204e9800998ecf8427e', + 'z8cd98f00b204e9800998ecf8427e', + 'z4aaaa1d8cd98f00b204e9800998ecf8427e', + ], +) def test_returns_failed_validation_on_invalid_md5(value): result = validators.md5(value) assert isinstance(result, validators.ValidationFailure) diff --git a/tests/test_sha1.py b/tests/test_sha1.py index b729009a..727a961f 100644 --- a/tests/test_sha1.py +++ b/tests/test_sha1.py @@ -4,19 +4,25 @@ import validators -@pytest.mark.parametrize('value', [ - 'da39a3ee5e6b4b0d3255bfef95601890afd80709', - 'DA39A3EE5E6B4B0D3255BFEF95601890AFD80709' -]) +@pytest.mark.parametrize( + 'value', + [ + 'da39a3ee5e6b4b0d3255bfef95601890afd80709', + 'DA39A3EE5E6B4B0D3255BFEF95601890AFD80709', + ], +) def test_returns_true_on_valid_sha1(value): assert validators.sha1(value) -@pytest.mark.parametrize('value', [ - 'za39a3ee5e6b4b0d3255bfef95601890afd80709', - 'da39e5e6b4b0d3255bfef95601890afd80709', - 'daaaa39a3ee5e6b4b0d3255bfef95601890afd80709' -]) +@pytest.mark.parametrize( + 'value', + [ + 'za39a3ee5e6b4b0d3255bfef95601890afd80709', + 'da39e5e6b4b0d3255bfef95601890afd80709', + 'daaaa39a3ee5e6b4b0d3255bfef95601890afd80709', + ], +) def test_returns_failed_validation_on_invalid_sha1(value): result = validators.sha1(value) assert isinstance(result, validators.ValidationFailure) diff --git a/tests/test_sha224.py b/tests/test_sha224.py index 225275b9..e508b370 100644 --- a/tests/test_sha224.py +++ b/tests/test_sha224.py @@ -4,19 +4,25 @@ import validators -@pytest.mark.parametrize('value', [ - 'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', - 'D14A028C2A3A2BC9476102BB288234C415A2B01F828EA62AC5B3E42F' -]) +@pytest.mark.parametrize( + 'value', + [ + 'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', + 'D14A028C2A3A2BC9476102BB288234C415A2B01F828EA62AC5B3E42F', + ], +) def test_returns_true_on_valid_sha224(value): assert validators.sha224(value) -@pytest.mark.parametrize('value', [ - 'z14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', - 'd028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', - 'daaa14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f' -]) +@pytest.mark.parametrize( + 'value', + [ + 'z14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', + 'd028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', + 'daaa14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', + ], +) def test_returns_failed_validation_on_invalid_sha224(value): result = validators.sha224(value) assert isinstance(result, validators.ValidationFailure) diff --git a/tests/test_sha256.py b/tests/test_sha256.py index b9c20776..8c9bbcb6 100644 --- a/tests/test_sha256.py +++ b/tests/test_sha256.py @@ -4,19 +4,25 @@ import validators -@pytest.mark.parametrize('value', [ - 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', - 'E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855' -]) +@pytest.mark.parametrize( + 'value', + [ + 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', + 'E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', + ], +) def test_returns_true_on_valid_sha256(value): assert validators.sha256(value) -@pytest.mark.parametrize('value', [ - 'z3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', - 'ec44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', - 'eaaaa3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', -]) +@pytest.mark.parametrize( + 'value', + [ + 'z3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', + 'ec44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', + 'eaaaa3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', + ], +) def test_returns_failed_validation_on_invalid_sha256(value): result = validators.sha256(value) assert isinstance(result, validators.ValidationFailure) diff --git a/tests/test_sha512.py b/tests/test_sha512.py index 7a7aabba..28d2d8d7 100644 --- a/tests/test_sha512.py +++ b/tests/test_sha512.py @@ -4,34 +4,40 @@ import validators -@pytest.mark.parametrize('value', [ - ( - 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d' - '13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e' - ), - ( - 'CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D' - '13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E' - ) -]) +@pytest.mark.parametrize( + 'value', + [ + ( + 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d' + '13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e' + ), + ( + 'CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D' + '13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E' + ), + ], +) def test_returns_true_on_valid_sha512(value): assert validators.sha512(value) -@pytest.mark.parametrize('value', [ - ( - 'zf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d' - '13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e' - ), - ( - 'cf8357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c' - '5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e' - ), - ( - 'cf8aaaa3e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce4' - '7d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e' - ) -]) +@pytest.mark.parametrize( + 'value', + [ + ( + 'zf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d' + '13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e' + ), + ( + 'cf8357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c' + '5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e' + ), + ( + 'cf8aaaa3e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce4' + '7d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e' + ), + ], +) def test_returns_failed_validation_on_invalid_sha512(value): result = validators.sha512(value) assert isinstance(result, validators.ValidationFailure) diff --git a/tests/test_slug.py b/tests/test_slug.py index a42fe5f7..8a74a780 100644 --- a/tests/test_slug.py +++ b/tests/test_slug.py @@ -4,20 +4,13 @@ from validators import slug, ValidationFailure -@pytest.mark.parametrize('value', [ - '123-12312-asdasda', - '123____123', - 'dsadasd-dsadas', -]) +@pytest.mark.parametrize( + 'value', ['123-12312-asdasda', '123____123', 'dsadasd-dsadas',] +) def test_returns_true_on_valid_slug(value): assert slug(value) -@pytest.mark.parametrize('value', [ - 'some.slug', - '1231321%', - ' 21312', - '123asda&', -]) +@pytest.mark.parametrize('value', ['some.slug', '1231321%', ' 21312', '123asda&',]) def test_returns_failed_validation_on_invalid_slug(value): assert isinstance(slug(value), ValidationFailure) diff --git a/tests/test_uuid.py b/tests/test_uuid.py index 42415c2a..c8258e12 100644 --- a/tests/test_uuid.py +++ b/tests/test_uuid.py @@ -4,18 +4,19 @@ from validators import uuid, ValidationFailure -@pytest.mark.parametrize(('value',), [ - ('2bc1c94f-0deb-43e9-92a1-4775189ec9f8',), -]) +@pytest.mark.parametrize(('value',), [('2bc1c94f-0deb-43e9-92a1-4775189ec9f8',),]) def test_returns_true_on_valid_mac_address(value): assert uuid(value) -@pytest.mark.parametrize(('value',), [ - ('2bc1c94f-deb-43e9-92a1-4775189ec9f8',), - ('2bc1c94f-0deb-43e9-92a1-4775189ec9f',), - ('gbc1c94f-0deb-43e9-92a1-4775189ec9f8',), - ('2bc1c94f 0deb-43e9-92a1-4775189ec9f8',), -]) +@pytest.mark.parametrize( + ('value',), + [ + ('2bc1c94f-deb-43e9-92a1-4775189ec9f8',), + ('2bc1c94f-0deb-43e9-92a1-4775189ec9f',), + ('gbc1c94f-0deb-43e9-92a1-4775189ec9f8',), + ('2bc1c94f 0deb-43e9-92a1-4775189ec9f8',), + ], +) def test_returns_failed_validation_on_invalid_mac_address(value): assert isinstance(uuid(value), ValidationFailure) diff --git a/tests/test_validation_failure.py b/tests/test_validation_failure.py index 0601aa6f..e3a915d6 100644 --- a/tests/test_validation_failure.py +++ b/tests/test_validation_failure.py @@ -2,9 +2,7 @@ import validators -obj_repr = ( - "ValidationFailure(func=between" -) +obj_repr = ('ValidationFailure(func=between') class TestValidationFailure(object): diff --git a/validators/__init__.py b/validators/__init__.py index 7244ad9a..5b354e67 100644 --- a/validators/__init__.py +++ b/validators/__init__.py @@ -1,15 +1,6 @@ from .between import between from .btc_address import btc_address -from .card import ( - amex, - card_number, - diners, - discover, - jcb, - mastercard, - unionpay, - visa -) +from .card import amex, card_number, diners, discover, jcb, mastercard, unionpay, visa from .domain import domain from .email import email from .extremes import Max, Min @@ -25,11 +16,41 @@ from .utils import ValidationFailure, validator from .uuid import uuid -__all__ = ('between', 'domain', 'email', 'Max', 'Min', 'md5', 'sha1', 'sha224', - 'sha256', 'sha512', 'fi_business_id', 'fi_ssn', 'iban', 'ipv4', - 'ipv4_cidr', 'ipv6', 'ipv6_cidr', 'length', 'mac_address', 'slug', - 'truthy', 'url', 'ValidationFailure', 'validator', 'uuid', - 'card_number', 'visa', 'mastercard', 'amex', 'unionpay', 'diners', - 'jcb', 'discover', 'btc_address') +__all__ = ( + 'between', + 'domain', + 'email', + 'Max', + 'Min', + 'md5', + 'sha1', + 'sha224', + 'sha256', + 'sha512', + 'fi_business_id', + 'fi_ssn', + 'iban', + 'ipv4', + 'ipv4_cidr', + 'ipv6', + 'ipv6_cidr', + 'length', + 'mac_address', + 'slug', + 'truthy', + 'url', + 'ValidationFailure', + 'validator', + 'uuid', + 'card_number', + 'visa', + 'mastercard', + 'amex', + 'unionpay', + 'diners', + 'jcb', + 'discover', + 'btc_address', +) __version__ = '0.18.0' diff --git a/validators/between.py b/validators/between.py index 46f223c9..6d67c372 100644 --- a/validators/between.py +++ b/validators/between.py @@ -44,9 +44,7 @@ def between(value, min=None, max=None): .. versionadded:: 0.2 """ if min is None and max is None: - raise AssertionError( - 'At least one of `min` or `max` must be specified.' - ) + raise AssertionError('At least one of `min` or `max` must be specified.') if min is None: min = Min if max is None: diff --git a/validators/card.py b/validators/card.py index 8b8cbeea..c8eed55e 100644 --- a/validators/card.py +++ b/validators/card.py @@ -136,9 +136,7 @@ def diners(value): :param value: Diners Club card number string to validate """ pattern = re.compile(r'^(30|36|38|39)') - return ( - card_number(value) and len(value) in [14, 16] and pattern.match(value) - ) + return card_number(value) and len(value) in [14, 16] and pattern.match(value) @validator