Skip to content

added close and result set size #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ let graph = new RedisGraph("social");
let record = res.next();
// See path.js for more path API.
console.log(record.get("p").nodeCount);
graph.deleteGraph();
process.exit();
}
graph.deleteGraph();
graph.close();

})();

Expand Down
6 changes: 3 additions & 3 deletions examples/redisGraphExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ try {
let record = res.next();
// See path.js for more path API.
console.log(record.get("p").nodeCount);
graph.deleteGraph();
process.exit();
}
}
graph.deleteGraph();
graph.close();
})();
} catch (err) {
console.log(err);
Expand Down
12 changes: 9 additions & 3 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ class Graph {
this._propertyPromise = undefined; // used as a synchronization mechanizom for property names retrival
this._relationshipPromise = undefined; // used as a synchronization mechanizom for relationship types retrival

let client =
this._client =
host instanceof redis.RedisClient
? host
: redis.createClient(port, host, options);
this._sendCommand = util.promisify(client.send_command).bind(client);
}
this._sendCommand = util.promisify(this._client.send_command).bind(this._client);
}
/**
* Closes the client.
*/
close() {
this._client.quit();
}

/**
* Auxiliary function to extract string(s) data from procedures such as:
Expand Down
9 changes: 8 additions & 1 deletion src/resultSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,14 @@ class ResultSet {
*/
getStatistics() {
return this._statistics;
}
}

/**
* @returns {int} Result set size.
*/
size() {
return this._resultsCount;
}
}

module.exports = ResultSet;
51 changes: 34 additions & 17 deletions test/redisGraphAPITest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,41 @@ const assert = require("assert"),
deepEqual = require("deep-equal");

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

beforeEach(() => {
return api.deleteGraph().catch(() => {});
});

it("test connection from port and host", async () => {
// Assuming this test is running against redis server at: localhost:6379 with no password.
let graph = new RedisGraph("social", "127.0.0.1", 6379, {password:undefined});
let result = await graph.query("CREATE ({name:'roi', age:34})");
it("test connection from port and host", async () => {
// Assuming this test is running against redis server at: localhost:6379 with no password.
let graph = new RedisGraph("social", "127.0.0.1", 6379, {
password: undefined,
});
let result = await graph.query("CREATE ({name:'roi', age:34})");
assert.equal(result.size(), 0);
assert.ok(!result.hasNext());
assert.equal(1, result.getStatistics().nodesCreated());
graph.deleteGraph();
})
assert.equal(1, result.getStatistics().nodesCreated());
graph.deleteGraph();
graph.close();
});

it("test connection from client", async () => {
// Assuming this test is running against redis server at: localhost:6379 with no password.
let graph = new RedisGraph("social", redis.createClient());
let result = await graph.query("CREATE ({name:'roi', age:34})");
// Assuming this test is running against redis server at: localhost:6379 with no password.
let graph = new RedisGraph("social", redis.createClient());
let result = await graph.query("CREATE ({name:'roi', age:34})");
assert.equal(result.size(), 0);
assert.ok(!result.hasNext());
assert.equal(1, result.getStatistics().nodesCreated());
graph.deleteGraph();
assert.equal(1, result.getStatistics().nodesCreated());
graph.deleteGraph();
graph.close();
});

