MCPcopy
hub / github.com/django/django / decorator

Function decorator

django/test/testcases.py:1566–1618  ·  view source on GitHub ↗
(test_func)

Source from the content-addressed store, hash-verified

1564
1565def _deferredSkip(condition, reason, name):
1566 def decorator(test_func):
1567 nonlocal condition
1568 if not (
1569 isinstance(test_func, type) and issubclass(test_func, unittest.TestCase)
1570 ):
1571
1572 @wraps(test_func)
1573 def skip_wrapper(*args, **kwargs):
1574 if (
1575 args
1576 and isinstance(args[0], unittest.TestCase)
1577 and connection.alias not in getattr(args[0], "databases", {})
1578 ):
1579 raise ValueError(
1580 "%s cannot be used on %s as %s doesn't allow queries "
1581 "against the %r database."
1582 % (
1583 name,
1584 args[0],
1585 args[0].__class__.__qualname__,
1586 connection.alias,
1587 )
1588 )
1589 if condition():
1590 raise unittest.SkipTest(reason)
1591 return test_func(*args, **kwargs)
1592
1593 test_item = skip_wrapper
1594 else:
1595 # Assume a class is decorated
1596 test_item = test_func
1597 databases = getattr(test_item, "databases", None)
1598 if not databases or connection.alias not in databases:
1599 # Defer raising to allow importing test class's module.
1600 def condition():
1601 raise ValueError(
1602 "%s cannot be used on %s as it doesn't allow queries "
1603 "against the '%s' database."
1604 % (
1605 name,
1606 test_item,
1607 connection.alias,
1608 )
1609 )
1610
1611 # Retrieve the possibly existing value from the class's dict to
1612 # avoid triggering the descriptor.
1613 skip = test_func.__dict__.get("__unittest_skip__")
1614 if isinstance(skip, CheckCondition):
1615 test_item.__unittest_skip__ = skip.add_condition(condition, reason)
1616 elif skip is not True:
1617 test_item.__unittest_skip__ = CheckCondition((condition, reason))
1618 return test_item
1619
1620 return decorator
1621

Callers

nothing calls this directly

Calls 3

CheckConditionClass · 0.85
add_conditionMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected