-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPipelineTest.java
More file actions
309 lines (249 loc) · 13 KB
/
PipelineTest.java
File metadata and controls
309 lines (249 loc) · 13 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
package com.falkordb;
import java.util.*;
import com.falkordb.test.BaseTestContainerTestIT;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.falkordb.graph_entities.Node;
import com.falkordb.graph_entities.Property;
import com.falkordb.impl.resultset.ResultSetImpl;
public class PipelineTest extends BaseTestContainerTestIT {
private GraphContextGenerator api;
@BeforeEach
public void createApi() {
api = FalkorDB.driver(getFalkordbHost(),getFalkordbPort()).graph("social");
}
@AfterEach
public void deleteGraph() {
api.deleteGraph();
api.close();
}
@Test
public void testSync() {
try (GraphContext c = api.getContext()) {
GraphPipeline pipeline = c.pipelined();
pipeline.set("x", "1");
pipeline.query("CREATE (:Person {name:'a'})");
pipeline.query("CREATE (:Person {name:'b'})");
pipeline.incr("x");
pipeline.get("x");
pipeline.query("MATCH (n:Person{name:'a'}) RETURN n");
pipeline.callProcedure("db.labels");
List<Object> results = pipeline.syncAndReturnAll();
// Redis set command
Assertions.assertEquals(String.class, results.get(0).getClass());
Assertions.assertEquals("OK", results.get(0));
// Redis graph command
Assertions.assertEquals(ResultSetImpl.class, results.get(1).getClass());
ResultSet resultSet = (ResultSet) results.get(1);
Assertions.assertEquals(1, resultSet.getStatistics().nodesCreated());
Assertions.assertEquals(1, resultSet.getStatistics().propertiesSet());
Assertions.assertEquals(ResultSetImpl.class, results.get(2).getClass());
resultSet = (ResultSet) results.get(2);
Assertions.assertEquals(1, resultSet.getStatistics().nodesCreated());
Assertions.assertEquals(1, resultSet.getStatistics().propertiesSet());
// Redis incr command
Assertions.assertEquals(Long.class, results.get(3).getClass());
Assertions.assertEquals(2L, results.get(3));
// Redis get command
Assertions.assertEquals(String.class, results.get(4).getClass());
Assertions.assertEquals("2", results.get(4));
// Graph query result
Assertions.assertEquals(ResultSetImpl.class, results.get(5).getClass());
resultSet = (ResultSet) results.get(5);
Assertions.assertNotNull(resultSet.getHeader());
Header header = resultSet.getHeader();
List<String> schemaNames = header.getSchemaNames();
Assertions.assertNotNull(schemaNames);
Assertions.assertEquals(1, schemaNames.size());
Assertions.assertEquals("n", schemaNames.get(0));
Property<String> nameProperty = new Property<>("name", "a");
Node expectedNode = new Node();
expectedNode.setId(0);
expectedNode.addLabel("Person");
expectedNode.addProperty(nameProperty);
// see that the result were pulled from the right graph
Assertions.assertEquals(1, resultSet.size());
Iterator<Record> iterator = resultSet.iterator();
Assertions.assertTrue(iterator.hasNext());
Record record = iterator.next();
Assertions.assertFalse(iterator.hasNext());
Assertions.assertEquals(Arrays.asList("n"), record.keys());
Assertions.assertEquals(expectedNode, record.getValue("n"));
Assertions.assertEquals(ResultSetImpl.class, results.get(6).getClass());
resultSet = (ResultSet) results.get(6);
Assertions.assertNotNull(resultSet.getHeader());
header = resultSet.getHeader();
schemaNames = header.getSchemaNames();
Assertions.assertNotNull(schemaNames);
Assertions.assertEquals(1, schemaNames.size());
Assertions.assertEquals("label", schemaNames.get(0));
Assertions.assertEquals(1, resultSet.size());
iterator = resultSet.iterator();
Assertions.assertTrue(iterator.hasNext());
record = iterator.next();
Assertions.assertFalse(iterator.hasNext());
Assertions.assertEquals(Arrays.asList("label"), record.keys());
Assertions.assertEquals("Person", record.getValue("label"));
}
}
@Test
public void testReadOnlyQueries() {
try (GraphContext c = api.getContext()) {
GraphPipeline pipeline = c.pipelined();
pipeline.set("x", "1");
pipeline.query("CREATE (:Person {name:'a'})");
pipeline.query("CREATE (:Person {name:'b'})");
pipeline.readOnlyQuery("MATCH (n:Person{name:'a'}) RETURN n");
pipeline.callProcedure("db.labels");
List<Object> results = pipeline.syncAndReturnAll();
verifyReadOnlyQueryResults(results);
}
}
@Test
public void testReadOnlyQueriesWithParameters() {
try (GraphContext c = api.getContext()) {
GraphPipeline pipeline = c.pipelined();
pipeline.set("x", "1");
pipeline.query("CREATE (:Person {name:'a'})");
pipeline.query("CREATE (:Person {name:'b'})");
Map<String, Object> params = new HashMap<>();
params.put("name", "a");
pipeline.readOnlyQuery("MATCH (n:Person{name:$name}) RETURN n", params);
pipeline.callProcedure("db.labels");
List<Object> results = pipeline.syncAndReturnAll();
verifyReadOnlyQueryResults(results);
}
}
protected void verifyReadOnlyQueryResults(List<Object> results) {
// Redis set command
Assertions.assertEquals(String.class, results.get(0).getClass());
Assertions.assertEquals("OK", results.get(0));
// Redis graph command
Assertions.assertEquals(ResultSetImpl.class, results.get(1).getClass());
ResultSet resultSet = (ResultSet) results.get(1);
Assertions.assertEquals(1, resultSet.getStatistics().nodesCreated());
Assertions.assertEquals(1, resultSet.getStatistics().propertiesSet());
Assertions.assertEquals(ResultSetImpl.class, results.get(2).getClass());
resultSet = (ResultSet) results.get(2);
Assertions.assertEquals(1, resultSet.getStatistics().nodesCreated());
Assertions.assertEquals(1, resultSet.getStatistics().propertiesSet());
// Graph read-only query result
Assertions.assertEquals(ResultSetImpl.class, results.get(3).getClass());
resultSet = (ResultSet) results.get(3);
Assertions.assertNotNull(resultSet.getHeader());
Header header = resultSet.getHeader();
List<String> schemaNames = header.getSchemaNames();
Assertions.assertNotNull(schemaNames);
Assertions.assertEquals(1, schemaNames.size());
Assertions.assertEquals("n", schemaNames.get(0));
Property<String> nameProperty = new Property<>("name", "a");
Node expectedNode = new Node();
expectedNode.setId(0);
expectedNode.addLabel("Person");
expectedNode.addProperty(nameProperty);
// see that the result were pulled from the right graph
Assertions.assertEquals(1, resultSet.size());
Iterator<Record> iterator = resultSet.iterator();
Assertions.assertTrue(iterator.hasNext());
Record record = iterator.next();
Assertions.assertFalse(iterator.hasNext());
Assertions.assertEquals(Arrays.asList("n"), record.keys());
Assertions.assertEquals(expectedNode, record.getValue("n"));
Assertions.assertEquals(ResultSetImpl.class, results.get(4).getClass());
resultSet = (ResultSet) results.get(4);
Assertions.assertNotNull(resultSet.getHeader());
header = resultSet.getHeader();
schemaNames = header.getSchemaNames();
Assertions.assertNotNull(schemaNames);
Assertions.assertEquals(1, schemaNames.size());
Assertions.assertEquals("label", schemaNames.get(0));
Assertions.assertEquals(1, resultSet.size());
iterator = resultSet.iterator();
Assertions.assertTrue(iterator.hasNext());
record = iterator.next();
Assertions.assertFalse(iterator.hasNext());
Assertions.assertEquals(Arrays.asList("label"), record.keys());
Assertions.assertEquals("Person", record.getValue("label"));
}
@Test
public void testProfile() {
try (GraphContext c = api.getContext()) {
GraphPipeline pipeline = c.pipelined();
pipeline.query("CREATE (:person{name:'alice',age:30})");
pipeline.query("CREATE (:person{name:'bob',age:25})");
pipeline.profile("MATCH (a:person) WHERE (a.name = 'alice') RETURN a.age");
List<Object> results = pipeline.syncAndReturnAll();
// Check that profile result is not null and has expected structure
ResultSet profileResult = (ResultSet) results.get(2);
Assertions.assertNotNull(profileResult);
// Verify profile result contains execution plan operations
Assertions.assertTrue(profileResult.size() > 0, "Profile result should contain execution plan operations");
// Verify profile result has a header with columns
Header header = profileResult.getHeader();
Assertions.assertNotNull(header, "Profile result should have a header");
Assertions.assertTrue(header.getSchemaNames().size() > 0, "Profile result header should have columns");
// Verify profile result contains execution plan data
Iterator<Record> iterator = profileResult.iterator();
Assertions.assertTrue(iterator.hasNext(), "Profile result should have execution plan operations");
Record record = iterator.next();
Assertions.assertNotNull(record, "Profile result record should not be null");
Assertions.assertTrue(record.size() > 0, "Profile result record should have values");
// Verify profile result has statistics (execution metrics)
Assertions.assertNotNull(profileResult.getStatistics(), "Profile result should have statistics");
}
}
@Test
public void testGraphCopy() {
Iterator<Record> originalResultSetIterator;
try (GraphContext c = api.getContext()) {
// Create sample data and copy the graph
GraphPipeline pipeline = c.pipelined();
pipeline.query("CREATE (:person{name:'roi',age:32})-[:knows]->(:person{name:'amit',age:30})");
pipeline.query("MATCH (p:person)-[rel:knows]->(p2:person) RETURN p,rel,p2");
pipeline.copyGraph("social-copied");
List<Object> results = pipeline.syncAndReturnAll();
ResultSet originalResultSet = (ResultSet) results.get(1);
originalResultSetIterator = originalResultSet.iterator();
}
GraphContextGenerator api2 = FalkorDB.driver(getFalkordbHost(),getFalkordbPort()).graph("social-copied");
try {
// Compare graph contents
ResultSet copiedResultSet = api2.query("MATCH (p:person)-[rel:knows]->(p2:person) RETURN p,rel,p2");
Iterator<Record> copiedResultSetIterator = copiedResultSet.iterator();
while (originalResultSetIterator.hasNext()) {
Assertions.assertTrue(copiedResultSetIterator.hasNext());
Assertions.assertEquals(originalResultSetIterator.next(), copiedResultSetIterator.next());
}
} finally {
// Cleanup
api2.deleteGraph();
api2.close();
}
}
@Test
public void testExplainInPipeline(){
try (GraphContext c = api.getContext()) {
// Create some test data first
c.query("CREATE (:Person {name:'Bob'})");
GraphPipeline pipeline = c.pipelined();
pipeline.explain("MATCH (p:Person) RETURN p");
Map<String, Object> params = new HashMap<>();
params.put("name", "Bob");
pipeline.explain("MATCH (p:Person) WHERE p.name = $name RETURN p", params);
List<Object> results = pipeline.syncAndReturnAll();
// Check explain results
Assertions.assertTrue(results.get(0) instanceof List);
@SuppressWarnings("unchecked")
List<String> explainResult1 = (List<String>) results.get(0);
Assertions.assertNotNull(explainResult1);
Assertions.assertFalse(explainResult1.isEmpty());
Assertions.assertTrue(results.get(1) instanceof List);
@SuppressWarnings("unchecked")
List<String> explainResult2 = (List<String>) results.get(1);
Assertions.assertNotNull(explainResult2);
Assertions.assertFalse(explainResult2.isEmpty());
}
}
}