<s> from __future__ import absolute_import , unicode_literals <EOL> from django . test . client import RequestFactory <EOL> from drf_toolbox import serializers <EOL> from drf_toolbox . compat import django_pgfields_installed , models <EOL> from drf_toolbox . viewsets import ModelViewSet <EOL> from rest_framework . request import Request <EOL> from rest_framework . decorators import link <EOL> from tests import models as test_models <EOL> from tests . compat import mock <EOL> from tests . views import * <EOL> import unittest <EOL> import uuid <EOL> NO_DJANGOPG = '<STR_LIT>' <EOL> class ModelViewSetTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . request = Request ( RequestFactory ( ) . get ( '<STR_LIT>' ) ) <EOL> def test_get_queryset ( self ) : <EOL> """<STR_LIT>""" <EOL> mvs = ModelViewSet ( ) <EOL> with mock . patch . object ( ModelViewSet . mro ( ) [ <NUM_LIT:1> ] , '<STR_LIT>' ) as m : <EOL> qs = mvs . get_queryset ( ) <EOL> self . assertEqual ( qs , m . return_value ) <EOL> def test_get_queryset_with_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> mvs = ModelViewSet ( kwargs = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> with mock . patch . object ( ModelViewSet . mro ( ) [ <NUM_LIT:1> ] , '<STR_LIT>' ) as m : <EOL> m . return_value = mock . MagicMock ( ) <EOL> qs = mvs . get_queryset ( ) <EOL> self . assertEqual ( m . return_value . mock_calls , <EOL> [ mock . call . filter ( foo__pk = <NUM_LIT> ) ] ) <EOL> def test_get_serializer ( self ) : <EOL> """<STR_LIT>""" <EOL> class ViewSet ( ModelViewSet ) : <EOL> model = test_models . NormalModel <EOL> @ link ( ) <EOL> def foo ( self , request , pk ) : <EOL> return '<STR_LIT>' <EOL> vs = ViewSet ( request = self . request , kwargs = { } , format_kwarg = '<STR_LIT>' ) <EOL> serializer = vs . get_serializer ( ) <EOL> self . assertIsInstance ( serializer , serializers . ModelSerializer ) <EOL> self . assertEqual ( serializer . context [ '<STR_LIT>' ] , [ '<STR_LIT:foo>' ] ) <EOL> def test_parser_classes_standard ( self ) : <EOL> """<STR_LIT>""" <EOL> vs = NormalViewSet ( <EOL> request = RequestFactory ( ) . options ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) , <EOL> format_kwarg = '<STR_LIT>' , <EOL> ) <EOL> result = vs . options ( self . request ) <EOL> self . assertEqual ( result . data [ '<STR_LIT>' ] , [ <EOL> '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> @ unittest . skipUnless ( django_pgfields_installed , NO_DJANGOPG ) <EOL> def test_parser_classes_with_composite_field ( self ) : <EOL> """<STR_LIT>""" <EOL> class CompositeModel ( models . Model ) : <EOL> foo = test_models . CoordsField ( ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> class CompositeViewSet ( ModelViewSet ) : <EOL> model = CompositeModel <EOL> cvs = CompositeViewSet ( <EOL> request = RequestFactory ( ) . options ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) , <EOL> format_kwarg = '<STR_LIT>' , <EOL> ) <EOL> result = cvs . options ( self . request ) <EOL> self . assertEqual ( result . data [ '<STR_LIT>' ] , [ '<STR_LIT:application/json>' ] ) <EOL> @ unittest . skipUnless ( django_pgfields_installed , NO_DJANGOPG ) <EOL> def test_parser_classes_with_special_composite_field ( self ) : <EOL> """<STR_LIT>""" <EOL> class CompositeModel ( models . Model ) : <EOL> foo = test_models . SizeField ( ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> class CompositeViewSet ( ModelViewSet ) : <EOL> model = CompositeModel <EOL> cvs = CompositeViewSet ( <EOL> request = RequestFactory ( ) . options ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) , <EOL> format_kwarg = '<STR_LIT>' , <EOL> ) <EOL> result = cvs . options ( self . request ) <EOL> self . assertEqual ( result . data [ '<STR_LIT>' ] , [ '<STR_LIT:application/json>' ] ) <EOL> @ unittest . skipUnless ( django_pgfields_installed , NO_DJANGOPG ) <EOL> def test_parser_classes_with_special_composite_field_passthrough ( self ) : <EOL> """<STR_LIT>""" <EOL> class CompositeModel ( models . Model ) : <EOL> foo = test_models . SizeField ( ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> class CompositeViewSet ( ModelViewSet ) : <EOL> model = CompositeModel <EOL> try : <EOL> test_models . SizeSerializerField . suppress_form_parsing = False <EOL> cvs = CompositeViewSet ( <EOL> request = RequestFactory ( ) . options ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) , <EOL> format_kwarg = '<STR_LIT>' , <EOL> ) <EOL> result = cvs . options ( self . request ) <EOL> self . assertEqual ( result . data [ '<STR_LIT>' ] , [ <EOL> '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> finally : <EOL> test_models . SizeSerializerField . suppress_form_parsing = True <EOL> def test_get_serializer_nested ( self ) : <EOL> """<STR_LIT>""" <EOL> fmjvs = ChildViewSet ( request = self . request , <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> format_kwarg = '<STR_LIT>' ) <EOL> serializer = fmjvs . get_serializer ( ) <EOL> self . assertIsInstance ( serializer , serializers . ModelSerializer ) <EOL> self . assertTrue ( serializer . fields [ '<STR_LIT>' ] . read_only ) <EOL> self . assertFalse ( serializer . fields [ '<STR_LIT>' ] . required ) <EOL> self . assertEqual ( serializer . _initial , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_get_serializer_random_kwarg ( self ) : <EOL> """<STR_LIT>""" <EOL> class ViewSet ( ModelViewSet ) : <EOL> model = test_models . NormalModel <EOL> vs = ViewSet ( request = self . request , kwargs = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> format_kwarg = '<STR_LIT>' ) <EOL> serializer = vs . get_serializer ( ) <EOL> self . assertIsInstance ( serializer , serializers . ModelSerializer ) <EOL> self . assertEqual ( serializer . _initial , { } ) <EOL> def test_get_serializer_double_nested_kwarg ( self ) : <EOL> """<STR_LIT>""" <EOL> class ViewSet ( ModelViewSet ) : <EOL> model = test_models . NormalModel <EOL> vs = ViewSet ( request = self . request , kwargs = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> format_kwarg = '<STR_LIT>' ) <EOL> serializer = vs . get_serializer ( ) <EOL> self . assertIsInstance ( serializer , serializers . ModelSerializer ) <EOL> self . assertEqual ( serializer . _initial , { } ) <EOL> def test_get_serializer_excluded_nested_field ( self ) : <EOL> """<STR_LIT>""" <EOL> cvs3 = ChildViewSetIII ( request = self . request , <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> format_kwarg = '<STR_LIT>' ) <EOL> serializer = cvs3 . get_serializer ( ) <EOL> self . assertIsInstance ( serializer , serializers . ModelSerializer ) <EOL> self . assertEqual ( serializer . _initial , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_get_serializer_with_problematic_model_serializer ( self ) : <EOL> """<STR_LIT>""" <EOL> class BadFakeSerializer ( object ) : <EOL> pass <EOL> class ViewSet ( ModelViewSet ) : <EOL> model = test_models . NormalModel <EOL> serializer_class = BadFakeSerializer <EOL> vs = ViewSet ( request = self . request , <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT:bar>' } , <EOL> format_kwarg = '<STR_LIT>' ) <EOL> with self . assertRaises ( TypeError ) : <EOL> serializer = vs . get_serializer ( ) </s>
