abstract-blob-store
Version:
A test suite and interface you can use to implement streaming file (blob) storage modules for various storage backends and platforms.
108 lines (90 loc) • 3.27 kB
JavaScript
var from = require('from2-array')
var concat = require('concat-stream')
module.exports.blobWriteStream = function(test, common) {
test('piping a blob into a blob write stream', function(t) {
common.setup(test, function(err, store) {
t.notOk(err, 'no setup err')
var ws = store.createWriteStream({name: 'test.js'}, function(err, obj) {
t.error(err)
t.ok(obj.key, 'blob has key')
common.teardown(test, store, obj, function(err) {
t.error(err)
t.end()
})
})
from([new Buffer('foo'), new Buffer('bar')]).pipe(ws)
})
})
}
module.exports.blobReadStream = function(test, common) {
test('reading a blob as a stream', function(t) {
common.setup(test, function(err, store) {
t.notOk(err, 'no setup err')
var ws = store.createWriteStream({name: 'test.js'}, function(err, blob) {
t.notOk(err, 'no blob write err')
t.ok(blob.key, 'blob has key')
var rs = store.createReadStream(blob)
rs.on('error', function(e) {
t.false(e, 'no read stream err')
t.end()
})
rs.pipe(concat(function(file) {
t.equal(file.length, 6, 'blob length is correct')
common.teardown(test, store, blob, function(err) {
t.error(err)
t.end()
})
}))
})
from([new Buffer('foo'), new Buffer('bar')]).pipe(ws)
})
})
}
module.exports.blobReadError = function(test, common) {
test('reading a blob that does not exist', function(t) {
common.setup(test, function(err, store) {
t.notOk(err, 'no setup err')
var rs = store.createReadStream({name: 'test.js', key: '8843d7f92416211de9ebb963ff4ce28125932878'})
rs.on('error', function(e) {
t.ok(e, 'got a read stream err')
// t.ok(e.notFound, 'error reports not found')
common.teardown(test, store, undefined, function(err) {
t.error(err)
t.end()
})
})
})
})
}
module.exports.blobExists = function(test, common) {
test('check if a blob exists', function(t) {
common.setup(test, function(err, store) {
t.notOk(err, 'no setup err')
var blobMeta = {name: 'test.js', key: '8843d7f92416211de9ebb963ff4ce28125932878'}
store.exists(blobMeta, function(err, exists) {
t.error(err)
t.notOk(exists, 'does not exist')
var ws = store.createWriteStream({name: 'test.js'}, function(err, obj) {
t.notOk(err, 'no blob write err')
t.ok(obj.key, 'blob has key')
// on this .exists call use the metadata from the writeStream
store.exists(obj, function(err, exists) {
t.error(err)
t.ok(exists, 'exists')
common.teardown(test, store, obj, function(err) {
t.error(err)
t.end()
})
})
})
from([new Buffer('foo'), new Buffer('bar')]).pipe(ws)
})
})
})
}
module.exports.all = function (test, common) {
module.exports.blobWriteStream(test, common)
module.exports.blobReadStream(test, common)
module.exports.blobReadError(test, common)
module.exports.blobExists(test, common)
}