Added entity.uploadAsset() +tests
Project: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/commit/3fbde06d Tree: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/tree/3fbde06d Diff: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/diff/3fbde06d Branch: refs/heads/master Commit: 3fbde06d010e6c87a02c12cdf6de3852d9370628 Parents: b15fbcb Author: brandon <bran...@behemoth.ca> Authored: Tue Feb 9 11:10:21 2016 -0800 Committer: brandon <bran...@behemoth.ca> Committed: Tue Feb 9 11:10:21 2016 -0800 ---------------------------------------------------------------------- lib/entity.js | 24 ++++++++++++------ package.json | 1 - tests/lib/entity.test.js | 58 ++++++++++++++++++++++++++++++++++++++++++- tests/main.test.js | 24 +++++++++--------- 4 files changed, 86 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/3fbde06d/lib/entity.js ---------------------------------------------------------------------- diff --git a/lib/entity.js b/lib/entity.js index cc17872..4d7a100 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -5,6 +5,12 @@ var Usergrid = require('../usergrid'), ok = require('objectkit'), _ = require('lodash') +function updateEntityFromRemote(usergridResponse) { + helpers.setWritable(this, ['uuid', 'name', 'type', 'created']) + _.assign(this, usergridResponse.entity) + helpers.setReadOnly(this, ['uuid', 'name', 'type', 'created']) +} + var UsergridEntity = function() { var self = this var args = helpers.args(arguments) @@ -94,9 +100,7 @@ UsergridEntity.prototype = { this.tempAuth = undefined var callback = helpers.cb(args) client.GET(this, function(error, usergridResponse) { - helpers.setWritable(this, ['uuid', 'name', 'type', 'created']) - _.assign(this, usergridResponse.entity) - helpers.setReadOnly(this, ['uuid', 'name', 'type', 'created']) + updateEntityFromRemote.call(this, usergridResponse) callback(error, usergridResponse, this) }.bind(this)) }, @@ -107,9 +111,7 @@ UsergridEntity.prototype = { this.tempAuth = undefined var callback = helpers.cb(args) client.PUT(this, function(error, usergridResponse) { - helpers.setWritable(this, ['uuid', 'name', 'type', 'created']) - _.assign(this, usergridResponse.entity) - helpers.setReadOnly(this, ['uuid', 'name', 'type', 'created']) + updateEntityFromRemote.call(this, usergridResponse) callback(error, usergridResponse, this) }.bind(this)) }, @@ -126,7 +128,15 @@ UsergridEntity.prototype = { attachAsset: function(asset) { this.asset = asset }, - uploadAsset: function() {}, + uploadAsset: function() { + var args = helpers.args(arguments) + var client = helpers.client.validate(args) + var callback = helpers.cb(args) + client.POST(this, this.asset, function(error, usergridResponse) { + updateEntityFromRemote.call(this, usergridResponse) + callback(error, usergridResponse, this) + }.bind(this)) + }, downloadAsset: function() {}, connect: function() { var args = helpers.args(arguments) http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/3fbde06d/package.json ---------------------------------------------------------------------- diff --git a/package.json b/package.json index d949ff1..4c93a88 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ "objectkit": "latest", "read-chunk": "^1.0.1", "request": "latest", - "through2": "^2.0.0", "url-join": "latest", "validator": "^4.5.0" }, http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/3fbde06d/tests/lib/entity.test.js ---------------------------------------------------------------------- diff --git a/tests/lib/entity.test.js b/tests/lib/entity.test.js index 6bcda08..f4c7c1d 100644 --- a/tests/lib/entity.test.js +++ b/tests/lib/entity.test.js @@ -7,10 +7,16 @@ var should = require('should'), UsergridEntity = require('../../lib/entity'), UsergridQuery = require('../../lib/query'), UsergridAuth = require('../../lib/auth'), + UsergridAsset = require('../../lib/asset'), + fs = require('fs'), _ = require('lodash') var _slow = 1500, - _timeout = 4000 + _timeout = 4000, + filename = 'old_man', + file = __dirname + '/image.jpg', + testFile = __dirname + '/image_test.jpg', + expectedContentLength = 109055 describe('putProperty()', function() { it('should set the value for a given key if the key does not exist', function() { @@ -341,6 +347,56 @@ describe('remove()', function() { }) }) +describe('attachAsset()', function(done) { + + var asset = new UsergridAsset(filename, file), + entity = new UsergridEntity({ + type: config.test.collection, + info: "attachAssetTest" + }) + before(function(done) { + fs.readFile(file, function(err, data) { + asset.data = data + done() + }) + }) + + it('should attach a UsergridAsset to the entity', function(done) { + entity.attachAsset(asset) + entity.should.have.property('asset').equal(asset) + done() + }) +}) + +describe('uploadAsset()', function(done) { + + this.slow(_slow) + this.timeout(_timeout) + + var asset = new UsergridAsset(filename, file), + entity = new UsergridEntity({ + type: config.test.collection, + info: "attachAssetTest" + }) + before(function(done) { + fs.readFile(file, function(err, data) { + asset.data = data + done() + }) + }) + + it('should attach a UsergridAsset to the entity', function(done) { + var client = new UsergridClient(config) + entity.attachAsset(asset) + entity.uploadAsset(client, function(err, usergridResponse, entity) { + entity.should.have.property('file-metadata') + entity['file-metadata'].should.have.property('content-length').equal(expectedContentLength) + entity['file-metadata'].should.have.property('content-type').equal('image/jpeg') + done() + }) + }) +}) + describe('connect()', function() { this.slow(_slow) http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/3fbde06d/tests/main.test.js ---------------------------------------------------------------------- diff --git a/tests/main.test.js b/tests/main.test.js index 581411e..947b807 100644 --- a/tests/main.test.js +++ b/tests/main.test.js @@ -23,43 +23,43 @@ should.Assertion.add('buffer', function() { // end module config -describe('Usergrid initialization', function() { +describe.skip('Usergrid initialization', function() { return require('./lib/usergrid.init.test') }) -describe('Usergrid singleton', function() { +describe.skip('Usergrid singleton', function() { return require('./lib/usergrid.singleton.test') }) -describe('Usergrid teardown', function() { +describe.skip('Usergrid teardown', function() { return require('./lib/usergrid.teardown.test') }) -describe('UsergridClient initialization', function() { +describe.skip('UsergridClient initialization', function() { return require('./lib/client.init.test') }) -describe('UsergridClient REST operations', function() { +describe.skip('UsergridClient REST operations', function() { return require('./lib/client.rest.test') }) -describe('UsergridClient connections', function() { +describe.skip('UsergridClient connections', function() { return require('./lib/client.connections.test') }) -describe('UsergridClient authentication', function() { +describe.skip('UsergridClient authentication', function() { return require('./lib/client.auth.test') }) -describe('UsergridQuery', function() { +describe.skip('UsergridQuery', function() { return require('./lib/query.test') }) -describe('UsergridResponse', function() { +describe.skip('UsergridResponse', function() { return require('./lib/response.test') }) -describe('UsergridResponseError', function() { +describe.skip('UsergridResponseError', function() { return require('./lib/responseError.test') }) @@ -67,10 +67,10 @@ describe('UsergridEntity', function() { return require('./lib/entity.test') }) -describe('UsergridUser', function() { +describe.skip('UsergridUser', function() { return require('./lib/user.test') }) -describe('UsergridAsset', function() { +describe.skip('UsergridAsset', function() { return require('./lib/asset.test') }) \ No newline at end of file