imf-data-nodejs-sdk
Version:
IMFData Cloudant Node.js SDK
126 lines (113 loc) • 3.91 kB
JavaScript
/**
* Copyright 2015 IBM Corp. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var assert = require('assert');
var IMFData = require('../lib/imfdata.js');
var config = require('./utils/config.js');
var tools = require('./utils/assert-tools.js');
var fs = require('fs');
describe('db.attachment', function(){
var data;
var db;
var buffer;
var buffer1;
var doc_rev;
/**
* Gets data module and creates a database
*/
before(function(done){
this.timeout(10000);
IMFData.initialize(config, function(err, imfdata){
if (err) {
done(err);
} else {
data = imfdata;
data.db.create('testdb', function(err, body, headers){
if (err) {
done(err);
} else {
db = data.use('testdb');
buffer = fs.readFileSync(__dirname + '/utils/test.png');
buffer1 = fs.readFileSync(__dirname + '/utils/test1.png');
data.set_permissions({identity: config.appId, database: db.config.db, access: 'admins'}, function(err, response, headers){
if (err) {
done(err);
} else {
done();
}
});
}
});
}
});
});
/**
* Cleans up database
*/
after(function(done){
this.timeout(10000);
data.db.destroy('testdb', function(err, body, headers) {
if (err)
done(err);
else
done();
});
});
describe('.insert(doc_id, attachment_name, attachment, contentType, [params], [callback])', function(){
it('should successfully insert an attachment into a new document', function(done){
this.timeout(10000);
db.attachment.insert('test', 'test.png', buffer, 'image/png', function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
doc_rev = response.rev;
done();
});
});
it('should successfully update an attachment in an existing document', function(done){
this.timeout(10000);
db.attachment.insert('test', 'test.png', buffer1, 'image/png', {rev: doc_rev}, function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
doc_rev = response.rev;
done();
});
});
});
describe('.get(doc_id, attachment_name, [params], [callback])', function(){
it('should successfully get an attachment', function(done){
this.timeout(10000);
db.attachment.get('test', 'test.png', function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
assert.equal(response.toString('base64'), buffer1.toString('base64'));
done();
});
});
});
describe('.destroy(doc_id, attachment_name, rev, [callback])', function(){
it('should successfully delete an attachment', function(done){
this.timeout(10000);
db.attachment.destroy('test', 'test.png', doc_rev, function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
done();
});
});
});
});