nancy poll: use APIError instead of custom Exceptions (#13726)

This commit is contained in:
Josue Kouka 2016-10-25 04:00:15 +02:00
parent 282bd2cb6e
commit 38424fcead
2 changed files with 10 additions and 15 deletions

View File

@ -6,20 +6,12 @@ from django.utils.translation import ugettext_lazy as _
from passerelle.base.models import BaseResource
from passerelle.utils.api import endpoint
from passerelle.utils.jsonresponse import APIError
COLUMN_NAMES = 'street_start_number, street_end_number,,,street_side,,,,code,id,text,address,,,street_name,,canton,,,'
class ParameterError(Exception):
http_status = 400
log_error = False
class FileContentError(Exception):
http_status = 404
def to_unicode(value):
return unicode(value, 'utf-8')
@ -49,18 +41,18 @@ class NancyPoll(BaseResource):
street_name = request.GET.get('street_name')
if not street_no or not street_name:
raise ParameterError('All parameters are required')
raise APIError('All parameters are required')
try:
street_no = int(street_no)
except (ValueError):
raise ParameterError('Invalid street no value')
raise APIError('Invalid street no value')
titles = [t.strip() for t in COLUMN_NAMES.split(',')]
content = self.csv_file.read()
if not content:
raise FileContentError('No content found')
raise APIError('No content found')
reader = csv.reader(content.splitlines())
reader.next()
@ -91,7 +83,7 @@ class NancyPoll(BaseResource):
'canton': row[titles.index('canton')],
}
raise ObjectDoesNotExist('Polling Station Not Found')
raise APIError('Polling Station Not Found')
def street_name_like(self, street_name, elt):
return street_name.lower() in to_unicode(elt).lower()

View File

@ -55,22 +55,25 @@ def test_failure(setup):
qs = {'street_no': '37000', 'street_name': 'Rue du Marechal Juin'}
resp = json.loads(client.get(url, qs).content)
assert resp['err_desc'] == 'Polling Station Not Found'
assert int(resp['err']) != 0
def test_no_params(setup):
url, client = setup
qs = {}
resp = client.get(url, qs)
assert resp.status_code == 400
assert resp.status_code == 200
assert json.loads(resp.content)['err_desc'] == 'All parameters are required'
assert int(json.loads(resp.content)['err']) != 0
def test_invalid_street_no(setup):
url, client = setup
qs = {'street_no': 'lol', 'street_name': 'whatever'}
resp = client.get(url, qs)
assert resp.status_code == 400
assert resp.status_code == 200
assert json.loads(resp.content)['err_desc'] == 'Invalid street no value'
assert int(json.loads(resp.content)['err']) != 0
def test_success_i_side(setup):