Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@rtspecialty.com>2019-09-10 12:09:08 -0400
committerMike Gerwitz <mike.gerwitz@rtspecialty.com>2019-10-17 11:47:14 -0400
commit54b3f0db726cef32838653a396d8aff02f835d8e (patch)
tree70895790723dd7457144c7d5776fc9ce518289b1 /test
parent9ea66c0440d1e4f4117c4dd6c9dc90f4bb86a817 (diff)
downloadliza-54b3f0db726cef32838653a396d8aff02f835d8e.tar.gz
liza-54b3f0db726cef32838653a396d8aff02f835d8e.tar.bz2
liza-54b3f0db726cef32838653a396d8aff02f835d8e.zip
TokenDao: Nominal typing
This beings an experiment with nominal typing using what the TS community calls "branding". The lack of nominal types was one of my biggest disappointments with TS, so this should really help to mitigate bugs resulting from misappropriation of data. * src/server/token/Token.ts: New file. * src/server/token/TokenDao.ts: Use Token{Id,Namespace}. * src/server/token/TokenQueryResult.ts: Likewise. * src/types/misc.d.ts: Introduce NominalType and UnixTimestamp. * test/server/token/TokenDaoTest.ts: Use nominal types.
Diffstat (limited to 'test')
-rw-r--r--test/server/token/TokenDaoTest.ts50
1 files changed, 28 insertions, 22 deletions
diff --git a/test/server/token/TokenDaoTest.ts b/test/server/token/TokenDaoTest.ts
index b0b9f31..ad06159 100644
--- a/test/server/token/TokenDaoTest.ts
+++ b/test/server/token/TokenDaoTest.ts
@@ -29,6 +29,12 @@ import {
TokenData,
} from "../../../src/server/token/TokenDao";
+import {
+ TokenId,
+ TokenNamespace,
+} from "../../../src/server/token/Token";
+
+
import { expect, use as chai_use } from 'chai';
chai_use( require( 'chai-as-promised' ) );
@@ -41,20 +47,17 @@ describe( 'server.token.TokenDao', () =>
{
const field = 'foo_field';
const qid = 12345;
- const ns = 'namespace';
- const tok_id = 'tok123';
+ const ns = <TokenNamespace>'namespace';
+ const tok_id = <TokenId>'tok123';
const tok_type = 'DONE';
const data = "some data";
- const timestamp = 12345;
+ const timestamp = <UnixTimestamp>12345;
const root = field + '.' + ns;
const coll: MongoCollection = {
update( selector: any, given_data: any, options, callback )
{
- expect( given_data.$set[ `${root}.lastStatus` ].timestamp )
- .to.be.greaterThan( 0 );
-
const expected_entry: TokenStatus = {
type: tok_type,
timestamp: timestamp,
@@ -102,8 +105,9 @@ describe( 'server.token.TokenDao', () =>
};
return expect(
- new Sut( coll, 'foo', () => 0 )
- .updateToken( 0, 'ns', 'id', 'DONE', null )
+ new Sut( coll, 'foo', () => <UnixTimestamp>0 ).updateToken(
+ 0, <TokenNamespace>'ns', <TokenId>'id', 'DONE', null
+ )
).to.eventually.be.rejectedWith( expected_error );
} );
} );
@@ -113,22 +117,22 @@ describe( 'server.token.TokenDao', () =>
{
const field = 'get_field';
const qid = 12345;
- const ns = 'get_ns';
+ const ns = <TokenNamespace>'get_ns';
const expected_status: TokenStatus = {
type: 'ACTIVE',
- timestamp: 0,
+ timestamp: <UnixTimestamp>0,
data: "",
};
- ( <[string, string, TokenQueryResult, TokenData][]>[
+ ( <[string, TokenId, TokenQueryResult, TokenData][]>[
[
'retrieves token by id',
- 'tok123',
+ <TokenId>'tok123',
{
[field]: {
[ns]: {
- last: 'tok123',
+ last: <TokenId>'tok123',
lastStatus: expected_status,
tok123: {
@@ -139,18 +143,18 @@ describe( 'server.token.TokenDao', () =>
},
},
{
- id: 'tok123',
+ id: <TokenId>'tok123',
status: expected_status,
},
],
[
'returns null for namespace if token is not found',
- 'tok123',
+ <TokenId>'tok123',
{
[field]: {
[ns]: {
- last: 'something',
+ last: <TokenId>'something',
lastStatus: expected_status,
// just to make sure we don't grab another tok
@@ -166,7 +170,7 @@ describe( 'server.token.TokenDao', () =>
[
'returns null for field if namespace is not found',
- 'tok123',
+ <TokenId>'tok123',
{
[field]: {},
},
@@ -175,11 +179,11 @@ describe( 'server.token.TokenDao', () =>
[
'returns lastest modified token given no token id',
- '',
+ <TokenId>'',
{
[field]: {
[ns]: {
- last: 'toklast',
+ last: <TokenId>'toklast',
lastStatus: expected_status,
toklast: {
@@ -190,7 +194,7 @@ describe( 'server.token.TokenDao', () =>
},
},
{
- id: 'toklast',
+ id: <TokenId>'toklast',
status: expected_status,
},
],
@@ -207,7 +211,8 @@ describe( 'server.token.TokenDao', () =>
};
return expect(
- new Sut( coll, field, () => 0 ).getToken( qid, ns, tok_id )
+ new Sut( coll, field, () => <UnixTimestamp>0 )
+ .getToken( qid, ns, tok_id )
).to.eventually.deep.equal( expected );
} )
);
@@ -227,7 +232,8 @@ describe( 'server.token.TokenDao', () =>
};
return expect(
- new Sut( coll, 'foo', () => 0 ).getToken( 0, 'ns', 'id' )
+ new Sut( coll, 'foo', () => <UnixTimestamp>0 )
+ .getToken( 0, <TokenNamespace>'ns', <TokenId>'id' )
).to.eventually.be.rejectedWith( expected_error );
} );
} );