cpg2py 1.2.0__tar.gz → 1.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cpg2py-1.2.0/cpg2py.egg-info → cpg2py-1.2.2}/PKG-INFO +2 -3
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/__init__.py +1 -2
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_abc/graph.py +42 -36
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_cpg/graph.py +11 -7
- {cpg2py-1.2.0 → cpg2py-1.2.2/cpg2py.egg-info}/PKG-INFO +2 -3
- {cpg2py-1.2.0 → cpg2py-1.2.2}/pyproject.toml +1 -1
- {cpg2py-1.2.0 → cpg2py-1.2.2}/tests/test_edge.py +4 -9
- {cpg2py-1.2.0 → cpg2py-1.2.2}/tests/test_exceptions.py +2 -6
- {cpg2py-1.2.0 → cpg2py-1.2.2}/tests/test_generics.py +8 -21
- {cpg2py-1.2.0 → cpg2py-1.2.2}/tests/test_graph.py +17 -48
- {cpg2py-1.2.0 → cpg2py-1.2.2}/tests/test_node.py +12 -33
- {cpg2py-1.2.0 → cpg2py-1.2.2}/tests/test_storage.py +33 -96
- {cpg2py-1.2.0 → cpg2py-1.2.2}/LICENSE +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/MANIFEST.in +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/README.md +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_abc/__init__.py +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_abc/edge.py +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_abc/node.py +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_abc/storage.py +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_cpg/__init__.py +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_cpg/edge.py +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_cpg/node.py +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_exceptions.py +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py/_logger.py +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py.egg-info/SOURCES.txt +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py.egg-info/dependency_links.txt +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py.egg-info/requires.txt +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/cpg2py.egg-info/top_level.txt +0 -0
- {cpg2py-1.2.0 → cpg2py-1.2.2}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
2
|
Name: cpg2py
|
|
3
|
-
Version: 1.2.
|
|
3
|
+
Version: 1.2.2
|
|
4
4
|
Summary: A graph-based data structure designed for querying CSV files in Joern format in Python
|
|
5
5
|
Author-email: samhsu-dev <yxu166@jhu.edu>
|
|
6
6
|
License: MIT License
|
|
@@ -52,7 +52,6 @@ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
|
52
52
|
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
53
53
|
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
54
54
|
Requires-Dist: pylint>=2.17.0; extra == "dev"
|
|
55
|
-
Dynamic: license-file
|
|
56
55
|
|
|
57
56
|
# cpg2py
|
|
58
57
|
|
|
@@ -2,8 +2,7 @@ from csv import DictReader
|
|
|
2
2
|
from pathlib import Path
|
|
3
3
|
from typing import Union
|
|
4
4
|
|
|
5
|
-
from ._abc import Storage
|
|
6
|
-
from ._abc import AbcEdgeQuerier, AbcGraphQuerier, AbcNodeQuerier
|
|
5
|
+
from ._abc import AbcEdgeQuerier, AbcGraphQuerier, AbcNodeQuerier, Storage
|
|
7
6
|
from ._cpg import CpgEdge, CpgGraph, CpgNode
|
|
8
7
|
from ._exceptions import CPGError, EdgeNotFoundError, NodeNotFoundError, TopFileNotFoundError
|
|
9
8
|
from ._logger import get_logger
|
|
@@ -8,17 +8,13 @@ from .edge import AbcEdgeQuerier
|
|
|
8
8
|
from .node import AbcNodeQuerier
|
|
9
9
|
from .storage import Storage
|
|
10
10
|
|
|
11
|
-
# Type variables for
|
|
12
|
-
#
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
# Type variables for concrete implementations (invariant).
|
|
12
|
+
# Used as Generic params so type checkers infer node/edge types at call sites.
|
|
13
|
+
_GenericNode = TypeVar("_GenericNode", bound=AbcNodeQuerier)
|
|
14
|
+
_GenericEdge = TypeVar("_GenericEdge", bound=AbcEdgeQuerier)
|
|
15
15
|
|
|
16
|
-
# Type variables for concrete implementations (invariant)
|
|
17
|
-
_ConcreteNodeType = TypeVar("_ConcreteNodeType", bound=AbcNodeQuerier)
|
|
18
|
-
_ConcreteEdgeType = TypeVar("_ConcreteEdgeType", bound=AbcEdgeQuerier)
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
17
|
+
class AbcGraphQuerier(abc.ABC, Generic[_GenericNode, _GenericEdge]):
|
|
22
18
|
"""
|
|
23
19
|
Abstract base class for graph query operations.
|
|
24
20
|
|
|
@@ -33,17 +29,13 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
33
29
|
...
|
|
34
30
|
|
|
35
31
|
Type Parameters:
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
_GenericNode: The concrete node type returned by node() and related methods
|
|
33
|
+
_GenericEdge: The concrete edge type returned by edge() and used in conditions
|
|
38
34
|
"""
|
|
39
35
|
|
|
40
|
-
__NodeCondition = Callable[[_NodeType], bool]
|
|
41
|
-
__EdgeCondition = Callable[[_EdgeType], bool]
|
|
42
|
-
|
|
43
36
|
__always_true = lambda _: True
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
__EdgesResult = Iterable[_ConcreteEdgeType]
|
|
37
|
+
NodeCondition = Callable[[_GenericNode], bool]
|
|
38
|
+
EdgeCondition = Callable[[_GenericEdge], bool]
|
|
47
39
|
|
|
48
40
|
def __init__(self, target: Storage, maxdepth: int = -1) -> None:
|
|
49
41
|
"""
|
|
@@ -67,7 +59,7 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
67
59
|
return self.__graph
|
|
68
60
|
|
|
69
61
|
@abc.abstractmethod
|
|
70
|
-
def node(self, whose_id_is: str) -> Optional[
|
|
62
|
+
def node(self, whose_id_is: str) -> Optional[_GenericNode]:
|
|
71
63
|
"""
|
|
72
64
|
Returns a node by its ID.
|
|
73
65
|
|
|
@@ -80,7 +72,7 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
80
72
|
raise NotImplementedError
|
|
81
73
|
|
|
82
74
|
@abc.abstractmethod
|
|
83
|
-
def edge(self, fid: str, tid: str, eid: str) -> Optional[
|
|
75
|
+
def edge(self, fid: str, tid: str, eid: str) -> Optional[_GenericEdge]:
|
|
84
76
|
"""
|
|
85
77
|
Returns an edge by its source, target, and edge type.
|
|
86
78
|
|
|
@@ -95,8 +87,9 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
95
87
|
raise NotImplementedError
|
|
96
88
|
|
|
97
89
|
def nodes(
|
|
98
|
-
self,
|
|
99
|
-
|
|
90
|
+
self,
|
|
91
|
+
who_satisifies: Callable[[_GenericNode], bool] = __always_true,
|
|
92
|
+
) -> Iterable[_GenericNode]:
|
|
100
93
|
"""
|
|
101
94
|
Returns all nodes matching the condition.
|
|
102
95
|
|
|
@@ -112,8 +105,9 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
112
105
|
yield cur_node
|
|
113
106
|
|
|
114
107
|
def first_node(
|
|
115
|
-
self,
|
|
116
|
-
|
|
108
|
+
self,
|
|
109
|
+
who_satisifies: NodeCondition = __always_true,
|
|
110
|
+
) -> Optional[_GenericNode]:
|
|
117
111
|
"""
|
|
118
112
|
Returns the first node matching the condition.
|
|
119
113
|
|
|
@@ -126,8 +120,9 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
126
120
|
return next(self.nodes(who_satisifies), None)
|
|
127
121
|
|
|
128
122
|
def edges(
|
|
129
|
-
self,
|
|
130
|
-
|
|
123
|
+
self,
|
|
124
|
+
who_satisifies: EdgeCondition = __always_true,
|
|
125
|
+
) -> Iterable[_GenericEdge]:
|
|
131
126
|
"""
|
|
132
127
|
Returns all edges matching the condition.
|
|
133
128
|
|
|
@@ -143,8 +138,10 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
143
138
|
yield cur_edge
|
|
144
139
|
|
|
145
140
|
def succ(
|
|
146
|
-
self,
|
|
147
|
-
|
|
141
|
+
self,
|
|
142
|
+
of: _GenericNode,
|
|
143
|
+
who_satisifies: EdgeCondition = __always_true,
|
|
144
|
+
) -> Iterable[_GenericNode]:
|
|
148
145
|
"""
|
|
149
146
|
Returns successor nodes connected to the input node.
|
|
150
147
|
|
|
@@ -163,8 +160,10 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
163
160
|
yield node
|
|
164
161
|
|
|
165
162
|
def prev(
|
|
166
|
-
self,
|
|
167
|
-
|
|
163
|
+
self,
|
|
164
|
+
of: _GenericNode,
|
|
165
|
+
who_satisifies: EdgeCondition = __always_true,
|
|
166
|
+
) -> Iterable[_GenericNode]:
|
|
168
167
|
"""
|
|
169
168
|
Returns predecessor nodes connected to the input node.
|
|
170
169
|
|
|
@@ -183,8 +182,11 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
183
182
|
yield node
|
|
184
183
|
|
|
185
184
|
def __bfs_search(
|
|
186
|
-
self,
|
|
187
|
-
|
|
185
|
+
self,
|
|
186
|
+
root: _GenericNode,
|
|
187
|
+
condition: EdgeCondition,
|
|
188
|
+
reverse: bool,
|
|
189
|
+
) -> Iterable[_GenericNode]:
|
|
188
190
|
"""
|
|
189
191
|
Returns nodes from src node by BFS order (src node not included).
|
|
190
192
|
|
|
@@ -199,7 +201,7 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
199
201
|
if root is None:
|
|
200
202
|
return
|
|
201
203
|
visited_nids: List[str] = []
|
|
202
|
-
nodes_queue: Deque[
|
|
204
|
+
nodes_queue: Deque[_GenericNode] = deque([root, None])
|
|
203
205
|
depth = self.__maxdepth
|
|
204
206
|
while depth != 0 and len(nodes_queue) > 1:
|
|
205
207
|
cur_node = nodes_queue.popleft()
|
|
@@ -217,8 +219,10 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
217
219
|
yield cur_node
|
|
218
220
|
|
|
219
221
|
def descendants(
|
|
220
|
-
self,
|
|
221
|
-
|
|
222
|
+
self,
|
|
223
|
+
src: _GenericNode,
|
|
224
|
+
condition: EdgeCondition = __always_true,
|
|
225
|
+
) -> Iterable[_GenericNode]:
|
|
222
226
|
"""
|
|
223
227
|
Returns descendants from src node by BFS order (src node not included).
|
|
224
228
|
|
|
@@ -232,8 +236,10 @@ class AbcGraphQuerier(abc.ABC, Generic[_ConcreteNodeType, _ConcreteEdgeType]):
|
|
|
232
236
|
return self.__bfs_search(src, condition, reverse=False)
|
|
233
237
|
|
|
234
238
|
def ancestors(
|
|
235
|
-
self,
|
|
236
|
-
|
|
239
|
+
self,
|
|
240
|
+
src: _GenericNode,
|
|
241
|
+
condition: EdgeCondition = __always_true,
|
|
242
|
+
) -> Iterable[_GenericNode]:
|
|
237
243
|
"""
|
|
238
244
|
Returns ancestors from src node by BFS order (src node not included).
|
|
239
245
|
|
|
@@ -22,7 +22,7 @@ class CpgGraph(AbcGraphQuerier[CpgNode, CpgEdge]):
|
|
|
22
22
|
throughout the graph operations.
|
|
23
23
|
"""
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
EdgeCondition = Callable[[CpgEdge], bool]
|
|
26
26
|
__always_true = lambda _: True
|
|
27
27
|
|
|
28
28
|
def __init__(self, target: Storage) -> None:
|
|
@@ -104,7 +104,9 @@ class CpgGraph(AbcGraphQuerier[CpgNode, CpgEdge]):
|
|
|
104
104
|
logger.error("Cannot find top file node from node %s", of_nid)
|
|
105
105
|
raise TopFileNotFoundError(of_nid)
|
|
106
106
|
|
|
107
|
-
def succ(
|
|
107
|
+
def succ(
|
|
108
|
+
self, of: CpgNode, who_satisifies: EdgeCondition = __always_true
|
|
109
|
+
) -> Iterable[CpgNode]:
|
|
108
110
|
"""
|
|
109
111
|
Returns successor nodes connected to the input node.
|
|
110
112
|
|
|
@@ -117,7 +119,9 @@ class CpgGraph(AbcGraphQuerier[CpgNode, CpgEdge]):
|
|
|
117
119
|
"""
|
|
118
120
|
return super().succ(of, who_satisifies)
|
|
119
121
|
|
|
120
|
-
def prev(
|
|
122
|
+
def prev(
|
|
123
|
+
self, of: CpgNode, who_satisifies: EdgeCondition = __always_true
|
|
124
|
+
) -> Iterable[CpgNode]:
|
|
121
125
|
"""
|
|
122
126
|
Returns predecessor nodes connected to the input node.
|
|
123
127
|
|
|
@@ -130,7 +134,7 @@ class CpgGraph(AbcGraphQuerier[CpgNode, CpgEdge]):
|
|
|
130
134
|
"""
|
|
131
135
|
return super().prev(of, who_satisifies)
|
|
132
136
|
|
|
133
|
-
def children(self, of: CpgNode, extra:
|
|
137
|
+
def children(self, of: CpgNode, extra: EdgeCondition = __always_true) -> Iterable[CpgNode]:
|
|
134
138
|
"""
|
|
135
139
|
Returns child nodes connected via PARENT_OF edges.
|
|
136
140
|
|
|
@@ -143,7 +147,7 @@ class CpgGraph(AbcGraphQuerier[CpgNode, CpgEdge]):
|
|
|
143
147
|
"""
|
|
144
148
|
return self.succ(of, lambda e: extra(e) and (e.type == "PARENT_OF"))
|
|
145
149
|
|
|
146
|
-
def parent(self, of: CpgNode, extra:
|
|
150
|
+
def parent(self, of: CpgNode, extra: EdgeCondition = __always_true) -> Iterable[CpgNode]:
|
|
147
151
|
"""
|
|
148
152
|
Returns parent nodes connected via PARENT_OF edges.
|
|
149
153
|
|
|
@@ -156,7 +160,7 @@ class CpgGraph(AbcGraphQuerier[CpgNode, CpgEdge]):
|
|
|
156
160
|
"""
|
|
157
161
|
return self.prev(of, lambda e: extra(e) and (e.type == "PARENT_OF"))
|
|
158
162
|
|
|
159
|
-
def flow_to(self, of: CpgNode, extra:
|
|
163
|
+
def flow_to(self, of: CpgNode, extra: EdgeCondition = __always_true) -> Iterable[CpgNode]:
|
|
160
164
|
"""
|
|
161
165
|
Returns successor nodes connected via FLOWS_TO edges.
|
|
162
166
|
|
|
@@ -169,7 +173,7 @@ class CpgGraph(AbcGraphQuerier[CpgNode, CpgEdge]):
|
|
|
169
173
|
"""
|
|
170
174
|
return self.succ(of, lambda e: extra(e) and (e.type == "FLOWS_TO"))
|
|
171
175
|
|
|
172
|
-
def flow_from(self, of: CpgNode, extra:
|
|
176
|
+
def flow_from(self, of: CpgNode, extra: EdgeCondition = __always_true) -> Iterable[CpgNode]:
|
|
173
177
|
"""
|
|
174
178
|
Returns predecessor nodes connected via FLOWS_TO edges.
|
|
175
179
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
2
|
Name: cpg2py
|
|
3
|
-
Version: 1.2.
|
|
3
|
+
Version: 1.2.2
|
|
4
4
|
Summary: A graph-based data structure designed for querying CSV files in Joern format in Python
|
|
5
5
|
Author-email: samhsu-dev <yxu166@jhu.edu>
|
|
6
6
|
License: MIT License
|
|
@@ -52,7 +52,6 @@ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
|
52
52
|
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
53
53
|
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
54
54
|
Requires-Dist: pylint>=2.17.0; extra == "dev"
|
|
55
|
-
Dynamic: license-file
|
|
56
55
|
|
|
57
56
|
# cpg2py
|
|
58
57
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Unit tests for Edge class.
|
|
3
3
|
"""
|
|
4
|
+
|
|
4
5
|
from __future__ import annotations
|
|
5
6
|
|
|
6
7
|
import pytest
|
|
@@ -13,9 +14,7 @@ from cpg2py._cpg import CpgGraph
|
|
|
13
14
|
class TestEdge:
|
|
14
15
|
"""Test cases for Edge class."""
|
|
15
16
|
|
|
16
|
-
def test_edge_id_returns_edge_tuple(
|
|
17
|
-
self, graph_with_single_edge: CpgGraph
|
|
18
|
-
) -> None:
|
|
17
|
+
def test_edge_id_returns_edge_tuple(self, graph_with_single_edge: CpgGraph) -> None:
|
|
19
18
|
"""
|
|
20
19
|
Tests that edge.id returns the edge tuple.
|
|
21
20
|
|
|
@@ -146,9 +145,7 @@ class TestEdge:
|
|
|
146
145
|
edge = graph_with_single_edge.edge("node1", "node2", "TYPE")
|
|
147
146
|
assert edge.var == "variable_name"
|
|
148
147
|
|
|
149
|
-
def test_edge_set_property_sets_single_property(
|
|
150
|
-
self, graph_with_single_edge: CpgGraph
|
|
151
|
-
) -> None:
|
|
148
|
+
def test_edge_set_property_sets_single_property(self, graph_with_single_edge: CpgGraph) -> None:
|
|
152
149
|
"""set_property sets one edge property and get_property returns it."""
|
|
153
150
|
edge = graph_with_single_edge.edge("node1", "node2", "TYPE")
|
|
154
151
|
result = edge.set_property("weight", 0.5)
|
|
@@ -195,9 +192,7 @@ class TestEdge:
|
|
|
195
192
|
props = edge.properties
|
|
196
193
|
assert props is None or props == {}
|
|
197
194
|
|
|
198
|
-
def test_edge_id_is_tuple_of_strings(
|
|
199
|
-
self, graph: CpgGraph, storage: Storage
|
|
200
|
-
) -> None:
|
|
195
|
+
def test_edge_id_is_tuple_of_strings(self, graph: CpgGraph, storage: Storage) -> None:
|
|
201
196
|
"""edge_id is (from_nid, to_nid, edge_type) with all strings."""
|
|
202
197
|
storage.add_node("node1")
|
|
203
198
|
storage.add_node("node2")
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Unit tests for custom exceptions.
|
|
3
3
|
"""
|
|
4
|
+
|
|
4
5
|
from typing import List
|
|
5
6
|
|
|
6
7
|
import pytest
|
|
7
8
|
|
|
8
|
-
from cpg2py._exceptions import
|
|
9
|
-
CPGError,
|
|
10
|
-
EdgeNotFoundError,
|
|
11
|
-
NodeNotFoundError,
|
|
12
|
-
TopFileNotFoundError,
|
|
13
|
-
)
|
|
9
|
+
from cpg2py._exceptions import CPGError, EdgeNotFoundError, NodeNotFoundError, TopFileNotFoundError
|
|
14
10
|
|
|
15
11
|
|
|
16
12
|
@pytest.mark.unit
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Unit tests for generic type functionality.
|
|
3
3
|
"""
|
|
4
|
+
|
|
4
5
|
from __future__ import annotations
|
|
5
6
|
|
|
6
7
|
import pytest
|
|
@@ -25,9 +26,7 @@ class TestGenerics:
|
|
|
25
26
|
assert isinstance(graph, AbcGraphQuerier)
|
|
26
27
|
assert isinstance(graph, CpgGraph)
|
|
27
28
|
|
|
28
|
-
def test_graph_node_returns_concrete_node_type(
|
|
29
|
-
self, graph: CpgGraph, storage: Storage
|
|
30
|
-
) -> None:
|
|
29
|
+
def test_graph_node_returns_concrete_node_type(self, graph: CpgGraph, storage: Storage) -> None:
|
|
31
30
|
"""
|
|
32
31
|
Tests that graph.node returns CpgNode type (not just AbcNodeQuerier).
|
|
33
32
|
|
|
@@ -41,9 +40,7 @@ class TestGenerics:
|
|
|
41
40
|
assert isinstance(node, CpgNode)
|
|
42
41
|
assert isinstance(node, type(node)) # Type consistency
|
|
43
42
|
|
|
44
|
-
def test_graph_edge_returns_concrete_edge_type(
|
|
45
|
-
self, graph: CpgGraph, storage: Storage
|
|
46
|
-
) -> None:
|
|
43
|
+
def test_graph_edge_returns_concrete_edge_type(self, graph: CpgGraph, storage: Storage) -> None:
|
|
47
44
|
"""
|
|
48
45
|
Tests that graph.edge returns CpgEdge type (not just AbcEdgeQuerier).
|
|
49
46
|
|
|
@@ -58,9 +55,7 @@ class TestGenerics:
|
|
|
58
55
|
assert edge is not None
|
|
59
56
|
assert isinstance(edge, CpgEdge)
|
|
60
57
|
|
|
61
|
-
def test_graph_succ_returns_concrete_node_types(
|
|
62
|
-
self, populated_graph: CpgGraph
|
|
63
|
-
) -> None:
|
|
58
|
+
def test_graph_succ_returns_concrete_node_types(self, populated_graph: CpgGraph) -> None:
|
|
64
59
|
"""
|
|
65
60
|
Tests that graph.succ returns Iterable[CpgNode] with correct types.
|
|
66
61
|
|
|
@@ -75,9 +70,7 @@ class TestGenerics:
|
|
|
75
70
|
assert isinstance(succ, CpgNode)
|
|
76
71
|
assert hasattr(succ, "code")
|
|
77
72
|
|
|
78
|
-
def test_graph_prev_returns_concrete_node_types(
|
|
79
|
-
self, populated_graph: CpgGraph
|
|
80
|
-
) -> None:
|
|
73
|
+
def test_graph_prev_returns_concrete_node_types(self, populated_graph: CpgGraph) -> None:
|
|
81
74
|
"""
|
|
82
75
|
Tests that graph.prev returns Iterable[CpgNode] with correct types.
|
|
83
76
|
|
|
@@ -92,9 +85,7 @@ class TestGenerics:
|
|
|
92
85
|
assert isinstance(pred, CpgNode)
|
|
93
86
|
assert hasattr(pred, "code")
|
|
94
87
|
|
|
95
|
-
def test_graph_nodes_returns_concrete_node_types(
|
|
96
|
-
self, populated_graph: CpgGraph
|
|
97
|
-
) -> None:
|
|
88
|
+
def test_graph_nodes_returns_concrete_node_types(self, populated_graph: CpgGraph) -> None:
|
|
98
89
|
"""
|
|
99
90
|
Tests that graph.nodes returns Iterable[CpgNode] with correct types.
|
|
100
91
|
|
|
@@ -173,9 +164,7 @@ class TestGenerics:
|
|
|
173
164
|
for anc in ancestors:
|
|
174
165
|
assert isinstance(anc, CpgNode)
|
|
175
166
|
|
|
176
|
-
def test_graph_first_node_returns_concrete_node_type(
|
|
177
|
-
self, populated_graph: CpgGraph
|
|
178
|
-
) -> None:
|
|
167
|
+
def test_graph_first_node_returns_concrete_node_type(self, populated_graph: CpgGraph) -> None:
|
|
179
168
|
"""
|
|
180
169
|
Tests that graph.first_node returns CpgNode type.
|
|
181
170
|
|
|
@@ -187,9 +176,7 @@ class TestGenerics:
|
|
|
187
176
|
assert first_node is not None
|
|
188
177
|
assert isinstance(first_node, CpgNode)
|
|
189
178
|
|
|
190
|
-
def test_graph_first_node_returns_none_when_empty(
|
|
191
|
-
self, graph: CpgGraph
|
|
192
|
-
) -> None:
|
|
179
|
+
def test_graph_first_node_returns_none_when_empty(self, graph: CpgGraph) -> None:
|
|
193
180
|
"""
|
|
194
181
|
Tests that graph.first_node returns None when graph is empty.
|
|
195
182
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Unit and integration tests for Graph class and cpg_graph factory.
|
|
3
3
|
"""
|
|
4
|
+
|
|
4
5
|
from __future__ import annotations
|
|
5
6
|
|
|
6
7
|
from pathlib import Path
|
|
@@ -28,9 +29,7 @@ class TestGraph:
|
|
|
28
29
|
graph = CpgGraph(storage)
|
|
29
30
|
assert graph.storage == storage
|
|
30
31
|
|
|
31
|
-
def test_graph_node_returns_node_when_exists(
|
|
32
|
-
self, graph: CpgGraph, storage: Storage
|
|
33
|
-
) -> None:
|
|
32
|
+
def test_graph_node_returns_node_when_exists(self, graph: CpgGraph, storage: Storage) -> None:
|
|
34
33
|
"""
|
|
35
34
|
Tests that graph.node returns node when it exists.
|
|
36
35
|
|
|
@@ -44,9 +43,7 @@ class TestGraph:
|
|
|
44
43
|
assert node is not None
|
|
45
44
|
assert node.id == "node1"
|
|
46
45
|
|
|
47
|
-
def test_graph_node_raises_error_when_not_found(
|
|
48
|
-
self, graph: CpgGraph
|
|
49
|
-
) -> None:
|
|
46
|
+
def test_graph_node_raises_error_when_not_found(self, graph: CpgGraph) -> None:
|
|
50
47
|
"""
|
|
51
48
|
Tests that graph.node raises NodeNotFoundError when node not found.
|
|
52
49
|
|
|
@@ -58,9 +55,7 @@ class TestGraph:
|
|
|
58
55
|
graph.node("nonexistent")
|
|
59
56
|
assert exc_info.value.node_id == "nonexistent"
|
|
60
57
|
|
|
61
|
-
def test_graph_node_empty_string_id_raises_when_not_in_storage(
|
|
62
|
-
self, graph: CpgGraph
|
|
63
|
-
) -> None:
|
|
58
|
+
def test_graph_node_empty_string_id_raises_when_not_in_storage(self, graph: CpgGraph) -> None:
|
|
64
59
|
"""
|
|
65
60
|
Tests that graph.node with empty string id raises when node not in storage.
|
|
66
61
|
|
|
@@ -72,9 +67,7 @@ class TestGraph:
|
|
|
72
67
|
graph.node("")
|
|
73
68
|
assert exc_info.value.node_id == ""
|
|
74
69
|
|
|
75
|
-
def test_graph_edge_returns_edge_when_exists(
|
|
76
|
-
self, graph: CpgGraph, storage: Storage
|
|
77
|
-
) -> None:
|
|
70
|
+
def test_graph_edge_returns_edge_when_exists(self, graph: CpgGraph, storage: Storage) -> None:
|
|
78
71
|
"""
|
|
79
72
|
Tests that graph.edge returns edge when it exists.
|
|
80
73
|
|
|
@@ -108,9 +101,7 @@ class TestGraph:
|
|
|
108
101
|
assert exc_info.value.to_id == "node2"
|
|
109
102
|
assert exc_info.value.edge_type == "TYPE"
|
|
110
103
|
|
|
111
|
-
def test_graph_nodes_empty_graph_returns_empty_iterable(
|
|
112
|
-
self, graph: CpgGraph
|
|
113
|
-
) -> None:
|
|
104
|
+
def test_graph_nodes_empty_graph_returns_empty_iterable(self, graph: CpgGraph) -> None:
|
|
114
105
|
"""
|
|
115
106
|
Tests that graph.nodes on empty graph returns empty iterable.
|
|
116
107
|
|
|
@@ -120,9 +111,7 @@ class TestGraph:
|
|
|
120
111
|
"""
|
|
121
112
|
assert list(graph.nodes()) == []
|
|
122
113
|
|
|
123
|
-
def test_graph_edges_empty_graph_returns_empty_iterable(
|
|
124
|
-
self, graph: CpgGraph
|
|
125
|
-
) -> None:
|
|
114
|
+
def test_graph_edges_empty_graph_returns_empty_iterable(self, graph: CpgGraph) -> None:
|
|
126
115
|
"""
|
|
127
116
|
Tests that graph.edges on empty graph returns empty iterable.
|
|
128
117
|
|
|
@@ -132,9 +121,7 @@ class TestGraph:
|
|
|
132
121
|
"""
|
|
133
122
|
assert list(graph.edges()) == []
|
|
134
123
|
|
|
135
|
-
def test_graph_succ_returns_successor_nodes(
|
|
136
|
-
self, populated_graph: CpgGraph
|
|
137
|
-
) -> None:
|
|
124
|
+
def test_graph_succ_returns_successor_nodes(self, populated_graph: CpgGraph) -> None:
|
|
138
125
|
"""
|
|
139
126
|
Tests that graph.succ returns successor nodes.
|
|
140
127
|
|
|
@@ -163,9 +150,7 @@ class TestGraph:
|
|
|
163
150
|
node = graph.node("leaf")
|
|
164
151
|
assert list(graph.succ(node)) == []
|
|
165
152
|
|
|
166
|
-
def test_graph_prev_returns_predecessor_nodes(
|
|
167
|
-
self, populated_graph: CpgGraph
|
|
168
|
-
) -> None:
|
|
153
|
+
def test_graph_prev_returns_predecessor_nodes(self, populated_graph: CpgGraph) -> None:
|
|
169
154
|
"""
|
|
170
155
|
Tests that graph.prev returns predecessor nodes.
|
|
171
156
|
|
|
@@ -194,9 +179,7 @@ class TestGraph:
|
|
|
194
179
|
node = graph.node("root")
|
|
195
180
|
assert list(graph.prev(node)) == []
|
|
196
181
|
|
|
197
|
-
def test_graph_children_returns_child_nodes(
|
|
198
|
-
self, graph: CpgGraph, storage: Storage
|
|
199
|
-
) -> None:
|
|
182
|
+
def test_graph_children_returns_child_nodes(self, graph: CpgGraph, storage: Storage) -> None:
|
|
200
183
|
"""
|
|
201
184
|
Tests that graph.children returns child nodes.
|
|
202
185
|
|
|
@@ -234,9 +217,7 @@ class TestGraph:
|
|
|
234
217
|
node = graph.node("leaf")
|
|
235
218
|
assert list(graph.children(node)) == []
|
|
236
219
|
|
|
237
|
-
def test_graph_parent_returns_parent_nodes(
|
|
238
|
-
self, graph: CpgGraph, storage: Storage
|
|
239
|
-
) -> None:
|
|
220
|
+
def test_graph_parent_returns_parent_nodes(self, graph: CpgGraph, storage: Storage) -> None:
|
|
240
221
|
"""
|
|
241
222
|
Tests that graph.parent returns parent nodes.
|
|
242
223
|
|
|
@@ -254,9 +235,7 @@ class TestGraph:
|
|
|
254
235
|
assert len(parents) == 1
|
|
255
236
|
assert parents[0].id == "parent"
|
|
256
237
|
|
|
257
|
-
def test_graph_parent_no_parent_returns_empty(
|
|
258
|
-
self, graph: CpgGraph, storage: Storage
|
|
259
|
-
) -> None:
|
|
238
|
+
def test_graph_parent_no_parent_returns_empty(self, graph: CpgGraph, storage: Storage) -> None:
|
|
260
239
|
"""
|
|
261
240
|
Tests that parent for root node returns empty iterable.
|
|
262
241
|
|
|
@@ -268,9 +247,7 @@ class TestGraph:
|
|
|
268
247
|
node = graph.node("root")
|
|
269
248
|
assert list(graph.parent(node)) == []
|
|
270
249
|
|
|
271
|
-
def test_graph_flow_to_returns_flow_successors(
|
|
272
|
-
self, graph: CpgGraph, storage: Storage
|
|
273
|
-
) -> None:
|
|
250
|
+
def test_graph_flow_to_returns_flow_successors(self, graph: CpgGraph, storage: Storage) -> None:
|
|
274
251
|
"""
|
|
275
252
|
Tests that graph.flow_to returns flow-to successor nodes.
|
|
276
253
|
|
|
@@ -409,9 +386,7 @@ class TestGraph:
|
|
|
409
386
|
graph.topfile_node("node1")
|
|
410
387
|
assert exc_info.value.node_id == "node1"
|
|
411
388
|
|
|
412
|
-
def test_graph_nodes_returns_all_nodes(
|
|
413
|
-
self, populated_graph: CpgGraph
|
|
414
|
-
) -> None:
|
|
389
|
+
def test_graph_nodes_returns_all_nodes(self, populated_graph: CpgGraph) -> None:
|
|
415
390
|
"""
|
|
416
391
|
Tests that graph.nodes returns all nodes.
|
|
417
392
|
|
|
@@ -459,9 +434,7 @@ class TestGraph:
|
|
|
459
434
|
nodes = list(graph.nodes(lambda n: n.type == "ImpossibleType"))
|
|
460
435
|
assert nodes == []
|
|
461
436
|
|
|
462
|
-
def test_graph_edges_returns_all_edges(
|
|
463
|
-
self, graph: CpgGraph, storage: Storage
|
|
464
|
-
) -> None:
|
|
437
|
+
def test_graph_edges_returns_all_edges(self, graph: CpgGraph, storage: Storage) -> None:
|
|
465
438
|
"""
|
|
466
439
|
Tests that graph.edges returns all edges.
|
|
467
440
|
|
|
@@ -516,9 +489,7 @@ class TestGraph:
|
|
|
516
489
|
edges = list(graph.edges(lambda e: e.type == "ImpossibleType"))
|
|
517
490
|
assert edges == []
|
|
518
491
|
|
|
519
|
-
def test_graph_first_node_returns_first_matching_node(
|
|
520
|
-
self, populated_graph: CpgGraph
|
|
521
|
-
) -> None:
|
|
492
|
+
def test_graph_first_node_returns_first_matching_node(self, populated_graph: CpgGraph) -> None:
|
|
522
493
|
"""
|
|
523
494
|
Tests that graph.first_node returns first matching node.
|
|
524
495
|
|
|
@@ -600,9 +571,7 @@ class TestGraph:
|
|
|
600
571
|
node = graph.node("leaf")
|
|
601
572
|
assert list(graph.descendants(node)) == []
|
|
602
573
|
|
|
603
|
-
def test_graph_ancestors_returns_all_ancestors(
|
|
604
|
-
self, graph: CpgGraph, storage: Storage
|
|
605
|
-
) -> None:
|
|
574
|
+
def test_graph_ancestors_returns_all_ancestors(self, graph: CpgGraph, storage: Storage) -> None:
|
|
606
575
|
"""
|
|
607
576
|
Tests that graph.ancestors returns all ancestor nodes.
|
|
608
577
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Unit tests for Node class.
|
|
3
3
|
"""
|
|
4
|
+
|
|
4
5
|
from __future__ import annotations
|
|
5
6
|
|
|
6
7
|
import pytest
|
|
@@ -13,9 +14,7 @@ from cpg2py._cpg import CpgGraph, CpgNode
|
|
|
13
14
|
class TestNode:
|
|
14
15
|
"""Test cases for Node class."""
|
|
15
16
|
|
|
16
|
-
def test_node_id_returns_node_identifier(
|
|
17
|
-
self, graph: CpgGraph, storage: Storage
|
|
18
|
-
) -> None:
|
|
17
|
+
def test_node_id_returns_node_identifier(self, graph: CpgGraph, storage: Storage) -> None:
|
|
19
18
|
"""
|
|
20
19
|
Tests that node.id returns the node identifier.
|
|
21
20
|
|
|
@@ -27,9 +26,7 @@ class TestNode:
|
|
|
27
26
|
node = graph.node("node1")
|
|
28
27
|
assert node.id == "node1"
|
|
29
28
|
|
|
30
|
-
def test_node_code_returns_code_property(
|
|
31
|
-
self, graph: CpgGraph, storage: Storage
|
|
32
|
-
) -> None:
|
|
29
|
+
def test_node_code_returns_code_property(self, graph: CpgGraph, storage: Storage) -> None:
|
|
33
30
|
"""
|
|
34
31
|
Tests that node.code returns the code property.
|
|
35
32
|
|
|
@@ -72,9 +69,7 @@ class TestNode:
|
|
|
72
69
|
node = graph.node("node1")
|
|
73
70
|
assert node.label == "test_label"
|
|
74
71
|
|
|
75
|
-
def test_node_flags_returns_list_of_flags(
|
|
76
|
-
self, graph: CpgGraph, storage: Storage
|
|
77
|
-
) -> None:
|
|
72
|
+
def test_node_flags_returns_list_of_flags(self, graph: CpgGraph, storage: Storage) -> None:
|
|
78
73
|
"""
|
|
79
74
|
Tests that node.flags returns a list of flags.
|
|
80
75
|
|
|
@@ -149,9 +144,7 @@ class TestNode:
|
|
|
149
144
|
node = graph.node("node1")
|
|
150
145
|
assert node.line_num is None
|
|
151
146
|
|
|
152
|
-
def test_node_children_num_returns_integer(
|
|
153
|
-
self, graph: CpgGraph, storage: Storage
|
|
154
|
-
) -> None:
|
|
147
|
+
def test_node_children_num_returns_integer(self, graph: CpgGraph, storage: Storage) -> None:
|
|
155
148
|
"""
|
|
156
149
|
Tests that node.children_num returns integer.
|
|
157
150
|
|
|
@@ -164,9 +157,7 @@ class TestNode:
|
|
|
164
157
|
node = graph.node("node1")
|
|
165
158
|
assert node.children_num == 5
|
|
166
159
|
|
|
167
|
-
def test_node_func_id_returns_integer(
|
|
168
|
-
self, graph: CpgGraph, storage: Storage
|
|
169
|
-
) -> None:
|
|
160
|
+
def test_node_func_id_returns_integer(self, graph: CpgGraph, storage: Storage) -> None:
|
|
170
161
|
"""
|
|
171
162
|
Tests that node.func_id returns integer.
|
|
172
163
|
|
|
@@ -179,9 +170,7 @@ class TestNode:
|
|
|
179
170
|
node = graph.node("node1")
|
|
180
171
|
assert node.func_id == 10
|
|
181
172
|
|
|
182
|
-
def test_node_class_name_returns_string(
|
|
183
|
-
self, graph: CpgGraph, storage: Storage
|
|
184
|
-
) -> None:
|
|
173
|
+
def test_node_class_name_returns_string(self, graph: CpgGraph, storage: Storage) -> None:
|
|
185
174
|
"""
|
|
186
175
|
Tests that node.class_name returns string.
|
|
187
176
|
|
|
@@ -194,9 +183,7 @@ class TestNode:
|
|
|
194
183
|
node = graph.node("node1")
|
|
195
184
|
assert node.class_name == "TestClass"
|
|
196
185
|
|
|
197
|
-
def test_node_namespace_returns_string(
|
|
198
|
-
self, graph: CpgGraph, storage: Storage
|
|
199
|
-
) -> None:
|
|
186
|
+
def test_node_namespace_returns_string(self, graph: CpgGraph, storage: Storage) -> None:
|
|
200
187
|
"""
|
|
201
188
|
Tests that node.namespace returns string.
|
|
202
189
|
|
|
@@ -209,9 +196,7 @@ class TestNode:
|
|
|
209
196
|
node = graph.node("node1")
|
|
210
197
|
assert node.namespace == "com.example"
|
|
211
198
|
|
|
212
|
-
def test_node_name_returns_string(
|
|
213
|
-
self, graph: CpgGraph, storage: Storage
|
|
214
|
-
) -> None:
|
|
199
|
+
def test_node_name_returns_string(self, graph: CpgGraph, storage: Storage) -> None:
|
|
215
200
|
"""
|
|
216
201
|
Tests that node.name returns string.
|
|
217
202
|
|
|
@@ -254,9 +239,7 @@ class TestNode:
|
|
|
254
239
|
node = graph.node("node1")
|
|
255
240
|
assert node.end_num == 50
|
|
256
241
|
|
|
257
|
-
def test_node_comment_returns_string(
|
|
258
|
-
self, graph: CpgGraph, storage: Storage
|
|
259
|
-
) -> None:
|
|
242
|
+
def test_node_comment_returns_string(self, graph: CpgGraph, storage: Storage) -> None:
|
|
260
243
|
"""
|
|
261
244
|
Tests that node.comment returns string.
|
|
262
245
|
|
|
@@ -295,9 +278,7 @@ class TestNode:
|
|
|
295
278
|
node = graph.node("node1")
|
|
296
279
|
assert node.get_property("missing1", "missing2") is None
|
|
297
280
|
|
|
298
|
-
def test_node_set_property_nonexistent_node_returns_false(
|
|
299
|
-
self, storage: Storage
|
|
300
|
-
) -> None:
|
|
281
|
+
def test_node_set_property_nonexistent_node_returns_false(self, storage: Storage) -> None:
|
|
301
282
|
"""set_property returns False when node no longer exists in storage."""
|
|
302
283
|
storage.add_node("node1")
|
|
303
284
|
node = CpgNode(storage, "node1")
|
|
@@ -325,9 +306,7 @@ class TestNode:
|
|
|
325
306
|
result = node.set_properties({})
|
|
326
307
|
assert result is True
|
|
327
308
|
|
|
328
|
-
def test_node_type_returns_string(
|
|
329
|
-
self, graph: CpgGraph, storage: Storage
|
|
330
|
-
) -> None:
|
|
309
|
+
def test_node_type_returns_string(self, graph: CpgGraph, storage: Storage) -> None:
|
|
331
310
|
"""
|
|
332
311
|
Tests that node.type returns string.
|
|
333
312
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Unit tests for Storage class.
|
|
3
3
|
"""
|
|
4
|
+
|
|
4
5
|
from __future__ import annotations
|
|
5
6
|
|
|
6
7
|
from pathlib import Path
|
|
@@ -65,9 +66,7 @@ class TestStorage:
|
|
|
65
66
|
storage.add_node("node3")
|
|
66
67
|
assert len(list(storage.get_nodes())) == 3
|
|
67
68
|
|
|
68
|
-
def test_storage_contains_node_returns_true_for_existing(
|
|
69
|
-
self, storage: Storage
|
|
70
|
-
) -> None:
|
|
69
|
+
def test_storage_contains_node_returns_true_for_existing(self, storage: Storage) -> None:
|
|
71
70
|
"""
|
|
72
71
|
Tests that contains_node returns True for existing nodes.
|
|
73
72
|
|
|
@@ -78,9 +77,7 @@ class TestStorage:
|
|
|
78
77
|
storage.add_node("node1")
|
|
79
78
|
assert storage.contains_node("node1") is True
|
|
80
79
|
|
|
81
|
-
def test_storage_contains_node_returns_false_for_missing(
|
|
82
|
-
self, storage: Storage
|
|
83
|
-
) -> None:
|
|
80
|
+
def test_storage_contains_node_returns_false_for_missing(self, storage: Storage) -> None:
|
|
84
81
|
"""
|
|
85
82
|
Tests that contains_node returns False for missing nodes.
|
|
86
83
|
|
|
@@ -120,9 +117,7 @@ class TestStorage:
|
|
|
120
117
|
result = storage.add_edge(edge_id)
|
|
121
118
|
assert result is False
|
|
122
119
|
|
|
123
|
-
def test_storage_add_edge_missing_source_returns_false(
|
|
124
|
-
self, storage: Storage
|
|
125
|
-
) -> None:
|
|
120
|
+
def test_storage_add_edge_missing_source_returns_false(self, storage: Storage) -> None:
|
|
126
121
|
"""
|
|
127
122
|
Tests that adding edge with missing source node returns False.
|
|
128
123
|
|
|
@@ -135,9 +130,7 @@ class TestStorage:
|
|
|
135
130
|
result = storage.add_edge(edge_id)
|
|
136
131
|
assert result is False
|
|
137
132
|
|
|
138
|
-
def test_storage_add_edge_missing_target_returns_false(
|
|
139
|
-
self, storage: Storage
|
|
140
|
-
) -> None:
|
|
133
|
+
def test_storage_add_edge_missing_target_returns_false(self, storage: Storage) -> None:
|
|
141
134
|
"""
|
|
142
135
|
Tests that adding edge with missing target node returns False.
|
|
143
136
|
|
|
@@ -150,9 +143,7 @@ class TestStorage:
|
|
|
150
143
|
result = storage.add_edge(edge_id)
|
|
151
144
|
assert result is False
|
|
152
145
|
|
|
153
|
-
def test_storage_out_edges_returns_all_outgoing_edges(
|
|
154
|
-
self, storage: Storage
|
|
155
|
-
) -> None:
|
|
146
|
+
def test_storage_out_edges_returns_all_outgoing_edges(self, storage: Storage) -> None:
|
|
156
147
|
"""
|
|
157
148
|
Tests that out_edges returns all outgoing edges for a node.
|
|
158
149
|
|
|
@@ -172,9 +163,7 @@ class TestStorage:
|
|
|
172
163
|
assert edge1 in out_edges
|
|
173
164
|
assert edge2 in out_edges
|
|
174
165
|
|
|
175
|
-
def test_storage_in_edges_returns_all_incoming_edges(
|
|
176
|
-
self, storage: Storage
|
|
177
|
-
) -> None:
|
|
166
|
+
def test_storage_in_edges_returns_all_incoming_edges(self, storage: Storage) -> None:
|
|
178
167
|
"""
|
|
179
168
|
Tests that in_edges returns all incoming edges for a node.
|
|
180
169
|
|
|
@@ -194,9 +183,7 @@ class TestStorage:
|
|
|
194
183
|
assert edge1 in in_edges
|
|
195
184
|
assert edge2 in in_edges
|
|
196
185
|
|
|
197
|
-
def test_storage_successors_returns_all_successor_nodes(
|
|
198
|
-
self, storage: Storage
|
|
199
|
-
) -> None:
|
|
186
|
+
def test_storage_successors_returns_all_successor_nodes(self, storage: Storage) -> None:
|
|
200
187
|
"""
|
|
201
188
|
Tests that successors returns all successor nodes.
|
|
202
189
|
|
|
@@ -214,9 +201,7 @@ class TestStorage:
|
|
|
214
201
|
assert "node2" in successors
|
|
215
202
|
assert "node3" in successors
|
|
216
203
|
|
|
217
|
-
def test_storage_predecessors_returns_all_predecessor_nodes(
|
|
218
|
-
self, storage: Storage
|
|
219
|
-
) -> None:
|
|
204
|
+
def test_storage_predecessors_returns_all_predecessor_nodes(self, storage: Storage) -> None:
|
|
220
205
|
"""
|
|
221
206
|
Tests that predecessors returns all predecessor nodes.
|
|
222
207
|
|
|
@@ -250,9 +235,7 @@ class TestStorage:
|
|
|
250
235
|
assert node_props["name"] == "test"
|
|
251
236
|
assert node_props["type"] == "AST"
|
|
252
237
|
|
|
253
|
-
def test_storage_set_node_props_nonexistent_returns_false(
|
|
254
|
-
self, storage: Storage
|
|
255
|
-
) -> None:
|
|
238
|
+
def test_storage_set_node_props_nonexistent_returns_false(self, storage: Storage) -> None:
|
|
256
239
|
"""
|
|
257
240
|
Tests that set_node_props returns False for nonexistent node.
|
|
258
241
|
|
|
@@ -264,9 +247,7 @@ class TestStorage:
|
|
|
264
247
|
result = storage.set_node_props("node1", props)
|
|
265
248
|
assert result is False
|
|
266
249
|
|
|
267
|
-
def test_storage_get_node_props_returns_properties(
|
|
268
|
-
self, storage: Storage
|
|
269
|
-
) -> None:
|
|
250
|
+
def test_storage_get_node_props_returns_properties(self, storage: Storage) -> None:
|
|
270
251
|
"""
|
|
271
252
|
Tests that get_node_props returns node properties.
|
|
272
253
|
|
|
@@ -281,9 +262,7 @@ class TestStorage:
|
|
|
281
262
|
assert retrieved_props is not None
|
|
282
263
|
assert retrieved_props["name"] == "test"
|
|
283
264
|
|
|
284
|
-
def test_storage_get_node_props_nonexistent_returns_none(
|
|
285
|
-
self, storage: Storage
|
|
286
|
-
) -> None:
|
|
265
|
+
def test_storage_get_node_props_nonexistent_returns_none(self, storage: Storage) -> None:
|
|
287
266
|
"""
|
|
288
267
|
Tests that get_node_props returns None for nonexistent node.
|
|
289
268
|
|
|
@@ -294,9 +273,7 @@ class TestStorage:
|
|
|
294
273
|
props = storage.get_node_props("node1")
|
|
295
274
|
assert props is None
|
|
296
275
|
|
|
297
|
-
def test_storage_set_node_prop_sets_single_property(
|
|
298
|
-
self, storage: Storage
|
|
299
|
-
) -> None:
|
|
276
|
+
def test_storage_set_node_prop_sets_single_property(self, storage: Storage) -> None:
|
|
300
277
|
"""
|
|
301
278
|
Tests that set_node_prop sets a single property.
|
|
302
279
|
|
|
@@ -310,9 +287,7 @@ class TestStorage:
|
|
|
310
287
|
value = storage.get_node_prop("node1", "name")
|
|
311
288
|
assert value == "test"
|
|
312
289
|
|
|
313
|
-
def test_storage_get_node_prop_returns_property_value(
|
|
314
|
-
self, storage: Storage
|
|
315
|
-
) -> None:
|
|
290
|
+
def test_storage_get_node_prop_returns_property_value(self, storage: Storage) -> None:
|
|
316
291
|
"""
|
|
317
292
|
Tests that get_node_prop returns property value.
|
|
318
293
|
|
|
@@ -325,9 +300,7 @@ class TestStorage:
|
|
|
325
300
|
value = storage.get_node_prop("node1", "name")
|
|
326
301
|
assert value == "test"
|
|
327
302
|
|
|
328
|
-
def test_storage_get_node_prop_nonexistent_returns_none(
|
|
329
|
-
self, storage: Storage
|
|
330
|
-
) -> None:
|
|
303
|
+
def test_storage_get_node_prop_nonexistent_returns_none(self, storage: Storage) -> None:
|
|
331
304
|
"""
|
|
332
305
|
Tests that get_node_prop returns None for nonexistent property.
|
|
333
306
|
|
|
@@ -357,9 +330,7 @@ class TestStorage:
|
|
|
357
330
|
assert edge_props["weight"] == 1.0
|
|
358
331
|
assert edge_props["label"] == "test"
|
|
359
332
|
|
|
360
|
-
def test_storage_set_edge_props_nonexistent_returns_false(
|
|
361
|
-
self, storage: Storage
|
|
362
|
-
) -> None:
|
|
333
|
+
def test_storage_set_edge_props_nonexistent_returns_false(self, storage: Storage) -> None:
|
|
363
334
|
"""
|
|
364
335
|
Tests that set_edge_props returns False for nonexistent edge.
|
|
365
336
|
|
|
@@ -372,9 +343,7 @@ class TestStorage:
|
|
|
372
343
|
result = storage.set_edge_props(edge_id, props)
|
|
373
344
|
assert result is False
|
|
374
345
|
|
|
375
|
-
def test_storage_get_edge_props_returns_properties(
|
|
376
|
-
self, storage: Storage
|
|
377
|
-
) -> None:
|
|
346
|
+
def test_storage_get_edge_props_returns_properties(self, storage: Storage) -> None:
|
|
378
347
|
"""
|
|
379
348
|
Tests that get_edge_props returns edge properties.
|
|
380
349
|
|
|
@@ -392,9 +361,7 @@ class TestStorage:
|
|
|
392
361
|
assert retrieved_props is not None
|
|
393
362
|
assert retrieved_props["weight"] == 1.0
|
|
394
363
|
|
|
395
|
-
def test_storage_set_edge_prop_sets_single_property(
|
|
396
|
-
self, storage: Storage
|
|
397
|
-
) -> None:
|
|
364
|
+
def test_storage_set_edge_prop_sets_single_property(self, storage: Storage) -> None:
|
|
398
365
|
"""
|
|
399
366
|
Tests that set_edge_prop sets a single property.
|
|
400
367
|
|
|
@@ -411,9 +378,7 @@ class TestStorage:
|
|
|
411
378
|
value = storage.get_edge_prop(edge_id, "weight")
|
|
412
379
|
assert value == 1.0
|
|
413
380
|
|
|
414
|
-
def test_storage_get_edge_prop_returns_property_value(
|
|
415
|
-
self, storage: Storage
|
|
416
|
-
) -> None:
|
|
381
|
+
def test_storage_get_edge_prop_returns_property_value(self, storage: Storage) -> None:
|
|
417
382
|
"""
|
|
418
383
|
Tests that get_edge_prop returns property value.
|
|
419
384
|
|
|
@@ -442,9 +407,7 @@ class TestStorage:
|
|
|
442
407
|
assert result is True
|
|
443
408
|
assert storage.contains_node("node1") is False
|
|
444
409
|
|
|
445
|
-
def test_storage_remove_node_nonexistent_returns_false(
|
|
446
|
-
self, storage: Storage
|
|
447
|
-
) -> None:
|
|
410
|
+
def test_storage_remove_node_nonexistent_returns_false(self, storage: Storage) -> None:
|
|
448
411
|
"""
|
|
449
412
|
Tests that remove_node returns False for nonexistent node.
|
|
450
413
|
|
|
@@ -471,9 +434,7 @@ class TestStorage:
|
|
|
471
434
|
assert result is True
|
|
472
435
|
assert storage.contains_edge(edge_id) is False
|
|
473
436
|
|
|
474
|
-
def test_storage_remove_edge_nonexistent_returns_false(
|
|
475
|
-
self, storage: Storage
|
|
476
|
-
) -> None:
|
|
437
|
+
def test_storage_remove_edge_nonexistent_returns_false(self, storage: Storage) -> None:
|
|
477
438
|
"""
|
|
478
439
|
Tests that remove_edge returns False for nonexistent edge.
|
|
479
440
|
|
|
@@ -485,9 +446,7 @@ class TestStorage:
|
|
|
485
446
|
result = storage.remove_edge(edge_id)
|
|
486
447
|
assert result is False
|
|
487
448
|
|
|
488
|
-
def test_storage_get_nodes_empty_returns_empty_iterable(
|
|
489
|
-
self, storage: Storage
|
|
490
|
-
) -> None:
|
|
449
|
+
def test_storage_get_nodes_empty_returns_empty_iterable(self, storage: Storage) -> None:
|
|
491
450
|
"""
|
|
492
451
|
Tests that get_nodes on empty storage returns empty iterable.
|
|
493
452
|
|
|
@@ -513,9 +472,7 @@ class TestStorage:
|
|
|
513
472
|
assert "node1" in nodes
|
|
514
473
|
assert "node2" in nodes
|
|
515
474
|
|
|
516
|
-
def test_storage_get_edges_empty_returns_empty_iterable(
|
|
517
|
-
self, storage: Storage
|
|
518
|
-
) -> None:
|
|
475
|
+
def test_storage_get_edges_empty_returns_empty_iterable(self, storage: Storage) -> None:
|
|
519
476
|
"""
|
|
520
477
|
Tests that get_edges on empty storage returns empty iterable.
|
|
521
478
|
|
|
@@ -526,9 +483,7 @@ class TestStorage:
|
|
|
526
483
|
edges = list(storage.get_edges())
|
|
527
484
|
assert edges == []
|
|
528
485
|
|
|
529
|
-
def test_storage_out_edges_nonexistent_node_returns_empty(
|
|
530
|
-
self, storage: Storage
|
|
531
|
-
) -> None:
|
|
486
|
+
def test_storage_out_edges_nonexistent_node_returns_empty(self, storage: Storage) -> None:
|
|
532
487
|
"""
|
|
533
488
|
Tests that out_edges for nonexistent node returns empty iterable.
|
|
534
489
|
|
|
@@ -539,9 +494,7 @@ class TestStorage:
|
|
|
539
494
|
out = list(storage.out_edges("nonexistent"))
|
|
540
495
|
assert out == []
|
|
541
496
|
|
|
542
|
-
def test_storage_in_edges_nonexistent_node_returns_empty(
|
|
543
|
-
self, storage: Storage
|
|
544
|
-
) -> None:
|
|
497
|
+
def test_storage_in_edges_nonexistent_node_returns_empty(self, storage: Storage) -> None:
|
|
545
498
|
"""
|
|
546
499
|
Tests that in_edges for nonexistent node returns empty iterable.
|
|
547
500
|
|
|
@@ -552,9 +505,7 @@ class TestStorage:
|
|
|
552
505
|
inc = list(storage.in_edges("nonexistent"))
|
|
553
506
|
assert inc == []
|
|
554
507
|
|
|
555
|
-
def test_storage_successors_nonexistent_node_returns_empty(
|
|
556
|
-
self, storage: Storage
|
|
557
|
-
) -> None:
|
|
508
|
+
def test_storage_successors_nonexistent_node_returns_empty(self, storage: Storage) -> None:
|
|
558
509
|
"""
|
|
559
510
|
Tests that successors for nonexistent node returns empty iterable.
|
|
560
511
|
|
|
@@ -565,9 +516,7 @@ class TestStorage:
|
|
|
565
516
|
succ = list(storage.successors("nonexistent"))
|
|
566
517
|
assert succ == []
|
|
567
518
|
|
|
568
|
-
def test_storage_predecessors_nonexistent_node_returns_empty(
|
|
569
|
-
self, storage: Storage
|
|
570
|
-
) -> None:
|
|
519
|
+
def test_storage_predecessors_nonexistent_node_returns_empty(self, storage: Storage) -> None:
|
|
571
520
|
"""
|
|
572
521
|
Tests that predecessors for nonexistent node returns empty iterable.
|
|
573
522
|
|
|
@@ -578,9 +527,7 @@ class TestStorage:
|
|
|
578
527
|
pred = list(storage.predecessors("nonexistent"))
|
|
579
528
|
assert pred == []
|
|
580
529
|
|
|
581
|
-
def test_storage_set_node_props_empty_dict_succeeds(
|
|
582
|
-
self, storage: Storage
|
|
583
|
-
) -> None:
|
|
530
|
+
def test_storage_set_node_props_empty_dict_succeeds(self, storage: Storage) -> None:
|
|
584
531
|
"""
|
|
585
532
|
Tests that set_node_props with empty dict does not fail.
|
|
586
533
|
|
|
@@ -593,9 +540,7 @@ class TestStorage:
|
|
|
593
540
|
assert result is True
|
|
594
541
|
assert storage.contains_node("n1")
|
|
595
542
|
|
|
596
|
-
def test_storage_set_edge_props_empty_dict_succeeds(
|
|
597
|
-
self, storage: Storage
|
|
598
|
-
) -> None:
|
|
543
|
+
def test_storage_set_edge_props_empty_dict_succeeds(self, storage: Storage) -> None:
|
|
599
544
|
"""
|
|
600
545
|
Tests that set_edge_props with empty dict does not fail.
|
|
601
546
|
|
|
@@ -676,9 +621,7 @@ class TestStorage:
|
|
|
676
621
|
assert len(edges) == 1
|
|
677
622
|
assert edge_id in edges
|
|
678
623
|
|
|
679
|
-
def test_storage_save_json_empty_graph_succeeds(
|
|
680
|
-
self, storage: Storage, temp_dir: Path
|
|
681
|
-
) -> None:
|
|
624
|
+
def test_storage_save_json_empty_graph_succeeds(self, storage: Storage, temp_dir: Path) -> None:
|
|
682
625
|
"""
|
|
683
626
|
Tests that save_json on empty graph writes valid JSON and does not raise.
|
|
684
627
|
|
|
@@ -724,8 +667,7 @@ class TestStorage:
|
|
|
724
667
|
path = temp_dir / "no_props.json"
|
|
725
668
|
with open(path, "w", encoding="utf-8") as f:
|
|
726
669
|
f.write(
|
|
727
|
-
'{"nodes": {"1": {}, "2": {}}, '
|
|
728
|
-
'"edges": [{"from": "1", "to": "2", "type": "T"}]}'
|
|
670
|
+
'{"nodes": {"1": {}, "2": {}}, ' '"edges": [{"from": "1", "to": "2", "type": "T"}]}'
|
|
729
671
|
)
|
|
730
672
|
storage.load_json(path)
|
|
731
673
|
assert storage.contains_edge(("1", "2", "T"))
|
|
@@ -775,10 +717,7 @@ class TestStorage:
|
|
|
775
717
|
"""
|
|
776
718
|
path = temp_dir / "bad_edge.json"
|
|
777
719
|
with open(path, "w", encoding="utf-8") as f:
|
|
778
|
-
f.write(
|
|
779
|
-
'{"nodes": {"1": {}, "2": {}}, '
|
|
780
|
-
'"edges": [{"to": "2", "type": "T"}]}'
|
|
781
|
-
)
|
|
720
|
+
f.write('{"nodes": {"1": {}, "2": {}}, ' '"edges": [{"to": "2", "type": "T"}]}')
|
|
782
721
|
with pytest.raises(KeyError):
|
|
783
722
|
storage.load_json(path)
|
|
784
723
|
|
|
@@ -824,9 +763,7 @@ class TestStorage:
|
|
|
824
763
|
with pytest.raises(ValueError, match="nodes.*edges"):
|
|
825
764
|
storage.load_json(path)
|
|
826
765
|
|
|
827
|
-
def test_storage_from_json_creates_populated_storage(
|
|
828
|
-
self, temp_dir: Path
|
|
829
|
-
) -> None:
|
|
766
|
+
def test_storage_from_json_creates_populated_storage(self, temp_dir: Path) -> None:
|
|
830
767
|
"""storage_from_json returns Storage populated from JSON file."""
|
|
831
768
|
path = temp_dir / "graph.json"
|
|
832
769
|
with open(path, "w", encoding="utf-8") as f:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|