Debian Codegraph @

tests/api/_test_async_step34.py

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 # -*- coding: UTF-8 -*-
2 """
3 Unit tests for :mod:`behave.api.async_test`.
4 """
5
6 # -- IMPORTS:
7 from __future__ import absolute_import, print_function
8 from behave.api.async_step import AsyncContext, use_or_create_async_context
9 from behave._stepimport import use_step_import_modules
10 from behave.runner import Context, Runner
11 import sys
12 from mock import Mock
13 import pytest
14
15 from .testing_support import StopWatch, SimpleStepContainer
16 from .testing_support_async import AsyncStepTheory
17
18
19 # -----------------------------------------------------------------------------
20 # ASYNC STEP EXAMPLES:
21 # -----------------------------------------------------------------------------
22 # if python_version >= 3.5:
23 # @step('an async coroutine step waits "{duration:f}" seconds')
24 # @async_run_until_complete
25 # async def step_async_step_waits_seconds(context, duration):
26 # print("async_step: Should sleep for %.3f seconds" % duration)
27 # await asyncio.sleep(duration)
28 #
29 # if python_version >= 3.4:
30 # @step('a tagged-coroutine async step waits "{duration:f}" seconds')
31 # @async_run_until_complete
32 # @asyncio.coroutine
33 # def step_async_step_waits_seconds2(context, duration):
34 # print("async_step2: Should sleep for %.3f seconds" % duration)
35 # yield from asyncio.sleep(duration)
36 #
37 # -----------------------------------------------------------------------------
38 # TEST MARKERS:
39 # -----------------------------------------------------------------------------
40 python_version = float("%s.%s" % sys.version_info[:2])
41
42 # xfail = pytest.mark.xfail
43 py34_or_newer = pytest.mark.skipif(python_version < 3.4, reason="Needs Python >= 3.4")
44
45 # -----------------------------------------------------------------------------
46 # TESTSUITE:
47 # -----------------------------------------------------------------------------
48 @py34_or_newer
49 class TestAsyncStepDecorator34(object):
50
51 def test_step_decorator_async_run_until_complete2(self):
52 step_container = SimpleStepContainer()
53 with use_step_import_modules(step_container):
54 # -- STEP-DEFINITIONS EXAMPLE (as MODULE SNIPPET):
55 # VARIANT 2: Use @asyncio.coroutine def step_impl()
56 from behave import step
57 from behave.api.async_step import async_run_until_complete
58 import asyncio
59
60 @step('a tagged-coroutine async step waits "{duration:f}" seconds')
61 @async_run_until_complete
62 @asyncio.coroutine
63 def step_async_step_waits_seconds2(context, duration):
64 yield from asyncio.sleep(duration)
65
66 # -- USES: async def step_impl(...) as async-step (coroutine)
67 AsyncStepTheory.validate(step_async_step_waits_seconds2)
68
69 # -- RUN ASYNC-STEP: Verify that it is behaving correctly.
70 # ENSURE: Execution of async-step matches expected duration.
71 context = Context(runner=Runner(config={}))
72 with StopWatch() as stop_watch:
73 step_async_step_waits_seconds2(context, duration=0.2)
74 assert abs(stop_watch.duration - 0.2) <= 0.05
75
76
77 class TestAsyncContext(object):
78 @staticmethod
79 def make_context():
80 return Context(runner=Runner(config={}))
81
82 def test_use_or_create_async_context__when_missing(self):
83 # -- CASE: AsynContext attribute is created with default_name
84 context = self.make_context()
85 context._push()
86
87 async_context = use_or_create_async_context(context)
88 assert isinstance(async_context, AsyncContext)
89 assert async_context.name == "async_context"
90 assert getattr(context, "async_context", None) is async_context
91
92 context._pop()
93 assert getattr(context, "async_context", None) is None
94
95 def test_use_or_create_async_context__when_exists(self):
96 # -- CASE: AsynContext attribute exists with default_name
97 context = self.make_context()
98 async_context0 = context.async_context = AsyncContext()
99 assert context.async_context.name == "async_context"
100 assert hasattr(context, AsyncContext.default_name)
101
102 async_context = use_or_create_async_context(context)
103 assert isinstance(async_context, AsyncContext)
104 assert async_context.name == "async_context"
105 assert getattr(context, "async_context", None) is async_context
106 assert async_context is async_context0
107
108 def test_use_or_create_async_context__when_missing_with_name(self):
109 # -- CASE: AsynContext attribute is created with own name
110 loop0 = Mock()
111 context = self.make_context()
112 async_context = use_or_create_async_context(context, "acontext", loop=loop0)
113 assert isinstance(async_context, AsyncContext)
114 assert async_context.name == "acontext"
115 assert getattr(context, "acontext", None) is async_context
116 assert async_context.loop is loop0
117
118 def test_use_or_create_async_context__when_exists_with_name(self):
119 # -- CASE: AsynContext attribute exists with own name
120 loop0 = Mock()
121 context = self.make_context()
122 async_context0 = context.acontext = AsyncContext(name="acontext", loop=loop0)
123 assert context.acontext.name == "acontext"
124
125 loop1 = Mock()
126 async_context = use_or_create_async_context(context, "acontext", loop=loop1)
127 assert isinstance(async_context, AsyncContext)
128 assert async_context is async_context0
129 assert getattr(context, "acontext", None) is async_context
130 assert async_context.loop is loop0