项目作者: diegocedrim

项目描述 :
Implements a simple VAT validator
高级语言: Python
项目地址: git://github.com/diegocedrim/vat-validator.git
创建时间: 2017-09-20T13:59:43Z
项目社区:https://github.com/diegocedrim/vat-validator

开源协议:

下载


vat-validator

Implements a simple VAT validator by using the webservice available at http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl

Installation Instructions

This script was developed on Python 3.5. Also, it depends on Zeep (http://docs.python-zeep.org/), which is a Python SOAP client.
Before running, please install the Zeep package with the following command.

  1. pip install lxml==3.7.3 zeep

Running Tests

You can run all implemented tests with the following command. Make sure you run this command in the project root folder.

  1. python3.5 -m unittest discover test "*_test.py"

Running the Validator

The main function of the validator is in the vat_validator.py file. You can validate VAT numbers by passing a single
argument containing the VAT number to be validated. Some execution examples can be seen below.

  1. python3.5 vat_validator.py AAA2898738973 #exceptional case
  2. python3.5 vat_validator.py CZ28987373 #valid
  3. python3.5 vat_validator.py DE296459264 #valid
  4. python3.5 vat_validator.py CZ12345 #invalid
  5. python3.5 vat_validator.py #error: invalid input - no argument
  6. python3.5 vat_validator.py CZ12345 CZ12345 #invalid input - two arguments

Observations

A minimalist version of this project would simply get the argument from command line and validate it by using the webservice. Such version can be seen below.

  1. import sys
  2. import zeep.exceptions
  3. vat_number = sys.argv[1]
  4. country = vat_number[:2]
  5. number = vat_number[2:]
  6. try:
  7. client = zeep.Client('http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl')
  8. response = client.service.checkVat(country, number)
  9. if response.valid:
  10. print('Valid')
  11. else:
  12. print('Invalid')
  13. except zeep.exceptions.Error:
  14. print('Exception')

I added some complexity to the project to optimize the number of calls to the web service. First, the script validates the format of the input, and only if the input is in the valid format, I call the web service. The initial validation is performed in the vatin.py file. The vat_rpc.py file performs the actual call to the web service, but only after validating the format.

Also, some of the complexity of the code is related to the testability quality attribute. Some classes were created with arguments that enable us to inject mock objects for testing purposes. For instance, the client argument in the init method of VatRpcClient class.