Esempio n. 1
0
def test_service_id_hash_not_in_searched_fields(services_mapping):
    query = construct_query(services_mapping, build_query_params(keywords="some keywords"))

    assert not any("serviceIdHash" in key for key in query['query']['simple_query_string']['fields'])

    query = construct_query(services_mapping, build_query_params(filters={'serviceCategories': ["serviceType1"]}))

    assert not any("serviceIdHash" in key for key in query['highlight']['fields'])
Esempio n. 2
0
def test_aggregations_terms_added_for_each_param(services_mapping, aggregations):
    query = construct_query(services_mapping, build_query_params(), aggregations=aggregations)

    assert set(aggregations) == {x for x in query['aggregations']}
    assert {"_".join(("dmagg", x)) for x in aggregations} == {
        v['terms']['field'] for k, v in query['aggregations'].items()
    }
Esempio n. 3
0
def test_should_not_filter_on_unknown_keys(services_mapping):
    params = build_query_params(filters={'lot': "SaaS", 'serviceCategories': ["serviceCategories"]})
    params.add("this", "that")
    query = construct_query(services_mapping, params)
    terms = query["query"]["bool"]["filter"]["bool"]["must"]
    assert {"term": {'dmfilter_serviceCategories': 'serviceCategories'}} in terms
    assert {"term": {'dmfilter_lot': 'SaaS'}} in terms
    assert {"term": {'unknown': 'something to ignore'}} not in terms
Esempio n. 4
0
def test_should_make_multi_match_query_if_keywords_supplied(services_mapping):
    keywords = "these are my keywords"
    query = construct_query(services_mapping, build_query_params(keywords))
    assert "query" in query
    assert "simple_query_string" in query["query"]
    query_string_clause = query["query"]["simple_query_string"]
    assert query_string_clause["query"] == keywords
    assert query_string_clause["default_operator"] == "and"
    assert frozenset(query_string_clause["fields"]) == frozenset(
        "_".join(("dmtext", f)) for f in services_mapping.fields_by_prefix["dmtext"]
    )
Esempio n. 5
0
def test_should_have_filtered_term_service_types_clause(services_mapping):
    query = construct_query(
        services_mapping,
        build_query_params(filters={'serviceCategories': ["serviceCategories"]})
    )
    assert "term" in query["query"]["bool"]["filter"]["bool"]["must"][0]
    assert (
        query["query"]["bool"]["filter"]["bool"]["must"][0]["term"]["dmfilter_serviceCategories"]
        ==
        "serviceCategories"
    )
Esempio n. 6
0
def test_should_have_filtered_root_element_and_match_keywords(services_mapping):
    query = construct_query(
        services_mapping,
        build_query_params(
            keywords="some keywords",
            filters={'serviceCategories': ["my serviceCategories"]}
        )
    )["query"]["bool"]["must"]

    assert "simple_query_string" in query
    query_string_clause = query["simple_query_string"]
    assert query_string_clause["query"] == "some keywords"
    assert query_string_clause["default_operator"] == "and"
    assert frozenset(query_string_clause["fields"]) == frozenset(
        "_".join(("dmtext", f)) for f in services_mapping.fields_by_prefix["dmtext"]
    )
Esempio n. 7
0
def test_should_have_filtered_term_for_multiple_service_types_clauses(services_mapping):
    query = construct_query(
        services_mapping,
        build_query_params(
            filters={
                'serviceCategories': [
                    "serviceCategories1",
                    "serviceCategories2",
                    "serviceCategories3"
                ]
            }
        )
    )
    terms = query["query"]["bool"]["filter"]["bool"]["must"]
    assert {"term": {'dmfilter_serviceCategories': 'serviceCategories1'}} in terms
    assert {"term": {'dmfilter_serviceCategories': 'serviceCategories2'}} in terms
    assert {"term": {'dmfilter_serviceCategories': 'serviceCategories3'}} in terms
Esempio n. 8
0
def test_highlight_block_contains_correct_fields(services_mapping, example):
    query = construct_query(services_mapping, build_query_params(keywords="some keywords"))

    assert "highlight" in query
    assert any(re.match(field, "dmtext_" + example) for field in query["highlight"]["fields"]), example
Esempio n. 9
0
def test_should_be_able_to_override_pagesize(services_mapping):
    assert construct_query(services_mapping, build_query_params(), page_size=10)["size"] == 10
Esempio n. 10
0
def test_should_have_filtered_root_element_if_lot_search(services_mapping):
    query = construct_query(services_mapping, build_query_params(filters={'lot': "SaaS"}))
    assert "query" in query
    assert "bool" in query["query"]
    assert "must" in query["query"]["bool"]
