Exemplo n.º 1
0
 def test_request_too_large_error(self):
     with mock.patch.object(
             SearchClient,
             "_index_documents_actions",
             side_effect=RequestEntityTooLargeError("Error")):
         client = SearchClient("endpoint", "index name", CREDENTIAL)
         batch = IndexDocumentsBatch()
         batch.add_upload_actions("upload1")
         with pytest.raises(RequestEntityTooLargeError):
             client.index_documents(batch, extra="foo")
Exemplo n.º 2
0
    def test_repr(self):
        batch = IndexDocumentsBatch()
        assert repr(batch) == "<IndexDocumentsBatch [0 actions]>"

        batch._actions = [1, 2, 3]
        assert repr(batch) == "<IndexDocumentsBatch [3 actions]>"

        # a strict length test here would require constructing an actions list
        # with a length of ~10**24, so settle for this simple sanity check on
        # an extreme case.
        batch_actions = list(range(2000))
        assert len(repr(batch)) <= 1024
    def test_index_documents(self, mock_index):
        client = SearchIndexClient("endpoint", "index name", CREDENTIAL)

        batch = IndexDocumentsBatch()
        batch.add_upload_documents("upload1")
        batch.add_delete_documents("delete1", "delete2")
        batch.add_merge_documents(["merge1", "merge2", "merge3"])
        batch.add_merge_or_upload_documents("merge_or_upload1")

        client.index_documents(batch, extra="foo")
        assert mock_index.called
        assert mock_index.call_args[0] == ()
        assert len(mock_index.call_args[1]) == 2
        assert mock_index.call_args[1]["extra"] == "foo"
        index_documents = mock_index.call_args[1]["batch"]
        assert isinstance(index_documents, IndexBatch)
        assert index_documents.actions == batch.actions
Exemplo n.º 4
0
    def test_index_documents(self, mock_index):
        client = SearchClient("endpoint", "index name", CREDENTIAL)

        batch = IndexDocumentsBatch()
        actions = batch.add_upload_actions("upload1")
        assert len(actions) == 1
        for x in actions:
            assert x.action_type == "upload"
        actions = batch.add_delete_actions("delete1", "delete2")
        assert len(actions) == 2
        for x in actions:
            assert x.action_type == "delete"
        actions = batch.add_merge_actions(["merge1", "merge2", "merge3"])
        for x in actions:
            assert x.action_type == "merge"
        actions = batch.add_merge_or_upload_actions("merge_or_upload1")
        for x in actions:
            assert x.action_type == "mergeOrUpload"

        client.index_documents(batch, extra="foo")
        assert mock_index.called
        assert mock_index.call_args[0] == ()
        assert len(mock_index.call_args[1]) == 4
        assert mock_index.call_args[1]["headers"] == client._headers
        assert mock_index.call_args[1]["extra"] == "foo"
Exemplo n.º 5
0
    def test_add_method(self, method_name):
        batch = IndexDocumentsBatch()

        method = getattr(batch, method_name)

        method("doc1")
        assert len(batch.actions) == 1

        method("doc2", "doc3")
        assert len(batch.actions) == 3

        method(["doc4", "doc5"])
        assert len(batch.actions) == 5

        method(("doc6", "doc7"))
        assert len(batch.actions) == 7

        assert all(
            action.action_type == METHOD_MAP[method_name] for action in batch.actions
        )
        assert all(type(action) == IndexAction for action in batch.actions)

        expected = ["doc{}".format(i) for i in range(1, 8)]
        assert [action.additional_properties for action in batch.actions] == expected
Exemplo n.º 6
0
 def test_actions_returns_list_copy(self):
     batch = IndexDocumentsBatch()
     batch.actions.extend([1, 2, 3])
     assert type(batch.actions) is list
     assert batch.actions == []
     assert batch.actions is not batch._actions
Exemplo n.º 7
0
 def test_init(self):
     batch = IndexDocumentsBatch()
     assert batch.actions == []