Skip to content

Commit 9b42cc9

Browse files
committed
Added CCK for match acceptance. Refactured to fit new feature file design
1 parent 3077faf commit 9b42cc9

File tree

4 files changed

+514
-120
lines changed

4 files changed

+514
-120
lines changed

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ gulp.task('run-tck', ['download-tck', 'nodejs'], function() {
200200
return gulp.src(featureHome + "/*").pipe(cucumber({
201201
'steps': 'test/v1/tck/steps/*.js',
202202
'format': 'pretty',
203-
'tags' : "~@in_dev"
203+
'tags' : "~@in_dev,~@db"
204204
}));
205205
});
206206

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
var neo4j = require("../../../../lib/v1");
2+
var util = require("./util")
3+
4+
module.exports = function () {
5+
6+
this.Before(function( scenario, callback ) {
7+
this.driver = neo4j.driver("bolt://localhost");
8+
this.session = this.driver.session();
9+
this.session.run("MATCH (n) DETACH DELETE n").subscribe( {
10+
onCompleted : function( ) {
11+
callback();
12+
}
13+
});
14+
});
15+
16+
this.Given(/^init:(.*)$/, function (statement) {
17+
return this.session.run(statement);
18+
});
19+
20+
this.When(/^running:(.*)$/, function (statement) {
21+
this.rc = this.session.run(statement);
22+
});
23+
24+
this.When(/^running parametrized:(.*)$/, function (statement, table) {
25+
var param = util.literalTableToTestObject(table.hashes())[0];
26+
this.rc = this.session.run(statement, param);
27+
});
28+
29+
30+
this.Then(/^result:$/, function (table, callback) {
31+
this.expectedResults = util.literalTableToTestObject(table.hashes());
32+
var self = this;
33+
var errorCallback = function(err) {callback(new Error("Rejected Promise: " + err))}
34+
var successCallback = function(res) {
35+
var givenResults = [];
36+
var expectedPrint = printable(self.expectedResults);
37+
for (var i = 0 ; i < res.length; i++)
38+
{
39+
givenResults.push(getTestObject(res[i]));
40+
}
41+
if ( givenResults.length != self.expectedResults.length)
42+
{
43+
callback(new Error("Given and expected length of result array does not match. Give: " + givenResults.length + " Expected " + self.expectedResults.length));
44+
}
45+
if (!comapareResults(givenResults, self.expectedResults) ) {
46+
callback(new Error("Given and expected results does not match: " + printable(givenResults) + " Expected " + expectedPrint));
47+
}
48+
callback();
49+
}
50+
this.rc.then(successCallback).catch(errorCallback);
51+
});
52+
53+
54+
function comapareResults(given, expected) {
55+
if (! (typeof given === "object" && given instanceof Array) ) {
56+
throw new Error("Should be type Array")
57+
}
58+
if (! (typeof expected === "object" && expected instanceof Array) ) {
59+
throw new Error("Should be type Array")
60+
}
61+
for (var i = 0 ; i < given.length ; i++ ) {
62+
var sizeExpected = expected.length;
63+
for ( var j = 0 ; j < expected.length ; j++ ) {
64+
if ( compareResultObjects(given[i], expected[j]) ) {
65+
expected.splice(j,1);
66+
break;
67+
}
68+
}
69+
if (sizeExpected === expected.length) {
70+
return false;
71+
}
72+
}
73+
return true;
74+
}
75+
76+
function compareResultObjects(given, expected) {
77+
var keys = Object.keys(given);
78+
const union = new Set(keys.concat(Object.keys(expected)));
79+
if (union.size !== keys.length) {
80+
return false;
81+
}
82+
for ( var i = 0 ; i < keys.length ; i++ ) {
83+
if (!util.compareValues(given[keys[i]], expected[keys[i]]))
84+
{
85+
return false;
86+
}
87+
}
88+
return true;
89+
90+
}
91+
92+
function getTestObject(rels, func) {
93+
result = {}
94+
for (var key in rels)
95+
{
96+
var rel = rels[key];
97+
if (typeof rel === "object" && rel instanceof Array) {
98+
var relArray = [];
99+
for (var i in rel) {
100+
relArray.push(getTestValue(rel[i]));
101+
}
102+
result[key] = relArray;
103+
}
104+
else {
105+
result[key] = getTestValue(rel);
106+
}
107+
}
108+
return result;
109+
}
110+
111+
function getTestValue(val) {
112+
if ( val === null) {
113+
return val;
114+
}
115+
var con = val.constructor.name.toLowerCase()
116+
if (con === NODE) {
117+
return stripNode(val);
118+
}
119+
else if (con === RELATIONSHIP) {
120+
return stripRelationship(val);
121+
}
122+
else if (con === PATH) {
123+
return stripPath(val);
124+
}
125+
else {
126+
return val;
127+
}
128+
}
129+
130+
function stripRelationship(rel) {
131+
rel.start = neo4j.int(0);
132+
rel.end = neo4j.int(0);
133+
rel.identity = neo4j.int(0);
134+
return rel;
135+
}
136+
137+
function stripNode(node) {
138+
node.identity = neo4j.int(0);
139+
return node;
140+
}
141+
142+
function stripPath(path) {
143+
var id = 0;
144+
var startid = neo4j.int(path.start.identity.toString());
145+
var segments = path.segments;
146+
for (var i in segments) {
147+
var segment = segments[i];
148+
var relationship = segment.relationship;
149+
if (startid.notEquals(segment.start.identity)) {
150+
throw new Error("Path segment does not make sense")
151+
}
152+
var endId = neo4j.int(segment.end.identity.toString());
153+
relationship.identity = neo4j.int(0);
154+
segment.start.identity = neo4j.int(id++);
155+
segment.end.identity = neo4j.int(id);
156+
157+
if (relationship.start.equals(startid) && relationship.end.equals(endId)) {
158+
relationship.start = segment.start.identity;
159+
relationship.end = segment.end.identity;
160+
}
161+
else if (relationship.end.equals(startid) && relationship.start.equals(endId)) {
162+
relationship.end = segment.start.identity;
163+
relationship.start = segment.end.identity;
164+
}
165+
else {
166+
throw new Error("Path segment does not make sense")
167+
}
168+
startid = endId;
169+
}
170+
path.start.identity = neo4j.int(0);
171+
path.end.identity = neo4j.int(id);
172+
return path;
173+
}
174+
175+
function printable(array) {
176+
return JSON.stringify(array);
177+
}
178+
}

0 commit comments

Comments
 (0)