示例#1
0
def test_empty():
    """ Test that empty queries, including whitespace, raise a ValueError """
    with pytest.raises(ValueError):
        QueryEngine("")

    with pytest.raises(ValueError):
        QueryEngine("    ")
示例#2
0
def test_ambiguous():
    """ Test queries that lack AND/OR so are ambiguous """

    # This should work, the default action is to replace with AND
    QueryEngine("key:value oops:ambiguous")

    with pytest.raises(QueryException):
        QueryEngine("key:value oops:ambiguous", ambiguous_action="Exception")
示例#3
0
def test_simple_or():
    """ Test that a simple OR query works """
    assert QueryEngine("key1:value1 OR key2:value2", short_circuit=True).match(
        SIMPLE_DATA
    )
    assert QueryEngine("key1:value1 OR key2:value2", short_circuit=False).match(
        SIMPLE_DATA
    )
示例#4
0
def test_impossible_and():
    """ Test a condition that can never be true """
    assert (
        QueryEngine("key1:value1 AND key1:value2", short_circuit=True).match(
            SIMPLE_DATA
        )
        is False
    )
    assert (
        QueryEngine("key1:value1 AND key1:value2", short_circuit=False).match(
            SIMPLE_DATA
        )
        is False
    )
示例#5
0
def test_simple_and():
    """ Test that a simple AND query works """
    assert (
        QueryEngine("key1:value1 AND key2:value2", short_circuit=True).match(
            SIMPLE_DATA
        )
        is True
    )
示例#6
0
def test_missing_nested():
    """ Test for a nested key that does not exist, ensuring it does not raise an exception. """
    assert QueryEngine("foo.bar.baz:missing").match(SIMPLE_DATA) is False
示例#7
0
def test_missing():
    """ Test for a key that does not exist, ensuring it does not raise an exception. """
    assert QueryEngine("key3:value3").match(SIMPLE_DATA) is False
示例#8
0
def test_fuzzy():
    """ Test queries that include a fuzzy (Levenshtein) match """
    with pytest.raises(QueryException):
        QueryEngine("domain:microsoft~0.8")
示例#9
0
def test_not():
    """ Check that NOT queries perform correctly """
    assert QueryEngine("key1:value1 AND NOT key2:value1").match(SIMPLE_DATA)
示例#10
0
def test_nested():
    """ Test that nested dictionary fields match correctly """
    assert QueryEngine("country:England AND data.weather:Rainy").match(COMPLEX_DATA)
示例#11
0
def test_basic():
    """ Test a very basic query with a Word in a single field """
    QueryEngine("key:value")
示例#12
0
def test_max_depth():
    """ Query that generates an AST requiring too much recursion """
    with pytest.raises(QueryException):
        QueryEngine("((foo:bar) AND ((bar:baz) OR (baz:foo)))", max_depth=2)
示例#13
0
def test_invalid_query():
    """ An invalid query string that luqum will not parse """
    with pytest.raises(QueryException):
        QueryEngine("foo(:bar)")
示例#14
0
def test_invalid_args():
    """ An invalid action to take when statement without AND/OR is found """
    with pytest.raises(ValueError):
        QueryEngine("foo bar", ambiguous_action="DANCE")
示例#15
0
def test_bare_field():
    """ Test that passing a value with no fieldname raises NotImplementedError during matching """
    with pytest.raises(NotImplementedError):
        QueryEngine("value1", allow_bare_field=True).match(
            SIMPLE_DATA, default_field="key1"
        )
示例#16
0
def test_bare_field():
    """ Test that values without a field name are handled correctly """
    QueryEngine("value", allow_bare_field=True)

    with pytest.raises(QueryException):
        QueryEngine("value", allow_bare_field=False)
示例#17
0
def test_grouped_or():
    """ Test a more complex OR that includes a grouped condition with nested fields """
    assert QueryEngine(
        "country:France OR (country:England AND data.weather:Rainy)"
    ).match(COMPLEX_DATA)
示例#18
0
def test_none():
    """ Test that passing None raises a ValueError """
    with pytest.raises(ValueError):
        QueryEngine(None)
示例#19
0
def test_ambiguous_and():
    """ Ensure the default action for ambiguous queries is AND """
    assert QueryEngine("key1:value1 key2:value2").match(SIMPLE_DATA)
示例#20
0
def test_phrase():
    """ Ensure queries that generate a Phrase are matched """
    assert QueryEngine('key1:"value1"').match(SIMPLE_DATA)
示例#21
0
def test_default_field():
    """ Test that matching an unnamed field without passing the default key raises MatchException """
    with pytest.raises(MatchException):
        QueryEngine("foo", allow_bare_field=True).match(SIMPLE_DATA)
示例#22
0
def test_range():
    """ Test queries that generate a Range() object """
    with pytest.raises(QueryException):
        QueryEngine("price:[0 TO 10]")