From 20234bd90628d6fe6c1e02c383af304a23710316 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 12 Feb 2019 09:18:48 -0500 Subject: [PATCH] db: Restore previous save-all-meta behavior It looks like the metabucket is never initialized, so saving the quote is right now the only thing that sets default values. That should be fixed in the future. This also begins adding tests for the terrible MongoServerDao, that could use some refactoring. * src/server/db/MongoServerDao.js: Make `meta' mutable. I had forgotten to remove the code that mutates it (since our version of v8 right now does not blow up for const assignments), so this is all that's needed. * test/server/db/MongoServerDaoTest.js: New file to test this situation. --- src/server/db/MongoServerDao.js | 4 +- test/server/db/MongoServerDaoTest.js | 123 +++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 test/server/db/MongoServerDaoTest.js 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, + }; +}