Permalink
Browse files

done implementing basic node INSERT and DELETE calls; basic test cove…

…rage given
  • Loading branch information...
1 parent 7534cd6 commit 0ab219468a7f6fd38e67312e8c4b446fbc3d1d13 @philippkueng committed Apr 4, 2012
Showing with 45 additions and 30 deletions.
  1. +20 −11 main.js
  2. +25 −19 test/test.main.js
View
31 main.js
@@ -13,15 +13,14 @@ function Neo4j(url){
/* Insert a Node --------- */
Neo4j.prototype.InsertNode = function(node, callback){
+ var that = this;
request
.post(this.url + '/db/data/node')
.send(node)
.set('Accept', 'application/json')
.end(function(result){
- // console.log(result.body.data);
if(typeof result.body !== 'undefined'){
- // callback(null, JSON.parse(result.body));
- callback(null, result.body);
+ that.AddNodeId(result.body, callback);
} else {
callback(new Error('Response is empty'), null);
}
@@ -32,7 +31,6 @@ Neo4j.prototype.InsertNode = function(node, callback){
/* Delete a Node --------- */
// Nodes with Relationships cannot be deleted -> deliver proper error message
-// superagent has no method delete as of yet!!
Neo4j.prototype.DeleteNode = function(node_id, callback){
request
@@ -45,17 +43,28 @@ Neo4j.prototype.DeleteNode = function(node_id, callback){
console.log(result);
callback(new Error('Error when deleting Node'), null);
}
-
- // console.log(result);
- // callback(null, {});
});
};
-var getPathWithoutUsernameAndPassword = function(path){
- if(typeof path !== 'undefined'){
- return path.replace(/[^a-zA-Z0-9]+\:[^a-zA-Z0-9]+\@/, '');
+
+
+
+/* HELPER METHODS --------- */
+
+/* Strips username and password from URL so that the node_id can be extracted. */
+
+Neo4j.prototype.RemoveCredentials = function(path){
+ if(typeof path !== 'undefined' && path !== ''){
+ return path.replace(/[a-z0-9]+\:[a-z0-9]+\@/, '');
+ } else {
+ return '';
}
};
-module.exports.getPathWithoutUsernameAndPassword = getPathWithoutUsernameAndPassword;
+/* Extract node_id and add it as a property. */
+
+Neo4j.prototype.AddNodeId = function(node, callback){
+ node.id = node.self.replace(this.RemoveCredentials(this.url) + '/db/data/node/', '');
+ callback(null, node);
+};
View
@@ -7,39 +7,45 @@ var db = new neo4j(url);
describe('Testing Node specific operations for Neo4j', function(){
- describe('A simple valid node insertion', function(){
- it('should return the JSON for that node', function(done){
- db.InsertNode({name:'foobar'}, function(err, result){
- // console.log(result);
-
- // Javascript Regex to extract the node_id
- // console.log(result.self.replace(url + '/db/data/node/', ''));
-
- result.should.not.equal(null);
- result.data.name.should.equal('foobar');
- done();
+ describe('=> Create a Node', function(){
+ var node_id;
+
+ describe('-> A simple valid node insertion', function(){
+ it('should return the JSON for that node', function(done){
+ db.InsertNode({name:'foobar'}, function(err, result){
+
+ node_id = result.id;
+
+ result.should.not.equal(null);
+ result.data.name.should.equal('foobar');
+ result.id.should.not.equal('');
+ done();
+ });
});
});
+
+ // Remove Node afterwards.
+ after(function(done){
+ db.DeleteNode(node_id, function(err, result){
+ done();
+ });
+ });
});
+
- describe('- Deleting a Node', function(){
+ describe('=> Delete a Node', function(){
var node_id;
// Insert a Node.
before(function(done){
db.InsertNode({name:'foobar'}, function(err, result){
-
- // console.log(url);
- // console.log(neo4j.getPathWithoutUsernameAndPassword(url));
-
- node_id = result.self.replace(url + '/db/data/node/', '');
+ node_id = result.id;
done();
-
});
})
- describe(' - Deleting an existing Node without Relationships', function(){
+ describe('-> Deleting an existing Node without Relationships', function(){
it('should delete the Node without issues', function(done){
db.DeleteNode(node_id, function(err, result){
result.should.not.equal(null);

0 comments on commit 0ab2194

Please sign in to comment.