Esempio n. 11
0
def test_should_have_match_all_query_if_no_params(services_mapping):
    assert "query" in construct_query(services_mapping, build_query_params())
    assert "match_all" in construct_query(services_mapping, build_query_params())["query"]
Esempio n. 12
0
def test_should_have_filtered_term_for_lot_and_service_types_clause(services_mapping):
    query = construct_query(services_mapping,
                            build_query_params(filters={'lot': "SaaS", 'serviceCategories': ["serviceCategories"]}))
    terms = query["query"]["bool"]["filter"]["bool"]["must"]
    assert {"term": {'dmfilter_serviceCategories': 'serviceCategories'}} in terms
    assert {"term": {'dmfilter_lot': 'SaaS'}} in terms
Esempio n. 13
0
def test_should_have_filtered_term_lot_clause(services_mapping):
    query = construct_query(services_mapping, build_query_params(filters={'lot': "SaaS"}))
    assert "term" in query["query"]["bool"]["filter"]["bool"]["must"][0]
    assert query["query"]["bool"]["filter"]["bool"]["must"][0]["term"]["dmfilter_lot"] == "SaaS"
Esempio n. 14
0
def test_should_have_correct_root_element(services_mapping):
    assert "query" in construct_query(services_mapping, build_query_params())
Esempio n. 15
0
def test_page_should_set_from_parameter(services_mapping):
    assert construct_query(services_mapping, build_query_params(page=2))["from"] == 100
Esempio n. 16
0
def test_should_have_no_from_by_default(services_mapping):
    assert "from" not in construct_query(services_mapping, build_query_params())
Esempio n. 17
0
def test_should_have_page_size_set(services_mapping):
    assert construct_query(services_mapping, build_query_params())["size"] == 100
Esempio n. 18
0
def test_aggregations_root_element_not_present_if_no_aggregations(services_mapping):
    assert 'aggregations' not in construct_query(services_mapping, build_query_params(), aggregations=[])
Esempio n. 19
0
def test_should_have_highlight_block_on_filtered_search(services_mapping):
    query = construct_query(services_mapping, build_query_params(keywords="some keywords"))

    assert "highlight" in query
Esempio n. 20
0
def test_aggregation_throws_error_if_not_implemented(services_mapping):
    with pytest.raises(ValueError):
        construct_query(services_mapping, build_query_params(), aggregations=['missing'])
Esempio n. 21
0
def test_highlight_block_sets_encoder_to_html(services_mapping):
    query = construct_query(services_mapping, build_query_params(keywords="some keywords"))

    assert query["highlight"]["encoder"] == "html"
Esempio n. 22
0
def test_should_have_filtered_root_element_if_service_types_search(services_mapping):
    query = construct_query(services_mapping, build_query_params(filters={'serviceCategories': ["serviceCategories"]}))
    assert "query" in query
    assert "bool" in query["query"]
    assert "must" in query["query"]["bool"]
Esempio n. 23
0
def test_sort_results_by_score_and_service_id_hash(services_mapping):
    query = construct_query(services_mapping, build_query_params(keywords="some keywords"))
    assert query['sort'] == ['_score', {"sortonly_serviceIdHash": 'desc'}]
Esempio n. 24
0
def test_should_have_filtered_root_element_and_match_all_if_no_keywords(services_mapping):
    query = construct_query(
        services_mapping,
        build_query_params(filters={'serviceCategories': ["my serviceCategories"]})
    )
    assert "match_all" in query["query"]["bool"]["must"]
Esempio n. 25
0
def test_should_make_multi_match_query_if_keywords_supplied(services_mapping):
    keywords = "these are my keywords"
    query = construct_query(services_mapping, build_query_params(keywords))
    assert "query" in query
    assert "simple_query_string" in query["query"]
    query_string_clause = query["query"]["simple_query_string"]
    assert query_string_clause["query"] == keywords
    assert query_string_clause["default_operator"] == "and"
    assert frozenset(query_string_clause["fields"]) == frozenset(
        "_".join(("dmtext", f)) for f in services_mapping.fields_by_prefix["dmtext"]
    )


@pytest.mark.parametrize("query,expected", (
    (build_query_params(), False),
    (build_query_params(keywords="lot"), False),
    (build_query_params(filters={'lot': "lot"}), True),
    (build_query_params(keywords="something", filters={'lot': "lot"}), True),
    (build_query_params(filters={'serviceCategories': ["serviceCategories"]}), True)
))
def test_should_identify_filter_search_from_query_params(services_mapping, query, expected):
    assert is_filtered(services_mapping, query) == expected, query


def test_should_have_filtered_root_element_if_service_types_search(services_mapping):
    query = construct_query(services_mapping, build_query_params(filters={'serviceCategories': ["serviceCategories"]}))
    assert "query" in query
    assert "bool" in query["query"]
    assert "must" in query["query"]["bool"]