Skip to content

Commit

Permalink
Fix bug with get_node_id. Make behavior consistent with get_edge_by_i…
Browse files Browse the repository at this point in the history
…d. Add remove_node to writable methods for InMemoryCachedBackend. (#59)
  • Loading branch information
davidmezzetti authored Dec 8, 2024
1 parent 3543009 commit a9d6a52
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
18 changes: 9 additions & 9 deletions grand/backends/_sqlbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,16 @@ def get_node_by_id(self, node_name: Hashable):
"""

res = (
self._connection.execute(
self._node_table.select().where(
self._node_table.c[self._primary_key] == str(node_name)
)
res = self._connection.execute(
self._node_table.select().where(
self._node_table.c[self._primary_key] == str(node_name)
)
.fetchone()
._metadata
)
return res
).fetchone()

if res:
return res._metadata

raise KeyError(f"Node {node_name} not found")

def get_edge_by_id(self, u: Hashable, v: Hashable):
"""
Expand Down
2 changes: 2 additions & 0 deletions grand/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ class InMemoryCachedBackend(CachedBackend):
"add_edge",
"add_edges_from",
"ingest_from_edgelist_dataframe",
"remove_node"
]

_default_write_methods = [
Expand All @@ -347,6 +348,7 @@ class InMemoryCachedBackend(CachedBackend):
"add_edge",
"add_edges_from",
"ingest_from_edgelist_dataframe",
"remove_node"
]

def __init__(
Expand Down
2 changes: 2 additions & 0 deletions grand/backends/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def test_sqlite_persistence(self):
backend.remove_node(node1)
assert not backend.has_node(node1)
assert not backend.has_edge(node1, node2)
with pytest.raises(KeyError):
assert not backend.get_node_by_id(node1)

# cleanup
os.remove(dbpath)
Expand Down

0 comments on commit a9d6a52

Please sign in to comment.