tests: add non-regression tests on endpoints (#52084)

This commit is contained in:
Nicolas Roche 2021-03-26 11:17:27 +01:00
parent 6e02f79fb7
commit 49408b91ec
1 changed files with 234 additions and 10 deletions

View File

@ -9,6 +9,17 @@ from passerelle_montpellier_sig.views import prefix_cleanup, split_street
from passerelle.utils.jsonresponse import APIError
class Mixin(object):
def setUp(self):
self.connector, created = MontpellierSig.objects.get_or_create(
title='hebe', slug='hebe', description='hebe', service_url='http://hebe.example.net'
)
@httmock.all_requests
def hebe_http_error(url, request, *args, **kwargs):
return httmock.response(500, 'Internal Error')
class PrefixCleanupTestCase(TestCase):
def test_prefix_alle(self):
self.assertEqual(prefix_cleanup('ALL DE LA PAIX'), 'ALLEE DE LA PAIX')
@ -72,22 +83,235 @@ class ReverseGeolocTest(TestCase):
self.assertEqual(split_street('42 AV RAYMOND DUGRAND '), ('42', 'AVENUE RAYMOND DUGRAND'))
class ConnectionTest(TestCase):
def setUp(self):
self.connector, created = MontpellierSig.objects.get_or_create(
title='hebe', slug='hebe', description='hebe', service_url='http://hebe.montpellier3m.fr'
)
class ConnectionTest(Mixin, TestCase):
@httmock.all_requests
def hebe_connection_failed(url, request, *args, **kwargs):
raise httmock.requests.exceptions.RequestException('connection error')
@httmock.all_requests
def hebe_http_error(url, request, *args, **kwargs):
return httmock.response(500, 'Internal Error')
def test_connection_fail(self):
with self.assertRaises(APIError) as e:
with httmock.HTTMock(self.hebe_http_error):
self.connector.sig_request('commune')
self.assertEqual(str(e.exception), "endpoint 'commune' returned status code: 500")
class CommunesViewTest(Mixin, TestCase):
URL = '/montpellier-sig/hebe/communes?q=MAGUELONE'
def test_communes_view(self):
app = Client()
resp = app.get(self.URL)
assert json.loads(resp.content) == {"data": [{"id": "34337", "text": "VILLENEUVE-LES-MAGUELONE"}]}
class VoiesViewTest(Mixin, TestCase):
URL = '/montpellier-sig/hebe/voies/34259'
@httmock.urlmatch(path='/adresse/rest/commune/34259/voie')
def hebe_http_voies(url, request, *args, **kwargs):
data = [
{'attributes': {'nom_voie': 'IMP DES ADRETS'}},
{'attributes': {'nom_voie': 'IMP DES ALBYZZIAS'}},
{'attributes': {'nom_voie': 'IMP DES ARBOUSIERS'}},
]
return httmock.response(200, json.dumps(data))
def test_voies_view(self):
app = Client()
with httmock.HTTMock(self.hebe_http_voies):
resp = app.get(self.URL + '?q=ALBYZZIAS')
assert json.loads(resp.content) == {
"data": [{"id": "IMP DES ALBYZZIAS", "text": "IMPASSE DES ALBYZZIAS"}]
}
def test_voies_view_jsonp(self):
app = Client()
with httmock.HTTMock(self.hebe_http_voies):
resp = app.get(
self.URL
+ '?callback=jQuery110206937632912688186_1615823034444&q=IMPASSE+DES+ALBYZZIAS&_=1615823034445'
)
assert (
resp.content
== b'jQuery110206937632912688186_1615823034444({"data": [{"id": "IMP DES ALBYZZIAS", "text": "IMPASSE DES ALBYZZIAS"}]});'
)
class VoieCommuneViewTest(Mixin, TestCase):
URL = '/montpellier-sig/hebe/voiecommune/COLUCHE'
@httmock.urlmatch(path='/adresse/rest/voiecommune/COLUCHE')
def hebe_http_voie_commune(url, request, *args, **kwargs):
data = [
{
'attributes': {
'commune': 'BAILLARGUES',
'nom_voie': 'PL MICHEL COLUCCI DIT COLUCHE',
'code_insee': '34022',
}
},
{
'attributes': {
'commune': 'MONTPELLIER',
'nom_voie': 'R MICHEL COLUCCI DIT COLUCHE',
'code_insee': '34172',
}
},
]
return httmock.response(200, json.dumps(data))
def test_voie_commune_view(self):
app = Client()
with httmock.HTTMock(self.hebe_http_voie_commune):
resp = app.get(self.URL)
assert sorted(json.loads(resp.content)['data'], key=lambda x: x['insee']) == [
{
'id': 'PL MICHEL COLUCCI DIT COLUCHE',
'text': 'PL MICHEL COLUCCI DIT COLUCHE',
'commune': 'BAILLARGUES',
'insee': '34022',
},
{
'id': 'R MICHEL COLUCCI DIT COLUCHE',
'text': 'R MICHEL COLUCCI DIT COLUCHE',
'commune': 'MONTPELLIER',
'insee': '34172',
},
]
class VoiesCommuneViewTest(Mixin, TestCase):
URL = '/montpellier-sig/hebe/voies/34172/R MICHEL COLUCCI DIT COLUCHE/numero'
@httmock.urlmatch(path='/adresse/rest/commune/34172/voie/R%20MICHEL%20COLUCCI%20DIT%20COLUCHE/numero')
def hebe_http_voies_commune(url, request, *args, **kwargs):
data = [
{'attributes': {'nom_voie': 'R MICHEL COLUCCI DIT COLUCHE', 'numero': 150}},
{'attributes': {'nom_voie': 'R MICHEL COLUCCI DIT COLUCHE', 'numero': 261}},
{'attributes': {'nom_voie': 'R MICHEL COLUCCI DIT COLUCHE', 'numero': 289}},
]
return httmock.response(200, json.dumps(data))
@httmock.urlmatch(path='/adresse/rest/commune/34172/voie/R%20MICHEL%20COLUCCI%20DIT%20COLUCHE/numero/261')
def hebe_http_voies_commune_with_q(url, request, *args, **kwargs):
data = [
{
'attributes': {'nom_voie': 'R MICHEL COLUCCI DIT COLUCHE', 'numero': 261},
'geometry': {'y': 6276883.007800002, 'x': 770103.6123000011},
}
]
return httmock.response(200, json.dumps(data))
def test_voies_commune_view(self):
app = Client()
with httmock.HTTMock(self.hebe_http_voies_commune):
resp = app.get(self.URL)
assert json.loads(resp.content) == {
'data': [
{'id': '150 R MICHEL COLUCCI DIT COLUCHE', 'text': '150 R MICHEL COLUCCI DIT COLUCHE'},
{'id': '261 R MICHEL COLUCCI DIT COLUCHE', 'text': '261 R MICHEL COLUCCI DIT COLUCHE'},
{'id': '289 R MICHEL COLUCCI DIT COLUCHE', 'text': '289 R MICHEL COLUCCI DIT COLUCHE'},
]
}
def test_voies_commune_view_with_q(self):
app = Client()
with httmock.HTTMock(self.hebe_http_voies_commune_with_q):
resp = app.get(self.URL + '?q=261')
assert json.loads(resp.content) == {
'data': [
{
'id': '261 R MICHEL COLUCCI DIT COLUCHE',
'text': '261 R MICHEL COLUCCI DIT COLUCHE',
'x': 770103.6123000011,
'y': 6276883.007800002,
}
]
}
class DistrictViewTest(Mixin, TestCase):
URL = '/montpellier-sig/hebe/quartier/34172/R MICHEL COLUCCI DIT COLUCHE/261/'
@httmock.urlmatch(path='/adresse/rest/commune/34172/voie/R%20MICHEL%20COLUCCI%20DIT%20COLUCHE/numero/261')
def hebe_http_voies_commune(url, request, *args, **kwargs):
data = [
{
'attributes': {'nom_voie': 'R MICHEL COLUCCI DIT COLUCHE', 'numero': 261},
'geometry': {'y': 6276883.007800002, 'x': 770103.6123000011},
}
]
return httmock.response(200, json.dumps(data))
@httmock.urlmatch(path='/adresse/rest/adresse/770103.6123000011/6276883.007800002')
def hebe_http_district(url, request, *args, **kwargs):
data = {
'codepostal': '34070',
'commune': 'MONTPELLIER',
'voie': '261 R MICHEL COLUCCI DIT COLUCHE',
'iris': 'MARQUEROSE',
'quartier': "Croix d'Argent",
'sousquartier': "Croix d'Argent",
}
return httmock.response(200, json.dumps(data))
def test_district_view(self):
app = Client()
with httmock.HTTMock(self.hebe_http_voies_commune, self.hebe_http_district):
resp = app.get(self.URL)
assert json.loads(resp.content) == {
'data': {
'codepostal': '34070',
'commune': 'MONTPELLIER',
'voie': '261 R MICHEL COLUCCI DIT COLUCHE',
'iris': 'MARQUEROSE',
'quartier': "Croix d'Argent",
'sousquartier': "Croix d'Argent",
}
}
class AdresseViewTest(Mixin, TestCase):
URL = '/montpellier-sig/hebe/reverse?lat=43.5998&lon=3.8775'
@httmock.urlmatch(path='/adresse/rest/adresse/770873.6936367409/6278254.389145848')
def hebe_http_address(url, request, *args, **kwargs):
data = {
'codepostal': '34000',
'commune': 'MONTPELLIER',
'voie': '3 R LABBE',
'iris': 'MION',
'quartier': "Prés d'Arènes",
'sousquartier': 'Saint-Martin',
}
return httmock.response(200, json.dumps(data))
def test_adresse_view(self):
app = Client()
with httmock.HTTMock(self.hebe_http_address):
resp = app.get(self.URL)
assert json.loads(resp.content) == {
'address': {
'road': 'RUE LABBE',
'house_number': '3',
'city': 'MONTPELLIER',
'neighbourhood': 'Saint-Martin',
'postcode': '34000',
'suburb': "Pr\u00e9s d'Ar\u00e8nes",
'country': 'France',
}
}