Esempio n. 1
0
def test_exclusive_match():
    f = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    f.exclusive_match("material.elements", "Al")
    res1 = f.search()
    assert check_field(res1, "material.elements", "Al") == 0

    f.exclusive_match("material.elements", ["Al", "Cu"])
    res2 = f.search()
    assert check_field(res2, "material.elements", "Al") == 1
    assert check_field(res2, "material.elements", "Cu") == 1
    assert check_field(res2, "material.elements", "Cp") == -1
    assert check_field(res2, "material.elements", "Fe") == -1
Esempio n. 2
0
def test_add_sort_external():
    f = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    # Sort ascending by atomic number
    f.match_field("mdf.source_name", "oqmd")
    f.add_sort('crystal_structure.number_of_atoms', True)
    res = f.search(limit=1, reset_query=False)
    assert res[0]['crystal_structure']['number_of_atoms'] == 1

    # Sort descending by composition, with multi-sort
    f.add_sort('material.composition', False)
    res = f.search(limit=1)
    assert res[0]['crystal_structure']['number_of_atoms'] == 1
    assert res[0]['material']['composition'].startswith('Zr')
Esempio n. 3
0
def test_anonymous(capsys):
    f = SearchHelper(INDEX, anonymous=True)
    # Test search
    assert len(
        f.search("mdf.source_name:ab_initio_solute_database",
                 advanced=True,
                 limit=300)) == 300
Esempio n. 4
0
def test_match_field():
    f = SearchHelper(INDEX, search_client=SEARCH_CLIENT)

    # Basic usage
    f.match_field("mdf.source_name", "khazana_vasp")
    res1 = f.search()
    assert check_field(res1, "mdf.source_name", "khazana_vasp") == 0

    # Check that query clears
    assert f.current_query() == ""

    # Also checking check_field and no-op
    f.match_field("material.elements", "Al")
    f.match_field("", "")
    res2 = f.search()  # Enough so that we'd find at least 1 non-Al example
    assert check_field(res2, "material.elements", "Al") == 1
Esempio n. 5
0
def test_reset_query():
    f = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    # Term will return results
    f.match_field("material.elements", "Al")
    f.reset_query()

    # Specifying no query will raise an error
    with pytest.raises(ValueError):
        assert f.search() == []
Esempio n. 6
0
def test_exclude_field():
    f = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    # Basic usage
    f.exclude_field("material.elements", "Al")
    f.exclude_field("", "")
    f.match_field("mdf.source_name", "ab_initio_solute_database")
    f.match_field("mdf.resource_type", "record")
    res1 = f.search()
    assert check_field(res1, "material.elements", "Al") == -1
Esempio n. 7
0
def test_search(capsys):
    # Error on no query
    with pytest.raises(ValueError):
        f = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
        f.search()

    # Return info if requested
    res2 = f.search("Al", info=False)
    assert isinstance(res2, list)
    assert isinstance(res2[0], dict)

    res3 = f.search("Al", info=True)
    assert isinstance(res3, tuple)
    assert isinstance(res3[0], list)
    assert isinstance(res3[0][0], dict)
    assert isinstance(res3[1], dict)

    # Check limit
    res4 = f.match_term("Al").search(limit=3)
    assert len(res4) == 3

    # Check reset_query
    f.match_field("mdf.source_name", "ta_melting")
    res5 = f.search(reset_query=False)
    res6 = f.search()
    assert all([r in res6 for r in res5]) and all([r in res5 for r in res6])

    # Check default index
    f2 = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    assert (f2.match_term("data").search(
        limit=1,
        info=True)[1]["index_uuid"] == mdf_toolbox.translate_index(INDEX))
Esempio n. 8
0
def test_exclude_range():
    # Single-value use
    f = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    f.exclude_range("material.elements", "Am", "*")
    f.exclude_range("material.elements", "*", "Ak")
    f.match_field("material.elements", "*")
    res1, info1 = f.search(info=True)
    assert (check_field(res1, "material.elements", "Al") == 0
            or check_field(res1, "material.elements", "Al") == 2)

    res2, info2 = f.search("material.elements:Al", advanced=True, info=True)
    assert info1["total_query_matches"] <= info2["total_query_matches"]

    # Non-matching use, test inclusive
    f.exclude_range("material.elements", "Am", "*")
    f.exclude_range("material.elements", "*", "Ak")
    f.exclude_range("material.elements", "Al", "Al", inclusive=False)
    f.match_field("material.elements", "*")
    res3, info3 = f.search(info=True)
    assert info1["total_query_matches"] == info3["total_query_matches"]

    # Nothing to match
    assert f.exclude_range("field", start=None, stop=None) == f
Esempio n. 9
0
def test_match_range():
    # Single-value use
    f = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    f.match_range("material.elements", "Al", "Al")
    res1, info1 = f.search(info=True)
    assert check_field(res1, "material.elements", "Al") == 1

    res2, info2 = f.search("material.elements:Al", advanced=True, info=True)
    assert info1["total_query_matches"] == info2["total_query_matches"]

    # Non-matching use, test inclusive
    f.match_range("material.elements", "Al", "Al", inclusive=False)
    assert f.search() == []

    # Actual range
    f.match_range("material.elements", "Al", "Cu")
    res4, info4 = f.search(info=True)
    assert info1["total_query_matches"] < info4["total_query_matches"]
    assert (check_field(res4, "material.elements", "Al") >= 0
            or check_field(res4, "material.elements", "Cu") >= 0)

    # Nothing to match
    assert f.match_range("field", start=None, stop=None) == f
Esempio n. 10
0
def test_chaining():
    # Internal
    q = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    q._field("source_name", "cip")
    q._and_join()
    q._field("elements", "Al")
    res1 = q._ex_search(limit=10000)
    res2 = (SearchHelper(INDEX, search_client=SEARCH_CLIENT)._field(
        "source_name", "cip")._and_join()._field("elements",
                                                 "Al")._ex_search(limit=10000))
    assert all([r in res2 for r in res1]) and all([r in res1 for r in res2])

    # External
    f = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    f.match_field("source_name", "cip")
    f.match_field("material.elements", "Al")
    res1 = f.search()
    res2 = f.match_field("source_name",
                         "cip").match_field("material.elements",
                                            "Al").search()
    assert all([r in res2 for r in res1]) and all([r in res1 for r in res2])
Esempio n. 11
0
def test_match_not_exists():
    f = SearchHelper(INDEX, search_client=SEARCH_CLIENT)
    # Basic usage
    f.match_not_exists("services.citrine")
    assert check_field(f.search(), "services.citrine", ".*") == -1