33
44from __future__ import unicode_literals
55
6+ from django .db .models import Prefetch
67from rest_framework .permissions import BasePermission
78from rest_framework .renderers import BrowsableAPIRenderer , JSONRenderer
89from rest_framework .settings import api_settings
@@ -388,19 +389,14 @@ class Meta:
388389 ProductViewSet .sideloading_serializer_class = ProductSideloadableSerializer
389390
390391 def test_correct_exception_raised (self ):
391- with self .assertRaises (RuntimeError ) as cm :
392+ expected_error_message = "Sideloadable prefetch values must be a list of strings or Prefetch objects"
393+ with self .assertRaisesMessage (RuntimeError , expected_error_message ):
392394 self .client .get (
393395 reverse ("product-list" ),
394396 data = {"sideload" : "categories,suppliers,partners" },
395397 format = "json" ,
396398 )
397399
398- expected_error_message = "Sideloadable prefetch values must be presented either as a list or a string"
399-
400- raised_exception = cm .exception
401- self .assertEqual (str (raised_exception ), expected_error_message )
402-
403-
404400class TestDrfSideloadingValidPrefetches (TestCase ):
405401 """Run tests while including mixin but not defining sideloading"""
406402
@@ -412,13 +408,15 @@ class ProductSideloadableSerializer(SideLoadableSerializer):
412408 products = ProductSerializer (many = True )
413409 categories = CategorySerializer (source = "category" , many = True )
414410 suppliers = SupplierSerializer (source = "supplier" , many = True )
411+ suppliers_ordered_by_name = SupplierSerializer (source = "supplier" , many = True )
415412 partners = PartnerSerializer (many = True )
416413
417414 class Meta :
418415 primary = "products"
419416 prefetches = {
420417 "categories" : "category" ,
421418 "suppliers" : ["supplier" ],
419+ "suppliers_ordered_by_name" : Prefetch ("supplier" , queryset = Supplier .objects .order_by ("name" )),
422420 "partners" : None ,
423421 }
424422
@@ -427,7 +425,7 @@ class Meta:
427425 def test_sideloading_with_prefetches (self ):
428426 self .client .get (
429427 reverse ("product-list" ),
430- data = {"sideload" : "categories,suppliers,partners" },
428+ data = {"sideload" : "categories,suppliers,suppliers_ordered_by_name, partners" },
431429 format = "json" ,
432430 )
433431
0 commit comments