<s> from __future__ import absolute_import , unicode_literals <EOL> from django . test . client import RequestFactory <EOL> from drf_toolbox . serializers . fields . api import * <EOL> from rest_framework . request import Request <EOL> from tests . compat import mock <EOL> from tests . models import * <EOL> import unittest <EOL> import uuid <EOL> class APIEndpointTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . aef = APIEndpointsField ( ) <EOL> self . aef . context = { '<STR_LIT>' : Request ( RequestFactory ( ) . get ( '<STR_LIT>' ) ) } <EOL> def test_no_url_in_field_to_native_singular ( self ) : <EOL> """<STR_LIT>""" <EOL> aef_singular = APIEndpointField ( ) <EOL> with mock . patch . object ( aef_singular , '<STR_LIT>' ) as gbu : <EOL> gbu . return_value = None <EOL> self . assertEqual ( aef_singular . field_to_native ( None , '<STR_LIT>' ) , <EOL> None ) <EOL> def test_no_get_absolute_url ( self ) : <EOL> """<STR_LIT>""" <EOL> fm = ExplicitAPIEndpointsModel ( ) <EOL> self . assertEqual ( self . aef . field_to_native ( fm , '<STR_LIT>' ) , { } ) <EOL> def test_no_host ( self ) : <EOL> """<STR_LIT>""" <EOL> m = NormalModel ( id = <NUM_LIT> ) <EOL> with mock . patch . object ( self . aef . context [ '<STR_LIT>' ] , '<STR_LIT>' ) as gh : <EOL> gh . return_value = None <EOL> endpoints = self . aef . field_to_native ( m , '<STR_LIT>' ) <EOL> self . assertEqual ( endpoints , { '<STR_LIT>' : '<STR_LIT>' % m . id } ) <EOL> def test_child_endpoints ( self ) : <EOL> """<STR_LIT>""" <EOL> m = NormalModel ( id = <NUM_LIT> ) <EOL> with mock . patch . dict ( self . aef . context , child_endpoints = { '<STR_LIT:foo>' , '<STR_LIT:bar>' } ) : <EOL> endpoints = self . aef . field_to_native ( m , '<STR_LIT>' ) <EOL> self . assertEqual ( endpoints , { <EOL> '<STR_LIT>' : '<STR_LIT>' % m . id , <EOL> '<STR_LIT:foo>' : '<STR_LIT>' % m . id , <EOL> '<STR_LIT:bar>' : '<STR_LIT>' % m . id , <EOL> } ) <EOL> def test_unhonored_child_endpoints ( self ) : <EOL> """<STR_LIT>""" <EOL> aef = APIEndpointsField ( ) <EOL> aef . context = { } <EOL> with mock . patch . object ( APIEndpointField , '<STR_LIT>' ) as init : <EOL> aef . initialize ( None , '<STR_LIT>' ) <EOL> init . assert_called_once_with ( field_name = '<STR_LIT>' , <EOL> parent = None ) <EOL> self . assertFalse ( aef . _honor_child_endpoints ) <EOL> aef . context = { '<STR_LIT>' : [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] } <EOL> with mock . patch . object ( aef , '<STR_LIT>' ) as gbu : <EOL> gbu . return_value = '<STR_LIT>' <EOL> fields = aef . field_to_native ( object ( ) , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , fields ) <EOL> self . assertNotIn ( '<STR_LIT:foo>' , fields ) <EOL> self . assertNotIn ( '<STR_LIT:bar>' , fields ) <EOL> def test_honored_child_endpoints ( self ) : <EOL> """<STR_LIT>""" <EOL> aef = APIEndpointsField ( ) <EOL> aef . context = { '<STR_LIT>' : [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] } <EOL> with mock . patch . object ( APIEndpointField , '<STR_LIT>' ) as init : <EOL> aef . initialize ( None , '<STR_LIT>' ) <EOL> init . assert_called_once_with ( field_name = '<STR_LIT>' , <EOL> parent = None ) <EOL> self . assertTrue ( aef . _honor_child_endpoints ) <EOL> with mock . patch . object ( aef , '<STR_LIT>' ) as gbu : <EOL> gbu . return_value = '<STR_LIT>' <EOL> fields = aef . field_to_native ( object ( ) , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , fields ) <EOL> self . assertIn ( '<STR_LIT:foo>' , fields ) <EOL> self . assertIn ( '<STR_LIT:bar>' , fields ) <EOL> def test_format ( self ) : <EOL> """<STR_LIT>""" <EOL> m = NormalModel ( id = <NUM_LIT> ) <EOL> with mock . patch . dict ( self . aef . context , format = '<STR_LIT>' ) : <EOL> endpoints = self . aef . field_to_native ( m , '<STR_LIT>' ) <EOL> self . assertEqual ( endpoints , { <EOL> '<STR_LIT>' : '<STR_LIT>' % m . id , <EOL> } ) </s>
