2017-07-10 09:58:21 -04:00
|
|
|
/**
|
|
|
|
* Tests ProgramQuoteCleaner
|
|
|
|
*
|
2018-10-21 23:41:58 -04:00
|
|
|
* Copyright (C) 2017, 2018 R-T Specialty, LLC.
|
2017-07-10 09:58:21 -04: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 Affero 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 Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { expect } = require( 'chai' );
|
|
|
|
const Sut = require( '../../../' ).server.quote.ProgramQuoteCleaner;
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'ProgramQuoteCleaner', () =>
|
|
|
|
{
|
2018-10-21 23:41:58 -04:00
|
|
|
describe( "group cleaning", () =>
|
|
|
|
{
|
|
|
|
[
|
|
|
|
{
|
|
|
|
label: "expands indexes of linked and non-linked groups",
|
|
|
|
|
|
|
|
group_index: {
|
|
|
|
one: 'field11', // linked
|
|
|
|
two: 'field11', // linked
|
|
|
|
three: 'field31',
|
|
|
|
},
|
|
|
|
|
|
|
|
exclusive: {
|
|
|
|
one: [ "field11", "field12" ],
|
|
|
|
two: [ "field21", "field22" ],
|
2018-11-12 14:53:58 -05:00
|
|
|
three: [ "field31", "field32", "unknown_ignore_me" ],
|
2018-10-21 23:41:58 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
defaults: {
|
|
|
|
field12: "12default",
|
|
|
|
},
|
|
|
|
|
2018-11-12 14:53:58 -05:00
|
|
|
qtypes: {
|
|
|
|
"field11": { type: "text" },
|
|
|
|
"field12": { type: "text" },
|
|
|
|
"field21": { type: "text" },
|
|
|
|
"field22": { type: "text" },
|
|
|
|
"field31": { type: "text" },
|
2018-11-19 11:10:16 -05:00
|
|
|
|
|
|
|
// ancient pre-"liza" data representation
|
|
|
|
"field32": "text",
|
2018-11-12 14:53:58 -05:00
|
|
|
},
|
|
|
|
|
2018-10-21 23:41:58 -04:00
|
|
|
existing: {
|
|
|
|
"field11": [ "1", "", "3" ], // leader one, two
|
|
|
|
"field12": [ "a", "b" ],
|
|
|
|
"field21": [ "e" ],
|
|
|
|
"field22": [ "I", "II" ],
|
|
|
|
"field31": [ "i", "ii" ], // leader three
|
|
|
|
"field32": [ "x" ],
|
|
|
|
},
|
|
|
|
|
|
|
|
expected: {
|
|
|
|
"field12": [ , , "12default" ],
|
|
|
|
"field21": [ , "", "" ],
|
|
|
|
"field22": [ , , "" ],
|
|
|
|
"field32": [ , "" ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
].forEach( test =>
|
|
|
|
it( test.label, done =>
|
|
|
|
{
|
|
|
|
const quote = createStubQuote( test.existing, {} );
|
|
|
|
const program = createStubProgram( {} );
|
|
|
|
|
|
|
|
program.defaults = test.defaults;
|
|
|
|
program.groupIndexField = test.group_index;
|
|
|
|
program.groupExclusiveFields = test.exclusive;
|
2018-11-12 14:53:58 -05:00
|
|
|
program.meta.qtypes = test.qtypes;
|
2018-10-21 23:41:58 -04:00
|
|
|
|
|
|
|
const updates = {};
|
|
|
|
|
|
|
|
quote.setData = given =>
|
|
|
|
Object.keys( given ).forEach( k => updates[ k ] = given[ k ] );
|
|
|
|
|
|
|
|
Sut( program ).clean( quote, err =>
|
|
|
|
{
|
|
|
|
expect( err ).to.deep.equal( null );
|
|
|
|
expect( updates ).to.deep.equal( test.expected );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2017-07-10 09:58:21 -04:00
|
|
|
describe( "metadata cleaning", () =>
|
|
|
|
{
|
|
|
|
[
|
|
|
|
{
|
|
|
|
label: "populates all fields when empty",
|
|
|
|
existing: {},
|
|
|
|
fields: { foo: {}, bar: {} },
|
|
|
|
expected: { foo: [], bar: [] },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "populates only missing fields when non-empty",
|
|
|
|
existing: { foo: [ 1 ], baz: [ 2 ] },
|
|
|
|
fields: { foo: {}, bar: {} },
|
|
|
|
expected: { foo: [ 1 ], bar: [], baz: [ 2 ] },
|
|
|
|
},
|
2017-07-14 15:19:35 -04:00
|
|
|
{
|
|
|
|
label: "does nothing with no fields",
|
|
|
|
existing: { foo: [ 1 ], baz: [ 2 ] },
|
|
|
|
fields: {},
|
|
|
|
expected: { foo: [ 1 ], baz: [ 2 ] },
|
|
|
|
},
|
2017-07-10 09:58:21 -04:00
|
|
|
].forEach( ( { label, existing, fields, expected } ) =>
|
|
|
|
it( label, done =>
|
|
|
|
{
|
2018-10-21 23:41:58 -04:00
|
|
|
const quote = createStubQuote( {}, existing );
|
2017-07-10 09:58:21 -04:00
|
|
|
const program = createStubProgram( fields );
|
|
|
|
|
|
|
|
Sut( program ).clean( quote, err =>
|
|
|
|
{
|
|
|
|
expect( err ).to.equal( null );
|
|
|
|
expect( quote.getMetabucket().getData() )
|
|
|
|
.to.deep.equal( expected );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2018-10-21 23:41:58 -04:00
|
|
|
function createStubQuote( data, metadata )
|
2017-07-10 09:58:21 -04:00
|
|
|
{
|
|
|
|
return {
|
|
|
|
getProgramId: () => 'foo',
|
|
|
|
setData: () => {},
|
2018-10-21 23:41:58 -04:00
|
|
|
getDataByName: name => data[ name ],
|
2017-07-10 09:58:21 -04:00
|
|
|
getMetabucket: () => ( {
|
|
|
|
getDataByName: name => metadata[ name ],
|
|
|
|
getData: () => metadata,
|
|
|
|
setValues: data =>
|
|
|
|
{
|
|
|
|
Object.keys( data ).forEach( field_name =>
|
|
|
|
metadata[ field_name ] = data[ field_name ]
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createStubProgram( meta_fields )
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
getId: () => 'foo',
|
|
|
|
meta: { fields: meta_fields },
|
2018-10-21 23:41:58 -04:00
|
|
|
defaults: {},
|
2017-07-10 09:58:21 -04:00
|
|
|
};
|
|
|
|
}
|