2016-12-21 11:20:45 -05:00
|
|
|
/**
|
|
|
|
* Test case for MemoryStore
|
|
|
|
*
|
2017-06-08 14:46:51 -04:00
|
|
|
* Copyright (C) 2016 R-T Specialty, LLC.
|
2016-12-21 11:20:45 -05:00
|
|
|
*
|
|
|
|
* This file is part of the Liza Data Collection Framework
|
|
|
|
*
|
|
|
|
* Liza is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2016-12-30 13:58:06 -05:00
|
|
|
var store = require( '../../' ).store,
|
|
|
|
chai = require( 'chai' ),
|
|
|
|
expect = chai.expect,
|
|
|
|
Class = require( 'easejs' ).Class,
|
|
|
|
Trait = require( 'easejs' ).Trait,
|
|
|
|
Sut = store.MemoryStore;
|
|
|
|
|
|
|
|
chai.use( require( 'chai-as-promised' ) );
|
2016-12-21 11:20:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
describe( 'store.MemoryStore', () =>
|
|
|
|
{
|
|
|
|
describe( '#add', () =>
|
|
|
|
{
|
|
|
|
it( 'adds item to store when missing', () =>
|
|
|
|
{
|
|
|
|
const sut = Sut();
|
|
|
|
const item = {};
|
|
|
|
|
2016-12-30 13:58:06 -05:00
|
|
|
return expect(
|
2016-12-21 11:20:45 -05:00
|
|
|
sut.add( 'foo', item )
|
2016-12-30 13:58:06 -05:00
|
|
|
.then( () => sut.get( 'foo' ) )
|
|
|
|
).to.eventually.equal( item );
|
2016-12-21 11:20:45 -05:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'replaces item in store if existing', () =>
|
|
|
|
{
|
|
|
|
const sut = Sut();
|
|
|
|
const item = {};
|
|
|
|
|
2016-12-30 13:58:06 -05:00
|
|
|
return expect(
|
2016-12-21 11:20:45 -05:00
|
|
|
sut.add( 'foo', [] )
|
2016-12-30 13:58:06 -05:00
|
|
|
.then( () => sut.add( 'foo', item ) )
|
|
|
|
.then( () => sut.get( 'foo' ) )
|
|
|
|
).to.eventually.equal( item );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2017-01-26 16:35:29 -05:00
|
|
|
it( 'returns self with promise', () =>
|
2016-12-30 13:58:06 -05:00
|
|
|
{
|
2017-01-26 16:35:29 -05:00
|
|
|
const sut = Sut();
|
2016-12-30 13:58:06 -05:00
|
|
|
|
|
|
|
return expect(
|
2017-01-26 16:35:29 -05:00
|
|
|
sut.add( 'foo', 'bar' )
|
|
|
|
).to.eventually.equal( sut );
|
2016-12-21 11:20:45 -05:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2017-08-24 14:27:01 -04:00
|
|
|
describe( '#populate', () =>
|
|
|
|
{
|
|
|
|
it( "#add's each element of object to store", () =>
|
|
|
|
{
|
|
|
|
const obj = { foo: {}, bar: {} };
|
|
|
|
const sut = Sut();
|
|
|
|
|
|
|
|
return sut.populate( obj )
|
|
|
|
.then( ps =>
|
|
|
|
{
|
|
|
|
// by reference
|
|
|
|
expect( sut.get( 'foo' ) )
|
|
|
|
.to.eventually.equal( obj.foo );
|
|
|
|
expect( sut.get( 'bar' ) )
|
|
|
|
.to.eventually.equal( obj.bar );
|
|
|
|
|
|
|
|
expect( ps.length )
|
|
|
|
.to.equal( Object.keys( obj ).length );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( "fails if any add fails", () =>
|
|
|
|
{
|
|
|
|
const e = Error( 'ok' );
|
|
|
|
|
|
|
|
const sut = Sut.extend( {
|
|
|
|
'override add': ( k, v ) => Promise.reject( e )
|
|
|
|
} )();
|
|
|
|
|
|
|
|
return expect( sut.populate( { a: 1 } ) )
|
|
|
|
.to.eventually.be.rejectedWith( e );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2016-12-21 11:20:45 -05:00
|
|
|
// most things implicitly tested above
|
|
|
|
describe( '#get', () =>
|
|
|
|
{
|
2016-12-30 13:58:06 -05:00
|
|
|
it( 'rejects promise if store item does not exist', () =>
|
2016-12-21 11:20:45 -05:00
|
|
|
{
|
2016-12-30 13:58:06 -05:00
|
|
|
return expect( Sut().get( 'unknown' ) )
|
2016-12-30 15:38:21 -05:00
|
|
|
.to.eventually.be.rejected
|
|
|
|
.and.be.instanceof( store.StoreMissError );
|
2016-12-21 11:20:45 -05:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( '#clear', () =>
|
|
|
|
{
|
|
|
|
it( 'removes all items from store', () =>
|
|
|
|
{
|
|
|
|
const sut = Sut();
|
|
|
|
const keys = [ 'foo', 'bar', 'baz' ];
|
|
|
|
|
|
|
|
keys.forEach( key => sut.add( key ) );
|
|
|
|
|
|
|
|
// should remove all items
|
2016-12-30 13:58:06 -05:00
|
|
|
return sut.clear().then( () =>
|
|
|
|
{
|
|
|
|
return Promise.all(
|
|
|
|
keys.map( key => {
|
|
|
|
expect( sut.get( key ) )
|
|
|
|
.to.eventually.be.rejected
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
} );
|
2016-12-21 11:20:45 -05:00
|
|
|
} );
|
2017-01-27 12:34:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
it( 'returns self with promise', () =>
|
|
|
|
{
|
|
|
|
const sut = Sut();
|
|
|
|
|
|
|
|
return expect(
|
|
|
|
sut.clear()
|
|
|
|
).to.eventually.equal( sut );
|
|
|
|
} );
|
2016-12-21 11:20:45 -05:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'with mixin', () =>
|
|
|
|
{
|
2017-08-24 14:27:01 -04:00
|
|
|
it( 'allows overriding #add', done =>
|
2016-12-21 11:20:45 -05:00
|
|
|
{
|
|
|
|
const expected_key = 'foo';
|
|
|
|
const expected_value = {};
|
|
|
|
|
|
|
|
Sut.use(
|
|
|
|
Trait.extend( Sut,
|
|
|
|
{
|
|
|
|
'override add'( key, value )
|
|
|
|
{
|
|
|
|
expect( key ).to.equal( expected_key );
|
|
|
|
expect( value ).to.equal( expected_value );
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
)().add( expected_key, expected_value );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2017-08-24 14:27:01 -04:00
|
|
|
it( "allows overriding #populate", () =>
|
|
|
|
{
|
|
|
|
const obj = {};
|
|
|
|
let called = false;
|
|
|
|
|
|
|
|
return Sut.use(
|
|
|
|
Trait.extend( Sut,
|
|
|
|
{
|
|
|
|
'override populate'( given )
|
|
|
|
{
|
|
|
|
expect( given ).to.equal( obj );
|
|
|
|
called = true;
|
|
|
|
|
|
|
|
return Promise.resolve( true );
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
)().populate( obj )
|
|
|
|
.then( () => expect( called ).to.equal( true ) );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'allows overriding #get', done =>
|
2016-12-21 11:20:45 -05:00
|
|
|
{
|
|
|
|
const expected_key = 'bar';
|
|
|
|
|
|
|
|
Sut.use(
|
|
|
|
Trait.extend( Sut,
|
|
|
|
{
|
|
|
|
'override get'( key )
|
|
|
|
{
|
|
|
|
expect( key ).to.equal( expected_key );
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
)().get( expected_key );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2017-08-24 14:27:01 -04:00
|
|
|
it( 'allows overriding #clear', done =>
|
2016-12-21 11:20:45 -05:00
|
|
|
{
|
|
|
|
Sut.use(
|
|
|
|
Trait.extend( Sut,
|
|
|
|
{
|
|
|
|
'override clear'( key )
|
|
|
|
{
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
)().clear();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( '#reduce', () =>
|
|
|
|
{
|
|
|
|
it( 'folds each stored item', () =>
|
|
|
|
{
|
|
|
|
const StubSut = Sut.extend(
|
|
|
|
{
|
|
|
|
sum()
|
|
|
|
{
|
|
|
|
return this.reduce(
|
|
|
|
( accum, item, key ) =>
|
|
|
|
{
|
|
|
|
// correct key for item?
|
|
|
|
expect( item ).to.equal( vals[ key ] );
|
|
|
|
|
|
|
|
return accum + item;
|
|
|
|
},
|
|
|
|
5
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
const sut = StubSut();
|
|
|
|
const vals = {
|
|
|
|
one: 1,
|
|
|
|
two: 2,
|
|
|
|
three: 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys( vals ).forEach(
|
|
|
|
( key, i ) => sut.add( key, vals[ key ] )
|
|
|
|
);
|
|
|
|
|
|
|
|
// implicitly tests initial
|
2016-12-30 13:58:06 -05:00
|
|
|
return expect( sut.sum() )
|
|
|
|
to.equal( 11 );
|
2016-12-21 11:20:45 -05:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|