sivin: compute euro5 performance level from environment data (#69107)

This commit is contained in:
Serghei Mihai 2022-09-15 15:00:28 +02:00
parent 2cb2eb1c18
commit f6e1dde2dd
4 changed files with 58 additions and 1 deletions

1
debian/control vendored
View File

@ -32,6 +32,7 @@ Depends: python3-cmislib,
python3-pyexcel-xls,
python3-pyproj,
python3-requests,
python3-roman,
python3-setuptools,
python3-suds,
python3-unidecode,

View File

@ -17,6 +17,7 @@
import re
from urllib.parse import urljoin
import roman
from django.core.cache import cache
from django.db import models
from django.utils.translation import gettext_lazy as _
@ -86,6 +87,25 @@ class Resource(BaseResource):
raise APIError('failed to call %s: %s' % (url, e))
return resp.json()
def is_euro5(self, env_perf):
env_perf = env_perf.upper()
if not 'EURO' in env_perf:
return False
euro_index = env_perf.split('EURO')[-1]
# remove weird data in parantheses
euro_index = re.sub(r'\(\w+\)', '', euro_index)
# remove remaining spaces
euro_index = re.sub(r'[^\w]', '', euro_index)
if not euro_index:
return False
digit_index = re.search(r'^\d', euro_index)
if digit_index:
return int(digit_index.group(0)) >= 5
try:
return roman.fromRoman(euro_index) >= 5
except roman.InvalidRomanNumeralError:
return False
def get_infos_by_immat(self, endpoint, immat, codesra=None):
# remove dashes / spaces in immat to avoid lookup issues
immat = immat.strip().replace('-', '').replace(' ', '').upper()
@ -95,7 +115,10 @@ class Resource(BaseResource):
payload = {'immat': immat}
if codesra is not None:
payload['codesra'] = codesra
return {'data': self.call(endpoint, payload)}
result = self.call(endpoint, payload)
if 'clEnvironPrf' in result:
result['is_euro5'] = self.is_euro5(result['clEnvironPrf'])
return {'data': result}
@endpoint(
perm='can_access',

View File

@ -167,6 +167,7 @@ setup(
'Levenshtein',
'python-ldap',
'pyOpenSSL',
'roman',
],
cmdclass={
'build': build,

View File

@ -27,6 +27,31 @@ from passerelle.base.models import AccessRight, ApiUser
pytestmark = pytest.mark.django_db
EURO5_INDEXES = [
('0555*0651C Euro 4', False),
('0555*0651G Euro 5', True),
('0555*0651G EURO 5', True),
('0555*0874G (EURO 5)', True),
('134/2014EURO4', False),
('134/2014EURO5', True),
('175/2007*195/2013EURO6', True),
('200/55*2008/74EURO5', True),
('2005/55*51euro5', True),
('2006/51G-EUROV (G)', True),
('2006/96euro4', False),
('59/2009*2016/1718EURO6', True),
('595/2009*2018/932DEUROVI', True),
('595/2009*2018/932EURO', False),
('595/2009*627/2014EUROIV', False),
('70/220*1999/102EURO3', False),
('EURO 3', False),
('EURO III', False),
('EURO2', False),
('EURO5G', True),
('EURO6B', True),
('2001/100A', False),
]
VEHICLE_DETAILS = {
"carrosserie": "BERLINE",
"clEnvironPrf": "70/220 2001/100EURO3",
@ -189,6 +214,8 @@ def test_get_vehicle_theorical_finition(mocked_post, app, conn, immat, sent_imma
assert mocked_post.call_count == 2
assert mocked_post.mock_calls[-1].kwargs['json'] == {'immat': sent_immat}
assert not resp['err']
assert 'is_euro5' in resp['data']
resp['data'].pop('is_euro5')
assert resp['data'] == VEHICLE_THEORICAL_FINITION
@ -205,3 +232,8 @@ def test_connection_timeout(mocked_post, app, conn):
resp['err_desc']
== 'failed to call https://api.rec.sivin.fr/sivin/v2/consulterflotteparsiren: timeout'
)
@pytest.mark.parametrize('clEnvironPrf, is_euro5', EURO5_INDEXES)
def test_compute_euro_index(app, conn, clEnvironPrf, is_euro5):
assert conn.is_euro5(clEnvironPrf) == is_euro5