it("test Create Node", async () => {
// Create a node
let result = await api.query("CREATE ({name:'roi', age:34})");
assert.equal(result.size(), 0);
assert.ok(!result.hasNext());
assert.equal(1, result.getStatistics().nodesCreated());
assert.ifError(
Expand Down Expand Up @@ -71,6 +78,7 @@ describe("RedisGraphAPI Test", () => {
it("test Create Labeled Node", async () => {
// Create a node with a label
let result = await api.query("CREATE (:human {name:'danny', age:12})");
assert.equal(result.size(), 0);
assert.ok(!result.hasNext());
assert.equal(
"1",
Expand All @@ -97,6 +105,7 @@ describe("RedisGraphAPI Test", () => {
"MATCH (a:person {name:'roi'}), \
(b:person {name:'amit'}) CREATE (a)-[:knows]->(b)"
);
assert.equal(matchResult.size(), 0);
assert.ok(!matchResult.hasNext());
assert.ifError(
matchResult.getStatistics().getStringValue(Label.NODES_CREATED)
Expand All @@ -122,6 +131,7 @@ describe("RedisGraphAPI Test", () => {
let resultSet = await api.query(
"MATCH (r:human)-[:knows]->(a:human) RETURN r.age, r.name"
);
assert.equal(resultSet.size(), 1);
assert.ok(resultSet.hasNext());
assert.equal(0, resultSet.getStatistics().nodesCreated());
assert.equal(0, resultSet.getStatistics().nodesDeleted());
Expand Down Expand Up @@ -162,7 +172,7 @@ describe("RedisGraphAPI Test", () => {
let resultSet = await api.query(
"MATCH (a:person)-[r:knows]->(b:person) RETURN a,r"
);

assert.equal(resultSet.size(), 1);
assert.ok(resultSet.hasNext());
assert.equal(0, resultSet.getStatistics().nodesCreated());
assert.equal(0, resultSet.getStatistics().nodesDeleted());
Expand Down Expand Up @@ -199,6 +209,7 @@ describe("RedisGraphAPI Test", () => {
it("test null value to string", async () => {
await api.query("CREATE ( {nullValue:null} )");
let resultSet = await api.query("MATCH (n) RETURN n.nullValue");
assert.equal(resultSet.size(), 1);
assert.ok(resultSet.hasNext());
let record = resultSet.next();
assert.equal(undefined, record.get(0));
Expand All @@ -217,6 +228,7 @@ describe("RedisGraphAPI Test", () => {
"MATCH (a:person)-[:knows]->(:person) RETURN a"
);

assert.equal(resultSet.size(), 0);
assert.ok(!resultSet.hasNext());
assert.equal(resultSet.getHeader()[0], "a");
assert.equal(0, resultSet.getStatistics().nodesCreated());
Expand All @@ -236,24 +248,27 @@ describe("RedisGraphAPI Test", () => {
await api.query("CREATE (:person{name:'a',age:32,array:[0,1,2]})");
await api.query("CREATE (:person{name:'b',age:30,array:[3,4,5]})");
let resultSet = await api.query("WITH [0,1,2] as x return x");

assert.equal(resultSet.size(), 1);
assert.ok(resultSet.hasNext());
assert.deepStrictEqual(["x"], resultSet.getHeader());
let record = resultSet.next();
assert.deepStrictEqual([0, 1, 2], record.get(0));
assert.ok(!resultSet.hasNext());

let newResultSet = await api.query("MATCH(n) return collect(n) as x");
assert.equal(newResultSet.size(), 1);
assert.ok(newResultSet.hasNext());
var nodeA = new Node("person", {
name: "a",
age: 32,
array: [0, 1, 2]
array: [0, 1, 2],
});
nodeA.setId(0);
var nodeB = new Node("person", {
name: "b",
age: 30,
array: [3, 4, 5]
array: [3, 4, 5],
});
nodeB.setId(1);
assert.deepStrictEqual(["x"], newResultSet.getHeader());
Expand All @@ -269,6 +284,7 @@ describe("RedisGraphAPI Test", () => {
}
let values = await Promise.all(promises);
for (var resultSet of values) {
assert.equal(resultSet.size(), 1);
let record = resultSet.next();
let n = record.get(0);
assert.equal(34, n.properties["age"]);
Expand Down Expand Up @@ -367,6 +383,7 @@ describe("RedisGraphAPI Test", () => {
.build();

let paths = new Set([path01, path12, path02]);
assert.equal(response.size(), 3);
while (response.hasNext()) {
let p = response.next().get("p");
let pInPaths = false;
Expand Down Expand Up @@ -394,7 +411,7 @@ describe("RedisGraphAPI Test", () => {
"str",
[1, 2, 3],
["1", "2", "3"],
null
null,
];
let promises = [];
for (var i = 0; i < params.length; i++) {
Expand Down