whoosh / tests /

from __future__ import with_statement
import inspect
import random
import sys

from whoosh import fields, query, scoring
from whoosh.compat import u, xrange, permutations
from whoosh.util.testing import TempIndex

def _weighting_classes(ignore):
    # Get all the subclasses of Weighting in whoosh.scoring
    return [c for _, c in inspect.getmembers(scoring, inspect.isclass)
            if scoring.WeightingModel in c.__bases__ and c not in ignore]

def test_all():
    domain = [u"alfa", u"bravo", u"charlie", u"delta", u"echo",
    schema = fields.Schema(text=fields.TEXT)
    with TempIndex(schema) as ix:
        with ix.writer() as w:
            for _ in xrange(100):
                text = u" ".join(random.choice(domain)
                                   for _ in xrange(random.randint(10, 20)))

        # List ABCs that should not be tested
        abcs = ()
        # Provide initializer arguments for any weighting classes that require
        # them
        init_args = {"MultiWeighting": ([scoring.BM25F()],
                                        {"text": scoring.Frequency()}),
                     "ReverseWeighting": ([scoring.BM25F()], {}),
                     "FunctionWeighting": ([lambda s, fn, t, m: 1.0], {})}

        for wclass in _weighting_classes(abcs):
                if wclass.__name__ in init_args:
                    args, kwargs = init_args[wclass.__name__]
                    weighting = wclass(*args, **kwargs)
                    weighting = wclass()
            except TypeError:
                e = sys.exc_info()[1]
                raise TypeError("Error instantiating %r: %s" % (wclass, e))

            with ix.searcher(weighting=weighting) as s:
                    for word in domain:
              "text", word))
                except Exception:
                    e = sys.exc_info()[1]
                    e.msg = "Error searching with %r: %s" % (wclass, e)