def test_repr(self): self.assertEqual(repr(_Call()), 'call()') self.assertEqual(repr(_Call(('foo', ))), 'call.foo()') self.assertEqual(repr(_Call(((1, 2, 3), { 'a': 'b' }))), "call(1, 2, 3, a='b')") self.assertEqual(repr(_Call(('bar', (1, 2, 3), { 'a': 'b' }))), "call.bar(1, 2, 3, a='b')") self.assertEqual(repr(call), 'call') self.assertEqual(str(call), 'call') self.assertEqual(repr(call()), 'call()') self.assertEqual(repr(call(1)), 'call(1)') self.assertEqual(repr(call(zz='thing')), "call(zz='thing')") self.assertEqual(repr(call().foo), 'call().foo') self.assertEqual(repr(call(1).foo.bar(a=3).bing), 'call().foo.bar().bing') self.assertEqual(repr(call().foo(1, 2, a=3)), "call().foo(1, 2, a=3)") self.assertEqual(repr(call()()), "call()()") self.assertEqual(repr(call(1)(2)), "call()(2)") self.assertEqual(repr(call()().bar().baz.beep(1)), "call()().bar().baz.beep(1)")
def test_repr(self): self.assertEqual(repr(_Call()), 'call()') self.assertEqual(repr(_Call(('foo',))), 'call.foo()') self.assertEqual(repr(_Call(((1, 2, 3), {'a': 'b'}))), "call(1, 2, 3, a='b')") self.assertEqual(repr(_Call(('bar', (1, 2, 3), {'a': 'b'}))), "call.bar(1, 2, 3, a='b')") self.assertEqual(repr(call), 'call') self.assertEqual(str(call), 'call') self.assertEqual(repr(call()), 'call()') self.assertEqual(repr(call(1)), 'call(1)') self.assertEqual(repr(call(zz='thing')), "call(zz='thing')") self.assertEqual(repr(call().foo), 'call().foo') self.assertEqual(repr(call(1).foo.bar(a=3).bing), 'call().foo.bar().bing') self.assertEqual( repr(call().foo(1, 2, a=3)), "call().foo(1, 2, a=3)" ) self.assertEqual(repr(call()()), "call()()") self.assertEqual(repr(call(1)(2)), "call()(2)") self.assertEqual( repr(call()().bar().baz.beep(1)), "call()().bar().baz.beep(1)" )
def test_two_args_call(self): args = _Call(((1, 2), {'a': 3}), two=True) self.assertEqual(len(args), 2) self.assertEqual(args[0], (1, 2)) self.assertEqual(args[1], {'a': 3}) other_args = _Call(((1, 2), {'a': 3})) self.assertEqual(args, other_args)
def test_call_with_args(self): args = _Call(((1, 2, 3), {})) self.assertEqual(args, ((1, 2, 3), )) self.assertEqual(args, ('foo', (1, 2, 3))) self.assertEqual(args, ('foo', (1, 2, 3), {})) self.assertEqual(args, ((1, 2, 3), {}))
def test_call_with_kwargs(self): args = _Call(((), dict(a=3, b=4))) self.assertEqual(args, (dict(a=3, b=4), )) self.assertEqual(args, ('foo', dict(a=3, b=4))) self.assertEqual(args, ('foo', (), dict(a=3, b=4))) self.assertEqual(args, ((), dict(a=3, b=4)))
def test_call_with_kwargs(self): args = _Call(((), dict(a=3, b=4))) self.assertEqual(args, (dict(a=3, b=4),)) self.assertEqual(args, ('foo', dict(a=3, b=4))) self.assertEqual(args, ('foo', (), dict(a=3, b=4))) self.assertEqual(args, ((), dict(a=3, b=4)))
def test_call_with_args(self): args = _Call(((1, 2, 3), {})) self.assertEqual(args, ((1, 2, 3),)) self.assertEqual(args, ('foo', (1, 2, 3))) self.assertEqual(args, ('foo', (1, 2, 3), {})) self.assertEqual(args, ((1, 2, 3), {}))
def test_cache_http_errors(self): settings = opener_settings.OpenerSettings() settings.build_openers() opener = settings.get_custom_opener() url = URL('http://w3af.org/foo-bar-not-exists.htm') request = HTTPRequest(url, cache=False) with patch('w3af.core.data.url.handlers.cache.CacheClass') as cc_mock: store_in_cache = Mock() cc_mock.attach_mock(store_in_cache, 'store_in_cache') # If there is a response we should store it, even if it is a 404 try: response = opener.open(request) except urllib2.HTTPError: pass # Make sure the right call was made _call = _Call(('store_in_cache', (request, response))) self.assertEqual(cc_mock.mock_calls, [_call]) cc_mock.reset_mock() # And make sure the response was a 404 self.assertEqual(response.status, 404)
def test_connect(self): data = "\x80\x00\x1a\x10\x00\x20\x00\x46\x00\x13\xbb\x58\x2b\x41\x42\x0c\x11\xdb\xb0\xde\x08\x00\x20\x0c\x9a\x66" connect = PyOBEX.requests.Connect() connect.read_data(data) self._mns.connect(self._socket, connect) self.assertIs(connect, self._mns.remote_info) expected = _Call(((), (b'\xa0\x00\x07\x10\x00\x20\x00',),{})) self.assertEqual(expected, self._socket.sendall.call_args)
def test_connect(self): data = "\x80\x00\x1a\x10\x00\x20\x00\x46\x00\x13\xbb\x58\x2b\x41\x42\x0c\x11\xdb\xb0\xde\x08\x00\x20\x0c\x9a\x66" connect = PyOBEX.requests.Connect() connect.read_data(data) self._mns.connect(self._socket, connect) self.assertIs(connect, self._mns.remote_info) expected = _Call(((), (b'\xa0\x00\x07\x10\x00\x20\x00', ), {})) self.assertEqual(expected, self._socket.sendall.call_args)
def expect(self, *args, **kwargs): expector = Expector() self._expected_calls[repr(mock.call(args, kwargs))] = (expector, mock._Call( (args, kwargs), two=True)) return expector
def test_disconnect(self): self._mns.connected = True disconnect = PyOBEX.requests.Disconnect() self._mns.disconnect(self._socket, disconnect) print self._socket.mock_calls self.assertFalse(self._mns.connected) self.assertTrue(self._socket.sendall.called) expected = _Call(((), (b'\xa0\x00\x03', ), {})) self.assertEqual(expected, self._socket.sendall.call_args)
def test_disconnect(self): self._mns.connected = True disconnect = PyOBEX.requests.Disconnect() self._mns.disconnect(self._socket, disconnect) print self._socket.mock_calls self.assertFalse(self._mns.connected) self.assertTrue(self._socket.sendall.called) expected = _Call(((), (b'\xa0\x00\x03',),{})) self.assertEqual(expected, self._socket.sendall.call_args)
def test_empty__Call(self): args = _Call() self.assertEqual(args, ()) self.assertEqual(args, ('foo', )) self.assertEqual(args, ((), )) self.assertEqual(args, ('foo', ())) self.assertEqual(args, ('foo', (), {})) self.assertEqual(args, ('foo', {})) self.assertEqual(args, ({}, ))
def test_basic(self): url = URL('http://www.w3af.org') request = HTTPRequest(url, cache=True) cache = CacheHandler() self.assertEqual(cache.default_open(request), None) response = FakeHttplibHTTPResponse(200, 'OK', 'spameggs', Headers(), url.url_string) with patch('w3af.core.data.url.handlers.cache.CacheClass') as cc_mock: store_in_cache = Mock() cc_mock.attach_mock(store_in_cache, 'store_in_cache') # This stores the response cache.http_response(request, response) # Make sure the right call was made _call = _Call(('store_in_cache', (request, response))) self.assertEqual(cc_mock.mock_calls, [_call]) cc_mock.reset_mock() exists_in_cache = Mock() cc_mock.return_value = response cc_mock.attach_mock(exists_in_cache, 'exists_in_cache') # This retrieves the response from the "cache" cached_response = cache.default_open(request) # Make sure the right call was made _exists_call = _Call(('exists_in_cache', (request, ))) _retrieve_call = _Call(((request, ), {})) self.assertEqual(cc_mock.mock_calls, [_exists_call, _retrieve_call]) self.assertIsNotNone(cached_response) self.assertEqual(cached_response.code, response.code) self.assertEqual(cached_response.msg, response.msg) self.assertEqual(cached_response.read(), response.read()) self.assertEqual(Headers(cached_response.info().items()), response.info()) self.assertEqual(cached_response.geturl(), response.geturl())
def test_empty__Call(self): args = _Call() self.assertEqual(args, ()) self.assertEqual(args, ('foo',)) self.assertEqual(args, ((),)) self.assertEqual(args, ('foo', ())) self.assertEqual(args, ('foo',(), {})) self.assertEqual(args, ('foo', {})) self.assertEqual(args, ({},))
def test_basic(self): url = URL('http://www.w3af.org') request = HTTPRequest(url, cache=True) cache = CacheHandler() self.assertEqual(cache.default_open(request), None) response = FakeHttplibHTTPResponse(200, 'OK', 'spameggs', Headers(), url.url_string) with patch('w3af.core.data.url.handlers.cache.CacheClass') as cc_mock: store_in_cache = Mock() cc_mock.attach_mock(store_in_cache, 'store_in_cache') # This stores the response cache.http_response(request, response) # Make sure the right call was made _call = _Call(('store_in_cache', (request, response))) self.assertEqual(cc_mock.mock_calls, [_call]) cc_mock.reset_mock() exists_in_cache = Mock() cc_mock.return_value = response cc_mock.attach_mock(exists_in_cache, 'exists_in_cache') # This retrieves the response from the "cache" cached_response = cache.default_open(request) # Make sure the right call was made _exists_call = _Call(('exists_in_cache', (request,))) _retrieve_call = _Call(((request,), {})) self.assertEqual(cc_mock.mock_calls, [_exists_call, _retrieve_call]) self.assertIsNotNone(cached_response) self.assertEqual(cached_response.code, response.code) self.assertEqual(cached_response.msg, response.msg) self.assertEqual(cached_response.read(), response.read()) self.assertEqual(Headers(cached_response.info().items()), response.info()) self.assertEqual(cached_response.geturl(), response.geturl())
def test_named_empty_call(self): args = _Call(('foo', (), {})) self.assertEqual(args, ('foo', )) self.assertEqual(args, ('foo', ())) self.assertEqual(args, ('foo', (), {})) self.assertEqual(args, ('foo', {})) self.assertNotEqual(args, ((), )) self.assertNotEqual(args, ()) self.assertNotEqual(args, ({}, )) self.assertNotEqual(args, ('bar', )) self.assertNotEqual(args, ('bar', ())) self.assertNotEqual(args, ('bar', {}))
def test_named_empty_call(self): args = _Call(('foo', (), {})) self.assertEqual(args, ('foo',)) self.assertEqual(args, ('foo', ())) self.assertEqual(args, ('foo',(), {})) self.assertEqual(args, ('foo', {})) self.assertNotEqual(args, ((),)) self.assertNotEqual(args, ()) self.assertNotEqual(args, ({},)) self.assertNotEqual(args, ('bar',)) self.assertNotEqual(args, ('bar', ())) self.assertNotEqual(args, ('bar', {}))
def test_send_event_wrong_type(self): send_event = PyOBEX.requests.Put() send_event.add_header(PyOBEX.headers.Type("x-bt/wrong-type"), 50) self._mns.put(self._socket, send_event) expected = _Call(((), (b'\xc3\x00\x03', ), {})) self.assertEqual(expected, self._socket.sendall.call_args)
def test_call_with_call(self): kall = _Call() self.assertEqual(kall, _Call()) self.assertEqual(kall, _Call(('', ))) self.assertEqual(kall, _Call(((), ))) self.assertEqual(kall, _Call(({}, ))) self.assertEqual(kall, _Call(('', ()))) self.assertEqual(kall, _Call(('', {}))) self.assertEqual(kall, _Call(('', (), {}))) self.assertEqual(kall, _Call(('foo', ))) self.assertEqual(kall, _Call(('bar', ()))) self.assertEqual(kall, _Call(('baz', {}))) self.assertEqual(kall, _Call(('spam', (), {}))) kall = _Call(((1, 2, 3), )) self.assertEqual(kall, _Call(((1, 2, 3), ))) self.assertEqual(kall, _Call(('', (1, 2, 3)))) self.assertEqual(kall, _Call(((1, 2, 3), {}))) self.assertEqual(kall, _Call(('', (1, 2, 3), {}))) kall = _Call(((1, 2, 4), )) self.assertNotEqual(kall, _Call(('', (1, 2, 3)))) self.assertNotEqual(kall, _Call(('', (1, 2, 3), {}))) kall = _Call(( 'foo', (1, 2, 4), )) self.assertNotEqual(kall, _Call(('', (1, 2, 4)))) self.assertNotEqual(kall, _Call(('', (1, 2, 4), {}))) self.assertNotEqual(kall, _Call(('bar', (1, 2, 4)))) self.assertNotEqual(kall, _Call(('bar', (1, 2, 4), {}))) kall = _Call(({'a': 3}, )) self.assertEqual(kall, _Call(('', (), {'a': 3}))) self.assertEqual(kall, _Call(('', {'a': 3}))) self.assertEqual(kall, _Call(((), {'a': 3}))) self.assertEqual(kall, _Call(({'a': 3}, )))
def __new__(self, name, *args, **kwargs): return mock._Call( (name, args, kwargs) )
def test_call_non_tuples(self): kall = _Call(((1, 2, 3), )) for value in 1, None, self, int: self.assertNotEqual(kall, value) self.assertFalse(kall == value)
def test_call_with_args_call_empty_name(self): args = _Call(((1, 2, 3), {})) self.assertEqual(args, call(1, 2, 3)) self.assertEqual(call(1, 2, 3), args) self.assertTrue(call(1, 2, 3) in [args])
def test_call_ne(self): self.assertNotEqual(_Call(((1, 2, 3), )), call(1, 2)) self.assertFalse(_Call(((1, 2, 3), )) != call(1, 2, 3)) self.assertTrue(_Call(((1, 2), {})) != call(1, 2, 3))
def test_call_with_call(self): kall = _Call() self.assertEqual(kall, _Call()) self.assertEqual(kall, _Call(('',))) self.assertEqual(kall, _Call(((),))) self.assertEqual(kall, _Call(({},))) self.assertEqual(kall, _Call(('', ()))) self.assertEqual(kall, _Call(('', {}))) self.assertEqual(kall, _Call(('', (), {}))) self.assertEqual(kall, _Call(('foo',))) self.assertEqual(kall, _Call(('bar', ()))) self.assertEqual(kall, _Call(('baz', {}))) self.assertEqual(kall, _Call(('spam', (), {}))) kall = _Call(((1, 2, 3),)) self.assertEqual(kall, _Call(((1, 2, 3),))) self.assertEqual(kall, _Call(('', (1, 2, 3)))) self.assertEqual(kall, _Call(((1, 2, 3), {}))) self.assertEqual(kall, _Call(('', (1, 2, 3), {}))) kall = _Call(((1, 2, 4),)) self.assertNotEqual(kall, _Call(('', (1, 2, 3)))) self.assertNotEqual(kall, _Call(('', (1, 2, 3), {}))) kall = _Call(('foo', (1, 2, 4),)) self.assertNotEqual(kall, _Call(('', (1, 2, 4)))) self.assertNotEqual(kall, _Call(('', (1, 2, 4), {}))) self.assertNotEqual(kall, _Call(('bar', (1, 2, 4)))) self.assertNotEqual(kall, _Call(('bar', (1, 2, 4), {}))) kall = _Call(({'a': 3},)) self.assertEqual(kall, _Call(('', (), {'a': 3}))) self.assertEqual(kall, _Call(('', {'a': 3}))) self.assertEqual(kall, _Call(((), {'a': 3}))) self.assertEqual(kall, _Call(({'a': 3},)))
def _mock_call(_mock_self, *args, **kwargs): self = _mock_self self.called = True self.call_count += 1 self.call_args = mock._Call((args, kwargs), two=True) self.call_args_list.append(mock._Call((args, kwargs), two=True)) _new_name = self._mock_new_name _new_parent = self._mock_new_parent self.mock_calls.append(mock._Call(('', args, kwargs))) seen = set() skip_next_dot = _new_name == '()' do_method_calls = self._mock_parent is not None name = self._mock_name while _new_parent is not None: this_mock_call = mock._Call((_new_name, args, kwargs)) if _new_parent._mock_new_name: dot = '.' if skip_next_dot: dot = '' skip_next_dot = False if _new_parent._mock_new_name == '()': skip_next_dot = True _new_name = _new_parent._mock_new_name + dot + _new_name if do_method_calls: if _new_name == name: this_method_call = this_mock_call else: this_method_call = mock._Call(name, args, kwargs) _new_parent.method_calls.append(this_method_call) do_method_calls = _new_parent._mock_parent is not None if do_method_calls: name = _new_parent._mock_name + '.' + name _new_parent.mock_calls.append(this_mock_call) _new_parent = _new_parent._mock_new_parent # use ids here so as not to call __hash__ on the mocks _new_parent_id = id(_new_parent) if _new_parent_id in seen: break seen.add(_new_parent_id) if repr(mock.call(args, kwargs)) in self._expected_calls: expector, call_args = self._expected_calls[repr(mock.call(args, kwargs))] if expector._return_exception: raise expector._return_exception else: return expector._return_value ret_val = mock.DEFAULT effect = self.side_effect if effect is not None: if mock._is_exception(effect): raise effect if not mock._callable(effect): return next(effect) ret_val = effect(*args, **kwargs) if ret_val is mock.DEFAULT: ret_val = self.return_value if (self._mock_wraps is not None and self._mock_return_value is mock.DEFAULT): return self._mock_wraps(*args, **kwargs) if ret_val is mock.DEFAULT: ret_val = self.return_value return ret_val
def test_create_destroy_rpc_namespace_proxy(self): self.agent.init_host() self.agent.after_start() ctxt = context.Context('user', 'tenant') with mock.patch('oslo.messaging.proxy.get_proxy_server' ) as mock_get_proxy_server: mock_proxy_server = mock_get_proxy_server.return_value src_unix_target_send = ('topic=src_topic_send,' 'server=src_server_send') dst_transport_url_send = 'fake:///' dst_target_send = 'topic=dst_topic_send,server=dst_server_send' ns_proxy_id_send = self.agent.create_rpc_namespace_proxy( ctxt, src_unix_target_send, dst_transport_url_send, dst_target_send, 'send') src_unix_target_send = target.target_parse(src_unix_target_send) dst_target_send = target.target_parse(dst_target_send) mock_get_proxy_server.assert_called_once_with( self.mock_transport, src_unix_target_send, None, self.mock_transport, dst_target_send, None, executor=mock.ANY) # mock_proxy_server.start.assert_called_once_with() src_unix_target_recv = ('topic=src_topic_recv,' 'server=src_server_recv') dst_transport_url_recv = 'fake:///' dst_target_recv = 'topic=dst_topic_recv,server=dst_server_recv' self.agent.create_rpc_namespace_proxy( ctxt, src_unix_target_recv, dst_transport_url_recv, dst_target_recv, 'receive') src_unix_target_recv = target.target_parse(src_unix_target_recv) dst_target_recv = target.target_parse(dst_target_recv) # mock.call().__hash__()/mock.call.__hash__() doesn't work # due to __getattr__. So create it manually call_hash = mock._Call(name='().__hash__') mock_get_proxy_server.assert_has_calls([ mock.call(self.mock_transport, src_unix_target_send, None, self.mock_transport, dst_target_send, None, executor=mock.ANY), # mock.call().start(), call_hash(), mock.call(self.mock_transport, dst_target_recv, None, self.mock_transport, src_unix_target_recv, None, executor=mock.ANY), # mock.call().start(), call_hash()]) self.agent.destroy_rpc_namespace_proxy(ctxt, ns_proxy_id_send) mock_proxy_server.stop.assert_called_once_with() mock_proxy_server.wait.assert_called_once_with() self.agent.destroy_namespace_agent(ctxt) self.agent.wait() call_hash = mock._Call(name='__hash__') mock_proxy_server.assert_has_calls([ # mock.call.start(), call_hash(), # mock.call.start(), call_hash(), call_hash(), mock.call.stop(), mock.call.wait(), mock.call.stop(), mock.call.wait()]) self.mock_transport.cleanup.assert_called_once_with()
def _mock_call(_mock_self, *args, **kwargs): self = _mock_self self.called = True self.call_count += 1 self.call_args = mock._Call((args, kwargs), two=True) self.call_args_list.append(mock._Call((args, kwargs), two=True)) _new_name = self._mock_new_name _new_parent = self._mock_new_parent self.mock_calls.append(mock._Call(('', args, kwargs))) seen = set() skip_next_dot = _new_name == '()' do_method_calls = self._mock_parent is not None name = self._mock_name while _new_parent is not None: this_mock_call = mock._Call((_new_name, args, kwargs)) if _new_parent._mock_new_name: dot = '.' if skip_next_dot: dot = '' skip_next_dot = False if _new_parent._mock_new_name == '()': skip_next_dot = True _new_name = _new_parent._mock_new_name + dot + _new_name if do_method_calls: if _new_name == name: this_method_call = this_mock_call else: this_method_call = mock._Call(name, args, kwargs) _new_parent.method_calls.append(this_method_call) do_method_calls = _new_parent._mock_parent is not None if do_method_calls: name = _new_parent._mock_name + '.' + name _new_parent.mock_calls.append(this_mock_call) _new_parent = _new_parent._mock_new_parent # use ids here so as not to call __hash__ on the mocks _new_parent_id = id(_new_parent) if _new_parent_id in seen: break seen.add(_new_parent_id) if repr(mock.call(args, kwargs)) in self._expected_calls: expector, call_args = self._expected_calls[repr( mock.call(args, kwargs))] if expector._return_exception: raise expector._return_exception else: return expector._return_value ret_val = mock.DEFAULT effect = self.side_effect if effect is not None: if mock._is_exception(effect): raise effect if not mock._callable(effect): return next(effect) ret_val = effect(*args, **kwargs) if ret_val is mock.DEFAULT: ret_val = self.return_value if (self._mock_wraps is not None and self._mock_return_value is mock.DEFAULT): return self._mock_wraps(*args, **kwargs) if ret_val is mock.DEFAULT: ret_val = self.return_value return ret_val
def test_send_event_wrong_type(self): send_event = PyOBEX.requests.Put() send_event.add_header(PyOBEX.headers.Type("x-bt/wrong-type"), 50) self._mns.put(self._socket, send_event) expected = _Call(((), (b'\xc3\x00\x03',),{})) self.assertEqual(expected, self._socket.sendall.call_args)
def expect(self, *args, **kwargs): expector = Expector() self._expected_calls[repr(mock.call(args, kwargs))] = (expector, mock._Call((args, kwargs), two=True)) return expector
def test_call_ne(self): self.assertNotEqual(_Call(((1, 2, 3),)), call(1, 2)) self.assertFalse(_Call(((1, 2, 3),)) != call(1, 2, 3)) self.assertTrue(_Call(((1, 2), {})) != call(1, 2, 3))
def test_call_non_tuples(self): kall = _Call(((1, 2, 3),)) for value in 1, None, self, int: self.assertNotEqual(kall, value) self.assertFalse(kall == value)