2016-04-12 12:02:00 -04:00
|
|
|
/**
|
|
|
|
* Test field validity monitor
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 LoVullo Associates, Inc.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2016-04-20 11:06:54 -04:00
|
|
|
var root = require( '../../' ),
|
|
|
|
Sut = root.validate.ValidStateMonitor,
|
|
|
|
expect = require( 'chai' ).expect,
|
|
|
|
Failure = root.validate.Failure,
|
|
|
|
Field = root.field.BucketField;
|
|
|
|
|
2016-04-12 12:02:00 -04:00
|
|
|
|
|
|
|
var nocall = function( type )
|
|
|
|
{
|
|
|
|
return function()
|
|
|
|
{
|
|
|
|
throw Error( type + ' should not be called' );
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-04-20 11:06:54 -04:00
|
|
|
var mkfail = function( name, arr )
|
|
|
|
{
|
|
|
|
return arr.map( function( value, i )
|
|
|
|
{
|
|
|
|
return ( value === undefined )
|
|
|
|
? undefined
|
|
|
|
: Failure( Field( name, i ), value );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2016-04-12 12:02:00 -04:00
|
|
|
|
|
|
|
describe( 'ValidStateMonitor', function()
|
|
|
|
{
|
|
|
|
describe( '#update', function()
|
|
|
|
{
|
|
|
|
it( 'does nothing with no data or failures', function()
|
|
|
|
{
|
|
|
|
Sut()
|
|
|
|
.on( 'failure', nocall( 'failure' ) )
|
|
|
|
.on( 'fix', nocall( 'fix' ) )
|
|
|
|
.update( {}, {} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'does nothing with data but no failures', function()
|
|
|
|
{
|
|
|
|
Sut()
|
|
|
|
.on( 'failure', nocall( 'failure' ) )
|
|
|
|
.on( 'fix', nocall( 'fix' ) )
|
2016-04-20 11:06:54 -04:00
|
|
|
.update( { foo: mkfail( 'foo', [ 'bar' ] ) }, {} );
|
2016-04-12 12:02:00 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
// the failure should contain the value that caused it, so we do not
|
|
|
|
// need the data
|
|
|
|
describe( 'given failures', function()
|
|
|
|
{
|
|
|
|
it( 'marks failures even when given no data', function( done )
|
|
|
|
{
|
2016-04-20 11:06:54 -04:00
|
|
|
var fail = mkfail( 'foo', [ 'bar', 'baz' ] );
|
|
|
|
|
2016-04-12 12:02:00 -04:00
|
|
|
Sut()
|
2016-04-20 11:06:54 -04:00
|
|
|
.on( 'failure', function( failures )
|
2016-04-12 12:02:00 -04:00
|
|
|
{
|
|
|
|
expect( failures )
|
2016-04-20 11:06:54 -04:00
|
|
|
.to.deep.equal( { foo: [ fail[ 0 ], fail[ 1 ] ] } );
|
2016-04-12 12:02:00 -04:00
|
|
|
done();
|
|
|
|
} )
|
|
|
|
.on( 'fix', nocall( 'fix' ) )
|
2016-04-20 11:06:54 -04:00
|
|
|
.update( {}, { foo: fail } );
|
2016-04-12 12:02:00 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'marks failures with index gaps', function( done )
|
|
|
|
{
|
2016-04-20 11:06:54 -04:00
|
|
|
var fail = mkfail( 'foo', [ undefined, 'baz' ] );
|
|
|
|
|
2016-04-12 12:02:00 -04:00
|
|
|
Sut()
|
2016-04-20 11:06:54 -04:00
|
|
|
.on( 'failure', function( failures )
|
2016-04-12 12:02:00 -04:00
|
|
|
{
|
|
|
|
expect( failures )
|
2016-04-20 11:06:54 -04:00
|
|
|
.to.deep.equal( { foo: [ undefined, fail[ 1 ] ] } );
|
2016-04-12 12:02:00 -04:00
|
|
|
done();
|
|
|
|
} )
|
|
|
|
.on( 'fix', nocall( 'fix' ) )
|
2016-04-20 11:06:54 -04:00
|
|
|
.update( {}, { foo: fail } );
|
2016-04-12 12:02:00 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'retains past failures when setting new', function( done )
|
|
|
|
{
|
2016-04-20 11:06:54 -04:00
|
|
|
var sut = Sut(),
|
|
|
|
fail = mkfail( 'foo', [ 'bar', 'baz' ] );
|
2016-04-12 12:02:00 -04:00
|
|
|
|
|
|
|
var test_first = function( failures )
|
|
|
|
{
|
|
|
|
expect( failures )
|
2016-04-20 11:06:54 -04:00
|
|
|
.to.deep.equal( { foo: [ undefined, fail[ 1 ] ] } );
|
2016-04-12 12:02:00 -04:00
|
|
|
|
|
|
|
sut.once( 'failure', test_second );
|
|
|
|
};
|
|
|
|
|
|
|
|
var test_second = function( failures )
|
|
|
|
{
|
|
|
|
expect( failures )
|
2016-04-20 11:06:54 -04:00
|
|
|
.to.deep.equal( { foo: [ fail[ 0 ], fail[ 1 ] ] } );
|
2016-04-12 12:02:00 -04:00
|
|
|
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
sut
|
|
|
|
.once( 'failure', test_first )
|
|
|
|
.on( 'fix', nocall( 'fix' ) )
|
2016-04-20 11:06:54 -04:00
|
|
|
.update( {}, { foo: [ undefined, fail[ 1 ] ] } )
|
|
|
|
.update( {}, { foo: [ fail[ 0 ] ] } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
// deprecated
|
|
|
|
it( 'accepts failures as string for BC', function( done )
|
|
|
|
{
|
|
|
|
var fail = [ 'foo', 'bar' ];
|
|
|
|
|
|
|
|
Sut()
|
|
|
|
.on( 'failure', function( failures )
|
|
|
|
{
|
|
|
|
expect( failures )
|
|
|
|
.to.deep.equal( { foo: fail } );
|
|
|
|
done();
|
|
|
|
} )
|
|
|
|
.on( 'fix', nocall( 'fix' ) )
|
|
|
|
.update( {}, { foo: fail } );
|
2016-04-12 12:02:00 -04:00
|
|
|
} );
|
2016-04-28 11:53:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
it( 'does not discard existing failures', function( done )
|
|
|
|
{
|
|
|
|
var sut = Sut();
|
|
|
|
|
|
|
|
// has both failures
|
|
|
|
var fail1 = Failure(
|
|
|
|
Field( 'foo', 0 ),
|
|
|
|
'',
|
|
|
|
[ Field( 'cause1', 0 ), Field( 'cause2', 0 ) ]
|
|
|
|
);
|
|
|
|
|
|
|
|
// has only one of the two failures
|
|
|
|
var fail2 = Failure(
|
|
|
|
Field( 'foo', 1 ),
|
|
|
|
'',
|
|
|
|
[ Field( 'cause2', 1 ) ]
|
|
|
|
);
|
|
|
|
|
|
|
|
// the second failure has fewer causes than the first;
|
|
|
|
// we need to make sure that it doesn't overwrite,
|
|
|
|
// leading to fewer caues
|
|
|
|
sut
|
|
|
|
.update( {}, { foo: [ fail1 ] } )
|
|
|
|
.update( {}, { foo: [ fail2 ] } );
|
|
|
|
|
|
|
|
// if cause1 wasn't removed, then this will fix it
|
|
|
|
sut
|
|
|
|
.once( 'fix', function( fixed )
|
|
|
|
{
|
|
|
|
expect( fixed )
|
|
|
|
.to.deep.equal( { foo: [ 'causefix1' ] } );
|
|
|
|
|
|
|
|
// and then we should have no failures
|
|
|
|
expect( sut.hasFailures() ).to.be.false;
|
|
|
|
|
|
|
|
done();
|
|
|
|
} )
|
|
|
|
.update(
|
|
|
|
{ foo: [ 'moo' ], cause1: [ 'causefix1' ] },
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
} );
|
2016-04-12 12:02:00 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'given data with absence of failure', function()
|
|
|
|
{
|
|
|
|
it( 'removes non-failures if field is present', function( done )
|
|
|
|
{
|
2016-04-20 11:06:54 -04:00
|
|
|
var data = { foo: [ 'bardata', 'baz' ] },
|
|
|
|
fail = mkfail( 'foo', [ 'bar', 'baz' ] );
|
2016-04-12 12:02:00 -04:00
|
|
|
|
|
|
|
Sut()
|
|
|
|
.on( 'fix', function( fixed )
|
|
|
|
{
|
|
|
|
expect( fixed )
|
|
|
|
.to.deep.equal( { foo: [ 'bardata' ] } );
|
|
|
|
done();
|
|
|
|
} )
|
2016-04-20 11:06:54 -04:00
|
|
|
.update( data, { foo: [ fail[ 0 ], fail[ 1 ] ] } )
|
|
|
|
.update( data, { foo: [ undefined, fail[ 1 ] ] } );
|
2016-04-12 12:02:00 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'keeps failures if field is missing', function( done )
|
|
|
|
{
|
2016-04-20 11:06:54 -04:00
|
|
|
var data = { bar: [ 'baz', 'quux' ] },
|
|
|
|
fail_foo = mkfail( 'foo', [ 'bar', 'baz' ] ),
|
|
|
|
fail_bar = mkfail( 'bar', [ 'moo', 'cow' ] );
|
2016-04-12 12:02:00 -04:00
|
|
|
|
|
|
|
Sut()
|
|
|
|
.on( 'fix', function( fixed )
|
|
|
|
{
|
|
|
|
expect( fixed )
|
|
|
|
.to.deep.equal( { bar: [ 'baz', 'quux' ] } );
|
|
|
|
done();
|
|
|
|
} )
|
|
|
|
.update( data, {
|
2016-04-20 11:06:54 -04:00
|
|
|
foo: fail_foo, // does not exist in data
|
|
|
|
bar: fail_bar,
|
2016-04-12 12:02:00 -04:00
|
|
|
} )
|
|
|
|
.update( data, {} );
|
|
|
|
} );
|
2016-04-19 11:29:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
it( 'does not trigger failure event for existing', function()
|
|
|
|
{
|
|
|
|
var called = 0;
|
|
|
|
|
|
|
|
Sut()
|
|
|
|
.on( 'failure', function()
|
|
|
|
{
|
|
|
|
called++;
|
|
|
|
} )
|
2016-04-20 11:06:54 -04:00
|
|
|
.update( {}, { foo: mkfail( 'foo', [ 'bar' ] ) } )
|
2016-04-19 11:29:14 -04:00
|
|
|
.update( {}, {} ); // do not trigger failure event
|
|
|
|
|
|
|
|
expect( called ).to.equal( 1 );
|
|
|
|
} );
|
2016-04-20 12:11:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
describe( 'given a cause', function()
|
|
|
|
{
|
|
|
|
it( 'considers when recognizing fix', function( done )
|
|
|
|
{
|
|
|
|
// same index
|
|
|
|
var data = { cause: [ 'bar' ] },
|
|
|
|
field = Field( 'foo', 0 ),
|
|
|
|
cause = Field( 'cause', 0 ),
|
2016-04-21 16:23:41 -04:00
|
|
|
fail = Failure( field, 'reason', [ cause ] );
|
2016-04-20 12:11:17 -04:00
|
|
|
|
|
|
|
Sut()
|
|
|
|
.on( 'fix', function( fixed )
|
|
|
|
{
|
|
|
|
expect( fixed )
|
|
|
|
.to.deep.equal( { foo: [ 'bar' ] } );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} )
|
|
|
|
.update( data, { foo: [ fail ] } )
|
|
|
|
.update( data, {} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'considers different cause index', function( done )
|
|
|
|
{
|
|
|
|
// different index
|
|
|
|
var data = { cause: [ undefined, 'bar' ] },
|
|
|
|
field = Field( 'foo', 0 ),
|
|
|
|
cause = Field( 'cause', 1 ),
|
2016-04-21 16:23:41 -04:00
|
|
|
fail = Failure( field, 'reason', [ cause ] );
|
|
|
|
|
|
|
|
Sut()
|
|
|
|
.on( 'fix', function( fixed )
|
|
|
|
{
|
|
|
|
expect( fixed )
|
|
|
|
.to.deep.equal( { foo: [ 'bar' ] } );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} )
|
|
|
|
.update( data, { foo: [ fail ] } )
|
|
|
|
.update( data, {} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'considers any number of causes', function( done )
|
|
|
|
{
|
|
|
|
// different index
|
|
|
|
var data = { cause_fix: [ undefined, 'bar' ] },
|
|
|
|
field = Field( 'foo', 0 ),
|
|
|
|
cause1 = Field( 'cause_no', 1 ),
|
|
|
|
cause2 = Field( 'cause_fix', 1 ),
|
|
|
|
fail = Failure(
|
|
|
|
field,
|
|
|
|
'reason',
|
|
|
|
[ cause1, cause2 ]
|
|
|
|
);
|
2016-04-20 12:11:17 -04:00
|
|
|
|
|
|
|
Sut()
|
|
|
|
.on( 'fix', function( fixed )
|
|
|
|
{
|
|
|
|
expect( fixed )
|
|
|
|
.to.deep.equal( { foo: [ 'bar' ] } );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} )
|
|
|
|
.update( data, { foo: [ fail ] } )
|
|
|
|
.update( data, {} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'recognizes non-fix', function()
|
|
|
|
{
|
|
|
|
// no cause data
|
2016-04-21 16:23:41 -04:00
|
|
|
var data = { noncause: [ undefined, 'bar' ] },
|
|
|
|
field = Field( 'foo', 0 ),
|
|
|
|
cause1 = Field( 'cause', 1 ),
|
|
|
|
cause2 = Field( 'cause', 2 ),
|
|
|
|
fail = Failure(
|
|
|
|
field,
|
|
|
|
'reason',
|
|
|
|
[ cause1, cause2 ]
|
|
|
|
);
|
2016-04-20 12:11:17 -04:00
|
|
|
|
|
|
|
Sut()
|
|
|
|
.on( 'fix', nocall )
|
|
|
|
.update( data, { foo: [ fail ] } )
|
|
|
|
.update( data, {} );
|
|
|
|
} );
|
|
|
|
} );
|
2016-04-12 12:02:00 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'can emit both failure and fix', function( done )
|
|
|
|
{
|
2016-04-20 11:06:54 -04:00
|
|
|
var data = { bar: [ 'baz', 'quux' ] },
|
|
|
|
fail_foo = mkfail( 'foo', [ 'bar' ] );
|
2016-04-12 12:02:00 -04:00
|
|
|
|
|
|
|
Sut()
|
|
|
|
.update( data, {
|
2016-04-20 11:06:54 -04:00
|
|
|
bar: mkfail( 'bar', [ 'moo', 'cow' ] ) // fail
|
2016-04-12 12:02:00 -04:00
|
|
|
} )
|
|
|
|
.on( 'failure', function( failed )
|
|
|
|
{
|
|
|
|
expect( failed )
|
|
|
|
.to.deep.equal( {
|
2016-04-20 11:06:54 -04:00
|
|
|
foo: fail_foo,
|
2016-04-12 12:02:00 -04:00
|
|
|
} );
|
|
|
|
} )
|
|
|
|
.on( 'fix', function( fixed )
|
|
|
|
{
|
|
|
|
expect( fixed )
|
|
|
|
.to.deep.equal( { bar: [ 'baz', 'quux' ] } );
|
|
|
|
done();
|
|
|
|
} )
|
|
|
|
.update( data, {
|
2016-04-20 11:06:54 -04:00
|
|
|
foo: fail_foo, // fail
|
2016-04-12 12:02:00 -04:00
|
|
|
// fixes bar
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( '#getFailures', function()
|
|
|
|
{
|
|
|
|
it( 'is empty when no failures exist', function()
|
|
|
|
{
|
|
|
|
expect(
|
|
|
|
Sut()
|
|
|
|
.getFailures()
|
|
|
|
).to.deep.equal( {} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'retrieves current failures', function()
|
|
|
|
{
|
2016-04-20 11:06:54 -04:00
|
|
|
var fail = mkfail( 'foo', [ 'fail' ] );
|
|
|
|
|
2016-04-12 12:02:00 -04:00
|
|
|
expect(
|
|
|
|
Sut()
|
2016-04-20 11:06:54 -04:00
|
|
|
.update( {}, { foo: fail } )
|
2016-04-12 12:02:00 -04:00
|
|
|
.getFailures()
|
2016-04-20 11:06:54 -04:00
|
|
|
).to.deep.equal( { foo: fail } );
|
2016-04-12 12:02:00 -04:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( '#hasFailures', function()
|
|
|
|
{
|
|
|
|
it( 'is false when no failures exist', function()
|
|
|
|
{
|
|
|
|
expect( Sut().hasFailures() )
|
|
|
|
.to.be.false;
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'is true when failures exist', function()
|
|
|
|
{
|
|
|
|
expect(
|
|
|
|
Sut()
|
2016-04-20 11:06:54 -04:00
|
|
|
.update( {}, { foo: mkfail( 'foo', [ 'bar' ] ) } )
|
2016-04-12 12:02:00 -04:00
|
|
|
.hasFailures()
|
|
|
|
).to.be.true;
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|