diff --git a/tests/test_client_request.py b/tests/test_client_request.py index c354525dbf3..53fc577a51c 100644 --- a/tests/test_client_request.py +++ b/tests/test_client_request.py @@ -6,7 +6,7 @@ import io import re import unittest -import unittest.mock +from unittest import mock import urllib.parse import zlib from http.cookies import SimpleCookie @@ -403,9 +403,9 @@ def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) - self.transport = unittest.mock.Mock() - self.connection = unittest.mock.Mock() - self.protocol = unittest.mock.Mock() + self.transport = mock.Mock() + self.connection = mock.Mock() + self.protocol = mock.Mock() self.protocol.writer.drain.return_value = () self.stream = aiohttp.StreamParser(loop=self.loop) self.connector = BaseConnector(loop=self.loop) @@ -493,7 +493,7 @@ def test_post_data(self): self.loop.run_until_complete(req.close()) resp.close() - @unittest.mock.patch( + @mock.patch( 'aiohttp.client_reqrep.ClientRequest.update_body_from_data') def test_pass_falsy_data(self, _): req = ClientRequest( @@ -524,7 +524,7 @@ def test_bytes_data(self): self.loop.run_until_complete(req.close()) resp.close() - @unittest.mock.patch('aiohttp.client_reqrep.aiohttp') + @mock.patch('aiohttp.client_reqrep.aiohttp') def test_content_encoding(self, m_http): req = ClientRequest('get', 'http://python.org/', data='foo', compress='deflate', loop=self.loop) @@ -536,7 +536,7 @@ def test_content_encoding(self, m_http): self.loop.run_until_complete(req.close()) resp.close() - @unittest.mock.patch('aiohttp.client_reqrep.aiohttp') + @mock.patch('aiohttp.client_reqrep.aiohttp') def test_content_encoding_dont_set_headers_if_no_body(self, m_http): req = ClientRequest('get', 'http://python.org/', compress='deflate', loop=self.loop) @@ -546,7 +546,7 @@ def test_content_encoding_dont_set_headers_if_no_body(self, m_http): self.loop.run_until_complete(req.close()) resp.close() - @unittest.mock.patch('aiohttp.client_reqrep.aiohttp') + @mock.patch('aiohttp.client_reqrep.aiohttp') def test_content_encoding_header(self, m_http): req = ClientRequest( 'get', 'http://python.org/', data='foo', @@ -579,7 +579,7 @@ def test_chunked(self): self.loop.run_until_complete(req.close()) resp.close() - @unittest.mock.patch('aiohttp.client_reqrep.aiohttp') + @mock.patch('aiohttp.client_reqrep.aiohttp') def test_chunked_explicit(self, m_http): req = ClientRequest( 'get', 'http://python.org/', chunked=True, loop=self.loop) @@ -591,7 +591,7 @@ def test_chunked_explicit(self, m_http): self.loop.run_until_complete(req.close()) resp.close() - @unittest.mock.patch('aiohttp.client_reqrep.aiohttp') + @mock.patch('aiohttp.client_reqrep.aiohttp') def test_chunked_explicit_size(self, m_http): req = ClientRequest( 'get', 'http://python.org/', chunked=1024, loop=self.loop) @@ -700,8 +700,8 @@ def gen(): self.assertIsNone(req._writer) self.assertEqual( self.transport.write.mock_calls[-2:], - [unittest.mock.call(b'12\r\nbinary data result\r\n'), - unittest.mock.call(b'0\r\n\r\n')]) + [mock.call(b'12\r\nbinary data result\r\n'), + mock.call(b'0\r\n\r\n')]) self.loop.run_until_complete(req.close()) def test_data_file(self): @@ -719,8 +719,8 @@ def test_data_file(self): self.assertIsNone(req._writer) self.assertEqual( self.transport.write.mock_calls[-2:], - [unittest.mock.call(b'2\r\n' + b'*' * 2 + b'\r\n'), - unittest.mock.call(b'0\r\n\r\n')]) + [mock.call(b'2\r\n' + b'*' * 2 + b'\r\n'), + mock.call(b'0\r\n\r\n')]) self.loop.run_until_complete(req.close()) def test_data_stream_exc(self): @@ -813,8 +813,8 @@ def coro(): self.loop.run_until_complete(req._writer) self.assertEqual( self.transport.write.mock_calls[-2:], - [unittest.mock.call(b'12\r\nbinary data result\r\n'), - unittest.mock.call(b'0\r\n\r\n')]) + [mock.call(b'12\r\nbinary data result\r\n'), + mock.call(b'0\r\n\r\n')]) self.loop.run_until_complete(req.close()) resp.close() @@ -835,7 +835,7 @@ def coro(): self.loop.run_until_complete(req._writer) self.assertEqual( self.transport.write.mock_calls[-1], - unittest.mock.call(b'data')) + mock.call(b'data')) self.loop.run_until_complete(req.close()) resp.close() @@ -851,8 +851,8 @@ def gen(): self.loop.run_until_complete(req.close()) self.assertEqual( self.transport.write.mock_calls[-2:], - [unittest.mock.call(b'6\r\nresult\r\n'), - unittest.mock.call(b'0\r\n\r\n')]) + [mock.call(b'6\r\nresult\r\n'), + mock.call(b'0\r\n\r\n')]) self.loop.run_until_complete(req.close()) resp.close() @@ -873,7 +873,7 @@ def test_terminate(self): req = ClientRequest('get', 'http://python.org', loop=self.loop) resp = req.send(self.transport, self.protocol) self.assertIsNotNone(req._writer) - writer = req._writer = unittest.mock.Mock() + writer = req._writer = mock.Mock() req.terminate() self.assertIsNone(req._writer) @@ -886,7 +886,7 @@ def test_terminate_with_closed_loop(self): req = ClientRequest('get', 'http://python.org', loop=self.loop) resp = req.send(self.transport, self.protocol) self.assertIsNotNone(req._writer) - writer = req._writer = unittest.mock.Mock() + writer = req._writer = mock.Mock() self.loop.close() req.terminate() diff --git a/tests/test_client_response.py b/tests/test_client_response.py index 99458e8b6a5..cc25f07ebaa 100644 --- a/tests/test_client_response.py +++ b/tests/test_client_response.py @@ -4,7 +4,7 @@ import asyncio import gc import unittest -import unittest.mock +from unittest import mock import aiohttp from aiohttp import helpers @@ -17,7 +17,7 @@ def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) - self.connection = unittest.mock.Mock() + self.connection = mock.Mock() self.stream = aiohttp.StreamParser(loop=self.loop) self.response = ClientResponse('get', 'http://def-cl-resp.org') self.response._post_init(self.loop) @@ -32,7 +32,7 @@ def test_del(self): response = ClientResponse('get', 'http://del-cl-resp.org') response._post_init(self.loop) - connection = unittest.mock.Mock() + connection = mock.Mock() response._setup_connection(connection) self.loop.set_exception_handler(lambda loop, ctx: None) @@ -90,7 +90,7 @@ def side_effect(*args, **kwargs): fut = helpers.create_future(self.loop) fut.set_result(b'payload') return fut - content = self.response.content = unittest.mock.Mock() + content = self.response.content = mock.Mock() content.read.side_effect = side_effect res = self.loop.run_until_complete(self.response.read()) @@ -98,7 +98,7 @@ def side_effect(*args, **kwargs): self.assertIsNone(self.response._connection) def test_read_and_release_connection_with_error(self): - content = self.response.content = unittest.mock.Mock() + content = self.response.content = mock.Mock() content.read.return_value = helpers.create_future(self.loop) content.read.return_value.set_exception(ValueError) @@ -110,7 +110,7 @@ def test_read_and_release_connection_with_error(self): def test_release(self): fut = helpers.create_future(self.loop) fut.set_result(b'') - content = self.response.content = unittest.mock.Mock() + content = self.response.content = mock.Mock() content.readany.return_value = fut self.loop.run_until_complete(self.response.release()) @@ -118,7 +118,7 @@ def test_release(self): def test_read_decode_deprecated(self): self.response._content = b'data' - self.response.json = unittest.mock.Mock() + self.response.json = mock.Mock() self.response.json.return_value = helpers.create_future(self.loop) self.response.json.return_value.set_result('json') @@ -134,7 +134,7 @@ def side_effect(*args, **kwargs): return fut self.response.headers = { 'CONTENT-TYPE': 'application/json;charset=cp1251'} - content = self.response.content = unittest.mock.Mock() + content = self.response.content = mock.Mock() content.read.side_effect = side_effect res = self.loop.run_until_complete(self.response.text()) @@ -148,9 +148,9 @@ def side_effect(*args, **kwargs): return fut self.response.headers = { 'CONTENT-TYPE': 'application/json'} - content = self.response.content = unittest.mock.Mock() + content = self.response.content = mock.Mock() content.read.side_effect = side_effect - self.response._get_encoding = unittest.mock.Mock() + self.response._get_encoding = mock.Mock() res = self.loop.run_until_complete( self.response.text(encoding='cp1251')) @@ -164,7 +164,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut self.response.headers = {'CONTENT-TYPE': 'application/json'} - content = self.response.content = unittest.mock.Mock() + content = self.response.content = mock.Mock() content.read.side_effect = side_effect self.loop.run_until_complete(self.response.read()) @@ -179,7 +179,7 @@ def side_effect(*args, **kwargs): return fut self.response.headers = { 'CONTENT-TYPE': 'application/json;charset=cp1251'} - content = self.response.content = unittest.mock.Mock() + content = self.response.content = mock.Mock() content.read.side_effect = side_effect res = self.loop.run_until_complete(self.response.text()) @@ -193,7 +193,7 @@ def side_effect(*args, **kwargs): return fut self.response.headers = { 'CONTENT-TYPE': 'application/json;charset=cp1251'} - content = self.response.content = unittest.mock.Mock() + content = self.response.content = mock.Mock() content.read.side_effect = side_effect res = self.loop.run_until_complete(self.response.json()) @@ -211,7 +211,7 @@ def custom(content): res = self.loop.run_until_complete(self.response.json(loads=custom)) self.assertEqual(res, 'data-custom') - @unittest.mock.patch('aiohttp.client_reqrep.client_logger') + @mock.patch('aiohttp.client_reqrep.client_logger') def test_json_no_content(self, m_log): self.response.headers = { 'CONTENT-TYPE': 'data/octet-stream'} @@ -230,9 +230,9 @@ def side_effect(*args, **kwargs): return fut self.response.headers = { 'CONTENT-TYPE': 'application/json;charset=utf8'} - content = self.response.content = unittest.mock.Mock() + content = self.response.content = mock.Mock() content.read.side_effect = side_effect - self.response._get_encoding = unittest.mock.Mock() + self.response._get_encoding = mock.Mock() res = self.loop.run_until_complete( self.response.json(encoding='cp1251')) @@ -246,7 +246,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut self.response.headers = {'CONTENT-TYPE': 'application/json'} - content = self.response.content = unittest.mock.Mock() + content = self.response.content = mock.Mock() content.read.side_effect = side_effect res = self.loop.run_until_complete(self.response.json()) @@ -262,7 +262,7 @@ class MyResponse(ClientResponse): self.assertIsInstance(response.content, aiohttp.FlowControlDataQueue) response.close() - @unittest.mock.patch('aiohttp.client_reqrep.chardet') + @mock.patch('aiohttp.client_reqrep.chardet') def test_get_encoding_unknown(self, m_chardet): m_chardet.detect.return_value = {'encoding': None} diff --git a/tests/test_flowcontrol_streams.py b/tests/test_flowcontrol_streams.py index e5c45dc40c6..f7a54030ced 100644 --- a/tests/test_flowcontrol_streams.py +++ b/tests/test_flowcontrol_streams.py @@ -1,13 +1,13 @@ import asyncio import unittest -import unittest.mock +from unittest import mock from aiohttp import streams class TestFlowControlStreamReader(unittest.TestCase): def setUp(self): - self.stream = unittest.mock.Mock() + self.stream = mock.Mock() self.transp = self.stream.transport self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) @@ -61,7 +61,7 @@ def test_feed_data(self): class FlowControlMixin: def test_resume_on_init(self): - stream = unittest.mock.Mock() + stream = mock.Mock() stream.paused = True streams.FlowControlDataQueue(stream, limit=1, loop=self.loop) @@ -69,7 +69,7 @@ def test_resume_on_init(self): self.assertFalse(stream.paused) def test_no_transport_in_init(self): - stream = unittest.mock.Mock() + stream = mock.Mock() stream.paused = True stream.transport = None @@ -187,7 +187,7 @@ def test_no_pause_on_read_no_transport(self): class TestFlowControlDataQueue(unittest.TestCase, FlowControlMixin): def setUp(self): - self.stream = unittest.mock.Mock() + self.stream = mock.Mock() self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) @@ -202,7 +202,7 @@ def _make_one(self, *args, **kwargs): class TestFlowControlChunksQueue(unittest.TestCase, FlowControlMixin): def setUp(self): - self.stream = unittest.mock.Mock() + self.stream = mock.Mock() self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) diff --git a/tests/test_http_parser.py b/tests/test_http_parser.py index a4ec09f948f..66193fd93c1 100644 --- a/tests/test_http_parser.py +++ b/tests/test_http_parser.py @@ -3,7 +3,7 @@ import asyncio import zlib import unittest -import unittest.mock +from unittest import mock import aiohttp from aiohttp import errors @@ -110,14 +110,14 @@ def test_invalid_name(self): class TestDeflateBuffer(unittest.TestCase): def setUp(self): - self.stream = unittest.mock.Mock() + self.stream = mock.Mock() asyncio.set_event_loop(None) def test_feed_data(self): buf = aiohttp.FlowControlDataQueue(self.stream) dbuf = protocol.DeflateBuffer(buf, 'deflate') - dbuf.zlib = unittest.mock.Mock() + dbuf.zlib = mock.Mock() dbuf.zlib.decompress.return_value = b'line' dbuf.feed_data(b'data', 4) @@ -128,7 +128,7 @@ def test_feed_data_err(self): dbuf = protocol.DeflateBuffer(buf, 'deflate') exc = ValueError() - dbuf.zlib = unittest.mock.Mock() + dbuf.zlib = mock.Mock() dbuf.zlib.decompress.side_effect = exc self.assertRaises( @@ -138,7 +138,7 @@ def test_feed_eof(self): buf = aiohttp.FlowControlDataQueue(self.stream) dbuf = protocol.DeflateBuffer(buf, 'deflate') - dbuf.zlib = unittest.mock.Mock() + dbuf.zlib = mock.Mock() dbuf.zlib.flush.return_value = b'line' dbuf.feed_eof() @@ -149,7 +149,7 @@ def test_feed_eof_err(self): buf = aiohttp.FlowControlDataQueue(self.stream) dbuf = protocol.DeflateBuffer(buf, 'deflate') - dbuf.zlib = unittest.mock.Mock() + dbuf.zlib = mock.Mock() dbuf.zlib.flush.return_value = b'line' dbuf.zlib.eof = False @@ -159,7 +159,7 @@ def test_feed_eof_err(self): class TestParsePayload(unittest.TestCase): def setUp(self): - self.stream = unittest.mock.Mock() + self.stream = mock.Mock() asyncio.set_event_loop(None) def test_parse_eof_payload(self): @@ -386,7 +386,7 @@ def test_http_payload_parser_length_zero(self): class TestParseRequest(unittest.TestCase): def setUp(self): - self.stream = unittest.mock.Mock() + self.stream = mock.Mock() asyncio.set_event_loop(None) def test_http_request_parser_max_headers(self): @@ -509,7 +509,7 @@ def test_http_request_parser_bad_version(self): class TestParseResponse(unittest.TestCase): def setUp(self): - self.stream = unittest.mock.Mock() + self.stream = mock.Mock() asyncio.set_event_loop(None) def test_http_response_parser_utf8(self): diff --git a/tests/test_multipart.py b/tests/test_multipart.py index b5310f0776a..d31e9b81421 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -3,7 +3,7 @@ import io import os import unittest -import unittest.mock as mock +from unittest import mock import zlib import aiohttp.multipart diff --git a/tests/test_wsgi.py b/tests/test_wsgi.py index e754cd833e6..79162817770 100644 --- a/tests/test_wsgi.py +++ b/tests/test_wsgi.py @@ -4,7 +4,7 @@ import asyncio import socket import unittest -import unittest.mock +from unittest import mock import aiohttp from aiohttp import multidict @@ -19,13 +19,13 @@ def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) - self.wsgi = unittest.mock.Mock() - self.reader = unittest.mock.Mock() - self.writer = unittest.mock.Mock() + self.wsgi = mock.Mock() + self.reader = mock.Mock() + self.writer = mock.Mock() self.writer.drain.return_value = () - self.transport = unittest.mock.Mock() + self.transport = mock.Mock() self.transport.get_extra_info.side_effect = [ - unittest.mock.Mock(family=socket.AF_INET), + mock.Mock(family=socket.AF_INET), ('1.2.3.4', 1234), ('2.3.4.5', 80)] @@ -84,8 +84,8 @@ def test_environ_headers(self): self.assertEqual(environ['SERVER_PORT'], '80') get_extra_info_calls = self.transport.get_extra_info.mock_calls expected_calls = [ - unittest.mock.call('socket'), - unittest.mock.call('peername'), + mock.call('socket'), + mock.call('peername'), ] self.assertEqual(expected_calls, get_extra_info_calls) @@ -130,7 +130,7 @@ def test_wsgi_response_start_response_exc_status(self): resp.start_response, '500 Err', [('CONTENT-TYPE', 'text/plain')], ['', ValueError()]) - @unittest.mock.patch('aiohttp.wsgi.aiohttp') + @mock.patch('aiohttp.wsgi.aiohttp') def test_wsgi_response_101_upgrade_to_websocket(self, m_asyncio): srv = self._make_srv() resp = srv.create_wsgi_response(self.message) @@ -282,7 +282,7 @@ def test_http_1_0_no_host(self): def test_family_inet6(self): self.transport.get_extra_info.side_effect = [ - unittest.mock.Mock(family=socket.AF_INET6), + mock.Mock(family=socket.AF_INET6), ("::", 1122, 0, 0), ('2.3.4.5', 80)] self.message = protocol.RawRequestMessage( @@ -298,7 +298,7 @@ def test_family_unix(self): if not hasattr(socket, "AF_UNIX"): self.skipTest("No UNIX address family. (Windows?)") self.transport.get_extra_info.side_effect = [ - unittest.mock.Mock(family=socket.AF_UNIX)] + mock.Mock(family=socket.AF_UNIX)] headers = multidict.MultiDict({ 'SERVER_NAME': '1.2.3.4', 'SERVER_PORT': '5678', 'REMOTE_ADDR': '4.3.2.1', 'REMOTE_PORT': '8765'})