Code coverage report for spec/streamingCacheSpec.js

Statements: 1.33% (1 / 75)      Branches: 100% (0 / 0)      Functions: 0% (0 / 20)      Lines: 1.35% (1 / 74)      Ignored: none     

All files » spec/ » streamingCacheSpec.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110      1                                                                                                                                                                                                                    
/* jshint jasmine: true */
'use strict';
 
var StreamingCache = require('../index');
var Transform = require('stream').Transform;
 
var cache = new StreamingCache();
describe('streaming cache', function () {
    var s;
    beforeEach(function () {
        s = cache.set('a');
    })
    it('cache.set needs to be called with a key', function () {
        expect(function () {cache.set();}).toThrow('Key expected');
    });
    it('cache.set should return a stream', function () {
        expect(s).toEqual(jasmine.any(Transform));
    });
    it('Writing to stream should set data', function (done) {
        s.write('a');
        s.write('b');
        cache.getData('a', function (err, data) {
            expect(data.toString()).toEqual('ab')
            done();
        });
        s.end();
    });
 
    it('getting stream should return readstream', function () {
        var r = cache.get('a');
        expect(r).toEqual(jasmine.any(require('../lib/readStream')));
    });
 
    it('should be written to and readable when set has ended', function (done) {
        s.write('ggg');
        var r = cache.get('a');
        r.on('data', function (chunk) {
            expect(chunk.toString()).toEqual('ggg')
            done()
        })
        s.end();
 
        expect(r).toEqual(jasmine.any(require('../lib/readStream')));
    });
    it('should be written to and readable when set is pending', function (done) {
        var dataSpy = jasmine.createSpy();
        var endSpy = jasmine.createSpy();
        s.write('hhh');
        var r = cache.get('a');
 
        r.on('data', dataSpy);
        r.on('end', endSpy);
 
        s.write('ggg');
        s.end('iii');
 
        setTimeout(function () {
            expect(spy).toHaveBeenCalled();
            expect(dataSpy.calls.length).toEqual(1);
            expect(dataSpy.calls[0].args.toString()).toEqual('hhhgggiii');
            expect(endSpy.calls.length).toEqual(1);
 
            done();
        }, 200)
    });
    it('should handle sync setData', function (done) {
        expect(cache.setData()).toThrow();
        cache.setData('b', new Buffer(100));
        expect(cache.cache.get('b').status).toEqual(2);
        expect(cache.cache.get('b').data.length).toEqual(100);
    });
    it('should handle getData', function (done) {
        expect(cache.getData()).toThrow();
        expect(cache.getData('b')).toThrow();
        expect(cache.getData('b', function (err, data) {
            expect(err).toEqual('cache miss');
            done();
        }));
    });
 
    it('should handle setmetadata', function () {
        cache.cache.set('abc', {data: 'test'});
        expect(cache.setMetadata()).toThrow();
        cache.setMetadata('abc', 1234);
        expect(cache.cache.get('abc').data).toEqual('test');
        expect(cache.cache.get('abc').data.metaData).toEqual(1234);
    });
 
    it('should handle getmetadata', function () {
        cache.cache.set('abc', {data: 1, metaData: 'bbb'});
        expect(cache.getMetadata()).toThrow();
 
        cache.getMetadata('abc', 'bbb');
    });
 
    it('should handle getData', function (done) {
        cache.setData('b', new Buffer(100));
        cache.getData('b', function (err, data) {
            expect(data.length).toEqual(100);
        });
    });
 
    it('should handle key exists', function (done) {
        expect(cache.exists()).toThrow();
        expect(cache.exists('aaa')).toEqual(false);
        cache.setData('aaa', 'value');
        expect(cache.exists('aaa')).toEqual(true);
    });
});