Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add wrapper for constructing vec from any python iterable #1862

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions python/python/raphtory/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,25 @@ class Graph(GraphView):
path (str): The path to the cache file
"""

def create_node(
self,
timestamp: TimeInput,
id: str | int,
properties: Optional[PropInput] = None,
node_type: Optional[str] = None,
) -> MutableNode:
"""
Creates a new node with the given id and properties to the graph. It fails if the node already exists.

Arguments:
timestamp (TimeInput): The timestamp of the node.
id (str|int): The id of the node.
properties (PropInput, optional): The properties of the node.
node_type (str, optional): The optional string which will be used as a node type
Returns:
MutableNode: The created node
"""

@staticmethod
def deserialise(bytes: bytes):
"""
Expand Down Expand Up @@ -3506,6 +3525,26 @@ class PersistentGraph(GraphView):
path (str): The path to the cache file
"""

def create_node(
self,
timestamp: TimeInput,
id: str | int,
properties: dict = None,
node_type: str = None,
):
"""
Creates a new node with the given id and properties to the graph. It fails if the node already exists.

Arguments:
timestamp (TimeInput): The timestamp of the node.
id (str | int): The id of the node.
properties (dict): The properties of the node.
node_type (str) : The optional string which will be used as a node type

Returns:
MutableNode
"""

def delete_edge(
self, timestamp: int, src: str | int, dst: str | int, layer: str = None
):
Expand Down
22 changes: 19 additions & 3 deletions python/python/raphtory/graphql/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,6 @@ class RemoteEdgeAddition(object):
"""Create and return a new object. See help(type) for accurate signature."""

class RemoteGraph(object):
def __new__(self, path, client) -> RemoteGraph:
"""Create and return a new object. See help(type) for accurate signature."""

def add_constant_properties(self, properties: dict):
"""
Adds constant properties to the remote graph.
Expand Down Expand Up @@ -406,6 +403,25 @@ class RemoteGraph(object):
properties (dict): The temporal properties of the graph.
"""

def create_node(
self,
timestamp: int | str | datetime,
id: str | int,
properties: Optional[dict] = None,
node_type: Optional[str] = None,
):
"""
Create a new node with the given id and properties to the remote graph and fail if the node already exists.

