1
0
Fork 0

Avoid initializing bucket values for generated fields

master
Mike Gerwitz 2018-11-15 13:49:15 -05:00
commit b96479409c
6 changed files with 120 additions and 18 deletions

View File

@ -54,9 +54,12 @@ module.exports = Class( 'ProgramInit',
*/ */
'public init'( program, doc_data ) 'public init'( program, doc_data )
{ {
const defaults = program.defaults || {};
const data = doc_data || {}; const data = doc_data || {};
const groups = program.meta.groups;
const {
defaults = {},
meta: { groups = {}, qtypes = {} },
} = program;
Object.keys( program.groupExclusiveFields ).forEach( group => Object.keys( program.groupExclusiveFields ).forEach( group =>
{ {
@ -64,10 +67,17 @@ module.exports = Class( 'ProgramInit',
while ( i-- ) while ( i-- )
{ {
const field = program.groupExclusiveFields[ group ][ i ]; const field = program.groupExclusiveFields[ group ][ i ];
const default_value = defaults[ field ]; const default_value = defaults[ field ];
if ( !defaults.hasOwnProperty( field )) // generated questions with no types should never be part of
// the bucket
if ( !this._isKnownType( qtypes[ field ] ) )
{
continue;
}
if ( !defaults.hasOwnProperty( field ) )
{ {
continue; continue;
} }
@ -96,8 +106,25 @@ module.exports = Class( 'ProgramInit',
} }
} }
} }
}); } );
return Promise.resolve( data ); return Promise.resolve( data );
}, },
/**
* Determine whether question type QTYPE is known
*
* This assumes that the type is known unless QTYPE.type is "undefined".
*
* @param {Object} qtype type data for question
*
* @return {boolean} whether type is known
*/
'private _isKnownType'( qtype )
{
return qtype
&& ( typeof qtype.type === 'string' )
&& ( qtype.type !== 'undefined' );
},
} ); } );

View File

@ -536,6 +536,8 @@ module.exports = Class( 'Server' )
*/ */
'private _getDefaultBucket': function( program, quote_data ) 'private _getDefaultBucket': function( program, quote_data )
{ {
// TOOD: this duplicates some logic with ProgramQuoteCleaner; we
// probably do not need both of them
return this._progInit.init( program, quote_data.data ); return this._progInit.init( program, quote_data.data );
}, },

View File

@ -102,11 +102,19 @@ module.exports = Class( 'ProgramQuoteCleaner',
const update = {}; const update = {};
const group_fields = this._program.groupExclusiveFields[ group_id ]; const group_fields = this._program.groupExclusiveFields[ group_id ];
const qtypes = this._program.meta.qtypes || {};
group_fields.forEach( field => group_fields.forEach( field =>
{ {
const flen = ( quote.getDataByName( field ) || [] ).length; const flen = ( quote.getDataByName( field ) || [] ).length;
// generated questions with no types should never be part of
// the bucket
if ( !this._isKnownType( qtypes[ field ] ) )
{
return;
}
if ( flen >= length ) if ( flen >= length )
{ {
return; return;
@ -182,5 +190,22 @@ module.exports = Class( 'ProgramQuoteCleaner',
metabucket.setValues( { [field_name]: [] } ); metabucket.setValues( { [field_name]: [] } );
} ); } );
}, },
/**
* Determine whether question type QTYPE is known
*
* This assumes that the type is known unless QTYPE.type is "undefined".
*
* @param {Object} qtype type data for question
*
* @return {boolean} whether type is known
*/
'private _isKnownType'( qtype )
{
return qtype
&& ( typeof qtype.type === 'string' )
&& ( qtype.type !== 'undefined' );
},
} ); } );

View File

