Beispiel #1
0
def test_numeric_range():
    schema = fields.Schema(id=fields.STORED, number=fields.NUMERIC)
    qp = default.QueryParser("number", schema)

    teststart = 40
    testend = 100

    q = qp.parse("[%s to *]" % teststart)
    assert q == query.NullQuery

    q = qp.parse("[%s to]" % teststart)
    assert q.__class__ == query.NumericRange
    assert q.start == teststart
    assert q.end is None

    q = qp.parse("[to %s]" % testend)
    assert q.__class__ == query.NumericRange
    assert q.start is None
    assert q.end == testend

    q = qp.parse("[%s to %s]" % (teststart, testend))
    assert q.__class__ == query.NumericRange
    assert q.start == teststart
    assert q.end == testend
Beispiel #2
0
def test_fieldname_chars():
    s = fields.Schema(abc123=fields.TEXT, nisbah=fields.KEYWORD)
    qp = default.QueryParser("content", s)
    fieldmap = {'nisbah': [u('\u0646\u0633\u0628\u0629')], 'abc123': ['xyz']}
    qp.add_plugin(plugins.FieldAliasPlugin(fieldmap))

    q = qp.parse(u("abc123:456"))
    assert q.__class__ == query.Term
    assert q.fieldname == u('abc123')
    assert q.text == u('456')

    q = qp.parse(u("abc123:456 def"))
    assert text_type(q) == u("(abc123:456 AND content:def)")

    q = qp.parse(
        u('\u0646\u0633\u0628\u0629:\u0627\u0644\u0641\u0644\u0633'
          '\u0637\u064a\u0646\u064a'))
    assert q.__class__ == query.Term
    assert q.fieldname == u('nisbah')
    assert q.text == u(
        '\u0627\u0644\u0641\u0644\u0633\u0637\u064a\u0646\u064a')

    q = qp.parse(u("abc123 (xyz:123 OR qrs)"))
    assert text_type(q) == "(content:abc123 AND (abc123:123 OR content:qrs))"
def test_singlequotes():
    p = default.QueryParser(
        "t", None, [plugins.WhitespacePlugin(),
                    plugins.SingleQuotePlugin()])
    assert repr(p.process(
        "a 'b c' d")) == "<AndGroup <None:'a'>, <None:'b c'>, <None:'d'>>"
def test_empty_querystring():
    s = fields.Schema(content=fields.TEXT, title=fields.TEXT, id=fields.ID)
    qp = default.QueryParser("content", s)
    q = qp.parse(u(""))
    assert q == query.NullQuery
def test_fieldname_fieldname():
    qp = default.QueryParser("a", None)
    q = qp.parse("a:b:")
    assert q == query.Term("a", "b:")
def test_roundtrip():
    parser = default.QueryParser("a", None)
    q = parser.parse(u("a OR ((b AND c AND d AND e) OR f OR g) ANDNOT h"))
    assert text_type(
        q
    ) == "((a:a OR (a:b AND a:c AND a:d AND a:e) OR a:f OR a:g) ANDNOT a:h)"
def test_whitespace():
    p = default.QueryParser("t", None, [plugins.WhitespacePlugin()])
    assert repr(
        p.tag("hello there amiga")
    ) == "<AndGroup <None:'hello'>, < >, <None:'there'>, < >, <None:'amiga'>>"
def test_euro_chars():
    schema = fields.Schema(text=fields.TEXT)
    qp = default.QueryParser("text", schema)
    q = qp.parse(u("stra\xdfe"))
    assert q.__class__ == query.Term
    assert q.text == u("stra\xdfe")
Beispiel #9
0
def test_wild():
    p = default.QueryParser("t", None, [plugins.WhitespacePlugin(),
                                        plugins.WildcardPlugin()])
    assert_equal(repr(p.process("a b*c? d")),
                 "<AndGroup <None:'a'>, <None:Wild 'b*c?'>, <None:'d'>>")
Beispiel #10
0
def test_analyzing_terms():
    schema = fields.Schema(text=fields.TEXT(analyzer=analysis.StemmingAnalyzer()))
    qp = default.QueryParser("text", schema)
    q = qp.parse(u("Indexed!"))
    assert_equal(q.__class__, query.Term)
    assert_equal(q.text, "index")
Beispiel #11
0
def test_stopped():
    schema = fields.Schema(text=fields.TEXT)
    qp = default.QueryParser("text", schema)
    q = qp.parse(u("a b"), debug=True)
    assert_equal(q, query.NullQuery)
Beispiel #12
0
def test_groups():
    p = default.QueryParser("t", None, [plugins.WhitespacePlugin(),
                                        plugins.GroupPlugin()])

    ns = p.process("a ((b c) d) e")
    assert_equal(repr(ns), "<AndGroup <None:'a'>, <AndGroup <AndGroup <None:'b'>, <None:'c'>>, <None:'d'>>, <None:'e'>>")
Beispiel #13
0
def test_phrase():
    p = default.QueryParser("t", None, [plugins.WhitespacePlugin(),
                                        plugins.PhrasePlugin()])
    assert_equal(repr(p.process('a "b c"')), "<AndGroup <None:'a'>, <None:PhraseNode 'b c'~1>>")
    assert_equal(repr(p.process('"b c" d')), "<AndGroup <None:PhraseNode 'b c'~1>, <None:'d'>>")
    assert_equal(repr(p.process('"b c"')), "<AndGroup <None:PhraseNode 'b c'~1>>")
