diff --git a/src/server/db/MongoServerDao.js b/src/server/db/MongoServerDao.js index 5347c92..48563f6 100644 --- a/src/server/db/MongoServerDao.js +++ b/src/server/db/MongoServerDao.js @@ -287,8 +287,8 @@ module.exports = Class( 'MongoServerDao' ) quote, success_callback, failure_callback, save_data ) { - var dao = this; - const meta = {}; + var dao = this; + var meta = {}; // if we're not ready, then we can't save the quote! if ( this._ready === false ) diff --git a/test/server/db/MongoServerDaoTest.js b/test/server/db/MongoServerDaoTest.js new file mode 100644 index 0000000..fb637bd --- /dev/null +++ b/test/server/db/MongoServerDaoTest.js @@ -0,0 +1,123 @@ +/** + * Tests MongoServerDao + * + * Copyright (C) 2019 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 . + */ + +'use strict'; + +const chai = require( 'chai' ); +const expect = chai.expect; +const { MongoServerDao: Sut } = require( '../../../' ).server.db; + + +describe( 'MongoServerDao', () => +{ + describe( '#saveQuote', () => + { + describe( "with no save data", () => + { + it( "saves entire metabucket record individually", done => + { + const metadata = { + foo: [ 'bar', 'baz' ], + bar: [ { quux: 'quuux' } ], + }; + + const quote = createStubQuote( metadata ); + + const sut = Sut( createMockDb( + // update + ( selector, data ) => + { + expect( data.$set[ 'meta.foo' ] ) + .to.deep.equal( metadata.foo ); + + expect( data.$set[ 'meta.bar' ] ) + .to.deep.equal( metadata.bar ); + + done(); + } + ) ); + + sut.init( () => + sut.saveQuote( quote, () => {}, () => {} ) + ); + } ); + } ); + } ); +} ); + + +function createMockDb( on_update ) +{ + const collection_quotes = { + update: on_update, + createIndex: ( _, __, c ) => c(), + }; + + const collection_seq = { + find( _, __, c ) + { + c( null, { + toArray: c => c( null, { length: 5 } ), + } ); + }, + }; + + const db = { + collection( id, c ) + { + const coll = ( id === 'quotes' ) + ? collection_quotes + : collection_seq; + + c( null, coll ); + }, + }; + + const driver = { + open: c => c( null, db ), + on: () => {}, + }; + + return driver; +} + + +function createStubQuote( metadata ) +{ + return { + getBucket: () => ( { + getData: () => {}, + } ), + + getMetabucket: () => ( { + getData: () => metadata, + } ), + + getId: () => 1, + getProgramVersion: () => 0, + getLastPremiumDate: () => 0, + getRatedDate: () => 0, + getExplicitLockReason: () => "", + getExplicitLockStep: () => 0, + isImported: () => false, + isBound: () => false, + }; +}