@ -510,7 +510,7 @@ module.exports = Class( 'TabbedGroupUi' ).extend( GroupUi,
// select the tab based on selection index // select the tab based on selection index
var index = this._bucket.getDataByName( this._defaultSelectionField )[0]; var index = this._bucket.getDataByName( this._defaultSelectionField )[0];
if( this._isEligibleTab( index ) ) if ( ( index !== undefined ) && this._isEligibleTab( index ) )
{ {
this._selectTab( index || 0 ); this._selectTab( index || 0 );
} }

View File

@ -35,7 +35,11 @@ describe( 'ProgramInit', () =>
label: "initializes defaults", label: "initializes defaults",
defaults: { a: "one", b: "two" }, defaults: { a: "one", b: "two" },
meta: { meta: {
groups: {} groups: {},
qtypes: {
a: { type: "noyes" },
b: { type: "noyes" },
},
}, },
groupExclusiveFields: { groupExclusiveFields: {
Something: [ "a", "b" ] Something: [ "a", "b" ]
@ -73,7 +77,11 @@ describe( 'ProgramInit', () =>
bar: "test" bar: "test"
}, },
meta: { meta: {
groups: {} groups: {},
qtypes: {
foo: { type: "noyes" },
bar: { type: "noyes" },
},
}, },
groupExclusiveFields: { groupExclusiveFields: {
Something: [ "foo" ], Something: [ "foo" ],
@ -89,7 +97,10 @@ describe( 'ProgramInit', () =>
label: "keeps existing data with no defaults", label: "keeps existing data with no defaults",
defaults: {}, defaults: {},
meta: { meta: {
groups: {} groups: {},
qtypes: {
bar: { type: "text" },
},
}, },
groupExclusiveFields: { groupExclusiveFields: {
SomethingElse: [ "bar" ] SomethingElse: [ "bar" ]
@ -103,7 +114,10 @@ describe( 'ProgramInit', () =>
label: "does not overwrite existing data with defaults", label: "does not overwrite existing data with defaults",
defaults: { foo: "init" }, defaults: { foo: "init" },
meta: { meta: {
groups: {} groups: {},
qtypes: {
foo: { type: "text" },
},
}, },
groupExclusiveFields: { groupExclusiveFields: {
Something: [ "foo" ] Something: [ "foo" ]
@ -121,7 +135,10 @@ describe( 'ProgramInit', () =>
Something: { Something: {
min: 3 min: 3
} }
} },
qtypes: {
foo: { type: "text" },
},
}, },
groupExclusiveFields: { groupExclusiveFields: {
Something: [ "foo" ] Something: [ "foo" ]
@ -139,7 +156,10 @@ describe( 'ProgramInit', () =>
Something: { Something: {
min: 5 min: 5
} }
} },
qtypes: {
foo: { type: "text" },
},
}, },
groupExclusiveFields: { groupExclusiveFields: {
Something: [ "foo" ] Something: [ "foo" ]
@ -157,7 +177,10 @@ describe( 'ProgramInit', () =>
Something: { Something: {
min: 5 min: 5
} }
} },
qtypes: {
foo: { type: "text" },
},
}, },
groupExclusiveFields: { groupExclusiveFields: {
Something: [ "foo" ] Something: [ "foo" ]
@ -169,6 +192,21 @@ describe( 'ProgramInit', () =>
foo: [ "1", "2", "3", "4", "init" ], foo: [ "1", "2", "3", "4", "init" ],
}, },
}, },
{
label: "ignores undefined types",
defaults: { foo: "default" },
meta: {
groups: {},
qtypes: {
foo: { type: "undefined" },
},
},
groupExclusiveFields: {
Something: [ "foo" ]
},
doc_data: {},
expected: {},
},
].forEach( ({ label, doc_data, id, defaults, meta, groupExclusiveFields, expected }) => ].forEach( ({ label, doc_data, id, defaults, meta, groupExclusiveFields, expected }) =>
{ {
it( label, () => it( label, () =>
@ -176,10 +214,10 @@ describe( 'ProgramInit', () =>
const sut = Sut( null ); const sut = Sut( null );
const program = { const program = {
id: "foo", id: "foo",
defaults: defaults, defaults: defaults,
meta: meta, meta: meta,
groupExclusiveFields : groupExclusiveFields groupExclusiveFields: groupExclusiveFields
}; };
return expect( sut.init( program, doc_data ) ) return expect( sut.init( program, doc_data ) )

View File

@ -42,13 +42,22 @@ describe( 'ProgramQuoteCleaner', () =>
exclusive: { exclusive: {
one: [ "field11", "field12" ], one: [ "field11", "field12" ],
two: [ "field21", "field22" ], two: [ "field21", "field22" ],
three: [ "field31", "field32" ], three: [ "field31", "field32", "unknown_ignore_me" ],
}, },
defaults: { defaults: {
field12: "12default", field12: "12default",
}, },
qtypes: {
"field11": { type: "text" },
"field12": { type: "text" },
"field21": { type: "text" },
"field22": { type: "text" },
"field31": { type: "text" },
"field32": { type: "text" },
},
existing: { existing: {
"field11": [ "1", "", "3" ], // leader one, two "field11": [ "1", "", "3" ], // leader one, two
"field12": [ "a", "b" ], "field12": [ "a", "b" ],
@ -74,6 +83,7 @@ describe( 'ProgramQuoteCleaner', () =>
program.defaults = test.defaults; program.defaults = test.defaults;
program.groupIndexField = test.group_index; program.groupIndexField = test.group_index;
program.groupExclusiveFields = test.exclusive; program.groupExclusiveFields = test.exclusive;
program.meta.qtypes = test.qtypes;
const updates = {}; const updates = {};