Beispiel #14
0
def test_function_plugin():
    class FakeQuery(query.Query):
        def __init__(self, children, *args, **kwargs):
            self.children = children
            self.args = args
            self.kwargs = kwargs
            self.fieldname = None

        def __hash__(self):
            return hash(tuple(self.children)) ^ hash(self.args)

        def __unicode__(self):
            qs = "|".join(str(q) for q in self.children)
            args = ",".join(self.args)
            kwargs = ",".join(
                sorted("%s:%s" % item for item in self.kwargs.items()))
            return u("<%s %s %s>") % (qs, args, kwargs)

        __str__ = __unicode__

    def fuzzy(qs, prefix=0, maxdist=2):
        prefix = int(prefix)
        maxdist = int(maxdist)
        return query.FuzzyTerm(qs[0].fieldname,
                               qs[0].text,
                               prefixlength=prefix,
                               maxdist=maxdist)

    fp = plugins.FunctionPlugin({"foo": FakeQuery, "fuzzy": fuzzy})
    qp = default.QueryParser("f", None)
    qp.add_plugin(fp)

    def check(qstring, target):
        q = qp.parse(u(qstring), normalize=False)
        assert str(q) == target

    check("alfa #foo charlie delta",
          "(f:alfa AND <  > AND f:charlie AND f:delta)")

    check("alfa #foo(charlie delta) echo",
          "(f:alfa AND <f:charlie|f:delta  > AND f:echo)")

    check("alfa #foo(charlie AND delta) echo",
          "(f:alfa AND <(f:charlie AND f:delta)  > AND f:echo)")

    check("alfa #foo[a] charlie delta",
          "(f:alfa AND < a > AND f:charlie AND f:delta)")

    check("alfa #foo[a, b](charlie delta) echo",
          "(f:alfa AND <f:charlie|f:delta a,b > AND f:echo)")

    check("alfa #foo[a,b,c=d](charlie AND delta) echo",
          "(f:alfa AND <(f:charlie AND f:delta) a,b c:d> AND f:echo)")

    check("alfa #foo[a,b,c=d]() (charlie AND delta)",
          "(f:alfa AND < a,b c:d> AND ((f:charlie AND f:delta)))")

    check(
        "alfa #foo[a=1,b=2](charlie AND delta)^2.0 echo",
        "(f:alfa AND <(f:charlie AND f:delta)  a:1,b:2,boost:2.0> AND f:echo)")

    check("alfa #fuzzy[maxdist=2](bravo) charlie",
          "(f:alfa AND f:bravo~2 AND f:charlie)")
def test_prefix():
    p = default.QueryParser(
        "t", None, [plugins.WhitespacePlugin(),
                    plugins.PrefixPlugin()])
    assert repr(p.process(
        "a b* c")) == "<AndGroup <None:'a'>, <None:'b'*>, <None:'c'>>"
def test_boosts():
    qp = default.QueryParser("t", None)
    q = qp.parse("alfa ((bravo^2)^3)^4 charlie")
    assert q.__unicode__() == "(t:alfa AND t:bravo^24.0 AND t:charlie)"
Beispiel #17
0
def test_wildcard2():
    qp = default.QueryParser("content", None)
    q = qp.parse(u("*the?e*"))
    assert_equal(q.__class__, query.Wildcard)
    assert_equal(q.text, "*the?e*")
def test_stopped():
    schema = fields.Schema(text=fields.TEXT)
    qp = default.QueryParser("text", schema)
    q = qp.parse(u("a b"))
    assert q == query.NullQuery
Beispiel #19
0
def test_sq_range():
    p = default.QueryParser("t", None, [plugins.WhitespacePlugin(),
                                        plugins.SingleQuotePlugin(),
                                        plugins.RangePlugin()])
    assert_equal(repr(p.process("['a b' to ']']")),
                 "<AndGroup <None:['a b' ']']>>")
def test_many_clauses():
    qs = "1" + (" OR 1" * 1000)

    parser = default.QueryParser("content", None)
    parser.parse(qs)
def test_unicode_num():
    schema = fields.Schema(num=fields.NUMERIC)
    parser = default.QueryParser(u("num"), schema=schema)
    q = parser.parse(u("num:1"))

    _ = text_type(q)
def test_fieldname_space():
    qp = default.QueryParser("a", None)
    q = qp.parse("Man Ray: a retrospective")
    assert text_type(q) == "(a:Man AND a:Ray: AND a:a AND a:retrospective)"
def test_phrase_boost():
    qp = default.QueryParser("f", None)
    q = qp.parse(u('Dahmen ANDMAYBE "Besov Spaces"^9'))
    assert isinstance(q, query.AndMaybe)
    assert q[0] == query.Term("f", u("Dahmen"))
    assert q[1] == query.Phrase("f", [u("Besov"), u("Spaces")], boost=9)
def test_operators():
    p = default.QueryParser(
        "t", None, [plugins.WhitespacePlugin(),
                    plugins.OperatorsPlugin()])
    ns = p.process("a OR b")
    assert repr(ns) == "<AndGroup <OrGroup <None:'a'>, <None:'b'>>>"
def test_andmaybe_none():
    schema = fields.Schema(f=fields.TEXT, year=fields.NUMERIC)
    qp = default.QueryParser("f", schema)
    _ = qp.parse(u("Dahmen ANDMAYBE @year:[2000 TO]"))