Skip to content

VATIN tweaks #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdnum/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def get_cc_module(cc, name):
mod = __import__('stdnum.%s' % cc, globals(), locals(), [str(name)])
return getattr(mod, name, None)
except ImportError:
return
return None


# this is a cache of SOAP clients
Expand Down
14 changes: 10 additions & 4 deletions stdnum/vatin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
>>> validate('XX')
Traceback (most recent call last):
...
InvalidComponent: ...
ImportError: ...
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validate() function should only raise one of the exceptions listed in stdnum.exceptions.

"""

import re
Expand All @@ -53,6 +53,10 @@
from stdnum.util import clean, get_cc_module


# regular expression for matching country codes.
_country_code_re = re.compile(r'^[a-z]{2}$')


# Cache of country code modules
_country_modules = dict()

Expand All @@ -61,12 +65,12 @@ def _get_cc_module(cc):
"""Get the VAT number module based on the country code."""
# Greece uses a "wrong" country code, special case for Northern Ireland
cc = cc.lower().replace('el', 'gr').replace('xi', 'gb')
if not re.match(r'^[a-z]{2}$', cc):
raise InvalidFormat()
if not _country_code_re.match(cc):
raise ImportError()
if cc not in _country_modules:
_country_modules[cc] = get_cc_module(cc, 'vat')
if not _country_modules[cc]:
raise InvalidComponent() # unknown/unsupported country code
raise ImportError()
return _country_modules[cc]


Expand Down Expand Up @@ -94,5 +98,7 @@ def is_valid(number):
"""Check if the number is a valid VAT number."""
try:
return bool(validate(number))
except ImportError:
return False
except ValidationError:
return False
26 changes: 22 additions & 4 deletions tests/test_vatin.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,38 @@ Try validating not specifying a country:
>>> vatin.validate('')
Traceback (most recent call last):
...
InvalidFormat: ...
ImportError: ...
>>> vatin.validate('00')
Traceback (most recent call last):
...
InvalidFormat: ...
ImportError: ...


Try to validate for countries with no VAT validation:

>>> vatin.validate('XX')
Traceback (most recent call last):
...
InvalidComponent: ...
ImportError: ...
>>> vatin.validate('US')
Traceback (most recent call last):
...
InvalidComponent: ...
ImportError: ...


Check is_valid for several scenarios:

>>> vatin.is_valid('FR 40 303 265 045')
True
>>> vatin.is_valid('FR 40 303')
False
>>> vatin.is_valid('FR')
False
>>> vatin.is_valid('')
False
>>> vatin.is_valid('00')
False
>>> vatin.is_valid('XX')
False
>>> vatin.is_valid('US')
False