Repository navigation
Expand file tree
/
Copy pathtest_graph.py
More file actions
244 lines (199 loc) · 7.2 KB
/
Copy pathtest_graph.py
File metadata and controls
244 lines (199 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
import pyarrow as pa
import pytest
from lance_graph import CypherQuery, DirNamespace, GraphConfig
@pytest.fixture
def graph_env(tmp_path):
people_table = pa.table(
{
"person_id": [1, 2, 3, 4],
"name": ["Alice", "Bob", "Carol", "David"],
"age": [28, 34, 29, 42],
"city": ["New York", "San Francisco", "New York", "Chicago"],
}
)
companies_table = pa.table(
{
"company_id": [101, 102, 103],
"company_name": ["TechCorp", "DataInc", "CloudSoft"],
"industry": ["Technology", "Analytics", "Cloud"],
}
)
employment_table = pa.table(
{
"person_id": [1, 2, 3, 4],
"company_id": [101, 101, 102, 103],
"position": ["Engineer", "Designer", "Manager", "Director"],
"salary": [120000, 95000, 130000, 180000],
}
)
friendship_table = pa.table(
{
"person1_id": [1, 1, 2, 3],
"person2_id": [2, 3, 4, 4],
"friendship_type": ["close", "casual", "close", "casual"],
"years_known": [5, 2, 3, 1],
}
)
config = (
GraphConfig.builder()
.with_node_label("Person", "person_id")
.with_node_label("Company", "company_id")
.with_relationship("WORKS_FOR", "person_id", "company_id")
.with_relationship("FRIEND_OF", "person1_id", "person2_id")
.build()
)
datasets = {
"Person": people_table,
"Company": companies_table,
"WORKS_FOR": employment_table,
"FRIEND_OF": friendship_table,
}
return config, datasets, people_table
def test_basic_node_selection(graph_env):
config, datasets, _ = graph_env
query = CypherQuery("MATCH (p:Person) RETURN p.name, p.age").with_config(config)
result = query.execute({"Person": datasets["Person"]})
data = result.to_pydict()
assert set(data.keys()) == {"p.name", "p.age"}
assert len(data["p.name"]) == 4
assert "Alice" in set(data["p.name"])
def test_filtered_query(graph_env):
config, datasets, _ = graph_env
query = CypherQuery(
"MATCH (p:Person) WHERE p.age > 30 RETURN p.name, p.age"
).with_config(config)
result = query.execute({"Person": datasets["Person"]})
data = result.to_pydict()
assert len(data["p.name"]) == 2
assert set(data["p.name"]) == {"Bob", "David"}
assert all(age > 30 for age in data["p.age"])
def test_relationship_query(graph_env):
config, datasets, _ = graph_env
query = CypherQuery(
"MATCH (p:Person)-[:WORKS_FOR]->(c:Company) "
"RETURN p.person_id AS person_id, p.name AS name, c.company_id AS company_id"
).with_config(config)
result = query.execute(
{
"Person": datasets["Person"],
"Company": datasets["Company"],
"WORKS_FOR": datasets["WORKS_FOR"],
}
)
data = result.to_pydict()
assert len(data["person_id"]) == 4
assert data["person_id"] == [1, 2, 3, 4]
assert data["company_id"] == [101, 101, 102, 103]
def test_friendship_direct_and_network(graph_env):
config, datasets, _ = graph_env
# Direct friends of Alice (person_id = 1)
query_direct = CypherQuery(
"MATCH (a:Person)-[:FRIEND_OF]->(b:Person) "
"WHERE a.person_id = 1 "
"RETURN b.person_id AS friend_id"
).with_config(config)
result_direct = query_direct.execute(
{
"Person": datasets["Person"],
"FRIEND_OF": datasets["FRIEND_OF"],
}
)
data_direct = result_direct.to_pydict()
assert set(data_direct["friend_id"]) == {2, 3}
# Full friendship edges
query_edges = CypherQuery(
"MATCH (f:Person)-[r:FRIEND_OF]->(t:Person) "
"RETURN f.person_id AS person1_id, t.person_id AS person2_id"
).with_config(config)
result_edges = query_edges.execute(
{
"Person": datasets["Person"],
"FRIEND_OF": datasets["FRIEND_OF"],
}
)
data_edges = result_edges.to_pydict()
got = set(zip(data_edges["person1_id"], data_edges["person2_id"]))
assert got == {(1, 2), (1, 3), (2, 4), (3, 4)}
def test_two_hop_friends_of_friends(graph_env):
config, datasets, _ = graph_env
query = CypherQuery(
"MATCH (a:Person)-[:FRIEND_OF]->(b:Person)-[:FRIEND_OF]->(c:Person) "
"WHERE a.person_id = 1 "
"RETURN a.person_id AS a_id, b.person_id AS b_id, c.person_id AS c_id"
).with_config(config)
result = query.execute(
{
"Person": datasets["Person"],
"FRIEND_OF": datasets["FRIEND_OF"],
}
)
data = result.to_pydict()
assert set(data["c_id"]) == {4}
def test_variable_length_path(graph_env):
config, datasets, _ = graph_env
query = CypherQuery(
"MATCH (p1:Person)-[:FRIEND_OF*1..2]-(p2:Person) "
"RETURN p1.person_id AS p1, p2.person_id AS p2"
).with_config(config)
result = query.execute(
{
"Person": datasets["Person"],
"FRIEND_OF": datasets["FRIEND_OF"],
}
)
data = result.to_pydict()
got = set(zip(data["p1"], data["p2"]))
assert got == {(1, 2), (1, 3), (2, 4), (3, 4), (1, 4)}
def test_distinct_clause(graph_env):
config, datasets, _ = graph_env
query = CypherQuery(
"MATCH (p:Person)-[:WORKS_FOR]->(c:Company) RETURN DISTINCT c.company_name"
).with_config(config)
result = query.execute(
{
"Person": datasets["Person"],
"Company": datasets["Company"],
"WORKS_FOR": datasets["WORKS_FOR"],
}
)
data = result.to_pydict()
assert len(data["c.company_name"]) == 3
assert set(data["c.company_name"]) == {"TechCorp", "DataInc", "CloudSoft"}
@pytest.mark.requires_lance
def test_execute_with_directory_namespace(graph_env, tmp_path):
config, datasets, _ = graph_env
from lance import write_dataset
for name, table in datasets.items():
write_dataset(table, tmp_path / f"{name}.lance")
namespace = DirNamespace(str(tmp_path))
query = CypherQuery("MATCH (p:Person) WHERE p.age > 30 RETURN p.name").with_config(
config
)
result = query.execute_with_namespace(namespace)
data = result.to_pydict()
assert set(data["p.name"]) == {"Bob", "David"}
def test_cypher_parameter_syntax(graph_env):
"""Test Cypher parameter syntax ($)."""
config, datasets, _ = graph_env
# 1. Test $param
query_dollar = CypherQuery(
"MATCH (p:Person) WHERE p.age > $age RETURN p.name"
).with_config(config)
result = query_dollar.with_parameter("age", 30).execute(datasets)
data = result.to_pydict()
assert set(data["p.name"]) == {"Bob", "David"}
# 2. Test multiple parameters
query_multi = CypherQuery(
"MATCH (p:Person) WHERE p.age > $min_age AND p.age < $max_age RETURN p.name"
).with_config(config)
result = (
query_multi.with_parameter("min_age", 25)
.with_parameter("max_age", 35)
.execute(datasets)
)
data = result.to_pydict()
# Should get Alice (28), Carol (29), Bob (34)
# David is 42 (excluded)
assert set(data["p.name"]) == {"Alice", "Carol", "Bob"}