示例#1
0
 def test_get_document_count(self, mock_count):
     client = SearchIndexClient("endpoint", "index name", CREDENTIAL)
     client.get_document_count()
     assert mock_count.called
     assert mock_count.call_args[0] == ()
     assert len(mock_count.call_args[1]) == 1
     assert mock_count.call_args[1]["headers"] == client._headers
def authentication_with_api_key_credential():
    # [START create_search_client_with_key]
    from azure.search.documents import SearchApiKeyCredential, SearchIndexClient
    service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
    index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")
    key = os.getenv("AZURE_SEARCH_API_KEY")

    search_client = SearchIndexClient(service_endpoint, index_name,
                                      SearchApiKeyCredential(key))
    # [END create_search_client_with_key]

    result = search_client.get_document_count()

    print("There are {} documents in the {} search index.".format(
        result, repr(index_name)))
    def test_delete_documents_missing(self, api_key, endpoint, index_name, **kwargs):
        client = SearchIndexClient(
            endpoint, index_name, SearchApiKeyCredential(api_key)
        )
        results = client.delete_documents([{"hotelId": "1000"}, {"hotelId": "4"}])
        assert len(results) == 2
        assert set(x.status_code for x in results) == {200}

        # There can be some lag before a document is searchable
        time.sleep(3)

        assert client.get_document_count() == 9

        with pytest.raises(HttpResponseError):
            client.get_document(key="1000")

        with pytest.raises(HttpResponseError):
            client.get_document(key="4")
    def test_delete_documents_existing(self, api_key, endpoint, index_name, **kwargs):
        client = SearchIndexClient(
            endpoint, index_name, AzureKeyCredential(api_key)
        )
        results = client.delete_documents([{"hotelId": "3"}, {"hotelId": "4"}])
        assert len(results) == 2
        assert set(x.status_code for x in results) == {200}

        # There can be some lag before a document is searchable
        if self.is_live:
            time.sleep(TIME_TO_SLEEP)

        assert client.get_document_count() == 8

        with pytest.raises(HttpResponseError):
            client.get_document(key="3")

        with pytest.raises(HttpResponseError):
            client.get_document(key="4")
    def test_merge_or_upload_documents(self, api_key, endpoint, index_name, **kwargs):
        client = SearchIndexClient(
            endpoint, index_name, SearchApiKeyCredential(api_key)
        )
        results = client.merge_or_upload_documents(
            [{"hotelId": "1000", "rating": 1}, {"hotelId": "4", "rating": 2}]
        )
        assert len(results) == 2
        assert set(x.status_code for x in results) == {200, 201}

        # There can be some lag before a document is searchable
        time.sleep(3)

        assert client.get_document_count() == 11

        result = client.get_document(key="1000")
        assert result["rating"] == 1

        result = client.get_document(key="4")
        assert result["rating"] == 2
    def test_merge_documents_missing(self, api_key, endpoint, index_name, **kwargs):
        client = SearchIndexClient(
            endpoint, index_name, AzureKeyCredential(api_key)
        )
        results = client.merge_documents(
            [{"hotelId": "1000", "rating": 1}, {"hotelId": "4", "rating": 2}]
        )
        assert len(results) == 2
        assert set(x.status_code for x in results) == {200, 404}

        # There can be some lag before a document is searchable
        time.sleep(3)

        assert client.get_document_count() == 10

        with pytest.raises(HttpResponseError):
            client.get_document(key="1000")

        result = client.get_document(key="4")
        assert result["rating"] == 2
    def test_merge_documents_existing(self, api_key, endpoint, index_name, **kwargs):
        client = SearchIndexClient(
            endpoint, index_name, AzureKeyCredential(api_key)
        )
        results = client.merge_documents(
            [{"hotelId": "3", "rating": 1}, {"hotelId": "4", "rating": 2}]
        )
        assert len(results) == 2
        assert set(x.status_code for x in results) == {200}

        # There can be some lag before a document is searchable
        if self.is_live:
            time.sleep(TIME_TO_SLEEP)

        assert client.get_document_count() == 10

        result = client.get_document(key="3")
        assert result["rating"] == 1

        result = client.get_document(key="4")
        assert result["rating"] == 2
    def test_upload_documents_new(self, api_key, endpoint, index_name, **kwargs):
        client = SearchIndexClient(
            endpoint, index_name, SearchApiKeyCredential(api_key)
        )
        DOCUMENTS = [
            {"hotelId": "1000", "rating": 5, "rooms": [], "hotelName": "Azure Inn"},
            {"hotelId": "1001", "rating": 4, "rooms": [], "hotelName": "Redmond Hotel"},
        ]
        results = client.upload_documents(DOCUMENTS)
        assert len(results) == 2
        assert set(x.status_code for x in results) == {201}

        # There can be some lag before a document is searchable
        time.sleep(3)

        assert client.get_document_count() == 12
        for doc in DOCUMENTS:
            result = client.get_document(key=doc["hotelId"])
            assert result["hotelId"] == doc["hotelId"]
            assert result["hotelName"] == doc["hotelName"]
            assert result["rating"] == doc["rating"]
            assert result["rooms"] == doc["rooms"]
 def test_get_document_count(self, api_key, endpoint, index_name, **kwargs):
     client = SearchIndexClient(
         endpoint, index_name, AzureKeyCredential(api_key)
     )
     assert client.get_document_count() == 10
 def test_get_document_count(self, mock_count):
     client = SearchIndexClient("endpoint", "index name", CREDENTIAL)
     client.get_document_count()
     assert mock_count.called
     assert mock_count.call_args[0] == ()
     assert mock_count.call_args[1] == {}