Debian Codegraph @

tests/api/_test_async_step34.py

raw ◀ ▶
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 unittest.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 # DEPRECATED: @asyncio.coroutine decorator (since: Python >= 3.8)
41 _python_version = float("%s.%s" % sys.version_info[:2])
42 requires_py34_to_py37 = pytest.mark.skipif(not (3.4 <= _python_version < 3.8),
43 reason="Supported only for python.versions: 3.4 .. 3.7 (inclusive)")
44
45
46 # -----------------------------------------------------------------------------
47 # TESTSUITE:
48 # -----------------------------------------------------------------------------
•49 @requires_py34_to_py37
50 class TestAsyncStepDecorator34(object):
51
52 def test_step_decorator_async_run_until_complete2(self):
53 step_container = SimpleStepContainer()
54 with use_step_import_modules(step_container):
55 # -- STEP-DEFINITIONS EXAMPLE (as MODULE SNIPPET):
56 # VARIANT 2: Use @asyncio.coroutine def step_impl()
57 from behave import step
58 from behave.api.async_step import async_run_until_complete
59 import asyncio
60
61 @step('a tagged-coroutine async step waits "{duration:f}" seconds')
62 @async_run_until_complete
63 @asyncio.coroutine
64 def step_async_step_waits_seconds2(context, duration):
65 yield from asyncio.sleep(duration)
66
67 # -- USES: async def step_impl(...) as async-step (coroutine)
68 AsyncStepTheory.validate(step_async_step_waits_seconds2)
69
70 # -- RUN ASYNC-STEP: Verify that it is behaving correctly.
71 # ENSURE: Execution of async-step matches expected duration.
72 context = Context(runner=Runner(config={}))
73 with StopWatch() as stop_watch:
74 step_async_step_waits_seconds2(context, duration=0.2)
75 assert abs(stop_watch.duration - 0.2) <= 0.05
76
77
78 class TestAsyncContext(object):
79 @staticmethod
80 def make_context():
81 return Context(runner=Runner(config={}))
82
83 def test_use_or_create_async_context__when_missing(self):
84 # -- CASE: AsynContext attribute is created with default_name
85 context = self.make_context()
86 context._push()
87
88 async_context = use_or_create_async_context(context)
89 assert isinstance(async_context, AsyncContext)
90 assert async_context.name == "async_context"
91 assert getattr(context, "async_context", None) is async_context
92
93 context._pop()
94 assert getattr(context, "async_context", None) is None
95
96 def test_use_or_create_async_context__when_exists(self):
97 # -- CASE: AsynContext attribute exists with default_name
98 context = self.make_context()
99 async_context0 = context.async_context = AsyncContext()
100 assert context.async_context.name == "async_context"
101 assert hasattr(context, AsyncContext.default_name)
102
103 async_context = use_or_create_async_context(context)
104 assert isinstance(async_context, AsyncContext)
105 assert async_context.name == "async_context"
106 assert getattr(context, "async_context", None) is async_context
107 assert async_context is async_context0
108
109 def test_use_or_create_async_context__when_missing_with_name(self):
110 # -- CASE: AsynContext attribute is created with own name
111 loop0 = Mock()
112 context = self.make_context()
113 async_context = use_or_create_async_context(context, "acontext", loop=loop0)
114 assert isinstance(async_context, AsyncContext)
115 assert async_context.name == "acontext"
116 assert getattr(context, "acontext", None) is async_context
117 assert async_context.loop is loop0
118
119 def test_use_or_create_async_context__when_exists_with_name(self):
120 # -- CASE: AsynContext attribute exists with own name
121 loop0 = Mock()
122 context = self.make_context()
123 async_context0 = context.acontext = AsyncContext(name="acontext", loop=loop0)
124 assert context.acontext.name == "acontext"
125
126 loop1 = Mock()
127 async_context = use_or_create_async_context(context, "acontext", loop=loop1)
128 assert isinstance(async_context, AsyncContext)
129 assert async_context is async_context0
130 assert getattr(context, "acontext", None) is async_context
131 assert async_context.loop is loop0