Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'test/system/DeltaPublisherTest.ts')
-rw-r--r--test/system/DeltaPublisherTest.ts221
1 files changed, 173 insertions, 48 deletions
diff --git a/test/system/DeltaPublisherTest.ts b/test/system/DeltaPublisherTest.ts
index 40663d0..fcb788c 100644
--- a/test/system/DeltaPublisherTest.ts
+++ b/test/system/DeltaPublisherTest.ts
@@ -19,13 +19,25 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+import { AmqpConnection } from '../../src/system/amqp/AmqpConnection';
+import { Delta, DeltaResult, DeltaType } from '../../src/bucket/delta';
import { DeltaPublisher as Sut } from '../../src/system/DeltaPublisher';
-import { AmqpConfig } from '../../src/system/AmqpPublisher';
+import { DocumentId } from '../../src/document/Document';
+import { Duplex } from 'stream';
import { EventEmitter } from "events";
+import { hasContext } from '../../src/error/ContextError';
+import { AmqpError } from '../../src/error/AmqpError';
+import { Channel } from 'amqplib';
+import {
+ createAvroEncoder,
+ AvroEncoderCtr,
+ AvroSchema,
+} from '../../src/system/avro/AvroFactory';
import { expect, use as chai_use } from 'chai';
chai_use( require( 'chai-as-promised' ) );
+const sinon = require( 'sinon' );
describe( 'server.DeltaPublisher', () =>
{
@@ -33,24 +45,96 @@ describe( 'server.DeltaPublisher', () =>
{
it( 'sends a message', () =>
{
- const conf = createMockConf();
- const emitter = new EventEmitter();
+ let publish_called = false;
+ const delta = createMockDelta();
+ const bucket = createMockBucketData();
+ const ratedata = createMockBucketData();
+ const emitter = new EventEmitter();
+ const conn = createMockAmqpConnection();
+ conn.getAmqpChannel = () =>
+ {
+ return <Channel>{
+ publish: ( _: any, __: any, buf: any, ___: any ) =>
+ {
+ expect( buf instanceof Buffer ).to.be.true;
- console.log( new Sut( conf, emitter, ts_ctr ) );
- expect( true ).to.be.true
+ publish_called = true;
+
+ return true;
+ }
+ };
+ };
+
+ const sut = new Sut( emitter, ts_ctr, createAvroEncoder, conn );
+
+ return expect(
+ sut.publish( <DocumentId>123, delta, bucket, ratedata )
+ ).to.eventually.deep.equal( undefined )
+ .then( _ =>
+ {
+ expect( publish_called ).to.be.true;
+ } );
} );
- } );
- describe( '#sendMessage', () =>
- {
- it( 'sends a message', () =>
+ ( <[string, () => Channel | undefined, Error, string ][]>[
+ [
+ 'Throws an error when publishing was unsuccessful',
+ () =>
+ {
+ return <Channel>{
+ publish: ( _: any, __: any, _buf: any, ___: any ) =>
+ {
+ return false;
+ }
+ };
+ },
+ Error,
+ 'Delta publish failed'
+ ],
+ [
+ 'Throws an error when no amqp channel is found',
+ () =>
+ {
+ return undefined;
+ },
+ AmqpError,
+ 'Error sending message: No channel'
+ ]
+ ] ).forEach( ( [ label, getChannelF, error_type, err_msg ] ) =>
+ it( label, () =>
{
- const conf = createMockConf();
- const emitter = new EventEmitter();
+ const delta = createMockDelta();
+ const bucket = createMockBucketData();
+ const ratedata = createMockBucketData();
+ const emitter = new EventEmitter();
+ const conn = createMockAmqpConnection();
+ const doc_id = <DocumentId>123;
+ const expected = {
+ doc_id: doc_id,
+ delta_type: delta.type,
+ delta_ts: delta.timestamp
+ }
- console.log( new Sut( conf, emitter, ts_ctr ) );
- expect( true ).to.be.true
- } );
+ conn.getAmqpChannel = getChannelF;
+
+ const result = new Sut( emitter, ts_ctr, createAvroEncoder, conn )
+ .publish( doc_id, delta, bucket, ratedata );
+
+ return Promise.all( [
+ expect( result ).to.eventually.be.rejectedWith(
+ error_type, err_msg
+ ),
+ result.catch( e =>
+ {
+ if ( !hasContext( e ) )
+ {
+ return expect.fail();
+ }
+
+ return expect( e.context ).to.deep.equal( expected );
+ } )
+ ] );
+ } ) );
} );
describe( '#avroEncode parses', () =>
@@ -137,32 +221,26 @@ describe( 'server.DeltaPublisher', () =>
{
it( label, () =>
{
- let errorCalled = false;
+ const emitter = createMockEventEmitter();
+ const conn = createMockAmqpConnection();
+ const data = createMockData( delta_data );
+ const sut = new Sut(
+ emitter,
+ ts_ctr,
+ createAvroEncoder,
+ conn,
+ );
- const emitter = <EventEmitter>{
- emit( _event_id, _err )
+ sut.avroEncode( data )
+ .then( b =>
{
- errorCalled = true;
-
- console.log( 'server.DeltaPublisher.Error' + _err );
- }
- }
-
- const conf = createMockConf();
- const data = createMockData( delta_data );
- const sut = new Sut( conf, emitter, ts_ctr );
- const buffer = sut.avroEncode( data );
-
- if ( valid )
- {
- expect( typeof(buffer) ).to.equal( 'object' );
- }
- else
- {
- expect( buffer ).to.equal( null );
- }
-
- expect( valid ).to.equal( !errorCalled );
+ expect( typeof(b) ).to.equal( 'object' );
+ expect( valid ).to.be.true;
+ } )
+ .catch( _ =>
+ {
+ expect( valid ).to.be.false;
+ } );
} );
} );
} );
@@ -301,9 +379,16 @@ describe( 'server.DeltaPublisher', () =>
{
it( label, () =>
{
- const emitter = <EventEmitter>{}
- const conf = createMockConf();
- const sut = new Sut( conf, emitter, ts_ctr );
+ const encoded = 'FooBar';
+ const emitter = createMockEventEmitter();
+ const conn = createMockAmqpConnection();
+ const avroEncoderCtr = createMockEncoder( encoded );
+ const sut = new Sut(
+ emitter,
+ ts_ctr,
+ avroEncoderCtr,
+ conn,
+ );
const actual = sut.setDataTypes( delta_data );
expect( actual ).to.deep.equal( expected );
@@ -312,14 +397,39 @@ describe( 'server.DeltaPublisher', () =>
} );
} );
+
function ts_ctr(): UnixTimestamp
{
return <UnixTimestamp>Math.floor( new Date().getTime() / 1000 );
}
-function createMockConf(): AmqpConfig
+
+function createMockEncoder( mock_encoded_data: string ): AvroEncoderCtr
+{
+ return ( _schema: AvroSchema ) =>
+ {
+ const mock = sinon.mock( Duplex );
+
+ mock.on = ( _: string, __: any ) => {};
+ mock.end = ( _: any ) => { return mock_encoded_data; };
+
+ return mock;
+ };
+}
+
+
+function createMockEventEmitter(): EventEmitter
+{
+ return <EventEmitter>{};
+}
+
+
+function createMockAmqpConnection(): AmqpConnection
{
- return <AmqpConfig>{};
+ return <AmqpConnection>{
+ connect: () => {},
+ getExchangeName: () => { 'Foo' },
+ };
}
@@ -339,11 +449,8 @@ function createMockData( delta_data: any ): any
modified: 1573856916,
top_visited_step: '2',
},
- session: {
- entity_name: 'Foobar',
- entity_id: 123123 ,
- },
- data: null,
+ data: null,
+ ratedata: null,
delta: {
Data: {
bucket: delta_data,
@@ -356,4 +463,22 @@ function createMockData( delta_data: any ): any
},
},
};
+}
+
+
+function createMockBucketData(): Record<string, any>
+{
+ return {
+ foo: [ 'bar', 'baz' ]
+ }
+}
+
+
+function createMockDelta(): Delta<any>
+{
+ return <Delta<any>>{
+ type: <DeltaType>'data',
+ timestamp: <UnixTimestamp>123123123,
+ data: <DeltaResult<any>>{},
+ }
} \ No newline at end of file