Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add tests for seraph#link, seraph#readLink, and batch crud

  • Loading branch information...
commit d116ada90b53cea2ee34d442f5603e12393e62b0 1 parent 60273fc
@jonpacker jonpacker authored
Showing with 121 additions and 5 deletions.
  1. +22 −3 lib/seraph.js
  2. +99 −2 test/seraph.js
View
25 lib/seraph.js
@@ -11,9 +11,16 @@ var defaultOptions = {
, id: 'id'
}, optionKeys = Object.keys(defaultOptions);
+function inferOptions(options) {
+ if (typeof options === 'string') {
+ options = { endpoint: options };
+ }
+
+ return _.extend({}, defaultOptions, options);
+}
+
function Seraph(options) {
- // TODO: check if options is a url and handle that.
- this.options = options;
+ this.options = inferOptions(options);
// Copy all the fns from the main seraph, and always supply the first arg as
// our `options` variable.
@@ -46,7 +53,10 @@ var seraph = {
if (['object', 'undefined', 'function'].indexOf(typeof method) !== -1) {
if (typeof method === 'function') {
callback = method;
- } else if (typeof method === 'object') {
+ method = undefined;
+ }
+
+ if (typeof method === 'object') {
data = method;
method = 'POST';
} else {
@@ -84,6 +94,14 @@ var seraph = {
} else if (response.statusCode < 200 || response.statusCode >= 300) {
callback(new Error(body || response.statusCode));
} else {
+ if (typeof body === 'string') {
+ try {
+ body = JSON.parse(body);
+ } catch (e) {
+ callback(e);
+ }
+ }
+
callback(null, body, response);
}
});
@@ -176,6 +194,7 @@ var seraph = {
if (err) {
return callback(err);
} else {
+ body[options.id] = id;
return callback(null, body);
}
});
View
101 test/seraph.js
@@ -70,7 +70,7 @@ describe('seraph#call', function() {
describe('CRUD Operations', function() {
//TODO - split this out into each of the functions...
- it('perform a full crud cycle correctly', function(done) {
+ it('should be able to create and object and read it back', function(done) {
function create(done) {
db.save({ name: 'Jon', age: 23 }, function(err, user) {
assert.ok(!err);
@@ -91,6 +91,20 @@ describe('CRUD Operations', function() {
});
}
+
+ async.waterfall([create, read], done);
+ });
+
+ it('should perform an update on an object', function(done) {
+ function create(done) {
+ db.save({ name: 'Jan', age: 55 }, function(err, user) {
+ assert.ok(!err);
+ assert.equal(user.name, 'Jan');
+ assert.equal(user.age, 55);
+ done(null, user);
+ });
+ }
+
function update(user, done) {
user.name = 'Belinda';
user.age = 26;
@@ -102,6 +116,7 @@ describe('CRUD Operations', function() {
assert.equal(user.age, 26);
db.read(userId, function(err, user) {
assert.ok(!err);
+ assert.equal(userId, user.id);
assert.equal(user.name, 'Belinda');
assert.equal(user.age, 26);
done(null, user);
@@ -109,6 +124,19 @@ describe('CRUD Operations', function() {
});
}
+ async.waterfall([create, update], done);
+ });
+
+ it('should delete an object', function(done) {
+ function create(done) {
+ db.save({ name: 'Neil', age: 61 }, function(err, user) {
+ assert.ok(!err);
+ assert.equal(user.name, 'Neil');
+ assert.equal(user.age, 61);
+ done(null, user);
+ });
+ }
+
function del(user, done) {
db.delete(user, function(err) {
assert.ok(!err);
@@ -119,6 +147,75 @@ describe('CRUD Operations', function() {
});
}
- async.waterfall([create, read, update, del], done);
+ async.waterfall([create, del], done);
+ });
+
+ it('should batch CRUD operations', function(done) {
+ function createObjs(done) {
+ db.save([{name: 'Jon'}, {name: 'Helge'}], function(err, users) {
+ assert.ok(!err);
+ assert.equal(users[0].name, 'Jon');
+ assert.equal(users[1].name, 'Helge');
+ done(null, users[0], users[1]);
+ });
+ }
+
+ function readObjs(user1, user2, done) {
+ db.read([user1.id, user2.id], function(err, users) {
+ assert.ok(!err);
+ assert.equal(users[0].name, 'Jon');
+ assert.equal(users[1].name, 'Helge');
+ done(null, users[0], users[1]);
+ });
+ }
+
+ function delObjs(user1, user2, done) {
+ db.delete([user1.id, user2.id], function(err) {
+ assert.ok(!err);
+ db.read([user1.id, user2.id], function(err) {
+ assert.ok(!!err);
+ });
+ });
+ }
+
+ async.waterfall([createObjs, readObjs, delObjs], done);
+ });
+
+ it('should link two objects together', function(done) {
+ function createObjs(done) {
+ db.save([{name: 'Jon'}, {name: 'Helge'}], function(err, users) {
+ assert.ok(!err);
+ assert.equal(users[0].name, 'Jon');
+ assert.equal(users[1].name, 'Helge');
+ done(null, users[0], users[1]);
+ });
+ }
+
+ function linkObjs(user1, user2, done) {
+ db.link(user1, 'coworker', user2, function(err, link) {
+ assert.ok(!err);
+ assert.equal(link.start, user1.id);
+ assert.equal(link.end, user2.id);
+ assert.equal(link.type, 'coworker');
+ assert.deepEqual(link.properties, {});
+ assert.ok(link.id);
+ done(null, link);
+ });
+ }
+
+ function readLink(link, done) {
+ var linkId = link.id;
+ db.readLink(link.id, function(err, link) {
+ assert.ok(!err);
+ assert.equal(link.start, user1.id);
+ assert.equal(link.end, user2.id);
+ assert.equal(link.type, 'coworker');
+ assert.deepEqual(link.properties, {});
+ assert.equal(linkId, link.id);
+ done(null);
+ });
+ }
+
+ async.waterfall([createObjs, linkObjs, readLink], done);
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.