Kaydet (Commit) 24ba2035 authored tarafından Victor Stinner's avatar Victor Stinner

asyncio: Replace "unittest.mock" with "mock" in unit tests

Use "from unittest import mock". It should simplify my work to merge new tests
in Trollius, because Trollius uses "mock" backport for Python 2.
üst 71ec82a5
......@@ -12,7 +12,7 @@ import tempfile
import threading
import time
import unittest
import unittest.mock
from unittest import mock
from http.server import HTTPServer
from wsgiref.simple_server import WSGIRequestHandler, WSGIServer
......@@ -362,7 +362,7 @@ class TestLoop(base_events.BaseEventLoop):
def MockCallback(**kwargs):
return unittest.mock.Mock(spec=['__call__'], **kwargs)
return mock.Mock(spec=['__call__'], **kwargs)
class MockPattern(str):
......
......@@ -20,7 +20,7 @@ import threading
import time
import errno
import unittest
import unittest.mock
from unittest import mock
from test import support # find_unused_port, IPV6_ENABLED, TEST_HOME_DIR
......@@ -1812,7 +1812,7 @@ class HandleTests(unittest.TestCase):
return args
args = ()
h = asyncio.Handle(callback, args, unittest.mock.Mock())
h = asyncio.Handle(callback, args, mock.Mock())
self.assertIs(h._callback, callback)
self.assertIs(h._args, args)
self.assertFalse(h._cancelled)
......@@ -1844,15 +1844,15 @@ class HandleTests(unittest.TestCase):
def callback():
raise ValueError()
m_loop = unittest.mock.Mock()
m_loop.call_exception_handler = unittest.mock.Mock()
m_loop = mock.Mock()
m_loop.call_exception_handler = mock.Mock()
h = asyncio.Handle(callback, (), m_loop)
h._run()
m_loop.call_exception_handler.assert_called_with({
'message': test_utils.MockPattern('Exception in callback.*'),
'exception': unittest.mock.ANY,
'exception': mock.ANY,
'handle': h
})
......@@ -1862,7 +1862,7 @@ class TimerTests(unittest.TestCase):
def test_hash(self):
when = time.monotonic()
h = asyncio.TimerHandle(when, lambda: False, (),
unittest.mock.Mock())
mock.Mock())
self.assertEqual(hash(h), hash(when))
def test_timer(self):
......@@ -1871,7 +1871,7 @@ class TimerTests(unittest.TestCase):
args = ()
when = time.monotonic()
h = asyncio.TimerHandle(when, callback, args, unittest.mock.Mock())
h = asyncio.TimerHandle(when, callback, args, mock.Mock())
self.assertIs(h._callback, callback)
self.assertIs(h._args, args)
self.assertFalse(h._cancelled)
......@@ -1887,10 +1887,10 @@ class TimerTests(unittest.TestCase):
self.assertRaises(AssertionError,
asyncio.TimerHandle, None, callback, args,
unittest.mock.Mock())
mock.Mock())
def test_timer_comparison(self):
loop = unittest.mock.Mock()
loop = mock.Mock()
def callback(*args):
return args
......@@ -1935,7 +1935,7 @@ class TimerTests(unittest.TestCase):
class AbstractEventLoopTests(unittest.TestCase):
def test_not_implemented(self):
f = unittest.mock.Mock()
f = mock.Mock()
loop = asyncio.AbstractEventLoop()
self.assertRaises(
NotImplementedError, loop.run_forever)
......@@ -1995,13 +1995,13 @@ class AbstractEventLoopTests(unittest.TestCase):
NotImplementedError, loop.remove_signal_handler, 1)
self.assertRaises(
NotImplementedError, loop.connect_read_pipe, f,
unittest.mock.sentinel.pipe)
mock.sentinel.pipe)
self.assertRaises(
NotImplementedError, loop.connect_write_pipe, f,
unittest.mock.sentinel.pipe)
mock.sentinel.pipe)
self.assertRaises(
NotImplementedError, loop.subprocess_shell, f,
unittest.mock.sentinel)
mock.sentinel)
self.assertRaises(
NotImplementedError, loop.subprocess_exec, f)
......@@ -2009,7 +2009,7 @@ class AbstractEventLoopTests(unittest.TestCase):
class ProtocolsAbsTests(unittest.TestCase):
def test_empty(self):
f = unittest.mock.Mock()
f = mock.Mock()
p = asyncio.Protocol()
self.assertIsNone(p.connection_made(f))
self.assertIsNone(p.connection_lost(f))
......@@ -2055,7 +2055,7 @@ class PolicyTests(unittest.TestCase):
def test_get_event_loop_calls_set_event_loop(self):
policy = asyncio.DefaultEventLoopPolicy()
with unittest.mock.patch.object(
with mock.patch.object(
policy, "set_event_loop",
wraps=policy.set_event_loop) as m_set_event_loop:
......@@ -2073,7 +2073,7 @@ class PolicyTests(unittest.TestCase):
policy.set_event_loop(None)
self.assertRaises(AssertionError, policy.get_event_loop)
@unittest.mock.patch('asyncio.events.threading.current_thread')
@mock.patch('asyncio.events.threading.current_thread')
def test_get_event_loop_thread(self, m_current_thread):
def f():
......
......@@ -3,7 +3,7 @@
import concurrent.futures
import threading
import unittest
import unittest.mock
from unittest import mock
import asyncio
from asyncio import test_utils
......@@ -174,20 +174,20 @@ class FutureTests(unittest.TestCase):
self.assertRaises(AssertionError, test)
fut.cancel()
@unittest.mock.patch('asyncio.base_events.logger')
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_abandoned(self, m_log):
fut = asyncio.Future(loop=self.loop)
del fut
self.assertFalse(m_log.error.called)
@unittest.mock.patch('asyncio.base_events.logger')
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_result_unretrieved(self, m_log):
fut = asyncio.Future(loop=self.loop)
fut.set_result(42)
del fut
self.assertFalse(m_log.error.called)
@unittest.mock.patch('asyncio.base_events.logger')
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_result_retrieved(self, m_log):
fut = asyncio.Future(loop=self.loop)
fut.set_result(42)
......@@ -195,7 +195,7 @@ class FutureTests(unittest.TestCase):
del fut
self.assertFalse(m_log.error.called)
@unittest.mock.patch('asyncio.base_events.logger')
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_exception_unretrieved(self, m_log):
fut = asyncio.Future(loop=self.loop)
fut.set_exception(RuntimeError('boom'))
......@@ -203,7 +203,7 @@ class FutureTests(unittest.TestCase):
test_utils.run_briefly(self.loop)
self.assertTrue(m_log.error.called)
@unittest.mock.patch('asyncio.base_events.logger')
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_exception_retrieved(self, m_log):
fut = asyncio.Future(loop=self.loop)
fut.set_exception(RuntimeError('boom'))
......@@ -211,7 +211,7 @@ class FutureTests(unittest.TestCase):
del fut
self.assertFalse(m_log.error.called)
@unittest.mock.patch('asyncio.base_events.logger')
@mock.patch('asyncio.base_events.logger')
def test_tb_logger_exception_result_retrieved(self, m_log):
fut = asyncio.Future(loop=self.loop)
fut.set_exception(RuntimeError('boom'))
......@@ -236,7 +236,7 @@ class FutureTests(unittest.TestCase):
f2 = asyncio.wrap_future(f1)
self.assertIs(f1, f2)
@unittest.mock.patch('asyncio.futures.events')
@mock.patch('asyncio.futures.events')
def test_wrap_future_use_global_loop(self, m_events):
def run(arg):
return (arg, threading.get_ident())
......
"""Tests for lock.py"""
import unittest
import unittest.mock
from unittest import mock
import re
import asyncio
......@@ -27,7 +27,7 @@ class LockTests(unittest.TestCase):
self.loop.close()
def test_ctor_loop(self):
loop = unittest.mock.Mock()
loop = mock.Mock()
lock = asyncio.Lock(loop=loop)
self.assertIs(lock._loop, loop)
......@@ -250,7 +250,7 @@ class EventTests(unittest.TestCase):
self.loop.close()
def test_ctor_loop(self):
loop = unittest.mock.Mock()
loop = mock.Mock()
ev = asyncio.Event(loop=loop)
self.assertIs(ev._loop, loop)
......@@ -275,7 +275,7 @@ class EventTests(unittest.TestCase):
self.assertTrue(repr(ev).endswith('[set]>'))
self.assertTrue(RGX_REPR.match(repr(ev)))
ev._waiters.append(unittest.mock.Mock())
ev._waiters.append(mock.Mock())
self.assertTrue('waiters:1' in repr(ev))
self.assertTrue(RGX_REPR.match(repr(ev)))
......@@ -386,7 +386,7 @@ class ConditionTests(unittest.TestCase):
self.loop.close()
def test_ctor_loop(self):
loop = unittest.mock.Mock()
loop = mock.Mock()
cond = asyncio.Condition(loop=loop)
self.assertIs(cond._loop, loop)
......@@ -644,11 +644,11 @@ class ConditionTests(unittest.TestCase):
self.loop.run_until_complete(cond.acquire())
self.assertTrue('locked' in repr(cond))
cond._waiters.append(unittest.mock.Mock())
cond._waiters.append(mock.Mock())
self.assertTrue('waiters:1' in repr(cond))
self.assertTrue(RGX_REPR.match(repr(cond)))
cond._waiters.append(unittest.mock.Mock())
cond._waiters.append(mock.Mock())
self.assertTrue('waiters:2' in repr(cond))
self.assertTrue(RGX_REPR.match(repr(cond)))
......@@ -688,7 +688,7 @@ class SemaphoreTests(unittest.TestCase):
self.loop.close()
def test_ctor_loop(self):
loop = unittest.mock.Mock()
loop = mock.Mock()
sem = asyncio.Semaphore(loop=loop)
self.assertIs(sem._loop, loop)
......@@ -717,11 +717,11 @@ class SemaphoreTests(unittest.TestCase):
self.assertTrue('waiters' not in repr(sem))
self.assertTrue(RGX_REPR.match(repr(sem)))
sem._waiters.append(unittest.mock.Mock())
sem._waiters.append(mock.Mock())
self.assertTrue('waiters:1' in repr(sem))
self.assertTrue(RGX_REPR.match(repr(sem)))
sem._waiters.append(unittest.mock.Mock())
sem._waiters.append(mock.Mock())
self.assertTrue('waiters:2' in repr(sem))
self.assertTrue(RGX_REPR.match(repr(sem)))
......
"""Tests for queues.py"""
import unittest
import unittest.mock
from unittest import mock
import asyncio
from asyncio import test_utils
......@@ -72,7 +72,7 @@ class QueueBasicTests(_QueueTestBase):
self.assertTrue('_queue=[1]' in fn(q))
def test_ctor_loop(self):
loop = unittest.mock.Mock()
loop = mock.Mock()
q = asyncio.Queue(loop=loop)
self.assertIs(q._loop, loop)
......
......@@ -4,7 +4,7 @@ import functools
import gc
import socket
import unittest
import unittest.mock
from unittest import mock
try:
import ssl
except ImportError:
......@@ -29,7 +29,7 @@ class StreamReaderTests(unittest.TestCase):
self.loop.close()
gc.collect()
@unittest.mock.patch('asyncio.streams.events')
@mock.patch('asyncio.streams.events')
def test_ctor_global_loop(self, m_events):
stream = asyncio.StreamReader()
self.assertIs(stream._loop, m_events.get_event_loop.return_value)
......
"""Tests for transports.py."""
import unittest
import unittest.mock
from unittest import mock
import asyncio
from asyncio import transports
......@@ -23,7 +23,7 @@ class TransportTests(unittest.TestCase):
def test_writelines(self):
transport = asyncio.Transport()
transport.write = unittest.mock.Mock()
transport.write = mock.Mock()
transport.writelines([b'line1',
bytearray(b'line2'),
......@@ -70,7 +70,7 @@ class TransportTests(unittest.TestCase):
return 512
transport = MyTransport()
transport._protocol = unittest.mock.Mock()
transport._protocol = mock.Mock()
self.assertFalse(transport._protocol_paused)
......
......@@ -3,7 +3,7 @@
import sys
import test.support
import unittest
import unittest.mock
from unittest import mock
if sys.platform != 'win32':
raise unittest.SkipTest('Windows only')
......@@ -25,7 +25,7 @@ class WinsocketpairTests(unittest.TestCase):
csock.close()
ssock.close()
@unittest.mock.patch('asyncio.windows_utils.socket')
@mock.patch('asyncio.windows_utils.socket')
def test_winsocketpair_exc(self, m_socket):
m_socket.socket.return_value.getsockname.return_value = ('', 12345)
m_socket.socket.return_value.accept.return_value = object(), object()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment