2017-08-24 13:03:44 -04:00
|
|
|
/**
|
|
|
|
* Tests ProgramInit
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 R-T Specialty, LLC.
|
|
|
|
*
|
|
|
|
* 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';
|
|
|
|
|
|
|
|
const chai = require( 'chai' );
|
|
|
|
const expect = chai.expect;
|
|
|
|
const { ProgramInit: Sut } = require( '../../' ).program;
|
|
|
|
|
|
|
|
chai.use( require( 'chai-as-promised' ) );
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'ProgramInit', () =>
|
|
|
|
{
|
|
|
|
[
|
|
|
|
{
|
|
|
|
label: "initializes defaults",
|
|
|
|
defaults: { a: "one", b: "two" },
|
2018-06-08 15:43:51 -04:00
|
|
|
meta: {
|
|
|
|
groups: {}
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
Something: [ "a", "b" ]
|
|
|
|
},
|
2017-08-24 13:03:44 -04:00
|
|
|
doc_data: {},
|
|
|
|
expected: {
|
|
|
|
a: [ "one" ],
|
|
|
|
b: [ "two" ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "does nothing with no data or defaults",
|
|
|
|
defaults: {},
|
2018-06-08 15:43:51 -04:00
|
|
|
meta: {
|
|
|
|
groups: {}
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {},
|
2017-08-24 13:03:44 -04:00
|
|
|
doc_data: {},
|
|
|
|
expected: {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "produces empty object given undefined data",
|
|
|
|
defaults: {},
|
2018-06-08 15:43:51 -04:00
|
|
|
meta: {
|
|
|
|
groups: {}
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {},
|
2017-08-24 13:03:44 -04:00
|
|
|
doc_data: undefined,
|
|
|
|
expected: {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "keeps existing data with defaults",
|
2018-06-12 15:20:14 -04:00
|
|
|
defaults: {
|
|
|
|
foo: "init",
|
|
|
|
bar: "test"
|
|
|
|
},
|
2018-06-08 15:43:51 -04:00
|
|
|
meta: {
|
|
|
|
groups: {}
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
Something: [ "foo" ],
|
|
|
|
SomethingElse: [ "bar" ]
|
|
|
|
},
|
2017-08-24 13:03:44 -04:00
|
|
|
doc_data: { bar: [ "baz" ] },
|
|
|
|
expected: {
|
|
|
|
foo: [ "init" ],
|
|
|
|
bar: [ "baz" ],
|
|
|
|
},
|
|
|
|
},
|
2018-06-19 09:57:50 -04:00
|
|
|
{
|
|
|
|
label: "keeps existing data with no defaults",
|
|
|
|
defaults: {},
|
|
|
|
meta: {
|
|
|
|
groups: {}
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
SomethingElse: [ "bar" ]
|
|
|
|
},
|
|
|
|
doc_data: { bar: [ "baz" ] },
|
|
|
|
expected: {
|
|
|
|
bar: [ "baz" ],
|
|
|
|
},
|
|
|
|
},
|
2017-08-24 13:03:44 -04:00
|
|
|
{
|
|
|
|
label: "does not overwrite existing data with defaults",
|
|
|
|
defaults: { foo: "init" },
|
2018-06-08 15:43:51 -04:00
|
|
|
meta: {
|
|
|
|
groups: {}
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
Something: [ "foo" ]
|
|
|
|
},
|
2017-08-24 13:03:44 -04:00
|
|
|
doc_data: { foo: [ "bar" ] },
|
|
|
|
expected: {
|
|
|
|
foo: [ "bar" ],
|
|
|
|
},
|
|
|
|
},
|
2018-06-08 15:43:51 -04:00
|
|
|
{
|
|
|
|
label: "does not overwrite existing data with defaults and multiple rows",
|
|
|
|
defaults: { foo: "init" },
|
|
|
|
meta: {
|
|
|
|
groups: {
|
|
|
|
Something: {
|
|
|
|
min: 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
Something: [ "foo" ]
|
|
|
|
},
|
|
|
|
doc_data: { foo: [ "bar", "baz", "test" ] },
|
|
|
|
expected: {
|
|
|
|
foo: [ "bar", "baz", "test" ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "initializes with default bucket data",
|
|
|
|
defaults: { foo: "init" },
|
|
|
|
meta: {
|
|
|
|
groups: {
|
|
|
|
Something: {
|
|
|
|
min: 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
Something: [ "foo" ]
|
|
|
|
},
|
|
|
|
doc_data: {},
|
|
|
|
expected: {
|
|
|
|
foo: [ "init", "init", "init", "init", "init" ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "fix missing bucket data values",
|
|
|
|
defaults: { foo: "init" },
|
|
|
|
meta: {
|
|
|
|
groups: {
|
|
|
|
Something: {
|
|
|
|
min: 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
Something: [ "foo" ]
|
|
|
|
},
|
|
|
|
doc_data: {
|
|
|
|
foo: [ "1", "2", "3", "4" ],
|
|
|
|
},
|
|
|
|
expected: {
|
|
|
|
foo: [ "1", "2", "3", "4", "init" ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
].forEach( ({ label, doc_data, id, defaults, meta, groupExclusiveFields, expected }) =>
|
2017-08-24 13:03:44 -04:00
|
|
|
{
|
|
|
|
it( label, () =>
|
|
|
|
{
|
|
|
|
const sut = Sut( null );
|
|
|
|
|
|
|
|
const program = {
|
|
|
|
id: "foo",
|
|
|
|
defaults: defaults,
|
2018-06-08 15:43:51 -04:00
|
|
|
meta: meta,
|
|
|
|
groupExclusiveFields : groupExclusiveFields
|
2017-08-24 13:03:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return expect( sut.init( program, doc_data ) )
|
|
|
|
.to.eventually.deep.equal( expected );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|