Skip to content

Commit 5a1013b

Browse files
committed
Add Record and cleanup tests
1 parent 6a8cff6 commit 5a1013b

File tree

4 files changed

+67
-33
lines changed

4 files changed

+67
-33
lines changed

src/record.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Hold a query record
3+
*/
4+
module.exports = class Record {
5+
6+
constructor(header, values){
7+
this._header = header;
8+
this._values = values;
9+
}
10+
11+
getString(key) {
12+
let index = key;
13+
if(typeof key === "string"){
14+
index = this._header.indexOf(key);
15+
}
16+
return this._values[index];
17+
}
18+
19+
keys() {
20+
return this._header;
21+
}
22+
23+
values() {
24+
return this._values;
25+
}
26+
27+
containsKey(key) {
28+
return this._header.includes(key);
29+
}
30+
31+
size() {
32+
return this._header.length;
33+
}
34+
35+
toString() {
36+
return this._values.toString();
37+
}
38+
}

src/redisGraph.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = class RedisGraph {
3939
* @return delete running time statistics
4040
*/
4141
deleteGraph() {
42-
return this._sendCommand('graph.DELETE',[this._graphId, query])
42+
return this._sendCommand('graph.DELETE',[this._graphId])
4343
.then((res) => {
4444
return new ResultSet(res);
4545
});

src/resultSet.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
const Statistics = require('./statistics').Statistics;
1+
const Statistics = require('./statistics').Statistics,
2+
Record = require('./record');
23

34
/**
45
* Hold a query result
56
*/
67
module.exports = class ResultSet {
78

89
constructor(resp) {
9-
10-
this._position = 1;
10+
this._position = 0;
1111
this._statistics = new Statistics(resp[1]);
12-
this._results = resp[0];
12+
13+
let result = resp[0];
1314

1415
// Empty result set
15-
if(this._results === null || this._results.length === 0) {
16+
if(result === null || result.length === 0) {
1617
this._header = [];
1718
this._totalResults = 0;
19+
this._results = [];
1820
} else {
19-
this._header = this._results[0];
20-
21-
// First row is a header row
22-
this._totalResults = this._results.length - 1;
21+
this._header = result[0];
22+
this._totalResults = result.length - 1;
23+
this._results = new Array(this._totalResults);
24+
for(let i = 0 ; i < this._totalResults; ++i){
25+
this._results[i] = new Record(this._header, result[i+1]);
26+
}
2327
}
2428
}
2529

@@ -28,7 +32,7 @@ module.exports = class ResultSet {
2832
}
2933

3034
hasNext() {
31-
return this._position <= this._totalResults;
35+
return this._position < this._totalResults;
3236
}
3337

3438
next() {

test/redisGraphAPITest.js

+14-22
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@ Label = require('../src/statistics').Label,
33
RedisGraphAPI = require('../src/redisGraph');
44

55
describe('RedisGraphAPI Test', () =>{
6-
const api = new RedisGraphAPI("social");
7-
6+
const api = new RedisGraphAPI("social");
7+
88
beforeEach( () => {
9-
//api.deleteGraph(); - TODO add this back once we implement this API
10-
11-
// Method method = RedisGraphAPI.class.getDeclaredMethod("_conn");
12-
// method.setAccessible(true);
13-
// ((Jedis)method.invoke(api)).flushDB();
9+
return api.deleteGraph();
1410
});
1511

16-
it('test Create Node', (done) => {
12+
it('test Create Node', () => {
1713
// Create a node
18-
api.query("CREATE ({name:'roi',age:32})")
14+
return api.query("CREATE ({name:'roi',age:32})")
1915
.then( (result) => {
2016
assert.ok(!result.hasNext());
2117
assert.equal(1, result.getStatistics().nodesCreated());
@@ -24,56 +20,53 @@ describe('RedisGraphAPI Test', () =>{
2420
assert.ifError(result.getStatistics().getStringValue(Label.RELATIONSHIPS_DELETED));
2521
assert.equal(2, result.getStatistics().propertiesSet());
2622
assert.ok(result.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME));
27-
done();
2823
});
2924
});
3025

31-
it('test Create Labeled Node', (done) => {
26+
it('test Create Labeled Node', () => {
3227
// Create a node with a label
33-
api.query("CREATE (:human{name:'danny',age:12})")
28+
return api.query("CREATE (:human{name:'danny',age:12})")
3429
.then( (result) => {
3530
assert.ok(!result.hasNext());
3631
assert.equal("1", result.getStatistics().getStringValue(Label.NODES_CREATED));
3732
assert.equal("2", result.getStatistics().getStringValue(Label.PROPERTIES_SET));
3833
assert.ok(result.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME));
39-
done();
4034
});
4135
});
4236

43-
it('test Connect Nodes', (done) => {
37+
it('test Connect Nodes', () => {
4438
// Create both source and destination nodes
4539
let createResult1 = api.query("CREATE (:person{name:'roi',age:32})");
4640
let createResult2 = api.query("CREATE (:person{name:'amit',age:30})");
4741

4842
// Connect source and destination nodes.
49-
api.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[knows]->(a)")
43+
return api.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[knows]->(a)")
5044
.then( (matchResult) => {
5145
assert.ok(!matchResult.hasNext());
5246
assert.ifError(matchResult.getStatistics().getStringValue(Label.NODES_CREATED));
5347
assert.ifError(matchResult.getStatistics().getStringValue(Label.PROPERTIES_SET));
5448
assert.equal(1, matchResult.getStatistics().relationshipsCreated());
5549
assert.equal(0, matchResult.getStatistics().relationshipsDeleted());
5650
assert.ok(matchResult.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME));
57-
done();
5851
});
5952
});
6053

61-
it('test Query', (done) => {
54+
it('test Query', () => {
6255

6356
// Create both source and destination nodes
64-
api.query("CREATE (:qhuman{name:'roi',age:32})")
65-
.then( (create1Result) => {
57+
return api.query("CREATE (:qhuman{name:'roi',age:32})")
58+
.then( (create1Result) => {
6659
return api.query("CREATE (:qhuman{name:'amit',age:30})");
6760
})
6861
.then( (create2Result) => {
6962
// Connect source and destination nodes.
70-
return api.query("MATCH (a:qhuman), (b:qhuman) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[knows]->(b)");
63+
return api.query("MATCH (a:qhuman), (b:qhuman) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[knows]->(b)");
7164
})
7265
.then( (connectResult) => {
7366
// Query
7467
return api.query("MATCH (a:qhuman)-[knows]->(:qhuman) RETURN a");
7568
})
76-
.then( (resultSet) => {
69+
.then( (resultSet) => {
7770
assert.ok(resultSet.hasNext());
7871
assert.equal(0, resultSet.getStatistics().nodesCreated());
7972
assert.equal(0, resultSet.getStatistics().propertiesSet());
@@ -84,7 +77,6 @@ describe('RedisGraphAPI Test', () =>{
8477
let record = resultSet.next();
8578
assert.equal( "roi", record.getString(1));
8679
assert.equal( "32.000000", record.getString(0));
87-
done();
8880
});
8981
});
9082
})

0 commit comments

Comments
 (0)