def test_get_info_none(self, mock_is_loaded, mock_get): mock_is_loaded.return_value = True res = base.Resource(mock.Mock(), {}, loaded=True) info = res.get_info() self.assertTrue(mock_is_loaded.called) self.assertFalse(mock_get.called) self.assertEqual(mock_is_loaded.call_count, 1) self.assertEqual(info, {})
def test_get_info(self, mock_is_loaded, mock_get): mock_is_loaded.return_value = True payload = {'id': 'fakeid'} res = base.Resource(mock.Mock(), payload, loaded=True) info = res.get_info() self.assertTrue(mock_is_loaded.called) self.assertFalse(mock_get.called) self.assertEqual(mock_is_loaded.call_count, 1) self.assertEqual(info, payload) self.assertIsNot(info, payload)
def test_getattr_unloaded_getattr(self): manager = mock.Mock() manager.get.return_value = mock.Mock(_info={ 'id': 'fakeid', 'name': 'fakename' }) res = base.Resource(manager, {'id': 'fakeid'}) self.assertEqual(res.name, 'fakename') self.assertTrue(manager.get.called) self.assertEqual(manager.get.mock_calls, [mock.call('fakeid')]) self.assertEqual(res._info, {'id': 'fakeid', 'name': 'fakename'})
def test_getattr_loaded(self): manager = MockWithoutAttrs(exclude_attrs=('get', )) res = base.Resource(manager, { 'id': 'fakeid', 'name': 'fakename' }, loaded=True) self.assertTrue(hasattr(res, 'id')) self.assertTrue(hasattr(res, 'name')) self.assertEqual(res.id, 'fakeid') self.assertEqual(res.name, 'fakename')
def test_not_eq_by_info(self): manager = mock.Mock() res1 = base.Resource(manager, {'name': 'fakename1'}, loaded=True) res2 = base.Resource(manager, {'name': 'fakename2'}, loaded=True) self.assertFalse(res1 == res2)
def test_not_eq_by_isinstance(self): manager = mock.Mock() res1 = base.Resource(manager, {'id': 'fakeid'}, loaded=True) res2 = mock.Mock() self.assertFalse(res1 == res2)
def test_eq_by_id(self): manager = mock.Mock() res1 = base.Resource(manager, {'id': 'fakeid'}, loaded=True) res2 = base.Resource(manager, {'id': 'fakeid'}, loaded=True) self.assertTrue(res1 == res2)
def test_get_unloaded(self): manager = MockWithoutAttrs(exclude_attrs=('get', )) res = base.Resource(manager, {'id': 'fakeid'}) with self.assertRaises(AttributeError) as cm: name = res.name self.assertEqual(cm.exception.args, ('name', ))