From 6482dc3cd91c10aa9d381d342abf91eb25240b83 Mon Sep 17 00:00:00 2001 From: Rob Rudin Date: Thu, 4 Jan 2024 17:59:49 -0500 Subject: [PATCH] Can now perform update with serialized plan Realized this was missing when trying to debug something with client. --- marklogic/rows.py | 10 ++++++---- tests/remove-uri-plan.json | 31 +++++++++++++++++++++++++++++++ tests/test_rows_update.py | 14 ++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/remove-uri-plan.json diff --git a/marklogic/rows.py b/marklogic/rows.py index 22d657e..80c8852 100644 --- a/marklogic/rows.py +++ b/marklogic/rows.py @@ -83,6 +83,7 @@ def query( def update( self, dsl: str = None, + plan: dict = None, format: str = "json", tx: Transaction = None, return_response: bool = False, @@ -90,14 +91,15 @@ def update( ): """ Sends an update query to an endpoint at the MarkLogic rows service defined at - https://docs.marklogic.com/REST/client/row-management. Note that this feature - requires the use of MarkLogic version 11.2 or later. + https://docs.marklogic.com/REST/client/row-management. One of 'dsl' or + 'plan' must be defined. This feature requires the use of MarkLogic version + 11.2 or later. For more information about Optic Update and using the Optic DSL, see https://docs.marklogic.com/guide/app-dev/OpticAPI. - TODO - add links for Optic Update. :param dsl: an Optic DSL query + :param plan: a serialized Optic query :param tx: optional REST transaction in which to service this request. :param return_response: boolean specifying if the entire original response object should be returned (True) or if only the data should be returned (False) @@ -106,7 +108,7 @@ def update( """ path = "v1/rows/update" return self.__send_request( - path, dsl, None, None, None, None, format, tx, return_response, **kwargs + path, dsl, plan, None, None, None, format, tx, return_response, **kwargs ) def __send_request( diff --git a/tests/remove-uri-plan.json b/tests/remove-uri-plan.json new file mode 100644 index 0000000..75fee4a --- /dev/null +++ b/tests/remove-uri-plan.json @@ -0,0 +1,31 @@ +{ + "$optic" : { + "ns" : "op", + "fn" : "operators", + "args" : [ { + "ns" : "op", + "fn" : "from-doc-uris", + "args" : [ { + "ns" : "cts", + "fn" : "document-query", + "args" : [ [ { + "ns" : "xs", + "fn" : "string", + "args" : [ "/temp/doc2.json" ] + } ] ] + }, null ] + }, { + "ns" : "op", + "fn" : "remove", + "args" : [ { + "ns" : "op", + "fn" : "col", + "args" : [ { + "ns" : "xs", + "fn" : "string", + "args" : [ "uri" ] + } ] + } ] + } ] + } +} \ No newline at end of file diff --git a/tests/test_rows_update.py b/tests/test_rows_update.py index 844cd21..b973659 100644 --- a/tests/test_rows_update.py +++ b/tests/test_rows_update.py @@ -58,3 +58,17 @@ def test_update_dsl_wrong_path(admin_client): "Optic Update need to be run as update transaction" in response.content.decode("utf-8") ) + + +def test_update_via_serialized_plan(client): + DEFAULT_PERMS = {"python-tester": ["read", "update"]} + DOC_URI = "/temp/doc2.json" + client.documents.write([Document(DOC_URI, {"doc": 1}, permissions=DEFAULT_PERMS)]) + docs = client.documents.read(DOC_URI) + assert 1 == len(docs) + + plan = open("tests/remove-uri-plan.json", "rb") + response = client.rows.update(plan=plan, return_response=True) + assert 200 == response.status_code + docs = client.documents.read(DOC_URI) + assert 0 == len(docs)