Skip to content

Commit 79ed1c9

Browse files
authored
added close and result set size (#47)
* added close and result set size
1 parent a9b480f commit 79ed1c9

File tree

5 files changed

+56
-26
lines changed

5 files changed

+56
-26
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ let graph = new RedisGraph("social");
6565
let record = res.next();
6666
// See path.js for more path API.
6767
console.log(record.get("p").nodeCount);
68-
graph.deleteGraph();
69-
process.exit();
7068
}
69+
graph.deleteGraph();
70+
graph.close();
7171

7272
})();
7373

Diff for: examples/redisGraphExample.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ try {
3636
let record = res.next();
3737
// See path.js for more path API.
3838
console.log(record.get("p").nodeCount);
39-
graph.deleteGraph();
40-
process.exit();
41-
}
39+
}
40+
graph.deleteGraph();
41+
graph.close();
4242
})();
4343
} catch (err) {
4444
console.log(err);

Diff for: src/graph.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ class Graph {
2626
this._propertyPromise = undefined; // used as a synchronization mechanizom for property names retrival
2727
this._relationshipPromise = undefined; // used as a synchronization mechanizom for relationship types retrival
2828

29-
let client =
29+
this._client =
3030
host instanceof redis.RedisClient
3131
? host
3232
: redis.createClient(port, host, options);
33-
this._sendCommand = util.promisify(client.send_command).bind(client);
34-
}
33+
this._sendCommand = util.promisify(this._client.send_command).bind(this._client);
34+
}
35+
/**
36+
* Closes the client.
37+
*/
38+
close() {
39+
this._client.quit();
40+
}
3541

3642
/**
3743
* Auxiliary function to extract string(s) data from procedures such as:

Diff for: src/resultSet.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,14 @@ class ResultSet {
324324
*/
325325
getStatistics() {
326326
return this._statistics;
327-
}
327+
}
328+
329+
/**
330+
* @returns {int} Result set size.
331+
*/
332+
size() {
333+
return this._resultsCount;
334+
}
328335
}
329336

330337
module.exports = ResultSet;

Diff for: test/redisGraphAPITest.js

+34-17
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,41 @@ const assert = require("assert"),
1010
deepEqual = require("deep-equal");
1111

