1
0
Fork 0

[DEV-6353] Accept push data in MongoDao

master
Austin Schaffer 2019-11-07 14:19:35 -05:00 committed by Schaffer, Austin
parent 48aa315c2a
commit 26c4367ead
6 changed files with 88 additions and 11 deletions

View File

@ -41,12 +41,14 @@ export declare class MongoServerDao implements ServerDao
* @param success - function to call on success * @param success - function to call on success
* @param failure - function to call if save fails * @param failure - function to call if save fails
* @param save_data - quote data to save (optional) * @param save_data - quote data to save (optional)
* @param push_data - quote data to push (optional)
*/ */
saveQuote( saveQuote(
quote: ServerSideQuote, quote: ServerSideQuote,
success: Callback, success: Callback,
failure: Callback, failure: Callback,
save_data: Record<string, any>, save_data: Record<string, any>,
push_data: Record<string, any>,
): this; ): this;

View File

@ -280,11 +280,12 @@ module.exports = Class( 'MongoServerDao' )
* @param Function success_callback function to call on success * @param Function success_callback function to call on success
* @param Function failure_callback function to call if save fails * @param Function failure_callback function to call if save fails
* @param Object save_data quote data to save (optional) * @param Object save_data quote data to save (optional)
* @param Object push_data quote data to push (optional)
* *
* @return MongoServerDao self to allow for method chaining * @return MongoServerDao self to allow for method chaining
*/ */
'public saveQuote': function( 'public saveQuote': function(
quote, success_callback, failure_callback, save_data quote, success_callback, failure_callback, save_data, push_data
) )
{ {
var dao = this; var dao = this;
@ -341,10 +342,15 @@ module.exports = Class( 'MongoServerDao' )
key => save_data[ 'meta.' + key ] = meta[ key ] key => save_data[ 'meta.' + key ] = meta[ key ]
); );
// do not push empty objects
const document = ( !push_data || !Object.keys( push_data ).length )
? { '$set': save_data }
: { '$set': save_data, '$push': push_data };
// update the quote data if it already exists (same id), otherwise // update the quote data if it already exists (same id), otherwise
// insert it // insert it
this._collection.update( { id: id }, this._collection.update( { id: id },
{ '$set': save_data }, document,
// create record if it does not yet exist // create record if it does not yet exist
{ upsert: true }, { upsert: true },

View File

@ -45,12 +45,14 @@ export interface ServerDao
* @param success - function to call on success * @param success - function to call on success
* @param failure - function to call if save fails * @param failure - function to call if save fails
* @param save_data - quote data to save (optional) * @param save_data - quote data to save (optional)
* @param push_data - quote data to push (optional)
*/ */
saveQuote( saveQuote(
quote: ServerSideQuote, quote: ServerSideQuote,
success: Callback, success: Callback,
failure: Callback, failure: Callback,
save_data: Record<string, any>, save_data: Record<string, any>,
push_data: Record<string, any>,
): this; ): this;

View File

@ -276,7 +276,7 @@ export class RatingService
// data, which may cause a race condition with the below merge call) // data, which may cause a race condition with the below merge call)
this._dao.saveQuote( quote, c, c, { this._dao.saveQuote( quote, c, c, {
ratedata: data, ratedata: data,
} ); }, {} );
} }
else else
{ {

View File

@ -30,7 +30,7 @@ describe( 'MongoServerDao', () =>
{ {
describe( '#saveQuote', () => describe( '#saveQuote', () =>
{ {
describe( "with no save data", () => describe( "with no save data or push data", () =>
{ {
it( "saves entire metabucket record individually", done => it( "saves entire metabucket record individually", done =>
{ {
@ -51,6 +51,9 @@ describe( 'MongoServerDao', () =>
expect( data.$set[ 'meta.bar' ] ) expect( data.$set[ 'meta.bar' ] )
.to.deep.equal( metadata.bar ); .to.deep.equal( metadata.bar );
expect( data.$push ).to.equal( undefined );
done(); done();
} }
) ); ) );
@ -60,6 +63,70 @@ describe( 'MongoServerDao', () =>
); );
} ); } );
} ); } );
describe( "with push data", () =>
{
it( "adds push data to the collection", done =>
{
const push_data = {
foo: [ 'bar', 'baz' ],
bar: [ { quux: 'quuux' } ],
};
const quote = createStubQuote( {} );
const sut = Sut( createMockDb(
// update
( selector, data ) =>
{
expect( data.$push[ 'foo' ] )
.to.deep.equal( push_data.foo );
expect( data.$push[ 'bar' ] )
.to.deep.equal( push_data.bar );
done();
}
) );
sut.init( () =>
sut.saveQuote(
quote,
() => {},
() => {},
undefined,
push_data
)
);
} );
it( "skips push data when it is an empty object", done =>
{
const push_data = {};
const quote = createStubQuote( {} );
const sut = Sut( createMockDb(
// update
( selector, data ) =>
{
expect( data.$push ).to.equal( undefined );
done();
}
) );
sut.init( () =>
sut.saveQuote(
quote,
() => {},
() => {},
undefined,
push_data
)
);
} );
} );
} ); } );
} ); } );

View File

@ -157,15 +157,14 @@ describe( 'RatingService', () =>
let saved_rates = false; let saved_rates = false;
dao.saveQuote = ( dao.saveQuote = (
quote: ServerSideQuote, quote: ServerSideQuote,
success: ServerDaoCallback, success: ServerDaoCallback,
_failure: ServerDaoCallback, _failure: ServerDaoCallback,
save_data: Record<string, any>, save_data: Record<string, any>,
_push_data: Record<string, any>,
) => ) =>
{ {
expect( save_data ).to.deep.equal( { expect( save_data.ratedata ).to.deep.equal( stub_rate_data );
ratedata: stub_rate_data,
} );
saved_rates = true; saved_rates = true;
success( quote ); success( quote );
@ -453,6 +452,7 @@ function getStubs()
success: ServerDaoCallback, success: ServerDaoCallback,
_failure: ServerDaoCallback, _failure: ServerDaoCallback,
_save_data: Record<string, any>, _save_data: Record<string, any>,
_push_data: Record<string, any>,
): this ): this
{ {
success( quote ); success( quote );