Skip to content

Commit 562b12b

Browse files
authored
Merge pull request #22 from codeforberlin/allow-searching-by-name
Allow searching by name
2 parents 866285e + 1f50803 commit 562b12b

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

app/filters.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ def apply(self, query):
3737
column_to_filter = getattr(models.School, self.key)
3838
return query.filter(column_to_filter.in_(self.values))
3939

40+
class TextMatchFilter(Filter):
41+
supported_keys = ['name']
42+
43+
def apply(self, query):
44+
column_to_filter = getattr(models.School, self.key)
45+
return query.filter(column_to_filter.icontains(self.values))
46+
4047
class UpdateTimestampFilter(Filter):
4148
supported_keys = ['update_timestamp']
4249

@@ -67,7 +74,13 @@ def apply(self, query):
6774

6875

6976
class SchoolFilter:
70-
filter_classes = [StateFilter, BasicFilter, LatLonSorter, BoundingBoxFilter, UpdateTimestampFilter]
77+
filter_classes = [StateFilter,
78+
BasicFilter,
79+
LatLonSorter,
80+
BoundingBoxFilter,
81+
UpdateTimestampFilter,
82+
TextMatchFilter
83+
]
7184

7285
def __init__(self, params):
7386
self.used_filters = []

app/main.py

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def read_schools(skip: int = 0,
3636
state: Optional[List[State]] = Query(None),
3737
school_type: Optional[List[str]] = Query(None),
3838
legal_status: Optional[List[str]] = Query(None),
39+
name: Optional[str] = Query(None,
40+
description="Allows searching for names of schools."
41+
"Searches for case-insensitive substrings."),
3942
by_lat: Optional[float] = Query(None,
4043
description="Allows ordering result by distance from a geographical point."
4144
"Must be used in combination with `by_lon`"
@@ -66,6 +69,7 @@ def read_schools(skip: int = 0,
6669
"school_type": school_type,
6770
"legal_status": legal_status,
6871
"update_timestamp": update_timestamp,
72+
"name": name,
6973
}
7074
if by_lat or by_lon:
7175
if not (by_lon and by_lat):

test/test_api.py

+23
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,29 @@ def test_schools_by_update_date(self, client, db):
347347
assert response.status_code == 200
348348
assert len(response.json()) == 1
349349

350+
def test_schools_by_name(self, client, db):
351+
# Arrange
352+
for school in [
353+
SchoolFactory.create(name='Schule am kleinen Deich'),
354+
SchoolFactory.create(name='Schule an der Dorfstraßse'),
355+
]:
356+
db.add(school)
357+
db.commit()
358+
359+
# Act
360+
unfiltered_response = client.get("/schools")
361+
deich_response = client.get("/schools?name=deich")
362+
no_match_response = client.get("/schools?name=nicht%20da")
363+
364+
# Assert
365+
assert unfiltered_response.status_code == 200
366+
assert deich_response.status_code == 200
367+
assert no_match_response.status_code == 200
368+
369+
assert len(unfiltered_response.json()) == 2
370+
assert len(deich_response.json()) == 1
371+
assert len(no_match_response.json()) == 0
372+
350373
def test_get_single_no_result(self, client, db):
351374
# Arrange
352375
self.__setup_schools(db)

0 commit comments

Comments
 (0)