def test_dict_with_meta(self): resp = fakes.create_response() obj = resource.DictWithMeta({}, resp) self.assertEqual({}, obj) # Check request_id attribute is added to obj self.assertTrue(hasattr(obj, 'request_id')) self.assertEqual(fakes.FAKE_REQUEST_ID, obj.request_id)
def test_manager_list_return_string(self, mocked): json = {"resources": ["a", "b", "c"]} mocked.return_value = fakes.create_response(json) result = self.manager.list() self.assertEqual(3, len(result)) self.assertEqual(json['resources'], result) self.assertIsInstance(result, resource.ListWithMeta)
def test_mixin_meta(self): resp = fakes.create_response() text = self.manager.mixin_meta('text', resp) self.assertEqual('text', text) self.assertIsInstance(text, resource.StrWithMeta) text = self.manager.mixin_meta(u'text', resp) self.assertEqual('text', text) self.assertIsInstance(text, resource.UnicodeWithMeta) list_item = ['a', 'b', 'c'] list_mixin = self.manager.mixin_meta(list_item, resp) self.assertEqual(list_item, list_mixin) self.assertIsInstance(list_mixin, resource.ListWithMeta) tuple_item = ('a', 'b', 'c') tuple_mixin = self.manager.mixin_meta(tuple_item, resp) self.assertEqual(tuple_item, tuple_mixin) self.assertIsInstance(tuple_mixin, resource.TupleWithMeta) byte_item = six.binary_type('abc') byte_mixin = self.manager.mixin_meta(byte_item, resp) self.assertEqual(byte_item, byte_mixin) if six.PY2: self.assertIsInstance(byte_mixin, resource.StrWithMeta) elif six.PY3: self.assertIsInstance(byte_mixin, resource.BytesWithMeta)
def test_unicode_with_meta(self): resp = fakes.create_response() obj = resource.UnicodeWithMeta(u'test-unicode', resp) self.assertEqual(u'test-unicode', obj) # Check request_id attribute is added to obj self.assertTrue(hasattr(obj, 'request_id')) self.assertEqual(fakes.FAKE_REQUEST_ID, obj.request_id)
def test_tuple_with_meta(self): resp = fakes.create_response() expected_tuple = (1, 2) obj = resource.TupleWithMeta(expected_tuple, resp) self.assertEqual(expected_tuple, obj) # Check request_id attribute is added to obj self.assertTrue(hasattr(obj, 'request_id')) self.assertEqual(fakes.FAKE_REQUEST_ID, obj.request_id)
def test_manager_delete(self, mocked): mocked.return_value = fakes.create_response() result = self.manager.delete(self.resource) self.assertEqual(tuple(), result) self.assertIsInstance(result, resource.TupleWithMeta) expect_url = (fakes.FAKE_RESOURCE_ITEM_URL % fakes.FAKE_RESOURCE_ID) mocked.assert_called_once_with(expect_url, "DELETE", headers=self._headers)
def test_manager_create_return_none(self, mocked): mocked.return_value = fakes.create_response() result = self.manager.create(self.resource) self.assertIsInstance(result, resource.TupleWithMeta) self.assertEqual(result, ()) expect_url = fakes.FAKE_RESOURCE_COLLECTION_URL mocked.assert_called_once_with(expect_url, "POST", json=self.resource, headers=self._headers)
def test_manager_put(self, mocked): mocked.return_value = fakes.create_response(self.instance) result = self.manager.update_all(self.resource) self.assertFalse(result.has_attached()) self.assertIsInstance(result, fakes.FakeResource) expect_url = (fakes.FAKE_RESOURCE_ITEM_URL % fakes.FAKE_RESOURCE_ID) mocked.assert_called_once_with(expect_url, "PUT", json=self.resource, headers=self._headers) # return raw result = self.manager.update_all(self.resource, raw=True) self.assertIsInstance(result, resource.DictWithMeta) self.assertEqual(result, self.instance) # return None resp = fakes.create_response() mocked.return_value = resp result = self.manager.update_all(self.resource) self.assertEqual(result, resp.text) self.assertIsInstance(result, resource.StrWithMeta)
def test_manager_list(self, mocked): json = {"resources": [self.instance]} mocked.return_value = fakes.create_response(json) result = self.manager.list() self.assertEqual(1, len(result)) self.assertEqual([self.resource], result) self.assertIsInstance(result, resource.ListWithMeta) self.assertIsInstance(result[0], fakes.FakeResource) expect_url = fakes.FAKE_RESOURCE_COLLECTION_URL mocked.assert_called_once_with(expect_url, "GET", headers=self._headers, params={})
def test_manager_create(self, mocked): mocked.return_value = fakes.create_response(self.instance) result = self.manager.create(self.resource) self.assertIsInstance(result, fakes.FakeResource) self.assertEqual(result.original, self.instance) self.assertFalse(result.has_attached()) expect_url = fakes.FAKE_RESOURCE_COLLECTION_URL mocked.assert_called_once_with(expect_url, "POST", json=self.resource, headers=self._headers) result = self.manager.create() mocked.assert_called_with(expect_url, "POST", headers=self._headers)
def test_resource_object_with_request_id(self): resp_obj = fakes.create_response() r = SampleResource(None, {'name': '1'}, resp=resp_obj) self.assertEqual(fakes.FAKE_REQUEST_ID, r.request_id)