<s> import mock <EOL> from oslo_versionedobjects import exception as ovo_exc <EOL> from nova import db <EOL> from nova import objects <EOL> from nova . tests . unit . objects import test_objects <EOL> from nova . tests . unit . objects import test_security_group <EOL> fake_rule = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:id>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class _TestSecurityGroupRuleObject ( object ) : <EOL> def test_get_by_id ( self ) : <EOL> with mock . patch . object ( db , '<STR_LIT>' ) as sgrg : <EOL> sgrg . return_value = fake_rule <EOL> rule = objects . SecurityGroupRule . get_by_id ( <EOL> self . context , <NUM_LIT:1> ) <EOL> for field in fake_rule : <EOL> if field == '<STR_LIT>' : <EOL> self . assertEqual ( fake_rule [ field ] , str ( getattr ( rule , <EOL> field ) ) ) <EOL> else : <EOL> self . assertEqual ( fake_rule [ field ] , getattr ( rule , field ) ) <EOL> sgrg . assert_called_with ( self . context , <NUM_LIT:1> ) <EOL> def test_get_by_security_group ( self ) : <EOL> secgroup = objects . SecurityGroup ( ) <EOL> secgroup . id = <NUM_LIT> <EOL> rule = dict ( fake_rule ) <EOL> rule [ '<STR_LIT>' ] = dict ( test_security_group . fake_secgroup , id = <NUM_LIT> ) <EOL> stupid_method = '<STR_LIT>' <EOL> with mock . patch . object ( db , stupid_method ) as sgrgbsg : <EOL> sgrgbsg . return_value = [ rule ] <EOL> rules = ( objects . SecurityGroupRuleList . <EOL> get_by_security_group ( self . context , secgroup ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( rules ) ) <EOL> self . assertEqual ( <NUM_LIT> , rules [ <NUM_LIT:0> ] . grantee_group . id ) <EOL> @ mock . patch . object ( db , '<STR_LIT>' , <EOL> return_value = fake_rule ) <EOL> def test_create ( self , db_mock ) : <EOL> rule = objects . SecurityGroupRule ( context = self . context ) <EOL> rule . protocol = '<STR_LIT>' <EOL> secgroup = objects . SecurityGroup ( ) <EOL> secgroup . id = <NUM_LIT> <EOL> parentgroup = objects . SecurityGroup ( ) <EOL> parentgroup . id = <NUM_LIT> <EOL> rule . grantee_group = secgroup <EOL> rule . parent_group = parentgroup <EOL> rule . create ( ) <EOL> updates = db_mock . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertEqual ( fake_rule [ '<STR_LIT:id>' ] , rule . id ) <EOL> self . assertEqual ( updates [ '<STR_LIT>' ] , rule . grantee_group . id ) <EOL> self . assertEqual ( updates [ '<STR_LIT>' ] , rule . parent_group . id ) <EOL> @ mock . patch . object ( db , '<STR_LIT>' , <EOL> return_value = fake_rule ) <EOL> def test_set_id_failure ( self , db_mock ) : <EOL> rule = objects . SecurityGroupRule ( context = self . context ) <EOL> rule . create ( ) <EOL> self . assertRaises ( ovo_exc . ReadOnlyFieldError , setattr , <EOL> rule , '<STR_LIT:id>' , <NUM_LIT> ) <EOL> class TestSecurityGroupRuleObject ( test_objects . _LocalTest , <EOL> _TestSecurityGroupRuleObject ) : <EOL> pass <EOL> class TestSecurityGroupRuleObjectRemote ( test_objects . _RemoteTest , <EOL> _TestSecurityGroupRuleObject ) : <EOL> pass <EOL> fake_rules = [ <EOL> dict ( fake_rule , id = <NUM_LIT:1> , grantee_group = test_security_group . fake_secgroup ) , <EOL> dict ( fake_rule , id = <NUM_LIT:2> , grantee_group = test_security_group . fake_secgroup ) , <EOL> ] <EOL> class _TestSecurityGroupRuleListObject ( object ) : <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_instance ( self , mock_get ) : <EOL> mock_get . return_value = fake_rules <EOL> instance = objects . Instance ( uuid = '<STR_LIT>' ) <EOL> rules = objects . SecurityGroupRuleList . get_by_instance ( self . context , <EOL> instance ) <EOL> mock_get . assert_called_once_with ( self . context , instance . uuid ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( rules ) ) <EOL> self . assertEqual ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ x . id for x in rules ] ) <EOL> class TestSecurityGroupRuleListObject ( test_objects . _LocalTest , <EOL> _TestSecurityGroupRuleListObject ) : <EOL> pass <EOL> class TestSecurityGroupRuleListObjectRemote ( test_objects . _RemoteTest , <EOL> _TestSecurityGroupRuleListObject ) : <EOL> pass </s>
