Fixes to entity.reload, entity.save, entity.remove to support optional 'client' argument
diff --git a/lib/entity.js b/lib/entity.js
index a8b63a6..20c4669 100644
--- a/lib/entity.js
+++ b/lib/entity.js
@@ -80,8 +80,10 @@
this[key].shift()
}
},
- reload: function(callback) {
- var client = helpers.client.validate(Array.prototype.slice.call(arguments))
+ reload: function() {
+ var args = Array.prototype.slice.call(arguments)
+ var client = helpers.client.validate(args)
+ var callback = helpers.cb(_.last(args.filter(_.isFunction)))
client.GET(this, function(err, usergridResponse) {
helpers.setWritable(this, ['uuid', 'name', 'type', 'created'])
_.assign(this, usergridResponse.entity)
@@ -89,8 +91,10 @@
callback(err || usergridResponse.error, usergridResponse, this)
}.bind(this))
},
- save: function(callback) {
- var client = helpers.client.validate(Array.prototype.slice.call(arguments))
+ save: function() {
+ var args = Array.prototype.slice.call(arguments)
+ var client = helpers.client.validate(args)
+ var callback = helpers.cb(_.last(args.filter(_.isFunction)))
client.PUT(this, function(err, usergridResponse) {
helpers.setWritable(this, ['uuid', 'name', 'type', 'created'])
_.assign(this, usergridResponse.entity)
@@ -98,8 +102,10 @@
callback(err || usergridResponse.error, usergridResponse, this)
}.bind(this))
},
- remove: function(callback) {
- var client = helpers.client.validate(Array.prototype.slice.call(arguments))
+ remove: function() {
+ var args = Array.prototype.slice.call(arguments)
+ var client = helpers.client.validate(args)
+ var callback = helpers.cb(_.last(args.filter(_.isFunction)))
client.DELETE(this, function(err, usergridResponse) {
callback(err || usergridResponse.error, usergridResponse, this)
}.bind(this))
diff --git a/tests/lib/entity.test.js b/tests/lib/entity.test.js
index ad447e6..85d57f0 100644
--- a/tests/lib/entity.test.js
+++ b/tests/lib/entity.test.js
@@ -281,7 +281,24 @@
this.slow(_slow + 1000)
this.timeout(_timeout + 4000)
- it('should refresh an entity with the latest server copy of itself', function(done) {
+ it('should refresh an entity with the latest server copy of itself using the Usergrid shared instance', function(done) {
+ var client = new UsergridClient(config),
+ now = Date.now()
+ client.GET(config.test.collection, function(err, getResponse) {
+ var entity = new UsergridEntity(getResponse.first)
+ var modified = entity.modified
+ getResponse.first.putProperty('reloadSingletonTest', now)
+ client.PUT(getResponse.first, function(err, putResponse) {
+ entity.reload(function() {
+ entity.reloadSingletonTest.should.equal(now)
+ entity.modified.should.not.equal(modified)
+ done()
+ })
+ })
+ })
+ })
+
+ it('should refresh an entity with the latest server copy of itself using a UsergridClient instance argument', function(done) {
var client = new UsergridClient(config),
now = Date.now()
client.GET(config.test.collection, function(err, getResponse) {
@@ -289,7 +306,7 @@
var modified = entity.modified
getResponse.first.putProperty('reloadTest', now)
client.PUT(getResponse.first, function(err, putResponse) {
- entity.reload(function() {
+ entity.reload(client, function() {
entity.reloadTest.should.equal(now)
entity.modified.should.not.equal(modified)
done()
@@ -304,13 +321,26 @@
this.slow(_slow + 1000)
this.timeout(_timeout + 4000)
- it('should save an updated entity to the server', function(done) {
+ it('should save an updated entity to the server using the Usergrid shared instance', function(done) {
+ var client = new UsergridClient(config),
+ now = Date.now()
+ client.GET(config.test.collection, function(err, getResponse) {
+ var entity = new UsergridEntity(getResponse.first)
+ entity.putProperty('saveSingletonTest', now)
+ entity.save(function() {
+ entity.saveSingletonTest.should.equal(now)
+ done()
+ })
+ })
+ })
+
+ it('should save an updated entity to the server using a UsergridClient instance argument', function(done) {
var client = new UsergridClient(config),
now = Date.now()
client.GET(config.test.collection, function(err, getResponse) {
var entity = new UsergridEntity(getResponse.first)
entity.putProperty('saveTest', now)
- entity.save(function() {
+ entity.save(client, function() {
entity.saveTest.should.equal(now)
done()
})
@@ -323,10 +353,10 @@
this.slow(_slow + 1000)
this.timeout(_timeout + 4000)
- it('should remove an entity from the server', function(done) {
+ it('should remove an entity from the server using the Usergrid shared instance', function(done) {
var client = new UsergridClient(config)
client.POST(config.test.collection, {
- removeTest: 'test'
+ removeSingletonTest: 'test'
}, function(err, postResponse) {
var entity = new UsergridEntity(postResponse.first)
entity.remove(function(err, deleteResponse) {
@@ -337,6 +367,21 @@
})
})
})
+
+ it('should remove an entity from the server using a UsergridClient instance argument', function(done) {
+ var client = new UsergridClient(config)
+ client.POST(config.test.collection, {
+ removeTest: 'test'
+ }, function(err, postResponse) {
+ var entity = new UsergridEntity(postResponse.first)
+ entity.remove(client, function(err, deleteResponse) {
+ deleteResponse.statusCode.should.equal(200)
+ // best practice is to delete the entity object here, because it no longer exists on the server
+ entity = null
+ done()
+ })
+ })
+ })
})
describe('connect()', function() {