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

    with pytest.raises(ValueError):
        QueryEngine("    ")
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")
Exemple #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
    )
Exemple #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
    )
Exemple #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
    )
Exemple #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
Exemple #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
Exemple #8
0
def test_fuzzy():
    """ Test queries that include a fuzzy (Levenshtein) match """
    with pytest.raises(QueryException):
        QueryEngine("domain:microsoft~0.8")
Exemple #9
0
def test_not():
    """ Check that NOT queries perform correctly """
    assert QueryEngine("key1:value1 AND NOT key2:value1").match(SIMPLE_DATA)
Exemple #10
0
def test_nested():
    """ Test that nested dictionary fields match correctly """
    assert QueryEngine("country:England AND data.weather:Rainy").match(COMPLEX_DATA)
Exemple #11
0
def test_basic():
    """ Test a very basic query with a Word in a single field """
    QueryEngine("key:value")
Exemple #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)
Exemple #13
0
def test_invalid_query():
    """ An invalid query string that luqum will not parse """
    with pytest.raises(QueryException):
        QueryEngine("foo(:bar)")
Exemple #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")
Exemple #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"
        )
Exemple #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)
Exemple #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)
Exemple #18
0
def test_none():
    """ Test that passing None raises a ValueError """
    with pytest.raises(ValueError):
        QueryEngine(None)
Exemple #19
0
def test_ambiguous_and():
    """ Ensure the default action for ambiguous queries is AND """
    assert QueryEngine("key1:value1 key2:value2").match(SIMPLE_DATA)
Exemple #20
0
def test_phrase():
    """ Ensure queries that generate a Phrase are matched """
    assert QueryEngine('key1:"value1"').match(SIMPLE_DATA)
Exemple #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)
Exemple #22
0
def test_range():
    """ Test queries that generate a Range() object """
    with pytest.raises(QueryException):
        QueryEngine("price:[0 TO 10]")