def test__create(self): manager = base.Manager() manager.api = mock.Mock() manager.api.client = mock.Mock() response_key = "response_key" data_ = "test-data" body_ = {response_key: data_} url_ = "test_url_post" manager.api.client.post = mock.Mock(return_value=(url_, body_)) return_raw = True r = manager._create(url_, body_, response_key, return_raw) self.assertEqual(data_, r) return_raw = False @contextlib.contextmanager def completion_cache_mock(*arg, **kwargs): yield mockl = mock.Mock() mockl.side_effect = completion_cache_mock manager.completion_cache = mockl manager.resource_class = mock.Mock(return_value="test-class") r = manager._create(url_, body_, response_key, return_raw) self.assertEqual("test-class", r)
def setUp(self): super(MangerPaginationTests, self).setUp() self.manager = base.Manager() self.manager.api = mock.Mock() self.manager.api.client = mock.Mock() self.manager.resource_class = base.Resource self.response_key = "response_key" self.data = [{"foo": "p1"}, {"foo": "p2"}] self.next_data = [{"foo": "p3"}, {"foo": "p4"}] self.marker = 'test-marker' self.limit = '20' self.url = "http://test_url" self.next_url = '%s?marker=%s&limit=%s' % (self.url, self.marker, self.limit) self.links = [{'href': self.next_url, 'rel': 'next'}] self.body = {self.response_key: self.data, 'links': self.links} self.next_body = {self.response_key: self.next_data} def side_effect(url): if url == self.url: return None, self.body # In python 3 the order in the dictionary is not constant # between runs. So we cant rely on the URL params to be # in the same order if ('marker=%s' % self.marker in url and 'limit=%s' % self.limit in url): self.next_url = url return None, self.next_body self.manager.api.client.get = mock.Mock(side_effect=side_effect)
def setUp(self): super(ManagerListTest, self).setUp() @contextlib.contextmanager def completion_cache_mock(*arg, **kwargs): yield self.manager = base.Manager() self.manager.api = mock.Mock() self.manager.api.client = mock.Mock() self.response_key = "response_key" self.data_p = ["p1", "p2"] self.body_p = {self.response_key: self.data_p} self.url_p = "test_url_post" self.manager.api.client.post = mock.Mock(return_value=(self.url_p, self.body_p)) self.data_g = ["g1", "g2", "g3"] self.body_g = {self.response_key: self.data_g} self.url_g = "test_url_get" self.manager.api.client.get = mock.Mock(return_value=(self.url_g, self.body_g)) mockl = mock.Mock() mockl.side_effect = completion_cache_mock self.manager.completion_cache = mockl
def setUp(self): super(MangerPaginationTests, self).setUp() self.manager = base.Manager() self.manager.api = mock.Mock() self.manager.api.client = mock.Mock() self.manager.resource_class = base.Resource self.response_key = "response_key" self.data = [{"foo": "p1"}, {"foo": "p2"}] self.next_data = [{"foo": "p3"}, {"foo": "p4"}] self.marker = 'test-marker' self.limit = '20' self.url = "http://test_url" self.next_url = '%s?marker=%s&limit=%s' % (self.url, self.marker, self.limit) self.links = [{'href': self.next_url, 'rel': 'next'}] self.body = { self.response_key: self.data, 'links': self.links } self.next_body = {self.response_key: self.next_data} def side_effect(url): if url == self.url: return (None, self.body) if url == self.next_url: return (None, self.next_body) self.manager.api.client.get = mock.Mock(side_effect=side_effect)
def test_write_to_completion_cache(self): manager = base.Manager() # no cache object, nothing should happen manager.write_to_completion_cache("non-exist", "val") manager._mock_cache = mock.Mock() manager._mock_cache.write = mock.Mock(return_value=None) manager.write_to_completion_cache("mock", "val") self.assertEqual(1, manager._mock_cache.write.call_count)
def _get_mock(self): manager = base.Manager() manager.api = mock.Mock() manager.api.client = mock.Mock() def side_effect_func(self, body, loaded=True): return body manager.resource_class = mock.Mock(side_effect=side_effect_func) return manager
def test_completion_cache(self): manager = base.Manager() # handling exceptions mode = "w" cache_type = "unittest" obj_class = mock.Mock with manager.completion_cache(cache_type, obj_class, mode): pass os.makedirs = mock.Mock(side_effect=OSError) with manager.completion_cache(cache_type, obj_class, mode): pass
def test___init__(self): api = mock.Mock() base.Manager.__init__ = self.orig__init manager = base.Manager(api) self.assertEqual(api, manager.api)
def get_mock_mng_api_client(self): manager = base.Manager() manager.api = mock.Mock() manager.api.client = mock.Mock() return manager