Skip to content
This repository was archived by the owner on Oct 15, 2020. It is now read-only.

Commit 8170189

Browse files
flyusflyyohanboniface
authored andcommitted
144 allow to filter position collection by kind (#158)
* Fix #144 - Allow to filter Position collection by kind. * Fix #144 - Allow to filter Position collection by kind. * Fix #144 - Allowed_params * Fix #144 - Fixes: . Add Position.allowed_params. . Move get_where_clause() from PostCode to BaseCRUD. . Include get_where_clause() into BaseCRUD.on_get(). . Add 4 tests in Test_Position.py. * Fix #144 - Delete identifiers from ban/core/models.py * Fix #144 - Fix on Position & PostCode tests. . Check 'kind' value on Position tests. . Check 'code' value on PostCode tests.
1 parent 1af94d5 commit 8170189

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

ban/http/resources.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,19 @@ def get_collection(self, req, resp, **params):
8181
else [self.model.pk])
8282
return self.model.select().order_by(*order_by)
8383

84+
def get_where_clause(self, req, qs):
85+
for param in self.allowed_params:
86+
values = req.get_param_as_list(param)
87+
if values:
88+
qs = qs.where(getattr(self.model, param) << values)
89+
return qs
90+
8491
@auth.protect
8592
@app.endpoint()
8693
def on_get(self, req, resp, **params):
8794
"""Get {resource} collection."""
8895
qs = self.get_collection(req, resp, **params)
96+
qs = self.get_where_clause(req, qs)
8997
self.collection(req, resp, qs.as_resource())
9098

9199
@auth.protect
@@ -241,6 +249,7 @@ def get_bbox(self, req):
241249
class Position(VersionnedResource, BboxResource):
242250
"""Manipulate position resources."""
243251
model = models.Position
252+
allowed_params = ['kind']
244253

245254
def get_collection(self, req, resp, **kwargs):
246255
qs = super().get_collection(req, resp, **kwargs)
@@ -299,21 +308,6 @@ class Postcode(WithHousenumbers):
299308
order_by = [model.code, model.municipality]
300309
allowed_params = ['code']
301310

302-
def get_where_clause(self, req, qs):
303-
for param in self.allowed_params:
304-
values = req.get_param_as_list(param)
305-
if values:
306-
qs = qs.where(getattr(self.model, param) << values)
307-
return qs
308-
309-
@auth.protect
310-
@app.endpoint()
311-
def on_get(self, req, resp, **params):
312-
"""Get {resource} collection."""
313-
qs = self.get_collection(req, resp, **params)
314-
qs = self.get_where_clause(req, qs)
315-
self.collection(req, resp, qs.as_resource())
316-
317311

318312
class Municipality(VersionnedResource):
319313
model = models.Municipality

ban/tests/http/test_position.py

+47
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,50 @@ def test_get_position_collection_filtered_by_bbox_is_paginated(get, url):
442442
assert 'previous' in page2
443443
resp = get(page2['previous'])
444444
assert resp.json == page1
445+
446+
447+
@authorize
448+
def test_get_position_collection_filtered_by_1_kind_param(get, url):
449+
PositionFactory(kind='entrance')
450+
PositionFactory(kind='exit')
451+
resp = get(url('position', query_string={'kind': 'entrance'}))
452+
assert resp.status == falcon.HTTP_200
453+
assert resp.json['total'] == 1
454+
assert resp.json['collection'][0]['kind'] == 'entrance'
455+
456+
457+
@authorize
458+
def test_get_position_collection_filtered_by_2_equals_kind_params(get, url):
459+
PositionFactory(kind='entrance')
460+
PositionFactory(kind='exit')
461+
# 'kind' given by the user is used twice but with the same value.
462+
params = (('kind', 'entrance'), ('kind', 'entrance'))
463+
resp = get(url('position', query_string=params))
464+
assert resp.status == falcon.HTTP_200
465+
assert resp.json['total'] == 1
466+
assert resp.json['collection'][0]['kind'] == 'entrance'
467+
468+
469+
@authorize
470+
def test_get_position_collection_filtered_by_2_diff_kind_params(get, url):
471+
PositionFactory(kind='entrance')
472+
PositionFactory(kind='exit')
473+
# 'kind' given by the user is used with 2 differents values.
474+
params = (('kind', 'entrance'), ('kind', 'exit'))
475+
resp = get(url('position', query_string=params))
476+
assert resp.status == falcon.HTTP_200
477+
assert resp.json['total'] == 2
478+
assert resp.json['collection'][0]['kind'] == 'entrance'
479+
assert resp.json['collection'][1]['kind'] == 'exit'
480+
481+
482+
@authorize
483+
def test_get_position_collection_can_be_filtered_by_1_kind_and_1_pk(get, url):
484+
PositionFactory(kind='entrance')
485+
PositionFactory(kind='exit')
486+
# Only 'kind' param will be used to filter, not 'pk' one.
487+
params = (('kind', 'entrance'), ('pk', '405'))
488+
resp = get(url('position', query_string=params))
489+
assert resp.status == falcon.HTTP_200
490+
assert resp.json['total'] == 1
491+
assert resp.json['collection'][0]['kind'] == 'entrance'

ban/tests/http/test_postcode.py

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def test_get_postcode_collection_filtered_by_1_code_param(get, url):
5656
resp = get(url('postcode', query_string={'code': '90000'}))
5757
assert resp.status == falcon.HTTP_200
5858
assert resp.json['total'] == 1
59+
assert resp.json['collection'][0]['code'] == '90000'
5960

6061

6162
@authorize
@@ -67,6 +68,7 @@ def test_get_postcode_collection_filtered_by_2_equals_codes_param(get, url):
6768
resp = get(url('postcode', query_string=params))
6869
assert resp.status == falcon.HTTP_200
6970
assert resp.json['total'] == 1
71+
assert resp.json['collection'][0]['code'] == '90000'
7072

7173

7274
@authorize
@@ -78,6 +80,8 @@ def test_get_postcode_collection_filtered_by_2_diff_codes_param(get, url):
7880
resp = get(url('postcode', query_string=params))
7981
assert resp.status == falcon.HTTP_200
8082
assert resp.json['total'] == 2
83+
assert resp.json['collection'][0]['code'] == '90000'
84+
assert resp.json['collection'][1]['code'] == '91000'
8185

8286

8387
@authorize
@@ -89,3 +93,4 @@ def test_get_postcode_collection_can_be_filtered_by_1_code_and_1_pk(get, url):
8993
resp = get(url('postcode', query_string=params))
9094
assert resp.status == falcon.HTTP_200
9195
assert resp.json['total'] == 1
96+
assert resp.json['collection'][0]['code'] == '90000'

0 commit comments

Comments
 (0)