From 61a59db4e0e10a4bb3c6f2347192280c351e47b7 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 30 Jan 2017 00:30:12 -0500 Subject: [PATCH] system.client: working data.diffStore * src/system/client.js (data.diffStore): Compose all stores. * test/system/clientTest.js: Update test case. DEV-2296 --- src/system/client.js | 32 ++++++++++++++++++++++++++++++-- test/system/clientTest.js | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/system/client.js b/src/system/client.js index 1cd8568..54e8917 100644 --- a/src/system/client.js +++ b/src/system/client.js @@ -21,7 +21,7 @@ "use strict"; -const MemoryStore = require( '../store' ).MemoryStore; +const store = require( '../store' ); /** @@ -34,6 +34,34 @@ const MemoryStore = require( '../store' ).MemoryStore; */ module.exports = { data: { - diffStore: () => MemoryStore(), + /** + * Create a store suitable for comparing diffs + * + * This relies very much on assumptions about how the rest of the + * system works: + * - bstore expects the diff format to be provided directly to it; + * - cstore expects a full classification result set with which + * _it_ will compute the diff; and + * - the outer store proxies to cstore for 'c:*'. + */ + diffStore: () => { + const cstore = store.DiffStore(); + const bstore = store.MemoryStore(); + + const proxy = store.MemoryStore.use( + store.PatternProxy( [ + [ /^c:(.*)$/, cstore ], + [ /./, bstore ], + ] ) + )(); + + // TODO: breaking encapsulation should not be necessary in the + // future + return { + store: proxy, + cstore: cstore, + bstore: bstore, + }; + }, }, }; diff --git a/test/system/clientTest.js b/test/system/clientTest.js index 0b18838..1b52185 100644 --- a/test/system/clientTest.js +++ b/test/system/clientTest.js @@ -27,20 +27,49 @@ const root = require( '../../' ); const sut = root.system.client; const expect = require( 'chai' ).expect; -const Store = root.store.Store; const Class = require( 'easejs' ).Class; +const { Store, DiffStore } = root.store; + describe( 'client', () => { describe( 'data.diffStore', () => { - it( 'produces Store', () => + it( 'produces proper Stores', () => { - const result = sut.data.diffStore(); + const { store, cstore, bstore } = sut.data.diffStore(); - expect( Class.isA( Store, result ) ) + // we don't care what type of store these two are + expect( Class.isA( Store, store ) ) .to.be.true; + expect( Class.isA( Store, bstore ) ) + .to.be.true; + + // but it's essential that this is a DiffStore + expect( Class.isA( DiffStore, cstore ) ) + .to.be.true; + } ); + + + it( 'proxies c:* to cstore, others to bstore', () => + { + const { store, cstore, bstore } = sut.data.diffStore(); + + const cname = 'c:foo'; // Master Shifu + const cval = 'panda'; + + const bname = 'henry'; + const bval = 'liza'; + + return expect( + store.add( cname, cval ) + .then( () => store.add( bname, bval ) ) + .then( () => Promise.all( [ + cstore.get( cname.replace( /^c:/, '' ) ), + bstore.get( bname ) + ] ) ) + ).to.eventually.deep.equal( [ cval, bval ] ); } ); } ); } );