Exemple #1
0
def test_generate_inspire_search_links_no_self(inspire_app):
    expected_search_links = {}
    links = inspire_search_links({})
    assert links == expected_search_links

    links = inspire_search_links(None)
    assert links == expected_search_links
Exemple #2
0
def test_generate_inspire_search_links_gets_proper_formats(inspire_app):
    expected_links_test2 = {
        "self": "http://localhost:5000/api/test2/?q=&size=10&page=1",
        "format4": "http://localhost:5000/api/test2/?q=&size=10&page=1&format=format4",
        "format5": "http://localhost:5000/api/test2/?q=&size=10&page=1&format=format5",
    }
    expected_links_test = {
        "self": "http://localhost:5000/api/test/?q=&size=10&page=1",
        "format1": "http://localhost:5000/api/test/?q=&size=10&page=1&format=format1",
        "format2": "http://localhost:5000/api/test/?q=&size=10&page=1&format=format2",
        "format3": "http://localhost:5000/api/test/?q=&size=10&page=1&format=format3",
    }
    config = {
        "TEST": {
            "search_serializers_aliases": {
                "format1": "format/1",
                "format2": "format/2",
                "format3": "format/3",
            }
        },
        "TEST2": {
            "search_serializers_aliases": {"format4": "format/4", "format5": "format/5"}
        },
    }
    with override_config(**config):
        links_test = {"self": "http://localhost:5000/api/test/?q=&size=10&page=1"}
        links_test2 = {"self": "http://localhost:5000/api/test2/?q=&size=10&page=1"}
        with mock.patch("inspirehep.records.links.request") as mock_request:
            mock_request.values = MultiDict()
            mock_request.path = "/test"
            links_test = inspire_search_links(links_test)
            mock_request.path = "/test2"
            links_test2 = inspire_search_links(links_test2)
    assert links_test == expected_links_test
    assert links_test2 == expected_links_test2
Exemple #3
0
 def serialize_search(self,
                      pid_fetcher,
                      search_result,
                      links=None,
                      item_links_factory=None,
                      **kwargs):
     """Serialize a search result.
     :param pid_fetcher: Persistent identifier fetcher.
     :param search_result: Elasticsearch search result.
     :param links: Dictionary of links to add to response.
     """
     links = inspire_search_links(links)
     data = dict(
         hits=dict(
             hits=[
                 self.transform_search_hit(pid_fetcher(
                     hit["_id"], hit["_source"]),
                                           hit,
                                           links_factory=item_links_factory,
                                           **kwargs)
                 for hit in search_result["hits"]["hits"]
             ],
             total=search_result["hits"]["total"]["value"],
         ),
         links=links,
     )
     sort_options = self._get_sort_options()
     if sort_options:
         data["sort_options"] = sort_options
     return json.dumps(data, **self._format_args())
Exemple #4
0
def test_search_links_with_fields_filtering(inspire_app):
    expected_links_test = {
        "self": "http://localhost:5000/api/test/?q=&size=10&page=1&fields=ids,authors",
        "next": "http://localhost:5000/api/test/?q=&size=10&page=2&fields=ids,authors",
        "format1": "http://localhost:5000/api/test/?q=&size=10&page=1&format=format1",
        "json": "http://localhost:5000/api/test/?q=&size=10&page=1&fields=ids,authors&format=json",
    }
    config = {
        "TEST": {
            "search_serializers_aliases": {
                "format1": "format/1",
                "json": "application/json",
            }
        }
    }
    with override_config(**config):
        links_test = {
            "self": "http://localhost:5000/api/test/?q=&size=10&page=1",
            "next": "http://localhost:5000/api/test/?q=&size=10&page=2",
        }
        with mock.patch("inspirehep.records.links.request") as mock_request:
            mock_request.path = "/test"
            mock_request.values = MultiDict([("fields", "ids,authors")])
            links_test = inspire_search_links(links_test)
    assert links_test == expected_links_test