1212
describe("RedisGraphAPI Test", () => {
13-
// Assuming this test is running against redis server at: localhost:6379 with no password.
13+
// Assuming this test is running against redis server at: localhost:6379 with no password.
1414
const api = new RedisGraph("social");
1515

1616
beforeEach(() => {
1717
return api.deleteGraph().catch(() => {});
1818
});
1919

20-
it("test connection from port and host", async () => {
21-
// Assuming this test is running against redis server at: localhost:6379 with no password.
22-
let graph = new RedisGraph("social", "127.0.0.1", 6379, {password:undefined});
23-
let result = await graph.query("CREATE ({name:'roi', age:34})");
20+
it("test connection from port and host", async () => {
21+
// Assuming this test is running against redis server at: localhost:6379 with no password.
22+
let graph = new RedisGraph("social", "127.0.0.1", 6379, {
23+
password: undefined,
24+
});
25+
let result = await graph.query("CREATE ({name:'roi', age:34})");
26+
assert.equal(result.size(), 0);
2427
assert.ok(!result.hasNext());
25-
assert.equal(1, result.getStatistics().nodesCreated());
26-
graph.deleteGraph();
27-
})
28+
assert.equal(1, result.getStatistics().nodesCreated());
29+
graph.deleteGraph();
30+
graph.close();
31+
});
2832

2933
it("test connection from client", async () => {
30-
// Assuming this test is running against redis server at: localhost:6379 with no password.
31-
let graph = new RedisGraph("social", redis.createClient());
32-
let result = await graph.query("CREATE ({name:'roi', age:34})");
34+
// Assuming this test is running against redis server at: localhost:6379 with no password.
35+
let graph = new RedisGraph("social", redis.createClient());
36+
let result = await graph.query("CREATE ({name:'roi', age:34})");
37+
assert.equal(result.size(), 0);
3338
assert.ok(!result.hasNext());
34-
assert.equal(1, result.getStatistics().nodesCreated());
35-
graph.deleteGraph();
39+
assert.equal(1, result.getStatistics().nodesCreated());
40+
graph.deleteGraph();
41+
graph.close();
3642
});
3743

3844
it("test Create Node", async () => {
3945
// Create a node
4046
let result = await api.query("CREATE ({name:'roi', age:34})");
47+
assert.equal(result.size(), 0);
4148
assert.ok(!result.hasNext());
4249
assert.equal(1, result.getStatistics().nodesCreated());
4350
assert.ifError(
@@ -71,6 +78,7 @@ describe("RedisGraphAPI Test", () => {
7178
it("test Create Labeled Node", async () => {
7279
// Create a node with a label
7380
let result = await api.query("CREATE (:human {name:'danny', age:12})");
81+
assert.equal(result.size(), 0);
7482
assert.ok(!result.hasNext());
7583
assert.equal(
7684
"1",
@@ -97,6 +105,7 @@ describe("RedisGraphAPI Test", () => {
97105
"MATCH (a:person {name:'roi'}), \
98106
(b:person {name:'amit'}) CREATE (a)-[:knows]->(b)"
99107
);
108+
assert.equal(matchResult.size(), 0);
100109
assert.ok(!matchResult.hasNext());
101110
assert.ifError(
102111
matchResult.getStatistics().getStringValue(Label.NODES_CREATED)
@@ -122,6 +131,7 @@ describe("RedisGraphAPI Test", () => {
122131
let resultSet = await api.query(
123132
"MATCH (r:human)-[:knows]->(a:human) RETURN r.age, r.name"
124133
);
134+
assert.equal(resultSet.size(), 1);
125135
assert.ok(resultSet.hasNext());
126136
assert.equal(0, resultSet.getStatistics().nodesCreated());
127137
assert.equal(0, resultSet.getStatistics().nodesDeleted());
@@ -162,7 +172,7 @@ describe("RedisGraphAPI Test", () => {
162172
let resultSet = await api.query(
163173
"MATCH (a:person)-[r:knows]->(b:person) RETURN a,r"
164174
);
165-
175+
assert.equal(resultSet.size(), 1);
166176
assert.ok(resultSet.hasNext());
167177
assert.equal(0, resultSet.getStatistics().nodesCreated());
168178
assert.equal(0, resultSet.getStatistics().nodesDeleted());
@@ -199,6 +209,7 @@ describe("RedisGraphAPI Test", () => {
199209
it("test null value to string", async () => {
200210
await api.query("CREATE ( {nullValue:null} )");
201211
let resultSet = await api.query("MATCH (n) RETURN n.nullValue");
212+
assert.equal(resultSet.size(), 1);
202213
assert.ok(resultSet.hasNext());
203214
let record = resultSet.next();
204215
assert.equal(undefined, record.get(0));
@@ -217,6 +228,7 @@ describe("RedisGraphAPI Test", () => {
217228
"MATCH (a:person)-[:knows]->(:person) RETURN a"
218229
);
219230

231+
assert.equal(resultSet.size(), 0);
220232
assert.ok(!resultSet.hasNext());
221233
assert.equal(resultSet.getHeader()[0], "a");
222234
assert.equal(0, resultSet.getStatistics().nodesCreated());
@@ -236,24 +248,27 @@ describe("RedisGraphAPI Test", () => {
236248
await api.query("CREATE (:person{name:'a',age:32,array:[0,1,2]})");
237249
await api.query("CREATE (:person{name:'b',age:30,array:[3,4,5]})");
238250
let resultSet = await api.query("WITH [0,1,2] as x return x");
251+
252+
assert.equal(resultSet.size(), 1);
239253
assert.ok(resultSet.hasNext());
240254
assert.deepStrictEqual(["x"], resultSet.getHeader());
241255
let record = resultSet.next();
242256
assert.deepStrictEqual([0, 1, 2], record.get(0));
243257
assert.ok(!resultSet.hasNext());
244258

245259
let newResultSet = await api.query("MATCH(n) return collect(n) as x");
260+
assert.equal(newResultSet.size(), 1);
246261
assert.ok(newResultSet.hasNext());
247262
var nodeA = new Node("person", {
248263
name: "a",
249264
age: 32,
250-
array: [0, 1, 2]
265+
array: [0, 1, 2],
251266
});
252267
nodeA.setId(0);
253268
var nodeB = new Node("person", {
254269
name: "b",
255270
age: 30,
256-
array: [3, 4, 5]
271+
array: [3, 4, 5],
257272
});
258273
nodeB.setId(1);
259274
assert.deepStrictEqual(["x"], newResultSet.getHeader());
@@ -269,6 +284,7 @@ describe("RedisGraphAPI Test", () => {
269284
}
270285
let values = await Promise.all(promises);
271286
for (var resultSet of values) {
287+
assert.equal(resultSet.size(), 1);
272288
let record = resultSet.next();
273289
let n = record.get(0);
274290
assert.equal(34, n.properties["age"]);
@@ -367,6 +383,7 @@ describe("RedisGraphAPI Test", () => {
367383
.build();
368384

369385
let paths = new Set([path01, path12, path02]);
386+
assert.equal(response.size(), 3);
370387
while (response.hasNext()) {
371388
let p = response.next().get("p");
372389
let pInPaths = false;
@@ -394,7 +411,7 @@ describe("RedisGraphAPI Test", () => {
394411
"str",
395412
[1, 2, 3],
396413
["1", "2", "3"],
397-
null
414+
null,
398415
];
399416
let promises = [];
400417
for (var i = 0; i < params.length; i++) {

0 commit comments

Comments
 (0)