<s> import datetime <EOL> import mock <EOL> from testtools . matchers import HasLength <EOL> from ironic . common import exception <EOL> from ironic import objects <EOL> from ironic . tests . unit . db import base <EOL> from ironic . tests . unit . db import utils <EOL> class TestPortgroupObject ( base . DbTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestPortgroupObject , self ) . setUp ( ) <EOL> self . fake_portgroup = utils . get_test_portgroup ( ) <EOL> def test_get_by_id ( self ) : <EOL> portgroup_id = self . fake_portgroup [ '<STR_LIT:id>' ] <EOL> with mock . patch . object ( self . dbapi , '<STR_LIT>' , <EOL> autospec = True ) as mock_get_portgroup : <EOL> mock_get_portgroup . return_value = self . fake_portgroup <EOL> portgroup = objects . Portgroup . get ( self . context , portgroup_id ) <EOL> mock_get_portgroup . assert_called_once_with ( portgroup_id ) <EOL> self . assertEqual ( self . context , portgroup . _context ) <EOL> def test_get_by_uuid ( self ) : <EOL> uuid = self . fake_portgroup [ '<STR_LIT>' ] <EOL> with mock . patch . object ( self . dbapi , '<STR_LIT>' , <EOL> autospec = True ) as mock_get_portgroup : <EOL> mock_get_portgroup . return_value = self . fake_portgroup <EOL> portgroup = objects . Portgroup . get ( self . context , uuid ) <EOL> mock_get_portgroup . assert_called_once_with ( uuid ) <EOL> self . assertEqual ( self . context , portgroup . _context ) <EOL> def test_get_by_address ( self ) : <EOL> address = self . fake_portgroup [ '<STR_LIT:address>' ] <EOL> with mock . patch . object ( self . dbapi , '<STR_LIT>' , <EOL> autospec = True ) as mock_get_portgroup : <EOL> mock_get_portgroup . return_value = self . fake_portgroup <EOL> portgroup = objects . Portgroup . get ( self . context , address ) <EOL> mock_get_portgroup . assert_called_once_with ( address ) <EOL> self . assertEqual ( self . context , portgroup . _context ) <EOL> def test_get_by_name ( self ) : <EOL> name = self . fake_portgroup [ '<STR_LIT:name>' ] <EOL> with mock . patch . object ( self . dbapi , '<STR_LIT>' , <EOL> autospec = True ) as mock_get_portgroup : <EOL> mock_get_portgroup . return_value = self . fake_portgroup <EOL> portgroup = objects . Portgroup . get ( self . context , name ) <EOL> mock_get_portgroup . assert_called_once_with ( name ) <EOL> self . assertEqual ( self . context , portgroup . _context ) <EOL> def test_get_bad_id_and_uuid_and_address_and_name ( self ) : <EOL> self . assertRaises ( exception . InvalidIdentity , <EOL> objects . Portgroup . get , <EOL> self . context , <EOL> '<STR_LIT>' ) <EOL> def test_save ( self ) : <EOL> uuid = self . fake_portgroup [ '<STR_LIT>' ] <EOL> address = "<STR_LIT>" <EOL> test_time = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> with mock . patch . object ( self . dbapi , '<STR_LIT>' , <EOL> autospec = True ) as mock_get_portgroup : <EOL> mock_get_portgroup . return_value = self . fake_portgroup <EOL> with mock . patch . object ( self . dbapi , '<STR_LIT>' , <EOL> autospec = True ) as mock_update_portgroup : <EOL> mock_update_portgroup . return_value = ( <EOL> utils . get_test_portgroup ( address = address , <EOL> updated_at = test_time ) ) <EOL> p = objects . Portgroup . get_by_uuid ( self . context , uuid ) <EOL> p . address = address <EOL> p . save ( ) <EOL> mock_get_portgroup . assert_called_once_with ( uuid ) <EOL> mock_update_portgroup . assert_called_once_with ( <EOL> uuid , { '<STR_LIT:address>' : "<STR_LIT>" } ) <EOL> self . assertEqual ( self . context , p . _context ) <EOL> res_updated_at = ( p . updated_at ) . replace ( tzinfo = None ) <EOL> self . assertEqual ( test_time , res_updated_at ) <EOL> def test_refresh ( self ) : <EOL> uuid = self . fake_portgroup [ '<STR_LIT>' ] <EOL> returns = [ self . fake_portgroup , <EOL> utils . get_test_portgroup ( address = "<STR_LIT>" ) ] <EOL> expected = [ mock . call ( uuid ) , mock . call ( uuid ) ] <EOL> with mock . patch . object ( self . dbapi , '<STR_LIT>' , <EOL> side_effect = returns , <EOL> autospec = True ) as mock_get_portgroup : <EOL> p = objects . Portgroup . get_by_uuid ( self . context , uuid ) <EOL> self . assertEqual ( "<STR_LIT>" , p . address ) <EOL> p . refresh ( ) <EOL> self . assertEqual ( "<STR_LIT>" , p . address ) <EOL> self . assertEqual ( expected , mock_get_portgroup . call_args_list ) <EOL> self . assertEqual ( self . context , p . _context ) <EOL> def test_list ( self ) : <EOL> with mock . patch . object ( self . dbapi , '<STR_LIT>' , <EOL> autospec = True ) as mock_get_list : <EOL> mock_get_list . return_value = [ self . fake_portgroup ] <EOL> portgroups = objects . Portgroup . list ( self . context ) <EOL> self . assertThat ( portgroups , HasLength ( <NUM_LIT:1> ) ) <EOL> self . assertIsInstance ( portgroups [ <NUM_LIT:0> ] , objects . Portgroup ) <EOL> self . assertEqual ( self . context , portgroups [ <NUM_LIT:0> ] . _context ) <EOL> def test_list_by_node_id ( self ) : <EOL> with mock . patch . object ( self . dbapi , '<STR_LIT>' , <EOL> autospec = True ) as mock_get_list : <EOL> mock_get_list . return_value = [ self . fake_portgroup ] <EOL> node_id = self . fake_portgroup [ '<STR_LIT>' ] <EOL> portgroups = objects . Portgroup . list_by_node_id ( self . context , <EOL> node_id ) <EOL> self . assertThat ( portgroups , HasLength ( <NUM_LIT:1> ) ) <EOL> self . assertIsInstance ( portgroups [ <NUM_LIT:0> ] , objects . Portgroup ) <EOL> self . assertEqual ( self . context , portgroups [ <NUM_LIT:0> ] . _context ) </s>
