2017-02-20 12:08:55 -05:00
|
|
|
/**
|
|
|
|
* Test of staging key/value store
|
|
|
|
*
|
2017-06-08 14:46:51 -04:00
|
|
|
* Copyright (C) 2017 R-T Specialty, LLC.
|
2017-02-20 12:08:55 -05:00
|
|
|
*
|
|
|
|
* This file is part of liza.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* @todo This needs tests for the rest of StagingBucket
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { Class } = require( 'easejs' );
|
|
|
|
const root = require( '../../' );
|
|
|
|
const expect = require( 'chai' ).expect;
|
2017-08-11 11:55:14 -04:00
|
|
|
const sinon = require( 'sinon' );
|
2017-02-20 12:08:55 -05:00
|
|
|
|
|
|
|
const {
|
|
|
|
Bucket,
|
2017-09-05 16:33:46 -04:00
|
|
|
StagingBucket: Sut,
|
|
|
|
|
|
|
|
// TODO: decouple test from this
|
|
|
|
QuoteDataBucket,
|
2017-02-20 12:08:55 -05:00
|
|
|
} = root.bucket;
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'StagingBucket', () =>
|
|
|
|
{
|
2017-09-05 16:33:46 -04:00
|
|
|
it( 'pre-update event allows updating data before set', () =>
|
2017-02-20 12:08:55 -05:00
|
|
|
{
|
2017-09-05 16:33:46 -04:00
|
|
|
const sut = Sut( createStubBucket() );
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
const data = {
|
|
|
|
foo: [ 'bar', 'baz' ],
|
|
|
|
};
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
sut.on( 'preStagingUpdate', data =>
|
|
|
|
{
|
|
|
|
data.foo[ 1 ] = 'quux';
|
|
|
|
} );
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
// triggers setValues
|
|
|
|
sut.setValues( data );
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
expect( sut.getDataByName( 'foo' ) )
|
|
|
|
.to.deep.equal( [ 'bar', 'quux' ] );
|
|
|
|
} );
|
2017-02-20 12:08:55 -05:00
|
|
|
|
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz' ] },
|
|
|
|
update: { foo: [ 'bar', 'baz' ] },
|
|
|
|
is_change: false,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'baz' ] },
|
|
|
|
diff: {},
|
2017-02-20 12:08:55 -05:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
// actual changes
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz' ] },
|
|
|
|
update: { foo: [ 'change', 'baz' ] },
|
|
|
|
is_change: true,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'change', 'baz' ] },
|
|
|
|
diff: { foo: [ 'change' ] },
|
2017-02-20 12:08:55 -05:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz' ] },
|
|
|
|
update: { foo: [ 'bar', 'change' ] },
|
|
|
|
is_change: true,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'change' ] },
|
|
|
|
diff: { foo: [ , 'change' ] },
|
2017-02-20 12:08:55 -05:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz' ] },
|
|
|
|
update: { foo: [ undefined, 'change' ] },
|
|
|
|
is_change: true,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'change' ] },
|
|
|
|
diff: { foo: [ , 'change' ] },
|
2017-02-20 12:08:55 -05:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz' ] },
|
|
|
|
update: { foo: [ undefined, 'baz' ] },
|
|
|
|
is_change: false,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'baz' ] },
|
|
|
|
diff: {},
|
2017-02-20 12:08:55 -05:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz' ] },
|
|
|
|
update: { foo: [ 'bar', undefined ] },
|
|
|
|
is_change: false,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'baz' ] },
|
|
|
|
diff: {},
|
2017-02-20 12:08:55 -05:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz' ] },
|
|
|
|
update: { foo: [ 'bar', null ] },
|
|
|
|
is_change: true,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar' ] },
|
|
|
|
diff: { foo: [ , null ] },
|
2017-08-11 12:03:34 -04:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz' ] },
|
|
|
|
update: { foo: [ 'bar', 'baz', null ] },
|
|
|
|
is_change: false,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'baz' ] },
|
|
|
|
diff: {},
|
2017-08-11 12:03:34 -04:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz' ] },
|
|
|
|
update: { foo: [ 'bar', 'baz', 'quux' ] },
|
|
|
|
is_change: true,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'baz', 'quux' ] },
|
|
|
|
diff: { foo: [ , , 'quux' ]},
|
2017-08-11 12:03:34 -04:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz' ] },
|
|
|
|
update: { foo: [] },
|
|
|
|
is_change: false,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'baz' ] },
|
|
|
|
diff: {},
|
2017-08-11 12:03:34 -04:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// null not at end of set means unchanged
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz', 'quux' ] },
|
|
|
|
update: { foo: [ null, null, 'quux' ] },
|
|
|
|
is_change: false,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'baz', 'quux' ] },
|
|
|
|
diff: {},
|
2017-02-20 12:08:55 -05:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
// but the last one is
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz', 'quux' ] },
|
|
|
|
update: { foo: [ null, 'baz', null ] },
|
|
|
|
is_change: true,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'baz' ] },
|
|
|
|
diff: { foo: [ , , null ] },
|
2017-02-20 12:08:55 -05:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
// given a string of nulls, only the last one is terminating; the
|
|
|
|
// rest are interpreted as undefined (because JSON serializes
|
|
|
|
// undefined values to `null' -_-)
|
|
|
|
{
|
|
|
|
initial: { foo: [ 'bar', 'baz', 'quux' ] },
|
|
|
|
update: { foo: [ null, null, null ] },
|
|
|
|
is_change: true,
|
|
|
|
expected: {
|
|
|
|
data: { foo: [ 'bar', 'baz' ] },
|
|
|
|
diff: { foo: [ , , null ] },
|
2017-02-20 12:08:55 -05:00
|
|
|
},
|
2017-09-05 16:33:46 -04:00
|
|
|
},
|
|
|
|
].forEach( ( { initial, update, is_change, expected }, i ) =>
|
|
|
|
{
|
|
|
|
it( `pre-commit, properly processes diff and change (${i})`, () =>
|
|
|
|
{
|
|
|
|
const sut = Sut( createStubBucket() );
|
|
|
|
let called = false;
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
sut.setValues( initial );
|
|
|
|
|
|
|
|
expect( sut.getDiff() ).to.deep.equal( initial );
|
|
|
|
|
|
|
|
sut.on( 'preStagingUpdate', () => called = true );
|
|
|
|
sut.setValues( update );
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
expect( called ).to.equal( is_change );
|
|
|
|
|
|
|
|
if ( expected )
|
2017-02-20 12:08:55 -05:00
|
|
|
{
|
2017-09-05 16:33:46 -04:00
|
|
|
expect( sut.getData() ).to.deep.equal( expected.data );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( `post-commit, properly processes diff and change (${i})`, () =>
|
2017-02-20 12:08:55 -05:00
|
|
|
{
|
2017-09-05 16:33:46 -04:00
|
|
|
const sut = Sut( createStubBucket() );
|
|
|
|
let called = false;
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
sut.setValues( initial );
|
|
|
|
sut.commit();
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
sut.on( 'preStagingUpdate', () => called = true );
|
|
|
|
sut.setValues( update );
|
2017-02-20 12:08:55 -05:00
|
|
|
|
2017-09-05 16:33:46 -04:00
|
|
|
expect( called ).to.equal( is_change );
|
|
|
|
|
|
|
|
if ( expected )
|
|
|
|
{
|
|
|
|
expect( sut.getData() ).to.deep.equal( expected.data );
|
|
|
|
expect( sut.getDiff() ).to.deep.equal( expected.diff );
|
|
|
|
}
|
2017-02-20 12:08:55 -05:00
|
|
|
} );
|
|
|
|
} );
|
2017-08-11 11:55:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
describe( "#setCommittedValues", () =>
|
|
|
|
{
|
|
|
|
it( "bypasses staging bucket without no bypass flag", () =>
|
|
|
|
{
|
|
|
|
const b = createStubBucket();
|
|
|
|
const bmock = sinon.mock( b );
|
|
|
|
const data = { foo: [ "bar" ] };
|
|
|
|
const sut = Sut( b );
|
|
|
|
|
|
|
|
bmock.expects( 'setValues' )
|
|
|
|
.once()
|
|
|
|
.withExactArgs( data );
|
|
|
|
|
|
|
|
sut.setCommittedValues( data );
|
|
|
|
|
|
|
|
// no diff if bypassed
|
|
|
|
expect( sut.getDiff() ).to.deep.equal( {} );
|
|
|
|
|
|
|
|
bmock.verify();
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( "does not bypasses staging bucket with no bypass flag", () =>
|
|
|
|
{
|
|
|
|
const b = createStubBucket();
|
|
|
|
const bmock = sinon.mock( b );
|
|
|
|
const data = { foo: [ "bar" ] };
|
|
|
|
const sut = Sut( b );
|
|
|
|
|
|
|
|
bmock.expects( 'setValues' ).never();
|
|
|
|
|
|
|
|
sut.forbidBypass();
|
|
|
|
sut.setCommittedValues( data );
|
|
|
|
|
|
|
|
// should have been staged
|
|
|
|
expect( sut.getDiff() ).to.deep.equal( data );
|
|
|
|
|
|
|
|
bmock.verify();
|
|
|
|
} );
|
|
|
|
} );
|
2017-02-20 12:08:55 -05:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
function createStubBucket( bucket_obj )
|
|
|
|
{
|
2017-09-05 16:33:46 -04:00
|
|
|
return QuoteDataBucket();
|
2017-02-20 12:08:55 -05:00
|
|
|
}
|