Example #1
0
def test_experiment_institution_aggregation_and_filter(inspire_app,
                                                       override_config):
    config = {
        "RECORDS_REST_FACETS": {
            "records-experiments": {
                "filters": {
                    **current_app.config["EXPERIMENTS_FILTERS"]
                },
                "aggs": {
                    **experiment_institution_aggregation(1)
                },
            }
        }
    }
    with override_config(**config):
        create_record("exp", data={"institutions": [{"value": "CERN"}]})
        create_record("exp", data={"institutions": [{"value": "CERN"}]})
        desy_record = create_record("exp",
                                    data={"institutions": [{
                                        "value": "DESY"
                                    }]})

        # aggregation
        with inspire_app.test_client() as client:
            response = client.get("/experiments/facets").json
        expected_aggregation = {
            "meta": {
                "title": "Institution",
                "type": "checkbox",
                "order": 1
            },
            "doc_count_error_upper_bound":
            0,
            "sum_other_doc_count":
            0,
            "buckets": [
                {
                    "key": "CERN",
                    "doc_count": 2
                },
                {
                    "key": "DESY",
                    "doc_count": 1
                },
            ],
        }
        assert response["aggregations"]["institution"] == expected_aggregation

        # filter
        with inspire_app.test_client() as client:
            response = client.get("/experiments?institution=DESY").json
        assert len(response["hits"]["hits"]) == 1
        assert (response["hits"]["hits"][0]["metadata"]["control_number"] ==
                desy_record["control_number"])
def records_experiments(order=None):
    if order is None:
        order = count(start=1)
    return {
        "filters": {
            **current_app.config["EXPERIMENTS_FILTERS"]
        },
        "aggs": {
            **experiment_inspire_classification_aggregation(order=next(order)),
            **experiment_institution_aggregation(order=next(order)),
        },
    }
def test_records_experiments_facets(inspire_app):
    with current_app.test_request_context():
        expected_filters = {"classification", "institution"}
        expected_aggregations = {
            **experiment_inspire_classification_aggregation(order=1),
            **experiment_institution_aggregation(order=2),
        }

        filters = current_app.config["RECORDS_REST_FACETS"][
            "records-experiments"]()["filters"].keys()
        aggregations = current_app.config["RECORDS_REST_FACETS"][
            "records-experiments"]()["aggs"]
        assert filters == expected_filters
        assert aggregations == expected_aggregations