MCPcopy Create free account
hub / github.com/tortoise/tortoise-orm / requireCapability

Function requireCapability

tortoise/contrib/test/__init__.py:163–235  ·  view source on GitHub ↗

Skip a test if the required capabilities are not matched. .. note:: The database must be initialized *before* the decorated test runs. Usage: .. code-block:: python3 @requireCapability(dialect='sqlite') @pytest.mark.asyncio async def test_run_sqli

(
    connection_name: str = "models", **conditions: typing.Any
)

Source from the content-addressed store, hash-verified

161
162
163def requireCapability(
164 connection_name: str = "models", **conditions: typing.Any
165) -> Callable[[_FT], _FT]:
166 """
167 Skip a test if the required capabilities are not matched.
168
169 .. note::
170 The database must be initialized *before* the decorated test runs.
171
172 Usage:
173
174 .. code-block:: python3
175
176 @requireCapability(dialect='sqlite')
177 @pytest.mark.asyncio
178 async def test_run_sqlite_only(db):
179 ...
180
181 Or to conditionally skip a class:
182
183 .. code-block:: python3
184
185 @requireCapability(dialect='sqlite')
186 class TestSqlite:
187 @pytest.mark.asyncio
188 async def test_something(self, db):
189 ...
190
191 :param connection_name: name of the connection to retrieve capabilities from.
192 :param conditions: capability tests which must all pass for the test to run.
193 """
194
195 def decorator(test_item: _FT) -> _FT:
196 if not isinstance(test_item, type):
197
198 def check_capabilities() -> None:
199 db = get_connection(connection_name)
200 for key, val in conditions.items():
201 if getattr(db.capabilities, key) != val:
202 raise SkipTest(f"Capability {key} != {val}")
203
204 if inspect.iscoroutinefunction(test_item):
205
206 @wraps(test_item)
207 async def skip_wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
208 check_capabilities()
209 return await test_item(*args, **kwargs)
210
211 else:
212
213 @wraps(test_item)
214 def skip_wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
215 check_capabilities()
216 return test_item(*args, **kwargs)
217
218 return cast(_FT, skip_wrapper)
219
220 # Assume a class is decorated

Callers 1

decoratorFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…