forked from LadybugDB/ladybug-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parameter.py
More file actions
373 lines (326 loc) · 12.4 KB
/
Copy pathtest_parameter.py
File metadata and controls
373 lines (326 loc) · 12.4 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from __future__ import annotations
import datetime
import pytest
from type_aliases import ConnDB
# required by python-lint
def test_struct_param_access(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
batch = [
{
"id": "1",
"name": "Christopher",
"age": "61",
"net_worth": "21561030.8",
"email": "[email protected]",
"address": "588 Campbell Well Suite 335, Lawrencemouth, VI 07241",
"phone": "358-202-1821x630",
"comments": "Stage modern card send point future serve. Hot month might pass dinner chance.\nPerformance become own spring from analysis commercial. Draw effort structure many drug first.",
},
{
"id": "2",
"name": "Amanda",
"age": "42",
"net_worth": "31344480.2",
"email": "[email protected]",
"address": "6293 Bright Centers, Chenton, MO 36829",
"phone": "001-972-387-3913x966",
"comments": "None agent some if skill. Often onto dog wish. Listen live hair garden contain worry time. Economic or institution statement energy take sit.",
},
]
conn.execute(
"""
UNWIND $batch AS p
RETURN p.name,
p.age,
p.net_worth,
p.email,
p.address,
p.phone,
p.comments
""",
parameters={"batch": batch},
)
def test_array_binding(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
conn.execute(
"CREATE NODE TABLE node(id STRING, embedding DOUBLE[3], PRIMARY KEY(id))"
)
conn.execute("CREATE (d:node {id: 'test', embedding: $emb})", {"emb": [3, 5, 2]})
result = conn.execute(
"""
MATCH (d:node)
RETURN d.id, array_cosine_similarity(d.embedding, $emb)
""",
{"emb": [4.3, 5.2, 6.7]},
)
assert result.get_next() == ["test", pytest.approx(0.8922316795174099)]
# TODO(maxwell): fixme. The following case should be executed successfully.
# with pytest.raises(RuntimeError) as err:
# conn.execute(
# """
# MATCH (d:node)
# RETURN d.id, array_cosine_similarity($emb1, $emb)
# """, {"emb": [4.3, 5.2, 6.7], "emb1": [2.2, 3.3, 5.5]}
# )
# assert str(err.value) == "Binder exception: Left and right type are both ANY, which is not currently supported."
def test_bool_param(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
result = conn.execute(
"MATCH (a:person) WHERE a.isStudent = $1 AND a.isWorker = $k RETURN COUNT(*)",
{"1": False, "k": False},
)
assert result.has_next()
assert result.get_next() == [1]
assert not result.has_next()
result.close()
def test_int_param(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
result = conn.execute(
"MATCH (a:person) WHERE a.age < $AGE RETURN COUNT(*)", {"AGE": 1}
)
assert result.has_next()
assert result.get_next() == [0]
assert not result.has_next()
result.close()
def test_double_param(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
result = conn.execute(
"MATCH (a:person) WHERE a.eyeSight = $E RETURN COUNT(*)", {"E": 5.0}
)
assert result.has_next()
assert result.get_next() == [2]
assert not result.has_next()
result.close()
def test_str_param(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
result = conn.execute(
"MATCH (a:person) WHERE a.ID = 0 RETURN concat(a.fName, $S);", {"S": "HH"}
)
assert result.has_next()
assert result.get_next() == ["AliceHH"]
assert not result.has_next()
result.close()
def test_date_param(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
result = conn.execute(
"MATCH (a:person) WHERE a.birthdate = $1 RETURN COUNT(*);",
{"1": datetime.date(1900, 1, 1)},
)
assert result.has_next()
assert result.get_next() == [2]
assert not result.has_next()
result.close()
def test_timestamp_param(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
result = conn.execute(
"MATCH (a:person) WHERE a.registerTime = $1 RETURN COUNT(*);",
{"1": datetime.datetime(2011, 8, 20, 11, 25, 30)},
)
assert result.has_next()
assert result.get_next() == [1]
assert not result.has_next()
result.close()
def test_int64_list_param(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
result = conn.execute(
"MATCH (a:person {workedHours: $1}) RETURN COUNT(*);", {"1": [3, 4, 5, 6, 7]}
)
assert result.has_next()
assert result.get_next() == [1]
assert not result.has_next()
result.close()
def test_empty_list_param(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
result = conn.execute("RETURN list_contains($list, $item)", {"list": [], "item": 5})
assert result.has_next()
assert result.get_next() == [False]
assert not result.has_next()
result.close()
def test_int64_list_list_param(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
result = conn.execute(
"MATCH (a:person) WHERE a.courseScoresPerTerm = $1 OR a.courseScoresPerTerm = $2 RETURN COUNT(*);",
{"1": [[8, 10]], "2": [[7, 4], [8, 8], [9]]},
)
assert result.has_next()
assert result.get_next() == [2]
assert not result.has_next()
result.close()
def test_string_list_param(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
result = conn.execute(
"MATCH (a:person {usedNames: $1}) RETURN COUNT(*);", {"1": ["Carmen", "Fred"]}
)
assert result.has_next()
assert result.get_next() == [1]
assert not result.has_next()
result.close()
def test_map_param(conn_db_empty: ConnDB) -> None:
conn, _ = conn_db_empty
conn.execute(
"CREATE NODE TABLE tab(id int64, mp MAP(double, int64), mp2 MAP(int64, double), mp3 MAP(string, string), mp4 MAP(string, string)[], primary key(id))"
)
result = conn.execute(
"MERGE (t:tab {id: 0, mp: $1, mp2: $2, mp3: $3, mp4: $4}) RETURN t.*",
{
"1": {"key": [1.0, 2, 2.2], "value": [5, 3, -1]},
"2": {"key": [5, 4, 0], "value": [-0.5, 0, 2.2]},
"3": {"key": ["a", "b", "c"], "value": [1, "2", "3"]},
"4": [{"key": ["a"], "value": ["b"]}],
},
)
assert result.has_next()
assert result.get_next() == [
0,
{1.0: 5, 2.0: 3, 2.2: -1},
{5: -0.5, 4: -0.0, 0: 2.2},
{"a": "1", "b": "2", "c": "3"},
[{"a": "b"}],
]
assert not result.has_next()
result.close()
def test_general_list_param(conn_db_empty: ConnDB) -> None:
conn, _ = conn_db_empty
conn.execute(
"CREATE NODE TABLE tab(id int64, lst1 BOOL[], lst2 DOUBLE[], lst3 TIMESTAMP[], lst4 DATE[], lst5 INTERVAL[], lst6 STRING[], PRIMARY KEY(id))"
)
lst1 = [True, False]
lst2 = [1.0, 2.0]
lst3 = [
datetime.datetime(2019, 11, 12, 11, 25, 30),
datetime.datetime(1987, 2, 15, 3, 0, 2),
]
lst4 = [datetime.date(2019, 11, 12), datetime.date(1987, 2, 15)]
lst5 = [lst3[0] - lst3[1]]
lst6 = [1, "2", "3"]
lst6ToString = ["1", "2", "3"]
result = conn.execute(
"MERGE (t:tab {id: 0, lst1: $1, lst2: $2, lst3: $3, lst4: $4, lst5: $5, lst6: $6}) RETURN t.*",
{"1": lst1, "2": lst2, "3": lst3, "4": lst4, "5": lst5, "6": lst6},
)
assert result.has_next()
assert result.get_next() == [0, lst1, lst2, lst3, lst4, lst5, lst6ToString]
assert not result.has_next()
result.close()
def test_null_resolution(conn_db_empty: ConnDB) -> None:
conn, _ = conn_db_empty
conn.execute(
"CREATE NODE TABLE tab(id SERIAL, lst1 INT64[], mp1 MAP(STRING, STRING), "
"nest MAP(STRING, MAP(STRING, INT64))[], PRIMARY KEY(id))"
)
lst1 = [1, 2, 3, None]
mp1 = {"key": ["a", "b", "c", "o"], "value": ["x", "y", "z", None]}
nest = [
{"key": ["2"], "value": [{"key": ["foo", "bar"], "value": [1, 2]}]},
{"key": ["1"], "value": [{"key": [], "value": []}]},
]
result = conn.execute(
"MERGE (t:tab {lst1: $1, mp1: $2, nest: $3}) RETURN t.*",
{"1": lst1, "2": mp1, "3": nest},
)
assert result.has_next()
assert result.get_next() == [
0,
lst1,
{"a": "x", "b": "y", "c": "z", "o": None},
[{"2": {"foo": 1, "bar": 2}}, {"1": {}}],
]
assert not result.has_next()
result.close()
def test_param_error1(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
with pytest.raises(
RuntimeError,
match="Parameter name must be of type string but got <class 'int'>",
):
conn.execute(
"MATCH (a:person) WHERE a.registerTime = $1 RETURN COUNT(*);", {1: 1}
)
def test_param_error2(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
with pytest.raises(RuntimeError, match="Parameters must be a dict"):
conn.execute(
"MATCH (a:person) WHERE a.registerTime = $1 RETURN COUNT(*);", ["asd"]
)
def test_param_error3(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
with pytest.raises(RuntimeError, match="Parameters must be a dict"):
conn.execute(
"MATCH (a:person) WHERE a.registerTime = $1 RETURN COUNT(*);",
[("asd", 1, 1)],
)
def test_param(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
conn.execute("CREATE NODE TABLE NodeOne(id INT64, name STRING, PRIMARY KEY(id));")
conn.execute("CREATE NODE TABLE NodeTwo(id INT64, name STRING, PRIMARY KEY(id));")
conn.execute("CREATE Rel TABLE RelA(from NodeOne to NodeOne);")
conn.execute(
"CREATE Rel TABLE RelB(from NodeTwo to NodeOne, id int64, name String);"
)
conn.execute('CREATE (t: NodeOne {id:1, name: "Alice"});')
conn.execute('CREATE (t: NodeOne {id:2, name: "Jack"});')
conn.execute('CREATE (t: NodeTwo {id:3, name: "Bob"});')
result = conn.execute(
"MATCH (a:NodeOne { id: $a_id }),"
"(b:NodeTwo { id: $b_id }),"
"(c: NodeOne{ id: $c_id } )"
" MERGE"
" (a)-[:RelA]->(c),"
" (b)-[r:RelB { id: 2, name: $my_param }]->(c)"
" return r.*;",
{"a_id": 1, "b_id": 3, "c_id": 2, "my_param": None},
)
assert result.has_next()
assert result.get_next() == [2, None]
result.close()
def test_param_error4(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
with pytest.raises(
RuntimeError,
match="Runtime exception: Cannot convert Python object to Lbug value : INT8 is incompatible with TIMESTAMP",
):
conn.execute(
"MATCH (a:person {workedHours: $1}) RETURN COUNT(*);",
{"1": [1, 2, datetime.datetime(2023, 3, 25)]},
)
def test_dict_conversion(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
# Interpret as MAP.
result = conn.execute("RETURN $st", {"st": {"key": [1, 2, 3], "value": [3, 7, 98]}})
assert result.get_next() == [{1: 3, 2: 7, 3: 98}]
# Interpret as STRUCT since the first field name is not "key".
result = conn.execute(
"RETURN $st", {"st": {"key1": [1, 2, 3], "value": [3, 7, 98]}}
)
assert result.get_next() == [{"key1": [1, 2, 3], "value": [3, 7, 98]}]
# Interpret as STRUCT since the number of elements in key and value doesn't match.
result = conn.execute("RETURN $st", {"st": {"key": [1, 2], "value": [3, 7, 98, 4]}})
assert result.get_next() == [{"key": [1, 2], "value": [3, 7, 98, 4]}]
def test_null_handling(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
result = conn.execute(
"""
UNWIND $edges AS edge
WITH edge, size(edge.fact_embedding) AS score
ORDER BY score DESC
LIMIT $limit
RETURN edge.uuid;
""",
{
"edges": [
{
"uuid": "1c4e35a9-c6cb-4517-971f-37f209c71e64",
"fact_embedding": [
-0.010113425552845001,
-0.01922552101314068,
0.007402684073895216,
],
"expired_at": None,
}
],
"limit": 10,
},
)
assert result.has_next()
result.get_next()[0] = "1c4e35a9-c6cb-4517-971f-37f209c71e64"
assert not result.has_next()