<s> import mock <EOL> from neutron_lib . api import converters <EOL> from neutron_lib import constants <EOL> from neutron_lib import exceptions as n_exc <EOL> from oslo_utils import uuidutils <EOL> import webob . exc <EOL> from neutron . api . v2 import attributes <EOL> from neutron import context <EOL> from neutron . tests import base <EOL> class TestResDict ( base . BaseTestCase ) : <EOL> class _MyException ( Exception ) : <EOL> pass <EOL> _EXC_CLS = _MyException <EOL> def _test_fill_default_value ( self , attr_info , expected , res_dict ) : <EOL> attributes . fill_default_value ( attr_info , res_dict ) <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_fill_default_value ( self ) : <EOL> attr_info = { <EOL> '<STR_LIT:key>' : { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:default>' : constants . ATTR_NOT_SPECIFIED , <EOL> } , <EOL> } <EOL> self . _test_fill_default_value ( attr_info , { '<STR_LIT:key>' : '<STR_LIT:X>' } , { '<STR_LIT:key>' : '<STR_LIT:X>' } ) <EOL> self . _test_fill_default_value ( <EOL> attr_info , { '<STR_LIT:key>' : constants . ATTR_NOT_SPECIFIED } , { } ) <EOL> attr_info = { <EOL> '<STR_LIT:key>' : { <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> } <EOL> self . _test_fill_default_value ( attr_info , { '<STR_LIT:key>' : '<STR_LIT:X>' } , { '<STR_LIT:key>' : '<STR_LIT:X>' } ) <EOL> self . assertRaises ( ValueError , self . _test_fill_default_value , <EOL> attr_info , { '<STR_LIT:key>' : '<STR_LIT:X>' } , { } ) <EOL> self . assertRaises ( self . _EXC_CLS , attributes . fill_default_value , <EOL> attr_info , { } , self . _EXC_CLS ) <EOL> attr_info = { <EOL> '<STR_LIT:key>' : { <EOL> '<STR_LIT>' : False , <EOL> } , <EOL> } <EOL> self . assertRaises ( ValueError , self . _test_fill_default_value , <EOL> attr_info , { '<STR_LIT:key>' : '<STR_LIT:X>' } , { '<STR_LIT:key>' : '<STR_LIT:X>' } ) <EOL> self . _test_fill_default_value ( attr_info , { } , { } ) <EOL> self . assertRaises ( self . _EXC_CLS , attributes . fill_default_value , <EOL> attr_info , { '<STR_LIT:key>' : '<STR_LIT:X>' } , self . _EXC_CLS ) <EOL> def _test_convert_value ( self , attr_info , expected , res_dict ) : <EOL> attributes . convert_value ( attr_info , res_dict ) <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_convert_value ( self ) : <EOL> attr_info = { <EOL> '<STR_LIT:key>' : { <EOL> } , <EOL> } <EOL> self . _test_convert_value ( attr_info , <EOL> { '<STR_LIT:key>' : constants . ATTR_NOT_SPECIFIED } , <EOL> { '<STR_LIT:key>' : constants . ATTR_NOT_SPECIFIED } ) <EOL> self . _test_convert_value ( attr_info , { '<STR_LIT:key>' : '<STR_LIT:X>' } , { '<STR_LIT:key>' : '<STR_LIT:X>' } ) <EOL> self . _test_convert_value ( attr_info , <EOL> { '<STR_LIT>' : '<STR_LIT:X>' } , { '<STR_LIT>' : '<STR_LIT:X>' } ) <EOL> attr_info = { <EOL> '<STR_LIT:key>' : { <EOL> '<STR_LIT>' : converters . convert_to_int , <EOL> } , <EOL> } <EOL> self . _test_convert_value ( attr_info , <EOL> { '<STR_LIT:key>' : constants . ATTR_NOT_SPECIFIED } , <EOL> { '<STR_LIT:key>' : constants . ATTR_NOT_SPECIFIED } ) <EOL> self . _test_convert_value ( attr_info , { '<STR_LIT:key>' : <NUM_LIT:1> } , { '<STR_LIT:key>' : '<STR_LIT:1>' } ) <EOL> self . _test_convert_value ( attr_info , { '<STR_LIT:key>' : <NUM_LIT:1> } , { '<STR_LIT:key>' : <NUM_LIT:1> } ) <EOL> self . assertRaises ( n_exc . InvalidInput , self . _test_convert_value , <EOL> attr_info , { '<STR_LIT:key>' : <NUM_LIT:1> } , { '<STR_LIT:key>' : '<STR_LIT:a>' } ) <EOL> attr_info = { <EOL> '<STR_LIT:key>' : { <EOL> '<STR_LIT>' : { '<STR_LIT>' : None } , <EOL> } , <EOL> } <EOL> self . _test_convert_value ( attr_info , <EOL> { '<STR_LIT:key>' : constants . ATTR_NOT_SPECIFIED } , <EOL> { '<STR_LIT:key>' : constants . ATTR_NOT_SPECIFIED } ) <EOL> uuid_str = '<STR_LIT>' <EOL> self . _test_convert_value ( attr_info , <EOL> { '<STR_LIT:key>' : uuid_str } , { '<STR_LIT:key>' : uuid_str } ) <EOL> self . assertRaises ( ValueError , self . _test_convert_value , <EOL> attr_info , { '<STR_LIT:key>' : <NUM_LIT:1> } , { '<STR_LIT:key>' : <NUM_LIT:1> } ) <EOL> self . assertRaises ( self . _EXC_CLS , attributes . convert_value , <EOL> attr_info , { '<STR_LIT:key>' : <NUM_LIT:1> } , self . _EXC_CLS ) <EOL> def test_populate_tenant_id ( self ) : <EOL> tenant_id_1 = uuidutils . generate_uuid ( ) <EOL> tenant_id_2 = uuidutils . generate_uuid ( ) <EOL> ctx = context . Context ( user_id = None , tenant_id = tenant_id_1 ) <EOL> res_dict = { '<STR_LIT>' : tenant_id_2 } <EOL> self . assertRaises ( webob . exc . HTTPBadRequest , <EOL> attributes . populate_tenant_id , <EOL> ctx , res_dict , None , None ) <EOL> ctx . is_admin = True <EOL> self . assertIsNone ( attributes . populate_tenant_id ( ctx , res_dict , <EOL> None , None ) ) <EOL> res_dict2 = { } <EOL> attributes . populate_tenant_id ( ctx , res_dict2 , None , True ) <EOL> self . assertEqual ( { '<STR_LIT>' : ctx . tenant_id } , res_dict2 ) <EOL> res_dict3 = { } <EOL> attr_info = { '<STR_LIT>' : { '<STR_LIT>' : True } , } <EOL> ctx . tenant_id = None <EOL> self . assertRaises ( webob . exc . HTTPBadRequest , <EOL> attributes . populate_tenant_id , <EOL> ctx , res_dict3 , attr_info , True ) <EOL> class TestHelpers ( base . DietTestCase ) : <EOL> def _verify_port_attributes ( self , attrs ) : <EOL> for test_attribute in ( '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' ) : <EOL> self . assertIn ( test_attribute , attrs ) <EOL> def test_get_collection_info ( self ) : <EOL> attrs = attributes . get_collection_info ( '<STR_LIT>' ) <EOL> self . _verify_port_attributes ( attrs ) <EOL> def test_get_collection_info_missing ( self ) : <EOL> self . assertFalse ( attributes . get_collection_info ( '<STR_LIT>' ) ) <EOL> def test_get_resource_info ( self ) : <EOL> attributes . REVERSED_PLURALS . pop ( '<STR_LIT:port>' , None ) <EOL> attrs = attributes . get_resource_info ( '<STR_LIT:port>' ) <EOL> self . _verify_port_attributes ( attrs ) <EOL> self . assertIn ( '<STR_LIT:port>' , attributes . REVERSED_PLURALS ) <EOL> def test_get_resource_info_missing ( self ) : <EOL> self . assertFalse ( attributes . get_resource_info ( '<STR_LIT>' ) ) <EOL> def test_get_resource_info_cached ( self ) : <EOL> with mock . patch ( '<STR_LIT>' ) as mock_plurals : <EOL> attributes . REVERSED_PLURALS [ '<STR_LIT:port>' ] = '<STR_LIT>' <EOL> attrs = attributes . get_resource_info ( '<STR_LIT:port>' ) <EOL> self . _verify_port_attributes ( attrs ) <EOL> self . assertEqual ( <NUM_LIT:0> , mock_plurals . items . call_count ) </s>
