Skip to content

Commit

Permalink
import unittest.mock -> from unittest import mock
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jul 21, 2016
1 parent ad13b7f commit 80d1d1c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 66 deletions.
42 changes: 21 additions & 21 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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',
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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)
Expand All @@ -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()
Expand Down
36 changes: 18 additions & 18 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio
import gc
import unittest
import unittest.mock
from unittest import mock

import aiohttp
from aiohttp import helpers
Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -90,15 +90,15 @@ 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())
self.assertEqual(res, b'payload')
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)

Expand All @@ -110,15 +110,15 @@ 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())
self.assertIsNone(self.response._connection)

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')

Expand All @@ -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())
Expand All @@ -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'))
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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'}
Expand All @@ -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'))
Expand All @@ -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())
Expand All @@ -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}

Expand Down
12 changes: 6 additions & 6 deletions tests/test_flowcontrol_streams.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -61,15 +61,15 @@ 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)
self.assertTrue(stream.transport.resume_reading.called)
self.assertFalse(stream.paused)

def test_no_transport_in_init(self):
stream = unittest.mock.Mock()
stream = mock.Mock()
stream.paused = True
stream.transport = None

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
Loading

0 comments on commit 80d1d1c

Please sign in to comment.