2016-02-06 09:37:19 +00:00
|
|
|
# testing/exclusions.py
|
2016-02-22 11:13:36 +00:00
|
|
|
# Copyright (C) 2005-2016 the SQLAlchemy authors and contributors
|
2016-02-06 09:37:19 +00:00
|
|
|
# <see AUTHORS file>
|
|
|
|
#
|
|
|
|
# This module is part of SQLAlchemy and is released under
|
|
|
|
# the MIT License: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
|
|
|
|
|
|
|
|
import operator
|
|
|
|
from ..util import decorator
|
|
|
|
from . import config
|
|
|
|
from .. import util
|
|
|
|
import inspect
|
2016-02-22 11:13:36 +00:00
|
|
|
import contextlib
|
|
|
|
from sqlalchemy.util.compat import inspect_getargspec
|
|
|
|
|
|
|
|
|
|
|
|
def skip_if(predicate, reason=None):
|
|
|
|
rule = compound()
|
|
|
|
pred = _as_predicate(predicate, reason)
|
|
|
|
rule.skips.add(pred)
|
|
|
|
return rule
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def fails_if(predicate, reason=None):
|
|
|
|
rule = compound()
|
|
|
|
pred = _as_predicate(predicate, reason)
|
|
|
|
rule.fails.add(pred)
|
|
|
|
return rule
|
2016-02-06 09:37:19 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
|
|
|
|
class compound(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.fails = set()
|
|
|
|
self.skips = set()
|
|
|
|
self.tags = set()
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
def __add__(self, other):
|
2016-02-22 11:13:36 +00:00
|
|
|
return self.add(other)
|
|
|
|
|
|
|
|
def add(self, *others):
|
|
|
|
copy = compound()
|
|
|
|
copy.fails.update(self.fails)
|
|
|
|
copy.skips.update(self.skips)
|
|
|
|
copy.tags.update(self.tags)
|
|
|
|
for other in others:
|
|
|
|
copy.fails.update(other.fails)
|
|
|
|
copy.skips.update(other.skips)
|
|
|
|
copy.tags.update(other.tags)
|
|
|
|
return copy
|
|
|
|
|
|
|
|
def not_(self):
|
|
|
|
copy = compound()
|
|
|
|
copy.fails.update(NotPredicate(fail) for fail in self.fails)
|
|
|
|
copy.skips.update(NotPredicate(skip) for skip in self.skips)
|
|
|
|
copy.tags.update(self.tags)
|
|
|
|
return copy
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def enabled(self):
|
|
|
|
return self.enabled_for_config(config._current)
|
|
|
|
|
|
|
|
def enabled_for_config(self, config):
|
2016-02-22 11:13:36 +00:00
|
|
|
for predicate in self.skips.union(self.fails):
|
|
|
|
if predicate(config):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def matching_config_reasons(self, config):
|
|
|
|
return [
|
|
|
|
predicate._as_string(config) for predicate
|
|
|
|
in self.skips.union(self.fails)
|
|
|
|
if predicate(config)
|
|
|
|
]
|
|
|
|
|
|
|
|
def include_test(self, include_tags, exclude_tags):
|
|
|
|
return bool(
|
|
|
|
not self.tags.intersection(exclude_tags) and
|
|
|
|
(not include_tags or self.tags.intersection(include_tags))
|
|
|
|
)
|
|
|
|
|
|
|
|
def _extend(self, other):
|
|
|
|
self.skips.update(other.skips)
|
|
|
|
self.fails.update(other.fails)
|
|
|
|
self.tags.update(other.tags)
|
|
|
|
|
|
|
|
def __call__(self, fn):
|
|
|
|
if hasattr(fn, '_sa_exclusion_extend'):
|
|
|
|
fn._sa_exclusion_extend._extend(self)
|
|
|
|
return fn
|
|
|
|
|
|
|
|
@decorator
|
|
|
|
def decorate(fn, *args, **kw):
|
|
|
|
return self._do(config._current, fn, *args, **kw)
|
|
|
|
decorated = decorate(fn)
|
|
|
|
decorated._sa_exclusion_extend = self
|
|
|
|
return decorated
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2016-02-22 11:13:36 +00:00
|
|
|
def fail_if(self):
|
|
|
|
all_fails = compound()
|
|
|
|
all_fails.fails.update(self.skips.union(self.fails))
|
|
|
|
|
2016-02-06 09:37:19 +00:00
|
|
|
try:
|
|
|
|
yield
|
|
|
|
except Exception as ex:
|
2016-02-22 11:13:36 +00:00
|
|
|
all_fails._expect_failure(config._current, ex)
|
2016-02-06 09:37:19 +00:00
|
|
|
else:
|
2016-02-22 11:13:36 +00:00
|
|
|
all_fails._expect_success(config._current)
|
|
|
|
|
|
|
|
def _do(self, config, fn, *args, **kw):
|
|
|
|
for skip in self.skips:
|
|
|
|
if skip(config):
|
|
|
|
msg = "'%s' : %s" % (
|
|
|
|
fn.__name__,
|
|
|
|
skip._as_string(config)
|
|
|
|
)
|
|
|
|
config.skip_test(msg)
|
2016-02-06 09:37:19 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
try:
|
|
|
|
return_value = fn(*args, **kw)
|
|
|
|
except Exception as ex:
|
|
|
|
self._expect_failure(config, ex, name=fn.__name__)
|
|
|
|
else:
|
|
|
|
self._expect_success(config, name=fn.__name__)
|
|
|
|
return return_value
|
|
|
|
|
|
|
|
def _expect_failure(self, config, ex, name='block'):
|
|
|
|
for fail in self.fails:
|
|
|
|
if fail(config):
|
|
|
|
print(("%s failed as expected (%s): %s " % (
|
|
|
|
name, fail._as_string(config), str(ex))))
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
util.raise_from_cause(ex)
|
|
|
|
|
|
|
|
def _expect_success(self, config, name='block'):
|
|
|
|
if not self.fails:
|
|
|
|
return
|
|
|
|
for fail in self.fails:
|
|
|
|
if not fail(config):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise AssertionError(
|
|
|
|
"Unexpected success for '%s' (%s)" %
|
|
|
|
(
|
|
|
|
name,
|
|
|
|
" and ".join(
|
|
|
|
fail._as_string(config)
|
|
|
|
for fail in self.fails
|
2016-02-06 09:37:19 +00:00
|
|
|
)
|
2016-02-22 11:13:36 +00:00
|
|
|
)
|
|
|
|
)
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def requires_tag(tagname):
|
|
|
|
return tags([tagname])
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def tags(tagnames):
|
|
|
|
comp = compound()
|
|
|
|
comp.tags.update(tagnames)
|
|
|
|
return comp
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def only_if(predicate, reason=None):
|
|
|
|
predicate = _as_predicate(predicate)
|
|
|
|
return skip_if(NotPredicate(predicate), reason)
|
|
|
|
|
|
|
|
|
|
|
|
def succeeds_if(predicate, reason=None):
|
|
|
|
predicate = _as_predicate(predicate)
|
|
|
|
return fails_if(NotPredicate(predicate), reason)
|
|
|
|
|
|
|
|
|
|
|
|
class Predicate(object):
|
|
|
|
@classmethod
|
2016-02-22 11:13:36 +00:00
|
|
|
def as_predicate(cls, predicate, description=None):
|
|
|
|
if isinstance(predicate, compound):
|
|
|
|
return cls.as_predicate(predicate.enabled_for_config, description)
|
2016-02-06 09:37:19 +00:00
|
|
|
elif isinstance(predicate, Predicate):
|
2016-02-22 11:13:36 +00:00
|
|
|
if description and predicate.description is None:
|
|
|
|
predicate.description = description
|
2016-02-06 09:37:19 +00:00
|
|
|
return predicate
|
2016-02-22 11:13:36 +00:00
|
|
|
elif isinstance(predicate, (list, set)):
|
|
|
|
return OrPredicate(
|
|
|
|
[cls.as_predicate(pred) for pred in predicate],
|
|
|
|
description)
|
2016-02-06 09:37:19 +00:00
|
|
|
elif isinstance(predicate, tuple):
|
|
|
|
return SpecPredicate(*predicate)
|
|
|
|
elif isinstance(predicate, util.string_types):
|
|
|
|
tokens = predicate.split(" ", 2)
|
|
|
|
op = spec = None
|
|
|
|
db = tokens.pop(0)
|
|
|
|
if tokens:
|
|
|
|
op = tokens.pop(0)
|
|
|
|
if tokens:
|
|
|
|
spec = tuple(int(d) for d in tokens.pop(0).split("."))
|
2016-02-22 11:13:36 +00:00
|
|
|
return SpecPredicate(db, op, spec, description=description)
|
2016-02-06 09:37:19 +00:00
|
|
|
elif util.callable(predicate):
|
2016-02-22 11:13:36 +00:00
|
|
|
return LambdaPredicate(predicate, description)
|
2016-02-06 09:37:19 +00:00
|
|
|
else:
|
|
|
|
assert False, "unknown predicate type: %s" % predicate
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def _format_description(self, config, negate=False):
|
|
|
|
bool_ = self(config)
|
|
|
|
if negate:
|
|
|
|
bool_ = not negate
|
|
|
|
return self.description % {
|
|
|
|
"driver": config.db.url.get_driver_name(),
|
|
|
|
"database": config.db.url.get_backend_name(),
|
|
|
|
"doesnt_support": "doesn't support" if bool_ else "does support",
|
|
|
|
"does_support": "does support" if bool_ else "doesn't support"
|
|
|
|
}
|
|
|
|
|
|
|
|
def _as_string(self, config=None, negate=False):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
class BooleanPredicate(Predicate):
|
|
|
|
def __init__(self, value, description=None):
|
|
|
|
self.value = value
|
|
|
|
self.description = description or "boolean %s" % value
|
|
|
|
|
|
|
|
def __call__(self, config):
|
|
|
|
return self.value
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def _as_string(self, config, negate=False):
|
|
|
|
return self._format_description(config, negate=negate)
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SpecPredicate(Predicate):
|
|
|
|
def __init__(self, db, op=None, spec=None, description=None):
|
|
|
|
self.db = db
|
|
|
|
self.op = op
|
|
|
|
self.spec = spec
|
|
|
|
self.description = description
|
|
|
|
|
|
|
|
_ops = {
|
|
|
|
'<': operator.lt,
|
|
|
|
'>': operator.gt,
|
|
|
|
'==': operator.eq,
|
|
|
|
'!=': operator.ne,
|
|
|
|
'<=': operator.le,
|
|
|
|
'>=': operator.ge,
|
|
|
|
'in': operator.contains,
|
|
|
|
'between': lambda val, pair: val >= pair[0] and val <= pair[1],
|
|
|
|
}
|
|
|
|
|
|
|
|
def __call__(self, config):
|
|
|
|
engine = config.db
|
|
|
|
|
|
|
|
if "+" in self.db:
|
|
|
|
dialect, driver = self.db.split('+')
|
|
|
|
else:
|
|
|
|
dialect, driver = self.db, None
|
|
|
|
|
|
|
|
if dialect and engine.name != dialect:
|
|
|
|
return False
|
|
|
|
if driver is not None and engine.driver != driver:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.op is not None:
|
|
|
|
assert driver is None, "DBAPI version specs not supported yet"
|
|
|
|
|
|
|
|
version = _server_version(engine)
|
|
|
|
oper = hasattr(self.op, '__call__') and self.op \
|
|
|
|
or self._ops[self.op]
|
|
|
|
return oper(version, self.spec)
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def _as_string(self, config, negate=False):
|
2016-02-06 09:37:19 +00:00
|
|
|
if self.description is not None:
|
2016-02-22 11:13:36 +00:00
|
|
|
return self._format_description(config)
|
2016-02-06 09:37:19 +00:00
|
|
|
elif self.op is None:
|
|
|
|
if negate:
|
|
|
|
return "not %s" % self.db
|
|
|
|
else:
|
|
|
|
return "%s" % self.db
|
|
|
|
else:
|
|
|
|
if negate:
|
|
|
|
return "not %s %s %s" % (
|
|
|
|
self.db,
|
|
|
|
self.op,
|
|
|
|
self.spec
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return "%s %s %s" % (
|
|
|
|
self.db,
|
|
|
|
self.op,
|
|
|
|
self.spec
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class LambdaPredicate(Predicate):
|
|
|
|
def __init__(self, lambda_, description=None, args=None, kw=None):
|
2016-02-22 11:13:36 +00:00
|
|
|
spec = inspect_getargspec(lambda_)
|
2016-02-06 09:37:19 +00:00
|
|
|
if not spec[0]:
|
|
|
|
self.lambda_ = lambda db: lambda_()
|
|
|
|
else:
|
|
|
|
self.lambda_ = lambda_
|
|
|
|
self.args = args or ()
|
|
|
|
self.kw = kw or {}
|
|
|
|
if description:
|
|
|
|
self.description = description
|
|
|
|
elif lambda_.__doc__:
|
|
|
|
self.description = lambda_.__doc__
|
|
|
|
else:
|
|
|
|
self.description = "custom function"
|
|
|
|
|
|
|
|
def __call__(self, config):
|
|
|
|
return self.lambda_(config)
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def _as_string(self, config, negate=False):
|
|
|
|
return self._format_description(config)
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NotPredicate(Predicate):
|
2016-02-22 11:13:36 +00:00
|
|
|
def __init__(self, predicate, description=None):
|
2016-02-06 09:37:19 +00:00
|
|
|
self.predicate = predicate
|
2016-02-22 11:13:36 +00:00
|
|
|
self.description = description
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
def __call__(self, config):
|
|
|
|
return not self.predicate(config)
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def _as_string(self, config, negate=False):
|
|
|
|
if self.description:
|
|
|
|
return self._format_description(config, not negate)
|
|
|
|
else:
|
|
|
|
return self.predicate._as_string(config, not negate)
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrPredicate(Predicate):
|
|
|
|
def __init__(self, predicates, description=None):
|
|
|
|
self.predicates = predicates
|
|
|
|
self.description = description
|
|
|
|
|
|
|
|
def __call__(self, config):
|
|
|
|
for pred in self.predicates:
|
|
|
|
if pred(config):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def _eval_str(self, config, negate=False):
|
|
|
|
if negate:
|
|
|
|
conjunction = " and "
|
2016-02-06 09:37:19 +00:00
|
|
|
else:
|
2016-02-22 11:13:36 +00:00
|
|
|
conjunction = " or "
|
|
|
|
return conjunction.join(p._as_string(config, negate=negate)
|
|
|
|
for p in self.predicates)
|
2016-02-06 09:37:19 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def _negation_str(self, config):
|
2016-02-06 09:37:19 +00:00
|
|
|
if self.description is not None:
|
2016-02-22 11:13:36 +00:00
|
|
|
return "Not " + self._format_description(config)
|
2016-02-06 09:37:19 +00:00
|
|
|
else:
|
2016-02-22 11:13:36 +00:00
|
|
|
return self._eval_str(config, negate=True)
|
2016-02-06 09:37:19 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def _as_string(self, config, negate=False):
|
2016-02-06 09:37:19 +00:00
|
|
|
if negate:
|
2016-02-22 11:13:36 +00:00
|
|
|
return self._negation_str(config)
|
2016-02-06 09:37:19 +00:00
|
|
|
else:
|
|
|
|
if self.description is not None:
|
2016-02-22 11:13:36 +00:00
|
|
|
return self._format_description(config)
|
2016-02-06 09:37:19 +00:00
|
|
|
else:
|
2016-02-22 11:13:36 +00:00
|
|
|
return self._eval_str(config)
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
_as_predicate = Predicate.as_predicate
|
|
|
|
|
|
|
|
|
|
|
|
def _is_excluded(db, op, spec):
|
|
|
|
return SpecPredicate(db, op, spec)(config._current)
|
|
|
|
|
|
|
|
|
|
|
|
def _server_version(engine):
|
|
|
|
"""Return a server_version_info tuple."""
|
|
|
|
|
|
|
|
# force metadata to be retrieved
|
|
|
|
conn = engine.connect()
|
|
|
|
version = getattr(engine.dialect, 'server_version_info', ())
|
|
|
|
conn.close()
|
|
|
|
return version
|
|
|
|
|
|
|
|
|
|
|
|
def db_spec(*dbs):
|
|
|
|
return OrPredicate(
|
|
|
|
[Predicate.as_predicate(db) for db in dbs]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def open():
|
|
|
|
return skip_if(BooleanPredicate(False, "mark as execute"))
|
|
|
|
|
|
|
|
|
|
|
|
def closed():
|
|
|
|
return skip_if(BooleanPredicate(True, "marked as skip"))
|
|
|
|
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def fails(reason=None):
|
|
|
|
return fails_if(BooleanPredicate(True, reason or "expected to fail"))
|
2016-02-06 09:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@decorator
|
|
|
|
def future(fn, *arg):
|
|
|
|
return fails_if(LambdaPredicate(fn), "Future feature")
|
|
|
|
|
|
|
|
|
|
|
|
def fails_on(db, reason=None):
|
|
|
|
return fails_if(SpecPredicate(db), reason)
|
|
|
|
|
|
|
|
|
|
|
|
def fails_on_everything_except(*dbs):
|
|
|
|
return succeeds_if(
|
|
|
|
OrPredicate([
|
2016-02-22 11:13:36 +00:00
|
|
|
SpecPredicate(db) for db in dbs
|
|
|
|
])
|
2016-02-06 09:37:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def skip(db, reason=None):
|
|
|
|
return skip_if(SpecPredicate(db), reason)
|
|
|
|
|
|
|
|
|
|
|
|
def only_on(dbs, reason=None):
|
|
|
|
return only_if(
|
2016-02-22 11:13:36 +00:00
|
|
|
OrPredicate([Predicate.as_predicate(db) for db in util.to_list(dbs)])
|
2016-02-06 09:37:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def exclude(db, op, spec, reason=None):
|
|
|
|
return skip_if(SpecPredicate(db, op, spec), reason)
|
|
|
|
|
|
|
|
|
|
|
|
def against(config, *queries):
|
|
|
|
assert queries, "no queries sent!"
|
|
|
|
return OrPredicate([
|
|
|
|
Predicate.as_predicate(query)
|
|
|
|
for query in queries
|
|
|
|
])(config)
|