|
| 1 | +import re |
| 2 | + |
1 | 3 | from django.test import override_settings, tag |
2 | 4 | from django.urls import reverse |
3 | 5 |
|
@@ -72,7 +74,9 @@ def get_num_results(response) -> int: |
72 | 74 |
|
73 | 75 | self.assertIsNotNone(response.html.find("input", {"id": "searchbar"})) |
74 | 76 |
|
75 | | - response = self.app.get(list_url, params={"q": "bar"}, user=self.user) |
| 77 | + response = self.app.get( |
| 78 | + list_url, params={"q": "foo__icontains__bar"}, user=self.user |
| 79 | + ) |
76 | 80 |
|
77 | 81 | self.assertEqual(get_num_results(response), 1) |
78 | 82 |
|
@@ -126,3 +130,119 @@ def test_add_new_objectrecord(self): |
126 | 130 | response = form.submit() |
127 | 131 |
|
128 | 132 | self.assertEqual(object.records.count(), 1) |
| 133 | + |
| 134 | + @tag("gh-621") |
| 135 | + def test_object_admin_search_json_key_operator_value(self): |
| 136 | + object1 = ObjectFactory() |
| 137 | + ObjectRecordFactory( |
| 138 | + object=object1, |
| 139 | + data={"id_nummer": 1, "naam": "Boomgaard", "plantDate": "2025-01-01"}, |
| 140 | + ) |
| 141 | + object2 = ObjectFactory() |
| 142 | + ObjectRecordFactory( |
| 143 | + object=object2, |
| 144 | + data={"id_nummer": 2, "naam": "Appelboom", "plantDate": "2025-06-15"}, |
| 145 | + ) |
| 146 | + object3 = ObjectFactory() |
| 147 | + ObjectRecordFactory( |
| 148 | + object=object3, |
| 149 | + data={"id_nummer": 3, "naam": "Peren", "plantDate": "2025-12-31"}, |
| 150 | + ) |
| 151 | + object4 = ObjectFactory() |
| 152 | + ObjectRecordFactory( |
| 153 | + object=object4, |
| 154 | + data={ |
| 155 | + "id_nummer": 4, |
| 156 | + "naam": "Kersen", |
| 157 | + "plantDate": "2025-07-20", |
| 158 | + "location": {"city": "Amsterdam", "region": "Noord-Holland"}, |
| 159 | + }, |
| 160 | + ) |
| 161 | + |
| 162 | + list_url = reverse("admin:core_object_changelist") |
| 163 | + |
| 164 | + def get_row_pks(response): |
| 165 | + rows = response.html.select("#result_list tbody tr") |
| 166 | + pks = [] |
| 167 | + for row in rows: |
| 168 | + href = row.select_one("th a")["href"] |
| 169 | + pks.append(int(re.search(r"\d+", href).group())) |
| 170 | + return pks |
| 171 | + |
| 172 | + with self.subTest("Exact match"): |
| 173 | + response = self.app.get( |
| 174 | + list_url, params={"q": "id_nummer__exact__1"}, user=self.user |
| 175 | + ) |
| 176 | + self.assertEqual(get_row_pks(response), [object1.pk]) |
| 177 | + |
| 178 | + with self.subTest("Nested JSON value match"): |
| 179 | + response = self.app.get( |
| 180 | + list_url, |
| 181 | + params={"q": "location__city__exact__Amsterdam"}, |
| 182 | + user=self.user, |
| 183 | + ) |
| 184 | + self.assertEqual(get_row_pks(response), [object4.pk]) |
| 185 | + |
| 186 | + with self.subTest("Nested"): |
| 187 | + response = self.app.get( |
| 188 | + list_url, |
| 189 | + params={"q": "location__city__Amsterdam"}, |
| 190 | + user=self.user, |
| 191 | + ) |
| 192 | + self.assertEqual(get_row_pks(response), [object4.pk]) |
| 193 | + |
| 194 | + with self.subTest("icontains"): |
| 195 | + response = self.app.get( |
| 196 | + list_url, params={"q": "naam__icontains__boom"}, user=self.user |
| 197 | + ) |
| 198 | + self.assertCountEqual(get_row_pks(response), [object1.pk, object2.pk]) |
| 199 | + |
| 200 | + with self.subTest("Default operator"): |
| 201 | + response = self.app.get( |
| 202 | + list_url, params={"q": "naam__Boomgaard"}, user=self.user |
| 203 | + ) |
| 204 | + self.assertEqual(get_row_pks(response), [object1.pk]) |
| 205 | + |
| 206 | + with self.subTest("Numeric comparison gt"): |
| 207 | + response = self.app.get( |
| 208 | + list_url, params={"q": "id_nummer__gt__1"}, user=self.user |
| 209 | + ) |
| 210 | + self.assertCountEqual( |
| 211 | + get_row_pks(response), [object2.pk, object3.pk, object4.pk] |
| 212 | + ) |
| 213 | + |
| 214 | + with self.subTest("Date exact"): |
| 215 | + response = self.app.get( |
| 216 | + list_url, params={"q": "plantDate__exact__2025-06-15"}, user=self.user |
| 217 | + ) |
| 218 | + self.assertEqual(get_row_pks(response), [object2.pk]) |
| 219 | + |
| 220 | + with self.subTest("Date gt"): |
| 221 | + response = self.app.get( |
| 222 | + list_url, params={"q": "plantDate__gt__2025-01-01"}, user=self.user |
| 223 | + ) |
| 224 | + self.assertCountEqual( |
| 225 | + get_row_pks(response), [object2.pk, object3.pk, object4.pk] |
| 226 | + ) |
| 227 | + |
| 228 | + with self.subTest("Date lt"): |
| 229 | + response = self.app.get( |
| 230 | + list_url, params={"q": "plantDate__lt__2025-12-01"}, user=self.user |
| 231 | + ) |
| 232 | + self.assertCountEqual( |
| 233 | + get_row_pks(response), [object1.pk, object2.pk, object4.pk] |
| 234 | + ) |
| 235 | + |
| 236 | + with self.subTest("Date comparison gte"): |
| 237 | + response = self.app.get( |
| 238 | + list_url, params={"q": "plantDate__gte__2025-06-15"}, user=self.user |
| 239 | + ) |
| 240 | + self.assertCountEqual( |
| 241 | + get_row_pks(response), [object2.pk, object3.pk, object4.pk] |
| 242 | + ) |
| 243 | + |
| 244 | + with self.subTest("Date comparison lte"): |
| 245 | + response = self.app.get( |
| 246 | + list_url, params={"q": "plantDate__lte__2025-06-15"}, user=self.user |
| 247 | + ) |
| 248 | + self.assertCountEqual(get_row_pks(response), [object1.pk, object2.pk]) |
0 commit comments