Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'test/server/service/RatingServiceTest.ts')
-rw-r--r--test/server/service/RatingServiceTest.ts111
1 files changed, 92 insertions, 19 deletions
diff --git a/test/server/service/RatingServiceTest.ts b/test/server/service/RatingServiceTest.ts
index 5dd821f..8896c49 100644
--- a/test/server/service/RatingServiceTest.ts
+++ b/test/server/service/RatingServiceTest.ts
@@ -36,6 +36,7 @@ import { UserRequest } from "../../../src/server/request/UserRequest";
import { UserResponse } from "../../../src/server/request/UserResponse";
import { UserSession } from "../../../src/server/request/UserSession";
import { QuoteDataBucket } from "../../../src/bucket/QuoteDataBucket";
+import { Kv } from "../../../src/bucket/delta";
import {
ServerDao,
@@ -59,9 +60,10 @@ describe( 'RatingService', () =>
response,
quote,
stub_rate_data,
+ createDelta,
} = getStubs();
- const sut = new Sut( logger, dao, server, raters );
+ const sut = new Sut( logger, dao, server, raters, createDelta);
const expected = {
data: stub_rate_data,
@@ -85,9 +87,10 @@ describe( 'RatingService', () =>
response,
quote,
stub_rate_data,
+ createDelta,
} = getStubs();
- const sut = new Sut( logger, dao, server, raters );
+ const sut = new Sut( logger, dao, server, raters, createDelta );
let last_prem_called = false;
let rated_date_called = false;
@@ -152,6 +155,7 @@ describe( 'RatingService', () =>
response,
quote,
stub_rate_data,
+ createDelta,
} = getStubs();
let saved_rates = false;
@@ -172,7 +176,7 @@ describe( 'RatingService', () =>
return dao;
};
- const sut = new Sut( logger, dao, server, raters );
+ const sut = new Sut( logger, dao, server, raters, createDelta );
return sut.request( request, response, quote, "" )
.then( () =>
@@ -182,6 +186,55 @@ describe( 'RatingService', () =>
} );
+ it( "saves delta to it's own field", () =>
+ {
+ const {
+ logger,
+ server,
+ raters,
+ dao,
+ request,
+ response,
+ quote,
+ stub_rate_delta,
+ createDelta,
+ } = getStubs();
+
+ let saved_quote = false;
+
+ let timestamp = 0;
+
+ quote.setLastPremiumDate = ( ts: UnixTimestamp ) =>
+ {
+ timestamp = ts;
+ return quote;
+ };
+
+ dao.saveQuote = (
+ quote: ServerSideQuote,
+ success: ServerDaoCallback,
+ _failure: ServerDaoCallback,
+ _save_data: Record<string, any>,
+ push_data: Record<string, any>,
+ ) =>
+ {
+ stub_rate_delta[ "rdelta.ratedata" ].timestamp = timestamp;
+ saved_quote = true;
+
+ expect( push_data ).to.deep.equal( stub_rate_delta );
+ success( quote );
+
+ return dao;
+ };
+
+ const sut = new Sut( logger, dao, server, raters, createDelta );
+
+ return sut.request( request, response, quote, "" )
+ .then( () => { expect( saved_quote ).to.be.true; } );
+ } );
+
+
+
it( "rejects and responds with error", () =>
{
const {
@@ -194,13 +247,14 @@ describe( 'RatingService', () =>
request,
response,
server,
+ createDelta,
} = getStubs();
const expected_error = new Error( "expected error" );
rater.rate = () => { throw expected_error; };
- const sut = new Sut( logger, dao, server, raters );
+ const sut = new Sut( logger, dao, server, raters, createDelta );
let logged = false;
@@ -242,11 +296,12 @@ describe( 'RatingService', () =>
request,
response,
server,
+ createDelta,
} = getStubs();
const expected_message = 'expected foo';
- const sut = new Sut( logger, dao, server, raters );
+ const sut = new Sut( logger, dao, server, raters, createDelta );
rater.rate = (
_quote: ServerSideQuote,
@@ -279,6 +334,7 @@ describe( 'RatingService', () =>
response,
server,
stub_rate_data,
+ createDelta,
} = getStubs();
let sent = false;
@@ -304,7 +360,7 @@ describe( 'RatingService', () =>
return server;
};
- const sut = new Sut( logger, dao, server, raters );
+ const sut = new Sut( logger, dao, server, raters, createDelta );
return sut.request( request, response, quote, "" )
.then( () => expect( sent ).to.be.true );
@@ -325,6 +381,7 @@ describe( 'RatingService', () =>
request,
response,
quote,
+ createDelta,
} = getStubs();
dao.mergeBucket = () =>
@@ -341,7 +398,7 @@ describe( 'RatingService', () =>
{
processed = true;
}
- }( logger, dao, server, raters );
+ }( logger, dao, server, raters, createDelta );
sut.request( request, response, quote, 'something' );
} );
@@ -361,6 +418,7 @@ describe( 'RatingService', () =>
request,
response,
quote,
+ createDelta,
} = getStubs();
quote.getLastPremiumDate = () =>
@@ -371,7 +429,7 @@ describe( 'RatingService', () =>
quote.getRatedDate = () => initial_date;
- const sut = new Sut( logger, dao, server, raters );
+ const sut = new Sut( logger, dao, server, raters, createDelta );
server.sendResponse = ( _request: any, _quote: any, resp: any, _actions: any ) =>
{
@@ -404,6 +462,19 @@ function getStubs()
_unavailable_all: '0',
};
+ const stub_rate_delta: any = {
+ "rdelta.ratedata": {
+ data: {
+ _unavailable_all: [ undefined ]
+ },
+ timestamp: 123
+ }
+ };
+
+ const createDelta = ( _src: Kv, _dest: Kv ) => {
+ return stub_rate_delta[ "rdelta.ratedata" ][ "data" ];
+ };
+
const rater = new class implements Rater
{
rate(
@@ -518,16 +589,18 @@ function getStubs()
};
return {
- program: program,
- stub_rate_data: stub_rate_data,
- rater: rater,
- raters: raters,
- logger: logger,
- server: server,
- dao: dao,
- session: session,
- request: request,
- response: response,
- quote: quote,
+ program: program,
+ stub_rate_data: stub_rate_data,
+ stub_rate_delta: stub_rate_delta,
+ createDelta: createDelta,
+ rater: rater,
+ raters: raters,
+ logger: logger,
+ server: server,
+ dao: dao,
+ session: session,
+ request: request,
+ response: response,
+ quote: quote,
};
};