def test_resource_fetch(self, mock_session): mock_session.configure_mock(base_url=self.BASE) mock_session.fqname_to_id.side_effect = HttpError(http_status=404) r = Resource('foo', fq_name='domain:bar:foo') with self.assertRaises(ResourceNotFound): r.fetch() mock_session.fqname_to_id.side_effect = [ "07eeb3c0-42f5-427c-9409-6ae45b376aa2" ] mock_session.get_json.return_value = { 'foo': { 'uuid': '07eeb3c0-42f5-427c-9409-6ae45b376aa2', 'fq_name': ['domain', 'bar', 'foo'] } } r = Resource('foo', fq_name='domain:bar:foo') r.fetch() self.assertEqual(r.uuid, '07eeb3c0-42f5-427c-9409-6ae45b376aa2') mock_session.get_json.assert_called_with(r.href) r.fetch(exclude_children=True) mock_session.get_json.assert_called_with(r.href, exclude_children=True) r.fetch(exclude_back_refs=True) mock_session.get_json.assert_called_with(r.href, exclude_back_refs=True)
def post(url, data=None, headers=None): data = json.loads(data) result = mock.Mock() if data['type'] == "foo": result.json.return_value = { "uuid": "ec1afeaa-8930-43b0-a60a-939f23a50724" } return result if data['type'] == "bar": raise HttpError(http_status=404)
def test_not_found(self, mock_session): mock_session.id_to_fqname.side_effect = HttpError(http_status=404) mock_document = Document(text='cat foo') comp = ShellCompleter() try: Resource('foo', uuid='dda4574d-96bc-43fd-bdf7-12ac776f754c', check=True) except ResourceNotFound: pass completions = list(comp.get_completions(mock_document, None)) self.assertEqual(len(completions), 0)
def test_resource_check(self, mock_session): mock_session.configure_mock(base_url=self.BASE) mock_session.id_to_fqname.side_effect = HttpError(http_status=404) r = Resource('bar', uuid='57ef609c-6c9b-4b91-a542-26c61420c37b') self.assertFalse(r.exists) mock_session.id_to_fqname.side_effect = [{ 'type': 'bar', 'fq_name': FQName('domain:bar') }] r = Resource('bar', uuid='57ef609c-6c9b-4b91-a542-26c61420c37b') self.assertTrue(r.exists) mock_session.fqname_to_id.side_effect = HttpError(http_status=404) r = Resource('bar', fq_name='domain:foo') self.assertFalse(r.exists) mock_session.fqname_to_id.side_effect = [ '588e1a17-ae50-4b67-8078-95f061d833ca' ] self.assertTrue(r.exists)
def test_resource_parent(self, mock_session): mock_session.configure_mock(base_url=self.BASE) p = Resource('bar', uuid='57ef609c-6c9b-4b91-a542-26c61420c37b') r = Resource('foo', fq_name='domain:foo', parent=p) self.assertEqual(p, r.parent) mock_session.id_to_fqname.side_effect = HttpError(http_status=404) with self.assertRaises(ResourceNotFound): p = Resource('foobar', uuid='1fe29f52-28dc-44a5-90d0-43de1b02cbd8') Resource('foo', fq_name='domain:foo', parent=p) with self.assertRaises(ResourceMissing): r = Resource('foo', fq_name='domain:foo') r.parent
def test_exist_exception(self, mock_session): r = Resource('foo', uuid='ec1afeaa-8930-43b0-a60a-939f23a50724') test_suite = [ ("Delete when children still present: %s" % ['http://host%s' % r.path], ChildrenExists), ("Children http://host%s still exist" % r.path, ChildrenExists), ("Delete when resource still referred: %s" % ['http://host%s' % r.path], BackRefsExists), ("Back-References from http://host%s still exist" % r.path, BackRefsExists), ("Cannot modify system resource %s %s(%s)" % (r.type, r.fq_name, r.uuid), IsSystemResource), ] for msg, exception in test_suite: mock_session.get_json.side_effect = HttpError(http_status=409, message=msg) with self.assertRaises(exception) as e: r.fetch() self.assertListEqual([r], e.exception.resources)
def id_to_fqname(self, uuid, type=None): """ Return fq_name and type for uuid If `type` is provided check that uuid is actually a resource of type `type`. Raise HttpError if it's not the case. :param uuid: resource uuid :type uuid: UUIDv4 str :param type: resource type :type type: str :rtype: dict {'type': str, 'fq_name': FQName} :raises HttpError: uuid not found """ data = {"uuid": uuid} result = self.post_json(self.make_url("/id-to-fqname"), data) result['fq_name'] = FQName(result['fq_name']) if type is not None and not result['type'].replace('_', '-') == type: raise HttpError('uuid %s not found for type %s' % (uuid, type), http_status=404) return result
def test_collection_not_found(self, mock_session): mock_session.get_json.side_effect = HttpError(http_status=404) with self.assertRaises(CollectionNotFound): Collection('foo', fetch=True)
def post(url, json): if json['uuid'] == 'a5a1b67b-4246-4e2d-aa24-479d8d47435d': return {'type': 'foo', 'fq_name': ['domain', 'foo', 'uuid']} else: raise HttpError(http_status=404)