Beispiel #1
0
def test_term():
    q = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    # Single match test
    assert isinstance(q._term("term1"), SearchHelper)
    assert q._SearchHelper__query["q"] == "(term1"
    assert q.initialized is True
    # Multi-match test
    q._and_join()._term("term2")
    assert q._SearchHelper__query["q"] == "(term1 AND term2"
    # Grouping test
    q._or_join(close_group=True)._term("term3")
    assert q._SearchHelper__query["q"] == "(term1 AND term2) OR (term3"
Beispiel #2
0
def test_field():
    q1 = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    # Single field and return value test
    assert isinstance(q1._field("mdf.source_name", "oqmd"), SearchHelper)
    assert q1._SearchHelper__query["q"] == "(mdf.source_name:oqmd"
    # Multi-field and grouping test
    q1._and_join(close_group=True)._field("dc.title", "sample")
    assert q1._SearchHelper__query[
        "q"] == "(mdf.source_name:oqmd) AND (dc.title:sample"
    # Negation test
    q1._negate()
    assert q1._SearchHelper__query[
        "q"] == "(mdf.source_name:oqmd) AND (dc.title:sample NOT "
    # Explicit operator test
    # Makes invalid query for this case
    q1._operator("NOT")
    assert q1._SearchHelper__query[
        "q"] == "(mdf.source_name:oqmd) AND (dc.title:sample NOT  NOT "
    # Ensure advanced is set
    assert q1._SearchHelper__query["advanced"] is True

    # Test noop on blanks
    q2 = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    assert q2._SearchHelper__query["q"] == "("
    q2._field(field="", value="value")
    assert q2._SearchHelper__query["q"] == "("
    q2._field(field="field", value="")
    assert q2._SearchHelper__query["q"] == "("
    q2._field(field="", value="")
    assert q2._SearchHelper__query["q"] == "("
    q2._field(field="field", value="value")
    assert q2._SearchHelper__query["q"] == "(field:value"

    # Test auto-quote
    q3 = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    q3._field("dc.descriptions.description", "With Spaces")
    assert q3._SearchHelper__query[
        "q"] == '(dc.descriptions.description:"With Spaces"'
    q3._and_join(close_group=True)._field("dc.title", "Mark's")
    assert q3._SearchHelper__query["q"] == (
        '(dc.descriptions.description:"With Spaces") AND ('
        'dc.title:"Mark\'s"')
    q3._or_join(close_group=False)._field("dc.title", "The\nLarch")
    assert q3._SearchHelper__query["q"] == (
        '(dc.descriptions.description:"With Spaces") AND ('
        'dc.title:"Mark\'s" OR dc.title:"The\nLarch"')
    # No auto-quote on ranges
    q3._and_join(close_group=True)._field("block.range", "[5 TO 6]")
    assert q3._SearchHelper__query["q"] == (
        '(dc.descriptions.description:"With Spaces") AND ('
        'dc.title:"Mark\'s" OR dc.title:"The\nLarch") AND ('
        'block.range:[5 TO 6]')
Beispiel #3
0
def test_or_join(capsys):
    q = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    # Test not initialized
    with pytest.raises(ValueError) as excinfo:
        q._or_join()
    assert 'before adding an operator' in str(excinfo.value)

    # Regular join
    q._term("foo")._or_join()
    assert q._SearchHelper__query["q"] == "(foo OR "

    # close_group
    q._term("bar")._or_join(close_group=True)
    assert q._SearchHelper__query["q"] == "(foo OR bar) OR ("