Arguments:
timestamp (int|str|datetime): The timestamp of the node.
id (str|int): The id of the node.
properties (dict, optional): The properties of the node.
node_type (str, optional): The optional string which will be used as a node type
Returns:
RemoteNode
"""

def delete_edge(
self,
timestamp: int,
Expand Down
133 changes: 102 additions & 31 deletions python/tests/graphql/edit_graph/test_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def test_encode_graph():

encoded = encode_graph(g)
assert (
encoded
== "EgxaCgoIX2RlZmF1bHQSDBIKCghfZGVmYXVsdBoFCgNiZW4aCQoFaGFtemEYARoLCgdoYWFyb29uGAIiAhABIgYIAhABGAEiBBACGAIqAhoAKgQSAhABKgQSAhADKgIKACoGEgQIARABKgYSBAgBEAIqBAoCCAEqBhIECAIQAioGEgQIAhADKgQKAggCKgQ6AhABKgIyACoIOgYIARACGAEqBDICCAEqCDoGCAIQAxgCKgQyAggC"
encoded
== "EgxaCgoIX2RlZmF1bHQSDBIKCghfZGVmYXVsdBoFCgNiZW4aCQoFaGFtemEYARoLCgdoYWFyb29uGAIiAhABIgYIAhABGAEiBBACGAIqAhoAKgQSAhABKgQSAhADKgIKACoGEgQIARABKgYSBAgBEAIqBAoCCAEqBhIECAIQAioGEgQIAhADKgQKAggCKgQ6AhABKgIyACoIOgYIARACGAEqBDICCAEqCDoGCAIQAxgCKgQyAggC"
)


Expand All @@ -42,8 +42,8 @@ def test_wrong_url():
with pytest.raises(Exception) as excinfo:
client = RaphtoryClient("http://broken_url.com")
assert (
str(excinfo.value)
== "Could not connect to the given server - no response --error sending request for url (http://broken_url.com/)"
str(excinfo.value)
== "Could not connect to the given server - no response --error sending request for url (http://broken_url.com/)"
)


Expand Down Expand Up @@ -393,16 +393,14 @@ def test_create_node():

query_nodes = """{graph(path: "g") {nodes {list {name}}}}"""
assert client.query(query_nodes) == {
"graph": {
"nodes": {
"list": [{"name": "ben"}, {"name": "shivam"}]
}
}
"graph": {"nodes": {"list": [{"name": "ben"}, {"name": "shivam"}]}}
}

create_node_query = """{updateGraph(path: "g") { createNode(time: 0, name: "oogway") { success } }}"""

assert client.query(create_node_query) == {"updateGraph": {"createNode": {"success": True}}}
assert client.query(create_node_query) == {
"updateGraph": {"createNode": {"success": True}}
}
assert client.query(query_nodes) == {
"graph": {
"nodes": {
Expand All @@ -428,11 +426,7 @@ def test_create_node_using_client():

query_nodes = """{graph(path: "g") {nodes {list {name}}}}"""
assert client.query(query_nodes) == {
"graph": {
"nodes": {
"list": [{"name": "ben"}, {"name": "shivam"}]
}
}
"graph": {"nodes": {"list": [{"name": "ben"}, {"name": "shivam"}]}}
}

remote_graph = client.remote_graph(path="g")
Expand Down Expand Up @@ -460,23 +454,56 @@ def test_create_node_using_client_with_properties():
client = RaphtoryClient("http://localhost:1737")
client.send_graph(path="g", graph=g)

query_nodes = """{graph(path: "g") {nodes {list {name, properties { keys }}}}}"""
query_nodes = (
"""{graph(path: "g") {nodes {list {name, properties { keys }}}}}"""
)
assert client.query(query_nodes) == {
"graph": {
"nodes": {
"list": [{"name": "ben", 'properties': {'keys': []}}, {"name": "shivam", 'properties': {'keys': []}}]
"list": [
{"name": "ben", "properties": {"keys": []}},
{"name": "shivam", "properties": {"keys": []}},
]
}
}
}

remote_graph = client.remote_graph(path="g")
remote_graph.create_node(timestamp=0, id="oogway", properties={"prop1": 60, "prop2": 31.3, "prop3": "abc123", "prop4": True, "prop5": [1, 2, 3]})
nodes = json.loads(json.dumps(client.query(query_nodes)))['graph']['nodes']['list']
node_oogway = next(node for node in nodes if node['name'] == 'oogway')
assert sorted(node_oogway['properties']['keys']) == ['prop1', 'prop2', 'prop3', 'prop4', 'prop5']
remote_graph.create_node(
timestamp=0,
id="oogway",
properties={
"prop1": 60,
"prop2": 31.3,
"prop3": "abc123",
"prop4": True,
"prop5": [1, 2, 3],
},
)
nodes = json.loads(json.dumps(client.query(query_nodes)))["graph"]["nodes"][
"list"
]
node_oogway = next(node for node in nodes if node["name"] == "oogway")
assert sorted(node_oogway["properties"]["keys"]) == [
"prop1",
"prop2",
"prop3",
"prop4",
"prop5",
]

with pytest.raises(Exception) as excinfo:
remote_graph.create_node(timestamp=0, id="oogway", properties={"prop1": 60, "prop2": 31.3, "prop3": "abc123", "prop4": True, "prop5": [1, 2, 3]})
remote_graph.create_node(
timestamp=0,
id="oogway",
properties={
"prop1": 60,
"prop2": 31.3,
"prop3": "abc123",
"prop4": True,
"prop5": [1, 2, 3],
},
)

assert "Node already exists" in str(excinfo.value)

Expand All @@ -494,20 +521,57 @@ def test_create_node_using_client_with_properties_node_type():
assert client.query(query_nodes) == {
"graph": {
"nodes": {
"list": [{"name": "ben", 'nodeType': None, 'properties': {'keys': []}}, {"name": "shivam", 'nodeType': None, 'properties': {'keys': []}}]
"list": [
{"name": "ben", "nodeType": None, "properties": {"keys": []}},
{
"name": "shivam",
"nodeType": None,
"properties": {"keys": []},
},
]
}
}
}

remote_graph = client.remote_graph(path="g")
remote_graph.create_node(timestamp=0, id="oogway", properties={"prop1": 60, "prop2": 31.3, "prop3": "abc123", "prop4": True, "prop5": [1, 2, 3]}, node_type="master")
nodes = json.loads(json.dumps(client.query(query_nodes)))['graph']['nodes']['list']
node_oogway = next(node for node in nodes if node['name'] == 'oogway')
assert node_oogway['nodeType'] == 'master'
assert sorted(node_oogway['properties']['keys']) == ['prop1', 'prop2', 'prop3', 'prop4', 'prop5']
remote_graph.create_node(
timestamp=0,
id="oogway",
properties={
"prop1": 60,
"prop2": 31.3,
"prop3": "abc123",
"prop4": True,
"prop5": [1, 2, 3],
},
node_type="master",
)
nodes = json.loads(json.dumps(client.query(query_nodes)))["graph"]["nodes"][
"list"
]
node_oogway = next(node for node in nodes if node["name"] == "oogway")
assert node_oogway["nodeType"] == "master"
assert sorted(node_oogway["properties"]["keys"]) == [
"prop1",
"prop2",
"prop3",
"prop4",
"prop5",
]

with pytest.raises(Exception) as excinfo:
remote_graph.create_node(timestamp=0, id="oogway", properties={"prop1": 60, "prop2": 31.3, "prop3": "abc123", "prop4": True, "prop5": [1, 2, 3]}, node_type="master")
remote_graph.create_node(
timestamp=0,
id="oogway",
properties={
"prop1": 60,
"prop2": 31.3,
"prop3": "abc123",
"prop4": True,
"prop5": [1, 2, 3],
},
node_type="master",
)

assert "Node already exists" in str(excinfo.value)

Expand All @@ -525,7 +589,10 @@ def test_create_node_using_client_with_node_type():
assert client.query(query_nodes) == {
"graph": {
"nodes": {
"list": [{"name": "ben", 'nodeType': None}, {"name": "shivam", 'nodeType': None}]
"list": [
{"name": "ben", "nodeType": None},
{"name": "shivam", "nodeType": None},
]
}
}
}
Expand All @@ -535,7 +602,11 @@ def test_create_node_using_client_with_node_type():
assert client.query(query_nodes) == {
"graph": {
"nodes": {
"list": [{"name": "ben", 'nodeType': None}, {"name": "shivam", 'nodeType': None}, {"name": "oogway", 'nodeType': "master"}]
"list": [
{"name": "ben", "nodeType": None},
{"name": "shivam", "nodeType": None},
{"name": "oogway", "nodeType": "master"},
]
}
}
}
Expand Down
Loading