loopback-component-storage/test/storage-service.test.js

241 lines
6.9 KiB
JavaScript
Raw Normal View History

2020-02-10 18:57:12 +00:00
// Copyright IBM Corp. 2013,2019. All Rights Reserved.
2016-05-03 23:18:18 +00:00
// Node module: loopback-component-storage
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
2019-05-07 13:41:29 +00:00
2016-10-18 22:48:59 +00:00
'use strict';
2016-05-03 23:18:18 +00:00
2014-01-10 19:34:37 +00:00
var StorageService = require('../lib/storage-service.js');
var assert = require('assert');
var path = require('path');
2016-10-18 22:48:59 +00:00
var storageService = new StorageService({
root: path.join(__dirname, 'storage'),
provider: 'filesystem',
});
2016-10-18 22:06:55 +00:00
describe('Storage service', function() {
describe('container apis', function() {
it('should return an empty list of containers', function(done) {
storageService.getContainers(function(err, containers) {
2014-01-24 17:44:58 +00:00
assert(!err);
2018-10-15 22:17:47 +00:00
assert.equal(containers.length, 0);
2014-01-24 17:44:58 +00:00
done(err, containers);
});
});
2018-10-15 22:17:47 +00:00
it('should return an empty list of containers - promise', function(done) {
storageService.getContainers()
.then(function(containers) {
assert.equal(containers.length, 0);
done();
})
.catch(done);
});
2016-10-18 22:06:55 +00:00
it('should create a new container', function(done) {
storageService.createContainer({name: 'c1'}, function(err, container) {
2014-01-24 17:44:58 +00:00
assert(!err);
2014-03-31 23:30:35 +00:00
assert(container.getMetadata());
2014-01-24 17:44:58 +00:00
done(err, container);
});
});
2018-10-15 22:17:47 +00:00
it('should create a new container - promise', function(done) {
storageService.createContainer({name: 'c3'})
.then(function(container) {
assert(container.getMetadata());
done();
})
.catch(done);
});
2016-10-18 22:06:55 +00:00
it('should get a container c1', function(done) {
storageService.getContainer('c1', function(err, container) {
2014-01-24 17:44:58 +00:00
assert(!err);
2014-03-31 23:30:35 +00:00
assert(container.getMetadata());
2014-01-24 17:44:58 +00:00
done(err, container);
});
});
2018-10-15 22:17:47 +00:00
it('should get a container c1 - promise', function(done) {
storageService.getContainer('c1')
.then(function(container) {
assert(container.getMetadata());
done();
})
.catch(done);
});
2016-10-18 22:06:55 +00:00
it('should not get a container c2', function(done) {
storageService.getContainer('c2', function(err, container) {
2014-01-24 17:44:58 +00:00
assert(err);
done(null, container);
});
});
2018-10-15 22:17:47 +00:00
it('should destroy a container c3 - promise', function(done) {
storageService.destroyContainer('c3')
.then(function(container) {
done(null, container);
})
.catch(done);
});
2016-10-18 22:06:55 +00:00
it('should return one container', function(done) {
storageService.getContainers(function(err, containers) {
2014-01-24 17:44:58 +00:00
assert(!err);
2018-10-15 22:17:47 +00:00
assert.equal(containers.length, 1);
2014-01-24 17:44:58 +00:00
done(err, containers);
});
});
2016-10-18 22:06:55 +00:00
it('should destroy a container c1', function(done) {
storageService.destroyContainer('c1', function(err, container) {
2014-01-24 17:44:58 +00:00
assert(!err);
done(err, container);
});
});
2016-10-18 22:06:55 +00:00
it('should not get a container c1 after destroy', function(done) {
storageService.getContainer('c1', function(err, container) {
2014-01-24 17:44:58 +00:00
assert(err);
done(null, container);
});
});
});
2016-10-18 22:06:55 +00:00
describe('file apis', function() {
2014-01-24 17:44:58 +00:00
var fs = require('fs');
2016-10-18 22:06:55 +00:00
it('should create a new container', function(done) {
storageService.createContainer({name: 'c1'}, function(err, container) {
2014-01-24 17:44:58 +00:00
assert(!err);
done(err, container);
});
});
2016-10-18 22:06:55 +00:00
it('should upload a file', function(done) {
2014-01-24 17:44:58 +00:00
var writer = storageService.uploadStream('c1', 'f1.txt');
fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer);
writer.on('finish', done);
writer.on('error', done);
});
2016-10-18 22:06:55 +00:00
it('should emit success event', function(done) {
2015-06-01 07:55:04 +00:00
var writer = storageService.uploadStream('c1', 'f1.txt');
fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer);
writer.on('success', done);
writer.on('error', done);
});
2014-01-24 17:44:58 +00:00
2016-10-18 22:06:55 +00:00
it('should download a file', function(done) {
2014-01-24 17:44:58 +00:00
var reader = storageService.downloadStream('c1', 'f1.txt');
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));
reader.on('end', done);
reader.on('error', done);
});
2016-10-18 22:06:55 +00:00
it('should get files for a container', function(done) {
storageService.getFiles('c1', function(err, files) {
2014-01-24 17:44:58 +00:00
assert(!err);
2018-10-15 22:17:47 +00:00
assert.equal(files.length, 1);
2014-01-24 17:44:58 +00:00
done(err, files);
});
});
2018-10-15 22:17:47 +00:00
it('should get files for a container - promise', function(done) {
storageService.getFiles('c1')
.then(function(files) {
assert.equal(files.length, 1);
done();
})
.catch(done);
});
2016-10-18 22:06:55 +00:00
it('should get a file', function(done) {
storageService.getFile('c1', 'f1.txt', function(err, f) {
2014-01-24 17:44:58 +00:00
assert(!err);
assert.ok(f);
2014-03-31 23:30:35 +00:00
assert(f.getMetadata());
2014-01-24 17:44:58 +00:00
done(err, f);
});
});
2018-10-15 22:17:47 +00:00
it('should get a file - promise', function(done) {
storageService.getFile('c1', 'f1.txt')
.then(function(f) {
assert.ok(f);
assert(f.getMetadata());
done();
})
.catch(done);
});
2016-10-18 22:06:55 +00:00
it('should remove a file', function(done) {
storageService.removeFile('c1', 'f1.txt', function(err) {
2014-01-24 17:44:58 +00:00
assert(!err);
done(err);
});
});
2018-10-15 22:17:47 +00:00
it('should remove a file - promise', function(done) {
createFile('c1', 'f1.txt')
.then(function() {
return storageService.removeFile('c1', 'f1.txt')
.then(function() {
done();
});
})
.catch(done);
});
2016-10-18 22:06:55 +00:00
it('should get no files from a container', function(done) {
storageService.getFiles('c1', function(err, files) {
2014-01-24 17:44:58 +00:00
assert(!err);
2018-10-15 22:17:47 +00:00
assert.equal(files.length, 0);
2014-01-24 17:44:58 +00:00
done(err, files);
});
});
it('should not get a file from a container', function(done) {
storageService.getFile('c1', 'f1.txt', function(err, f) {
assert(err);
2018-10-15 22:17:47 +00:00
assert.equal(err.code, 'ENOENT');
assert.equal(err.status, 404);
assert(!f);
done();
});
});
2018-10-15 22:17:47 +00:00
it('should not get a file from a container - promise', function(done) {
storageService.getFile('c1', 'f1.txt')
.then(function() {
throw new Error('should not be throw');
})
.catch(function(err) {
assert.equal(err.code, 'ENOENT');
assert.equal(err.status, 404);
done();
});
});
2016-10-18 22:06:55 +00:00
it('should destroy a container c1', function(done) {
storageService.destroyContainer('c1', function(err, container) {
2014-01-24 17:44:58 +00:00
assert(!err);
done(err, container);
});
});
2018-10-15 22:17:47 +00:00
function createFile(container, file) {
return new Promise(function(resolve, reject) {
var writer = storageService.uploadStream(container, file);
fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer);
writer.on('finish', resolve);
writer.on('error', reject);
});
}
2014-01-24 17:44:58 +00:00
});
});