本文整理匯總了Python中asynctest.Mock.assert_has_calls方法的典型用法代碼示例。如果您正苦於以下問題:Python Mock.assert_has_calls方法的具體用法?Python Mock.assert_has_calls怎麽用?Python Mock.assert_has_calls使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類asynctest.Mock
的用法示例。
在下文中一共展示了Mock.assert_has_calls方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_handler_logger
# 需要導入模塊: from asynctest import Mock [as 別名]
# 或者: from asynctest.Mock import assert_has_calls [as 別名]
async def test_handler_logger(self):
mock = Mock()
record = {"request": None, "response": None}
class App(jj.App):
resolver = self.resolver
@LoggerMiddleware(self.resolver, mock)
@MethodMatcher(resolver, "*")
async def handler(request):
record["request"] = request
response = Response(status=200)
record["response"] = response
return response
async with run(App()) as client:
response = await client.get("/")
self.assertEqual(response.status, 200)
mock.assert_has_calls([
call.info(record["request"], extra={
"jj_request": record["request"],
}),
call.info(record["response"], extra={
"jj_request": record["request"],
"jj_response": record["response"],
}),
])
self.assertEqual(mock.info.call_count, 2)
示例2: test_root_and_app_and_handler_middlewares_priority
# 需要導入模塊: from asynctest import Mock [as 別名]
# 或者: from asynctest.Mock import assert_has_calls [as 別名]
async def test_root_and_app_and_handler_middlewares_priority(self):
mock = Mock()
class Do:
async def do(self, request, handler, app):
mock(self.__class__.__name__, sentinel.BEFORE)
response = await handler(request)
mock(self.__class__.__name__, sentinel.AFTER)
return response
class Middleware(Do, RootMiddleware):
resolver = self.resolver
class AppMiddleware(Do, BaseMiddleware):
resolver = self.resolver
class HandlerMiddleware(Do, BaseMiddleware):
resolver = self.resolver
@AppMiddleware()
class App(jj.App):
resolver = self.resolver
@MethodMatcher(resolver, "*")
@HandlerMiddleware()
async def handler(request):
mock(App.__name__, sentinel.BEFORE)
response = Response(status=200)
mock(App.__name__, sentinel.AFTER)
return response
async with run(App(), middlewares=[Middleware()]) as client:
response = await client.get("/")
self.assertEqual(response.status, 200)
mock.assert_has_calls([
call(AppMiddleware.__name__, sentinel.BEFORE),
call(HandlerMiddleware.__name__, sentinel.BEFORE),
call(Middleware.__name__, sentinel.BEFORE),
call(App.__name__, sentinel.BEFORE),
call(App.__name__, sentinel.AFTER),
call(Middleware.__name__, sentinel.AFTER),
call(HandlerMiddleware.__name__, sentinel.AFTER),
call(AppMiddleware.__name__, sentinel.AFTER),
])
self.assertEqual(mock.call_count, 8)
示例3: test_handler_with_matcher_and_decorators
# 需要導入模塊: from asynctest import Mock [as 別名]
# 或者: from asynctest.Mock import assert_has_calls [as 別名]
async def test_handler_with_matcher_and_decorators(self):
mock = Mock()
def decorator_before(fn):
async def before_handler(request):
mock(sentinel.BEFORE)
return await fn(request)
return before_handler
def decorator_after(fn):
async def after_handler(request):
mock(sentinel.AFTER)
return await fn(request)
return after_handler
path, status, text = "/route", 201, "text"
class App(jj.App):
resolver = self.resolver
@decorator_after
@PathMatcher(resolver, path)
@decorator_before
async def handler(request):
mock(sentinel.HANDLE)
return Response(status=status, text=text)
async with run(App()) as client:
response = await client.get(path)
self.assertEqual(response.status, status)
self.assertEqual(await response.text(), text)
response2 = await client.get("/")
self.assertEqual(response2.status, 404)
mock.assert_has_calls([
call(sentinel.AFTER),
call(sentinel.BEFORE),
call(sentinel.HANDLE)
])
self.assertEqual(mock.call_count, 3)
示例4: test_app_and_handler_middlewares_priority
# 需要導入模塊: from asynctest import Mock [as 別名]
# 或者: from asynctest.Mock import assert_has_calls [as 別名]
async def test_app_and_handler_middlewares_priority(self):
mock = Mock()
class Middleware1(BaseMiddleware):
async def do(self, request, handler, app):
mock(self.__class__.__name__, sentinel.BEFORE)
response = await handler(request)
mock(self.__class__.__name__, sentinel.AFTER)
return response
class Middleware2(Middleware1):
pass
@Middleware1(self.resolver)
class App(jj.App):
resolver = self.resolver
@MethodMatcher(resolver, "*")
@Middleware2(resolver)
async def handler(request):
mock(App.__name__, sentinel.BEFORE)
response = Response(status=200)
mock(App.__name__, sentinel.AFTER)
return response
async with run(App()) as client:
response = await client.get("/")
self.assertEqual(response.status, 200)
mock.assert_has_calls([
call(Middleware1.__name__, sentinel.BEFORE),
call(Middleware2.__name__, sentinel.BEFORE),
call(App.__name__, sentinel.BEFORE),
call(App.__name__, sentinel.AFTER),
call(Middleware2.__name__, sentinel.AFTER),
call(Middleware1.__name__, sentinel.AFTER),
])
self.assertEqual(mock.call_count, 6)