1
+ # frozen_string_literal: true
2
+
3
+ RSpec . describe Neo4j ::Driver do
4
+
5
+ describe '#execute_query' do
6
+ context 'when querying the database' do
7
+ it 'accepts query with default auth_token, config hash and parameters' do
8
+ expect {
9
+ driver . execute_query (
10
+ 'MATCH (p:Person {age: $age}) RETURN p.name AS name' ,
11
+ nil ,
12
+ { database : 'neo4j' } ,
13
+ age : 42
14
+ )
15
+ } . not_to raise_error
16
+ end
17
+ end
18
+
19
+ context 'when writing to the database' do
20
+ it 'accepts query with only keyword parameters' do
21
+ expect {
22
+ driver . execute_query (
23
+ 'CREATE (a:Person {name: $name}) CREATE (b:Person {name: $friend}) CREATE (a)-[:KNOWS]->(b)' ,
24
+ name : 'Alice' ,
25
+ friend : 'David'
26
+ )
27
+ } . not_to raise_error
28
+ end
29
+ end
30
+
31
+ context 'when reading from the database' do
32
+ it 'accepts query with no additional parameters' do
33
+ expect {
34
+ driver . execute_query ( 'MATCH (p:Person)-[:KNOWS]->(:Person) RETURN p.name AS name' )
35
+ } . not_to raise_error
36
+ end
37
+ end
38
+
39
+ context 'when updating the database' do
40
+ it 'accepts update query with keyword parameters' do
41
+ expect {
42
+ driver . execute_query (
43
+ 'MATCH (p:Person {name: $name}) SET p.age = $age' ,
44
+ name : 'Alice' ,
45
+ age : 42
46
+ )
47
+ } . not_to raise_error
48
+ end
49
+
50
+ it 'accepts relationship creation with keyword parameters' do
51
+ expect {
52
+ driver . execute_query (
53
+ 'MATCH (alice:Person {name: $name}) MATCH (bob:Person {name: $friend}) CREATE (alice)-[:KNOWS]->(bob)' ,
54
+ name : 'Alice' ,
55
+ friend : 'Bob'
56
+ )
57
+ } . not_to raise_error
58
+ end
59
+ end
60
+
61
+ context 'when deleting from the database' do
62
+ it 'accepts delete query with keyword parameters' do
63
+ expect {
64
+ driver . execute_query (
65
+ 'MATCH (p:Person {name: $name}) DETACH DELETE p' ,
66
+ name : 'Alice'
67
+ )
68
+ } . not_to raise_error
69
+ end
70
+ end
71
+
72
+ context 'when using query configuration' do
73
+ it 'accepts nil auth_token with config hash and parameters' do
74
+ expect {
75
+ driver . execute_query (
76
+ 'MATCH (p:Person) RETURN p.name' ,
77
+ nil ,
78
+ { database : 'neo4j' } ,
79
+ age : 42
80
+ )
81
+ } . not_to raise_error
82
+ end
83
+
84
+ it 'accepts auth_token with keyword parameters' do
85
+ auth_token = Neo4j ::Driver ::AuthTokens . basic ( neo4j_user , neo4j_password )
86
+
87
+ expect {
88
+ driver . execute_query (
89
+ 'MATCH (p:Person) RETURN p.name' ,
90
+ auth_token ,
91
+ age : 42
92
+ )
93
+ } . not_to raise_error
94
+ end
95
+ end
96
+
97
+ context 'when working with query summaries' do
98
+ it 'accepts UNWIND query without parameters' do
99
+ expect {
100
+ driver . execute_query ( "UNWIND ['Alice', 'Bob'] AS name MERGE (p:Person {name: name})" )
101
+ } . not_to raise_error
102
+ end
103
+
104
+ it 'accepts MERGE query with keyword parameters' do
105
+ expect {
106
+ driver . execute_query (
107
+ "MERGE (p:Person {name: $name}) MERGE (p)-[:KNOWS]->(:Person {name: $friend})" ,
108
+ name : 'Mark' ,
109
+ friend : 'Bob'
110
+ )
111
+ } . not_to raise_error
112
+ end
113
+
114
+ it 'accepts EXPLAIN query with keyword parameters' do
115
+ expect {
116
+ driver . execute_query (
117
+ 'EXPLAIN MATCH (p {name: $name}) RETURN p' ,
118
+ name : 'Alice'
119
+ )
120
+ } . not_to raise_error
121
+ end
122
+
123
+ it 'accepts shortestPath query with keyword parameters' do
124
+ expect {
125
+ driver . execute_query (
126
+ "MATCH p=shortestPath((:Person {name: $start})-[*]->(:Person {name: $end})) RETURN p" ,
127
+ start : 'Alice' ,
128
+ end : 'Bob'
129
+ )
130
+ } . not_to raise_error
131
+ end
132
+ end
133
+
134
+ context 'parameter handling' do
135
+ it 'handles mixed positional and keyword arguments correctly' do
136
+ expect {
137
+ driver . execute_query (
138
+ 'MATCH (p:Person {age: $age, name: $name}) RETURN p' ,
139
+ nil ,
140
+ { database : 'neo4j' } ,
141
+ age : 42 ,
142
+ name : 'Alice'
143
+ )
144
+ } . not_to raise_error
145
+ end
146
+
147
+ it 'handles only keyword arguments' do
148
+ expect {
149
+ driver . execute_query (
150
+ 'MATCH (p:Person {name: $name}) RETURN p' ,
151
+ name : 'Alice'
152
+ )
153
+ } . not_to raise_error
154
+ end
155
+ end
156
+ end
157
+ end
0 commit comments