def test_unicode_with_meta(self): resp = fakes.create_response_obj_with_header() obj = base.UnicodeWithMeta(u'test-unicode', resp) self.assertEqual(u'test-unicode', obj) # Check request_ids attribute is added to obj self.assertTrue(hasattr(obj, 'request_ids')) self.assertEqual(fakes.FAKE_REQUEST_ID_LIST, obj.request_ids)
def test_manager_list(self, mock_get): mock_get.return_value = (fakes.create_response_obj_with_header(), mock.MagicMock()) result = self.fake_manager.list() self.assertIsInstance(result, base.ListWithMeta) self.assertEqual([], result) expect_url = fakes.FAKE_RESOURCE_COLLECTION_URL mock_get.assert_called_once_with(expect_url, headers={})
def test_tuple_with_meta(self): resp = fakes.create_response_obj_with_header() expected_tuple = (1, 2) obj = base.TupleWithMeta(expected_tuple, resp) self.assertEqual(expected_tuple, obj) # Check request_ids attribute is added to obj self.assertTrue(hasattr(obj, 'request_ids')) self.assertEqual(fakes.FAKE_REQUEST_ID_LIST, obj.request_ids)
def test_manager_find_no_result(self, mock_get): mock_get.return_value = (fakes.create_response_obj_with_header(), { 'resources': [] }) self.assertRaises(exceptions.NotFound, self.fake_manager.find, uuid=fakes.FAKE_RESOURCE_ID, name=fakes.FAKE_RESOURCE_NAME) expect_collection_url = fakes.FAKE_RESOURCE_COLLECTION_URL mock_get.assert_called_once_with(expect_collection_url, headers={})
def test_manager_find(self, mock_get): fake_json_body_1 = dict(uuid=fakes.FAKE_RESOURCE_ID, name=fakes.FAKE_RESOURCE_NAME) fake_json_body_2 = dict(uuid='no_existed_id', name='no_existed_name') mock_get.side_effect = [(fakes.create_response_obj_with_header(), { 'resources': [fake_json_body_1, fake_json_body_2] }), (fakes.create_response_obj_with_header(), fake_json_body_1)] result = self.fake_manager.find(uuid=fakes.FAKE_RESOURCE_ID, name=fakes.FAKE_RESOURCE_NAME) self.assertIsInstance(result, base.Resource) self.assertEqual(fakes.FAKE_RESOURCE_ID, result.uuid) self.assertEqual(fakes.FAKE_RESOURCE_NAME, result.name) self.assertTrue(result.is_loaded()) expect_collection_url = fakes.FAKE_RESOURCE_COLLECTION_URL expect_item_url = (fakes.FAKE_RESOURCE_ITEM_URL % fakes.FAKE_RESOURCE_ID) mock_get.assert_has_calls([ mock.call(expect_collection_url, headers={}), mock.call(expect_item_url, headers={}) ])
def test_manager_delete(self, mock_delete): mock_delete.return_value = (fakes.create_response_obj_with_header(), None) fake_resource = fakes.FakeResource( None, dict(uuid=fakes.FAKE_RESOURCE_ID, name=fakes.FAKE_RESOURCE_NAME)) result = self.fake_manager.delete(fake_resource) self.assertIsInstance(result, base.TupleWithMeta) self.assertEqual(tuple(), result) expect_url = (fakes.FAKE_RESOURCE_ITEM_URL % fakes.FAKE_RESOURCE_ID) mock_delete.assert_called_once_with(expect_url, headers={})
def test_manager_get(self, mock_get): mock_get.return_value = (fakes.create_response_obj_with_header(), mock.MagicMock()) fake_resource = fakes.FakeResource( None, dict(uuid=fakes.FAKE_RESOURCE_ID, name=fakes.FAKE_RESOURCE_NAME)) result = self.fake_manager.get(fake_resource) self.assertIsInstance(result, base.Resource) self.assertIsInstance(result._info, mock.MagicMock) self.assertTrue(result.is_loaded()) expect_url = (fakes.FAKE_RESOURCE_ITEM_URL % fakes.FAKE_RESOURCE_ID) mock_get.assert_called_once_with(expect_url, headers={})
def test_manager_find_more_than_one_result(self, mock_get): fake_json_body_1 = dict(uuid=fakes.FAKE_RESOURCE_ID, name=fakes.FAKE_RESOURCE_NAME) fake_json_body_2 = copy.deepcopy(fake_json_body_1) mock_get.return_value = (fakes.create_response_obj_with_header(), { 'resources': [fake_json_body_1, fake_json_body_2] }) self.assertRaises(exceptions.NoUniqueMatch, self.fake_manager.find, uuid=fakes.FAKE_RESOURCE_ID, name=fakes.FAKE_RESOURCE_NAME) expect_collection_url = fakes.FAKE_RESOURCE_COLLECTION_URL mock_get.assert_called_once_with(expect_collection_url, headers={})
def test_manager_create(self, mock_post): mock_post.return_value = (fakes.create_response_obj_with_header(), mock.MagicMock()) fake_resource = fakes.FakeResource( None, dict(uuid=fakes.FAKE_RESOURCE_ID, name=fakes.FAKE_RESOURCE_NAME)) result = self.fake_manager.create(fake_resource) self.assertIsInstance(result, base.Resource) self.assertIsInstance(result._info, mock.MagicMock) self.assertFalse(result.is_loaded()) expect_url = fakes.FAKE_RESOURCE_COLLECTION_URL mock_post.assert_called_once_with(expect_url, data=fake_resource, headers={})
def test_resource_object_with_request_ids(self): resp_obj = fakes.create_response_obj_with_header() r = base.Resource(None, {'name': '1'}, resp=resp_obj) self.assertEqual(fakes.FAKE_REQUEST_ID_LIST, r.request_ids)