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: {
|
2018-11-12 14:53:58 -05:00
|
|
|
groups: {},
|
|
|
|
qtypes: {
|
|
|
|
a: { type: "noyes" },
|
|
|
|
b: { type: "noyes" },
|
|
|
|
},
|
2018-06-08 15:43:51 -04:00
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
Something: [ "a", "b" ]
|
2018-11-19 11:10:16 -05:00
|
|
|
},
|
|
|
|
doc_data: {},
|
|
|
|
expected: {
|
|
|
|
a: [ "one" ],
|
|
|
|
b: [ "two" ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// for ancient versions of liza-proguic (before it was even called
|
|
|
|
// "liza")
|
|
|
|
{
|
|
|
|
label: "initializes defaults for ancient data representations",
|
|
|
|
defaults: { a: "one", b: "two" },
|
|
|
|
meta: {
|
|
|
|
groups: {},
|
|
|
|
qtypes: {
|
|
|
|
a: "noyes",
|
|
|
|
b: "noyes",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
Something: [ "a", "b" ]
|
2018-06-08 15:43:51 -04:00
|
|
|
},
|
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: {
|
2018-11-12 14:53:58 -05:00
|
|
|
groups: {},
|
|
|
|
qtypes: {
|
|
|
|
foo: { type: "noyes" },
|
|
|
|
bar: { type: "noyes" },
|
|
|
|
},
|
2018-06-08 15:43:51 -04:00
|
|
|
},
|
|
|
|
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: {
|
2018-11-12 14:53:58 -05:00
|
|
|
groups: {},
|
|
|
|
qtypes: {
|
|
|
|
bar: { type: "text" },
|
|
|
|
},
|
2018-06-19 09:57:50 -04:00
|
|
|
},
|
|
|
|
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: {
|
2018-11-12 14:53:58 -05:00
|
|
|
groups: {},
|
|
|
|
qtypes: {
|
|
|
|
foo: { type: "text" },
|
|
|
|
},
|
2018-06-08 15:43:51 -04:00
|
|
|
},
|
|
|
|
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
|
|
|
|
}
|
2018-11-12 14:53:58 -05:00
|
|
|
},
|
|
|
|
qtypes: {
|
|
|
|
foo: { type: "text" },
|
|
|
|
},
|
2018-06-08 15:43:51 -04:00
|
|
|
},
|
|
|
|
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
|
|
|
|
}
|
2018-11-12 14:53:58 -05:00
|
|
|
},
|
|
|
|
qtypes: {
|
|
|
|
foo: { type: "text" },
|
|
|
|
},
|
2018-06-08 15:43:51 -04:00
|
|
|
},
|
|
|
|
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
|
|
|
|
}
|
2018-11-12 14:53:58 -05:00
|
|
|
},
|
|
|
|
qtypes: {
|
|
|
|
foo: { type: "text" },
|
|
|
|
},
|
2018-06-08 15:43:51 -04:00
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
Something: [ "foo" ]
|
|
|
|
},
|
|
|
|
doc_data: {
|
|
|
|
foo: [ "1", "2", "3", "4" ],
|
|
|
|
},
|
|
|
|
expected: {
|
|
|
|
foo: [ "1", "2", "3", "4", "init" ],
|
|
|
|
},
|
|
|
|
},
|
2018-11-12 14:53:58 -05:00
|
|
|
{
|
|
|
|
label: "ignores undefined types",
|
|
|
|
defaults: { foo: "default" },
|
|
|
|
meta: {
|
|
|
|
groups: {},
|
|
|
|
qtypes: {
|
|
|
|
foo: { type: "undefined" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
groupExclusiveFields: {
|
|
|
|
Something: [ "foo" ]
|
|
|
|
},
|
|
|
|
doc_data: {},
|
|
|
|
expected: {},
|
|
|
|
},
|
2018-06-08 15:43:51 -04:00
|
|
|
].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 = {
|
2018-11-12 14:53:58 -05:00
|
|
|
id: "foo",
|
|
|
|
defaults: defaults,
|
|
|
|
meta: meta,
|
|
|
|
groupExclusiveFields: groupExclusiveFields
|
2017-08-24 13:03:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return expect( sut.init( program, doc_data ) )
|
|
|
|
.to.eventually.deep.equal( expected );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|