diff --git a/hugegraph-python-client/pyproject.toml b/hugegraph-python-client/pyproject.toml index 2c847090..5200a58f 100644 --- a/hugegraph-python-client/pyproject.toml +++ b/hugegraph-python-client/pyproject.toml @@ -32,7 +32,7 @@ dependencies = [ "requests", "setuptools", "urllib3", - "rich", + "rich" ] [project.urls] diff --git a/hugegraph-python-client/src/pyhugegraph/api/graph.py b/hugegraph-python-client/src/pyhugegraph/api/graph.py index 4b6aab1c..c021f619 100644 --- a/hugegraph-python-client/src/pyhugegraph/api/graph.py +++ b/hugegraph-python-client/src/pyhugegraph/api/graph.py @@ -45,21 +45,21 @@ def addVertices(self, input_data): return [VertexData({"id": item}) for item in response] return None - @router.http("PUT", 'graph/vertices/"{vertex_id}"?action=append') + @router.http("PUT", 'graph/vertices/{vertex_id}?action=append') def appendVertex(self, vertex_id, properties): # pylint: disable=unused-argument data = {"properties": properties} if response := self._invoke_request(data=json.dumps(data)): return VertexData(response) return None - @router.http("PUT", 'graph/vertices/"{vertex_id}"?action=eliminate') + @router.http("PUT", 'graph/vertices/{vertex_id}?action=eliminate') def eliminateVertex(self, vertex_id, properties): # pylint: disable=unused-argument data = {"properties": properties} if response := self._invoke_request(data=json.dumps(data)): return VertexData(response) return None - @router.http("GET", 'graph/vertices/"{vertex_id}"') + @router.http("GET", 'graph/vertices/{vertex_id}') def getVertexById(self, vertex_id): # pylint: disable=unused-argument if response := self._invoke_request(): return VertexData(response) @@ -101,7 +101,7 @@ def getVertexByCondition(self, label="", limit=0, page=None, properties=None): return [VertexData(item) for item in response["vertices"]] return None - @router.http("DELETE", 'graph/vertices/"{vertex_id}"') + @router.http("DELETE", 'graph/vertices/{vertex_id}') def removeVertexById(self, vertex_id): # pylint: disable=unused-argument return self._invoke_request() @@ -200,8 +200,10 @@ def getVerticesById(self, vertex_ids) -> list[VertexData] | None: if not vertex_ids: return [] path = "traversers/vertices?" - for vertex_id in vertex_ids: - path += f'ids="{vertex_id}"&' # pylint: disable=consider-using-join + quoted_vertex_ids = map(json.dumps, vertex_ids) + + for vertex_id in quoted_vertex_ids: + path += f'ids={vertex_id}&' # pylint: disable=consider-using-join path = path.rstrip("&") if response := self._sess.request(path): return [VertexData(item) for item in response["vertices"]] diff --git a/hugegraph-python-client/src/pyhugegraph/utils/huge_router.py b/hugegraph-python-client/src/pyhugegraph/utils/huge_router.py index 580f78f0..8ecfc93a 100644 --- a/hugegraph-python-client/src/pyhugegraph/utils/huge_router.py +++ b/hugegraph-python-client/src/pyhugegraph/utils/huge_router.py @@ -17,6 +17,7 @@ import functools import inspect +import json import re import threading from collections.abc import Callable @@ -148,6 +149,14 @@ def wrapper(self: "HGraphContext", *args: Any, **kwargs: Any) -> Any: all_kwargs["graphspace"] = graphspace_arg or graphspace_cfg formatted_path = path.format(**all_kwargs) + elif "{vertex_id}" in path: + # fix vertex_id format process + # only quote string type vertex_id + vertex_id = all_kwargs.pop("vertex_id") + if isinstance(vertex_id, str): + vertex_id = "\"" + vertex_id + "\"" + all_kwargs['vertex_id'] = vertex_id + formatted_path = path.format(**all_kwargs) else: formatted_path = path.format(**all_kwargs) else: diff --git a/hugegraph-python-client/src/tests/api/test_graph.py b/hugegraph-python-client/src/tests/api/test_graph.py index a66c3fb9..279be058 100644 --- a/hugegraph-python-client/src/tests/api/test_graph.py +++ b/hugegraph-python-client/src/tests/api/test_graph.py @@ -22,6 +22,7 @@ from ..client_utils import ClientUtils + class TestGraphManager(unittest.TestCase): client = None graph = None @@ -57,17 +58,33 @@ def test_append_vertex(self): appended_vertex = self.graph.appendVertex(vertex.id, {"city": "Beijing"}) self.assertEqual(appended_vertex.properties["city"], "Beijing") + def test_append_vertex_with_number_id(self): + vertex = self.graph.addVertex("department", {"name": "DepartmentA", "headcount": 10, "floor": 1}) + appended_vertex = self.graph.appendVertex(vertex.id, {"headcount": 15}) + self.assertEqual(appended_vertex.properties["headcount"], 15) + def test_eliminate_vertex(self): vertex = self.graph.addVertex("person", {"name": "marko", "age": 29, "city": "Beijing"}) self.graph.eliminateVertex(vertex.id, {"city": "Beijing"}) eliminated_vertex = self.graph.getVertexById(vertex.id) self.assertIsNone(eliminated_vertex.properties.get("city")) + def test_eliminate_vertex_with_number_id(self): + vertex = self.graph.addVertex("department", {"name": "DepartmentA", "headcount": 10, "floor": 1}) + self.graph.eliminateVertex(vertex.id, {"floor": 1}) + eliminated_vertex = self.graph.getVertexById(vertex.id) + self.assertIsNone(eliminated_vertex.properties.get("floor")) + def test_get_vertex_by_id(self): vertex = self.graph.addVertex("person", {"name": "Alice", "age": 20}) retrieved_vertex = self.graph.getVertexById(vertex.id) self.assertEqual(retrieved_vertex.id, vertex.id) + def test_get_vertex_by_number_id(self): + vertex = self.graph.addVertex("department", {"name": "DepartmentA", "headcount": 10, "floor": 1}) + retrieved_vertex = self.graph.getVertexById(vertex.id) + self.assertEqual(retrieved_vertex.id, vertex.id) + def test_get_vertex_by_page(self): self.graph.addVertex("person", {"name": "Alice", "age": 20}) self.graph.addVertex("person", {"name": "Bob", "age": 23}) @@ -89,6 +106,16 @@ def test_remove_vertex_by_id(self): except NotFoundError as e: self.assertTrue("Alice\\' does not exist" in str(e)) + def test_remove_vertex_by_number_id(self): + vertex = self.graph.addVertex("department", {"name": "DepartmentA", "headcount": 10, "floor": 1}) + self.graph.removeVertexById(vertex.id) + try: + self.graph.getVertexById(vertex.id) + except NotFoundError as e: + msg = "\\'{}\\' does not exist".format(vertex.id) + logger.info(f'test_msg: {msg}') + self.assertTrue(msg in str(e)) + def test_add_edge(self): vertex1 = self.graph.addVertex("person", {"name": "Alice", "age": 20}) vertex2 = self.graph.addVertex("person", {"name": "Bob", "age": 23}) diff --git a/hugegraph-python-client/src/tests/client_utils.py b/hugegraph-python-client/src/tests/client_utils.py index 1914cdb0..2c429722 100644 --- a/hugegraph-python-client/src/tests/client_utils.py +++ b/hugegraph-python-client/src/tests/client_utils.py @@ -56,6 +56,8 @@ def init_property_key(self): schema.propertyKey("date").asDate().ifNotExist().create() schema.propertyKey("price").asInt().ifNotExist().create() schema.propertyKey("weight").asDouble().ifNotExist().create() + schema.propertyKey("headcount").asInt().ifNotExist().create() + schema.propertyKey("floor").asInt().ifNotExist().create() def init_vertex_label(self): schema = self.schema @@ -68,6 +70,9 @@ def init_vertex_label(self): schema.vertexLabel("book").useCustomizeStringId().properties("name", "price").nullableKeys( "price" ).ifNotExist().create() + schema.vertexLabel("department").properties("name", "headcount", "floor").nullableKeys( + "floor" + ).ifNotExist().create() def init_edge_label(self): schema = self.schema