1+ from datetime import timedelta
2+
13import faker
24import pytest
35
@@ -44,7 +46,14 @@ def test_response_person_fields(client, search_url, durham):
4446 "stop_action" : person .stop .get_action_display (),
4547 }
4648 assert result == expected
49+ # 'last_reported_stop' should only be included if no matching stops were found
4750 assert "last_reported_stop" not in response .data
51+ # 'age' should only be included if the user entered an age
52+ assert "age" not in response .data
53+ # 'start_date' should only be included if the user entered a start date
54+ assert "start_date" not in response .data
55+ # 'end_date' should only be included if the user entered an end date
56+ assert "end_date" not in response .data
4857
4958
5059@pytest .mark .django_db (databases = ["traffic_stops_nc" ])
@@ -70,3 +79,63 @@ def test_no_stops_found(client, search_url):
7079 assert response .status_code == status .HTTP_200_OK
7180 assert response .data .get ("results" ) == []
7281 assert response .data .get ("last_reported_stop" ) == agency .last_reported_stop
82+
83+
84+ @pytest .mark .django_db (databases = ["traffic_stops_nc" ])
85+ def test_age_adjusted (client , search_url , durham ):
86+ """Ensure people aged + or - 2 years of the age the user entered are included
87+ in search results.
88+ """
89+ age = 18
90+ # Create 5 stops with people within the expected age range
91+ people = [factories .PersonFactory (stop__agency = durham , age = i ) for i in range (age - 2 , age + 3 )]
92+ # Create 2 stops with people outside the expected age range. These should not
93+ # be included in the search results
94+ factories .PersonFactory (stop__agency = durham , age = age - 3 )
95+ factories .PersonFactory (stop__agency = durham , age = age + 3 )
96+
97+ response = client .get (search_url , data = {"agency" : durham .pk , "age" : age }, format = "json" )
98+
99+ assert len (response .data ["results" ]) == len (people )
100+ stop_ids = {stop ["stop_id" ] for stop in response .data ["results" ]}
101+ assert {p .stop .stop_id for p in people } == stop_ids
102+ # 'age' should be included in the response data, with the entered age and
103+ # the adjusted age range
104+ assert response .data ["age" ] == {"entered" : age , "adjusted" : (age - 2 , age + 2 )}
105+
106+
107+ @pytest .mark .django_db (databases = ["traffic_stops_nc" ])
108+ def test_stop_date_range_adjusted (client , search_url , durham ):
109+ """Ensure the date range entered by the user is adjusted such that the start_date
110+ is 2 days earlier and end_date is 2 days later.
111+ """
112+ start_date = fake .past_date ()
113+ end_date = fake .past_date (start_date = start_date )
114+ # Create some stops within the expected date range
115+ dates = (
116+ [start_date , end_date ]
117+ + [start_date - timedelta (d ) for d in [1 , 2 ]]
118+ + [end_date + timedelta (d ) for d in [1 , 2 ]]
119+ )
120+ people = [factories .PersonFactory (stop__agency = durham , stop__date = d ) for d in dates ]
121+ # Create 2 stops outside the expected date range. These should not be included
122+ # in the search results
123+ factories .PersonFactory (stop__agency = durham , stop__date = start_date - timedelta (3 ))
124+ factories .PersonFactory (stop__agency = durham , stop__date = end_date + timedelta (3 ))
125+
126+ response = client .get (
127+ search_url ,
128+ data = {"agency" : durham .pk , "stop_date_after" : start_date , "stop_date_before" : end_date },
129+ format = "json" ,
130+ )
131+
132+ assert len (response .data ["results" ]) == len (people )
133+ stop_ids = {stop ["stop_id" ] for stop in response .data ["results" ]}
134+ assert {p .stop .stop_id for p in people } == stop_ids
135+ # 'start_date' and 'end_date' should be included in the response data, with
136+ # the entered and adjusted date for each
137+ assert response .data ["start_date" ] == {
138+ "entered" : start_date ,
139+ "adjusted" : start_date - timedelta (2 ),
140+ }
141+ assert response .data ["end_date" ] == {"entered" : end_date , "adjusted" : end_date + timedelta (2 )}
0 commit comments