|
5 | 5 |
|
6 | 6 | from rest_framework import status |
7 | 7 |
|
8 | | -from nc.models import RACE_CHOICES |
| 8 | +from nc.models import RACE_CHOICES, Person |
9 | 9 | from nc.tests import factories |
10 | 10 |
|
11 | 11 | pytestmark = pytest.mark.django_db |
@@ -48,12 +48,6 @@ def test_response_person_fields(client, search_url, durham): |
48 | 48 | assert result == expected |
49 | 49 | # 'last_reported_stop' should only be included if no matching stops were found |
50 | 50 | 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 |
57 | 51 |
|
58 | 52 |
|
59 | 53 | @pytest.mark.django_db(databases=["traffic_stops_nc"]) |
@@ -81,61 +75,69 @@ def test_no_stops_found(client, search_url): |
81 | 75 | assert response.data.get("last_reported_stop") == agency.last_reported_stop |
82 | 76 |
|
83 | 77 |
|
| 78 | +@pytest.mark.parametrize("search_age", [True, False]) |
| 79 | +@pytest.mark.parametrize("search_start_date", [True, False]) |
| 80 | +@pytest.mark.parametrize("search_end_date", [True, False]) |
84 | 81 | @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. |
| 82 | +def test_stop_date_range_and_age_adjusted( |
| 83 | + client, search_url, durham, search_age, search_start_date, search_end_date |
| 84 | +): |
| 85 | + """Ensure the date range and age entered by the user is adjusted such that: |
| 86 | + - age is up to 2 years younger or older |
| 87 | + - start date is 2 days earlier |
| 88 | + - end date is 2 days later |
88 | 89 | """ |
89 | 90 | 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 | 91 | start_date = fake.past_date() |
113 | 92 | end_date = fake.past_date(start_date=start_date) |
114 | | - # Create some stops within the expected date range |
| 93 | + # Create some stops within the expected date range and age range |
115 | 94 | dates = ( |
116 | 95 | [start_date, end_date] |
117 | 96 | + [start_date - timedelta(d) for d in [1, 2]] |
118 | 97 | + [end_date + timedelta(d) for d in [1, 2]] |
119 | 98 | ) |
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) |
| 99 | + people = [ |
| 100 | + factories.PersonFactory( |
| 101 | + stop__agency=durham, stop__date=d, age=fake.random_int(age - 2, age + 2) |
| 102 | + ) |
| 103 | + for d in dates |
| 104 | + ] |
| 105 | + # Create 2 stops outside the expected date range and age range |
| 106 | + factories.PersonFactory(stop__agency=durham, stop__date=start_date - timedelta(3), age=age - 3) |
| 107 | + factories.PersonFactory(stop__agency=durham, stop__date=end_date + timedelta(3), age=age + 3) |
| 108 | + |
| 109 | + expected_results = Person.objects.filter(stop__agency=durham) |
| 110 | + expected_results_message = "" |
| 111 | + search_params = {"agency": durham.pk} |
| 112 | + if search_age: |
| 113 | + search_params["age"] = age |
| 114 | + expected_results = expected_results.filter(age__gte=age - 2, age__lte=age + 2) |
| 115 | + expected_results_message = f"with drivers aged {age - 2}-{age + 2}" |
| 116 | + if search_start_date: |
| 117 | + search_params["stop_date_after"] = start_date |
| 118 | + used_start_date = start_date - timedelta(2) |
| 119 | + expected_results = expected_results.filter(stop__date__gte=used_start_date) |
| 120 | + if search_end_date: |
| 121 | + expected_results_message += f" between {used_start_date:%B %d, %Y}" |
| 122 | + else: |
| 123 | + expected_results_message += f" after {used_start_date:%B %d, %Y}" |
| 124 | + if search_end_date: |
| 125 | + search_params["stop_date_before"] = end_date |
| 126 | + used_end_date = end_date + timedelta(2) |
| 127 | + expected_results = expected_results.filter(stop__date__lte=used_end_date) |
| 128 | + if search_start_date: |
| 129 | + expected_results_message += f" and {used_end_date:%B %d, %Y}" |
| 130 | + else: |
| 131 | + expected_results_message += f" before {used_end_date:%B %d, %Y}" |
| 132 | + |
| 133 | + response = client.get(search_url, data=search_params, format="json") |
| 134 | + |
| 135 | + assert len(response.data["results"]) == len(expected_results) |
133 | 136 | 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)} |
| 137 | + assert {p.stop.stop_id for p in expected_results} == stop_ids |
| 138 | + # If the user entered an age and/or a date range, the 'extra_results_message' |
| 139 | + # should be included in the response data |
| 140 | + if expected_results_message: |
| 141 | + assert response.data["extra_results_message"] == expected_results_message.strip() |
| 142 | + else: |
| 143 | + assert "extra_results_message" not in response.data |
0 commit comments