Ticket #15819: non-unique-related-fields-distinct2.diff
File non-unique-related-fields-distinct2.diff, 6.0 KB (added by , 14 years ago) |
---|
-
regressiontests/admin_changelist/tests.py
1 1 from django.contrib import admin 2 2 from django.contrib.admin.options import IncorrectLookupParameters 3 from django.contrib.admin.views.main import ChangeList 3 from django.contrib.admin.views.main import ChangeList, SEARCH_VAR 4 4 from django.core.paginator import Paginator 5 5 from django.template import Context, Template 6 6 from django.test import TransactionTestCase … … 148 148 band.genres.add(blues) 149 149 150 150 m = BandAdmin(Band, admin.site) 151 cl = ChangeList(MockFilteredRequestA(blues.pk), Band, m.list_display, 151 request = MockFilterRequest('genres', blues.pk) 152 153 cl = ChangeList(request, Band, m.list_display, 152 154 m.list_display_links, m.list_filter, m.date_hierarchy, 153 155 m.search_fields, m.list_select_related, m.list_per_page, 154 156 m.list_editable, m) 155 157 156 cl.get_results( MockFilteredRequestA(blues.pk))158 cl.get_results(request) 157 159 158 160 # There's only one Group instance 159 161 self.assertEqual(cl.result_count, 1) … … 169 171 Membership.objects.create(group=band, music=lead, role='bass player') 170 172 171 173 m = GroupAdmin(Group, admin.site) 172 cl = ChangeList(MockFilteredRequestB(lead.pk), Group, m.list_display, 174 request = MockFilterRequest('members', lead.pk) 175 176 cl = ChangeList(request, Group, m.list_display, 173 177 m.list_display_links, m.list_filter, m.date_hierarchy, 174 178 m.search_fields, m.list_select_related, m.list_per_page, 175 179 m.list_editable, m) 176 180 177 cl.get_results( MockFilteredRequestB(lead.pk))181 cl.get_results(request) 178 182 179 183 # There's only one Group instance 180 184 self.assertEqual(cl.result_count, 1) … … 191 195 Membership.objects.create(group=four, music=lead, role='guitar player') 192 196 193 197 m = QuartetAdmin(Quartet, admin.site) 194 cl = ChangeList(MockFilteredRequestB(lead.pk), Quartet, m.list_display, 198 request = MockFilterRequest('members', lead.pk) 199 200 cl = ChangeList(request, Quartet, m.list_display, 195 201 m.list_display_links, m.list_filter, m.date_hierarchy, 196 202 m.search_fields, m.list_select_related, m.list_per_page, 197 203 m.list_editable, m) 198 204 199 cl.get_results( MockFilteredRequestB(lead.pk))205 cl.get_results(request) 200 206 201 207 # There's only one Quartet instance 202 208 self.assertEqual(cl.result_count, 1) … … 213 219 Invitation.objects.create(band=three, player=lead, instrument='bass') 214 220 215 221 m = ChordsBandAdmin(ChordsBand, admin.site) 216 cl = ChangeList(MockFilteredRequestB(lead.pk), ChordsBand, m.list_display, 222 request = MockFilterRequest('members', lead.pk) 223 224 cl = ChangeList(request, ChordsBand, m.list_display, 217 225 m.list_display_links, m.list_filter, m.date_hierarchy, 218 226 m.search_fields, m.list_select_related, m.list_per_page, 219 227 m.list_editable, m) 220 228 221 cl.get_results( MockFilteredRequestB(lead.pk))229 cl.get_results(request) 222 230 223 231 # There's only one ChordsBand instance 224 232 self.assertEqual(cl.result_count, 1) 225 233 234 def test_distinct_for_non_unique_related_object_in_list_filter(self): 235 """ 236 Regressions tests for #15819: Results shouldn't appear more than 237 once for relationships like a non-unique foreign key. 238 """ 239 parent = Parent.objects.create(name='Mary') 240 # Two children with the same name 241 Child.objects.create(parent=parent, name='Daniel') 242 Child.objects.create(parent=parent, name='Daniel') 243 244 m = ParentAdmin(Parent, admin.site) 245 246 request = MockFilterRequest('child__name', 'Daniel') 247 cl = ChangeList(request, Parent, m.list_display, m.list_display_links, 248 m.list_filter, m.date_hierarchy, m.search_fields, 249 m.list_select_related, m.list_per_page, 250 m.list_editable, m) 251 252 # Make sure distinct() was called 253 self.assertEqual(cl.query_set.count(), 1) 254 255 def test_distinct_for_non_unique_related_object_in_search_fields(self): 256 """ 257 Regressions tests for #15819: Results shouldn't appear more than 258 once for relationships like a non-unique foreign key. 259 """ 260 parent = Parent.objects.create(name='Mary') 261 Child.objects.create(parent=parent, name='Danielle') 262 Child.objects.create(parent=parent, name='Daniel') 263 264 m = ParentAdmin(Parent, admin.site) 265 266 request = MockSearchRequest('daniel') 267 cl = ChangeList(request, Parent, m.list_display, m.list_display_links, 268 m.list_filter, m.date_hierarchy, m.search_fields, 269 m.list_select_related, m.list_per_page, 270 m.list_editable, m) 271 272 # Make sure distinct() was called 273 self.assertEqual(cl.query_set.count(), 1) 274 226 275 def test_pagination(self): 227 276 """ 228 277 Regression tests for #12893: Pagination in admins changelist doesn't … … 254 303 self.assertEqual(cl.paginator.page_range, [1, 2, 3]) 255 304 256 305 306 class ParentAdmin(admin.ModelAdmin): 307 list_filter = ['child__name'] 308 search_fields = ['child__name'] 309 310 257 311 class ChildAdmin(admin.ModelAdmin): 258 312 list_display = ['name', 'parent'] 259 313 list_per_page = 10 … … 288 342 class ChordsBandAdmin(admin.ModelAdmin): 289 343 list_filter = ['members'] 290 344 291 class MockFilter edRequestA(object):292 def __init__(self, pk):293 self.GET = { 'genres' : pk}345 class MockFilterRequest(object): 346 def __init__(self, filter, q): 347 self.GET = {filter: q} 294 348 295 class Mock FilteredRequestB(object):296 def __init__(self, pk):297 self.GET = { 'members': pk}349 class MockSearchRequest(object): 350 def __init__(self, q): 351 self.GET = {SEARCH_VAR: q}