Skip to content

Commit

Permalink
cleanup: use GRAPH_FIELD
Browse files Browse the repository at this point in the history
  • Loading branch information
aMahanna committed Oct 15, 2024
1 parent abf20dc commit 209e9cd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
8 changes: 4 additions & 4 deletions nx_arangodb/classes/dict/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def graph_attr_dict_factory(
# Graph #
#########

GRAPH_KEY = "networkx"
GRAPH_FIELD = "networkx"


def build_graph_attr_dict_data(
Expand Down Expand Up @@ -148,15 +148,15 @@ def __init__(
)

self.graph_id = f"{self.collection_name}/{self.graph_name}"
self.parent_keys = [GRAPH_KEY]
self.parent_keys = [GRAPH_FIELD]

self.collection = create_collection(db, self.collection_name)
self.graph_attr_dict_factory = graph_attr_dict_factory(
self.db, self.adb_graph, self.graph_id
)

result = doc_get_or_insert(self.db, self.collection_name, self.graph_id)
for k, v in result.get(GRAPH_KEY, {}).items():
for k, v in result.get(GRAPH_FIELD, {}).items():
self.data[k] = self.__process_graph_dict_value(k, v)

def __process_graph_dict_value(self, key: str, value: Any) -> Any:
Expand Down Expand Up @@ -277,7 +277,7 @@ def __init__(
self.graph = graph
self.graph_id: str = graph_id

self.parent_keys: list[str] = [GRAPH_KEY]
self.parent_keys: list[str] = [GRAPH_FIELD]
self.graph_attr_dict_factory = graph_attr_dict_factory(
self.db, self.graph, self.graph_id
)
Expand Down
38 changes: 20 additions & 18 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import nx_arangodb as nxadb
from nx_arangodb.classes.dict.adj import AdjListOuterDict, EdgeAttrDict, EdgeKeyDict
from nx_arangodb.classes.dict.graph import GRAPH_FIELD
from nx_arangodb.classes.dict.node import NodeAttrDict, NodeDict

from .conftest import create_grid_graph, create_line_graph, db, run_gpu_tests
Expand Down Expand Up @@ -1655,9 +1656,9 @@ def test_graph_dict_init_extended(load_karate_graph: Any) -> None:
G = nxadb.Graph(name="KarateGraph", foo="bar", bar={"baz": True})
G.graph["foo"] = "!!!"
G.graph["bar"]["baz"] = False
assert db.document(G.graph.graph_id)["networkx"]["foo"] == "!!!"
assert db.document(G.graph.graph_id)["networkx"]["bar"]["baz"] is False
assert "baz" not in db.document(G.graph.graph_id)["networkx"]
assert db.document(G.graph.graph_id)[GRAPH_FIELD]["foo"] == "!!!"
assert db.document(G.graph.graph_id)[GRAPH_FIELD]["bar"]["baz"] is False
assert "baz" not in db.document(G.graph.graph_id)[GRAPH_FIELD]


def test_graph_dict_clear_will_not_remove_remote_data(load_karate_graph: Any) -> None:
Expand All @@ -1675,7 +1676,7 @@ def test_graph_dict_clear_will_not_remove_remote_data(load_karate_graph: Any) ->
except KeyError:
raise AssertionError("Not allowed to fail.")

assert db.document(G.graph.graph_id)["networkx"]["ant"] == {"b": 6}
assert db.document(G.graph.graph_id)[GRAPH_FIELD]["ant"] == {"b": 6}


def test_graph_dict_set_item(load_karate_graph: Any) -> None:
Expand All @@ -1697,10 +1698,10 @@ def test_graph_dict_set_item(load_karate_graph: Any) -> None:
G.graph["json"] = value

if value is None:
assert "json" not in db.document(G.graph.graph_id)["networkx"]
assert "json" not in db.document(G.graph.graph_id)[GRAPH_FIELD]
else:
assert G.graph["json"] == value
assert db.document(G.graph.graph_id)["networkx"]["json"] == value
assert db.document(G.graph.graph_id)[GRAPH_FIELD]["json"] == value


def test_graph_dict_update(load_karate_graph: Any) -> None:
Expand All @@ -1715,7 +1716,7 @@ def test_graph_dict_update(load_karate_graph: Any) -> None:
assert G.graph.data["c"] == G.graph["c"] == "d"

# remote
adb_doc = db.document(f"_graphs/{G.name}")["networkx"]
adb_doc = db.document(f"_graphs/{G.name}")[GRAPH_FIELD]
assert adb_doc["a"] == "b"
assert adb_doc["c"] == "d"

Expand All @@ -1727,8 +1728,8 @@ def test_graph_attr_dict_nested_update(load_karate_graph: Any) -> None:
G.graph["a"].update({"d": "e"})
assert G.graph["a"]["b"] == "c"
assert G.graph["a"]["d"] == "e"
assert db.document(G.graph.graph_id)["networkx"]["a"]["b"] == "c"
assert db.document(G.graph.graph_id)["networkx"]["a"]["d"] == "e"
assert db.document(G.graph.graph_id)[GRAPH_FIELD]["a"]["b"] == "c"
assert db.document(G.graph.graph_id)[GRAPH_FIELD]["a"]["d"] == "e"


def test_graph_dict_nested_1(load_karate_graph: Any) -> None:
Expand All @@ -1737,7 +1738,7 @@ def test_graph_dict_nested_1(load_karate_graph: Any) -> None:

G.graph["a"] = {"b": icon}
assert G.graph["a"]["b"] == icon
assert db.document(G.graph.graph_id)["networkx"]["a"]["b"] == icon
assert db.document(G.graph.graph_id)[GRAPH_FIELD]["a"]["b"] == icon


def test_graph_dict_nested_2(load_karate_graph: Any) -> None:
Expand All @@ -1749,7 +1750,7 @@ def test_graph_dict_nested_2(load_karate_graph: Any) -> None:

assert G.graph["x"]["y"]["amount_of_goals"] == 1337
assert (
db.document(G.graph.graph_id)["networkx"]["x"]["y"]["amount_of_goals"] == 1337
db.document(G.graph.graph_id)[GRAPH_FIELD]["x"]["y"]["amount_of_goals"] == 1337
)


Expand All @@ -1758,10 +1759,10 @@ def test_graph_dict_empty_values(load_karate_graph: Any) -> None:

G.graph["empty"] = {}
assert G.graph["empty"] == {}
assert db.document(G.graph.graph_id)["networkx"]["empty"] == {}
assert db.document(G.graph.graph_id)[GRAPH_FIELD]["empty"] == {}

G.graph["none"] = None
assert "none" not in db.document(G.graph.graph_id)["networkx"]
assert "none" not in db.document(G.graph.graph_id)[GRAPH_FIELD]
assert "none" not in G.graph


Expand All @@ -1774,15 +1775,16 @@ def test_graph_dict_nested_overwrite(load_karate_graph: Any) -> None:
G.graph["a"]["b"]["football_icon"] = "ChangedIcon"
assert G.graph["a"]["b"]["football_icon"] == "ChangedIcon"
assert (
db.document(G.graph.graph_id)["networkx"]["a"]["b"]["football_icon"]
db.document(G.graph.graph_id)[GRAPH_FIELD]["a"]["b"]["football_icon"]
== "ChangedIcon"
)

# Overwrite entire nested dictionary
G.graph["a"] = {"b": icon2}
assert G.graph["a"]["b"]["basketball_icon"] == "MJ23"
assert (
db.document(G.graph.graph_id)["networkx"]["a"]["b"]["basketball_icon"] == "MJ23"
db.document(G.graph.graph_id)[GRAPH_FIELD]["a"]["b"]["basketball_icon"]
== "MJ23"
)


Expand All @@ -1794,7 +1796,7 @@ def test_graph_dict_complex_nested(load_karate_graph: Any) -> None:
G.graph["complex"] = complex_structure
assert G.graph["complex"]["level1"]["level2"]["level3"]["key"] == "value"
assert (
db.document(G.graph.graph_id)["networkx"]["complex"]["level1"]["level2"][
db.document(G.graph.graph_id)[GRAPH_FIELD]["complex"]["level1"]["level2"][
"level3"
]["key"]
== "value"
Expand All @@ -1808,12 +1810,12 @@ def test_graph_dict_nested_deletion(load_karate_graph: Any) -> None:
G.graph["x"] = {"y": icon}
del G.graph["x"]["y"]["amount_of_goals"]
assert "amount_of_goals" not in G.graph["x"]["y"]
assert "amount_of_goals" not in db.document(G.graph.graph_id)["networkx"]["x"]["y"]
assert "amount_of_goals" not in db.document(G.graph.graph_id)[GRAPH_FIELD]["x"]["y"]

# Delete top-level key
del G.graph["x"]
assert "x" not in G.graph
assert "x" not in db.document(G.graph.graph_id)["networkx"]
assert "x" not in db.document(G.graph.graph_id)[GRAPH_FIELD]


def test_readme(load_karate_graph: Any) -> None:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
AdjListOuterDict,
EdgeAttrDict,
)
from nx_arangodb.classes.dict.graph import GraphDict
from nx_arangodb.classes.dict.graph import GRAPH_FIELD, GraphDict
from nx_arangodb.classes.dict.node import NodeAttrDict, NodeDict

from .conftest import db
Expand Down Expand Up @@ -463,11 +463,11 @@ def test_graph_attr(self):
assert isinstance(G.graph, GraphDict)
assert G.graph["foo"] == "bar"
del G.graph["foo"]
graph_doc = get_doc(f"_graphs/{GRAPH_NAME}")["networkx"]
graph_doc = get_doc(f"_graphs/{GRAPH_NAME}")[GRAPH_FIELD]
assert G.graph == graph_doc
H = self.K3Graph(foo="bar")
assert H.graph["foo"] == "bar"
graph_doc = get_doc(f"_graphs/{GRAPH_NAME}")["networkx"]
graph_doc = get_doc(f"_graphs/{GRAPH_NAME}")[GRAPH_FIELD]
assert H.graph == graph_doc

def test_node_attr(self):
Expand Down Expand Up @@ -1105,7 +1105,7 @@ def test_update(self):
else:
for src, dst in G.edges():
assert G.adj[dst][src] == G.adj[src][dst]
assert G.graph == get_doc(G.graph.graph_id)["networkx"]
assert G.graph == get_doc(G.graph.graph_id)[GRAPH_FIELD]

# no keywords -- order is edges, nodes
G = self.K3Graph()
Expand All @@ -1126,7 +1126,7 @@ def test_update(self):
else:
for src, dst in G.edges():
assert G.adj[dst][src] == G.adj[src][dst]
assert G.graph == get_doc(G.graph.graph_id)["networkx"]
assert G.graph == get_doc(G.graph.graph_id)[GRAPH_FIELD]

# update using only a graph
G = self.K3Graph()
Expand Down

0 comments on commit 209e9cd

Please sign in to comment.