imf-data-nodejs-sdk
Version:
IMFData Cloudant Node.js SDK
128 lines (116 loc) • 3.83 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.multipart', 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, attachments, doc_id, [params], [callback])', function(){
it('should successfully create a doc with 2 attachments', function(done){
this.timeout(10000);
var attachments = [];
attachments.push({
name: 'test.png',
content_type: 'image/png',
data: buffer
});
attachments.push({
name: 'test1.png',
content_type: 'image/png',
data: buffer1
});
db.multipart.insert({test: 'foo'}, attachments, 'test', function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
done();
});
});
});
describe('.get(doc_id, [params], [callback])', function(){
it('should successfully get a doc with all attachments', function(done){
this.timeout(10000);
db.multipart.get('test', function(err, response, headers){
assert.ifError(err);
assert.ok(response);
assert.ok(response._attachments);
for (var att_name in response._attachments){
assert.ok(response._attachments[att_name]);
assert.ok(response._attachments[att_name].data);
switch(att_name){
case 'test.png':
assert.equal(response._attachments[att_name].data, buffer.toString('base64'));
break;
case 'test1.png':
assert.equal(response._attachments[att_name].data, buffer1.toString('base64'));
break;
default:
done(new Error("Expected 'test.png' or 'test1.png' and got " + att_name));
}
}
assert.ok(headers);
done();
});
});
});
});