decentralized-internet
Version:
An NPM library of programs to create decentralized web and distributed computing projects
1 lines • 3.86 kB
JavaScript
var request=require("request"),fs=require("fs"),Promise=require("bluebird"),path=require("path"),_=require("underscore");const Joi=require("@hapi/joi"),Lab=require("lab"),lab=exports.lab=Lab.script(),couchProvider=require("./couch.provider");var confexample={default:"testdb",testdb:{hostname:"http://localhost:5984",database:"testdb"},testdb1:{hostname:"http://localhost:5984",database:"testdb1",datapath:path.join(__dirname,"testdb1")}};couchProvider.setConfiguration(confexample);var joiokres=Joi.object().keys({ok:Joi.boolean().valid(!0),id:Joi.string(),rev:Joi.string()});lab.experiment("Test couch-provider",function(){var e={some_data:"This is a string with some data"},t=function(t){lab.test("returns true when the url of the couchdb server is retrieved",function(){var e=couchProvider.getCouchDBServer(t);if(t||(t=confexample.default,console.log(t)),e!==confexample[t].hostname+"/"+confexample[t].database)throw console.error(e,confexample[t].hostname),"Returned configuration hostname is not the same";return Promise.resolve()}),lab.test("returns true when a database is created",function(){return couchProvider.createDB(t).then(function(e){return console.log(e),!0})});var n=[];lab.test("returns true when a document is uploaded to the DB and when document is fetch from the database",function(){return couchProvider.uploadDocuments(e,t).then(function(e){n=_.pluck(e,"id")})}),lab.test("returns true when a when document is fetch from the database",function(){return Promise.map(n,function(e){return couchProvider.getDocument(e,t)}).then(function(e){console.log(e)})});var r=path.join(__dirname,"README.md");lab.test("returns true when attachment is added",function(){var e=fs.createReadStream(r);return Promise.map(n,function(n){return couchProvider.getDocument(n,t).then(function(n){return couchProvider.addDocumentAttachment(n,path.join("testname",path.basename(r)),e,t)})})}),lab.test("returns true when second attachment is added",function(){var e=fs.createReadStream(r);return Promise.map(n,function(n){return couchProvider.getDocument(n,t).then(function(n){return couchProvider.addDocumentAttachment(n,path.join("testname",path.basename(r)+"_2"),e,t)})})}),lab.test("returns true when the attachment is retrieved",function(){var e=fs.readFileSync(r);return Promise.map(n,function(n){return couchProvider.getDocument(n,t).then(function(e){return couchProvider.getDocumentAttachment(e,"testname/README.md",t)}).then(function(t){if(t.toString()!==e.toString())throw"File retrieved from DB is not the same"})})}),lab.test("returns true when attachment is deleted",function(){return Promise.map(n,function(e){return couchProvider.getDocument(e,t).then(function(e){return couchProvider.deleteAttachment(e,"testname/README.md",t)}).then(function(e){console.log(e)})})}),lab.test("returns error when same attachment is deleted",function(){return Promise.map(n,function(e){return couchProvider.getDocument(e,t).then(function(e){return couchProvider.deleteAttachment(e,"testname/README.md",t)}).catch(function(e){console.log(e)})})}),lab.test("returns true when second attachment is deleted",function(){return Promise.map(n,function(e){return couchProvider.getDocument(e,t).then(function(e){return couchProvider.deleteAttachment(e,"testname/README.md_2",t)}).then(function(e){console.log(e)})})}),lab.test("returns true when attachment is added again",function(){var e=fs.createReadStream(r);return Promise.map(n,function(n){return couchProvider.getDocument(n,t).then(function(n){return couchProvider.addDocumentAttachment(n,path.join("deletedoctest",path.basename(r)),e,t)})})}),lab.test("returns true when document is deleted",function(){return Promise.map(n,function(e){return couchProvider.getDocument(e,t).then(function(e){return couchProvider.deleteDocument(e,t)}).then(function(e){console.log("Document deleted",e)})})})};t(),t("testdb1")});