@@ -24,6 +24,8 @@ import {split} from 'split-array-stream';
2424import { Transform } from 'stream' ;
2525import * as through from 'through2' ;
2626
27+ import { TimestampBounds } from '../src/transaction' ;
28+
2729let promisified = false ;
2830const fakePfy = extend ( { } , pfy , {
2931 promisifyAll ( klass , options ) {
@@ -35,6 +37,26 @@ const fakePfy = extend({}, pfy, {
3537 } ,
3638} ) ;
3739
40+ class FakeTransaction {
41+ commit ( callback ) {
42+ callback ( null , { } ) ;
43+ }
44+ createReadStream ( ) {
45+ return through . obj ( ) ;
46+ }
47+ deleteRows ( name , keys ) { }
48+ end ( ) { }
49+ insert ( table , row ) { }
50+ replace ( table , row ) { }
51+ upsert ( table , row ) { }
52+ update ( table , row ) { }
53+ }
54+
55+ interface GetSnapshotCallback {
56+ ( err : Error , snapshot ?: null ) : void ;
57+ ( err : null , snapshot : FakeTransaction ) : void ;
58+ }
59+
3860describe ( 'Table' , ( ) => {
3961 const sandbox = sinon . createSandbox ( ) ;
4062
@@ -43,21 +65,11 @@ describe('Table', () => {
4365 // tslint:disable-next-line no-any variable-name
4466 let TableCached : any ;
4567 let table ;
68+ let transaction : FakeTransaction ;
4669
4770 const DATABASE = {
48- runTransaction : callback => callback ( null , TRANSACTION ) ,
49- getSnapshot : ( options , callback ) => callback ( null , TRANSACTION ) ,
50- } ;
51-
52- const TRANSACTION = {
53- commit : callback => callback ( ) ,
54- createReadStream : ( ) => through . obj ( ) ,
55- deleteRows : ( name , keys ) => { } ,
56- end : ( ) => { } ,
57- insert : ( table , row ) => { } ,
58- replace : ( table , row ) => { } ,
59- upsert : ( table , row ) => { } ,
60- update : ( table , row ) => { } ,
71+ runTransaction : callback => callback ( null , transaction ) ,
72+ getSnapshot : ( options , callback ) => callback ( null , transaction ) ,
6173 } ;
6274
6375 const NAME = 'table-name' ;
@@ -72,6 +84,7 @@ describe('Table', () => {
7284 beforeEach ( ( ) => {
7385 extend ( Table , TableCached ) ;
7486 table = new Table ( DATABASE , NAME ) ;
87+ transaction = new FakeTransaction ( ) ;
7588 } ) ;
7689
7790 afterEach ( ( ) => sandbox . restore ( ) ) ;
@@ -107,16 +120,16 @@ describe('Table', () => {
107120
108121 describe ( 'createReadStream' , ( ) => {
109122 let fakeReadStream : Transform ;
110- let getSnapshotStub : sinon . SinonStub ;
123+ let getSnapshotStub : sinon . SinonStub < [ TimestampBounds , GetSnapshotCallback ] , void > ;
111124
112125 const REQUEST = { keys : [ 'key' ] } ;
113126
114127 beforeEach ( ( ) => {
115128 fakeReadStream = through . obj ( ) ;
116- sandbox . stub ( TRANSACTION , 'createReadStream' ) . returns ( fakeReadStream ) ;
129+ sandbox . stub ( transaction , 'createReadStream' ) . returns ( fakeReadStream ) ;
117130 getSnapshotStub =
118131 sandbox . stub ( DATABASE , 'getSnapshot' )
119- . callsFake ( ( _ , callback ) => callback ( null , TRANSACTION ) ) ;
132+ . callsFake ( ( _ , callback ) => callback ( null , transaction ) ) ;
120133 } ) ;
121134
122135 it ( 'should destroy the user stream if unable to get a snapshot' , done => {
@@ -141,7 +154,7 @@ describe('Table', () => {
141154 } ) ;
142155
143156 it ( 'should destroy the user stream and end the txn on error' , done => {
144- const endStub = sandbox . stub ( TRANSACTION , 'end' ) ;
157+ const endStub = sandbox . stub ( transaction , 'end' ) ;
145158 const fakeError = new Error ( 'err' ) ;
146159
147160 table . createReadStream ( REQUEST ) . on ( 'error' , err => {
@@ -170,7 +183,7 @@ describe('Table', () => {
170183 } ) ;
171184
172185 it ( 'should end the transaction on stream end' , done => {
173- sandbox . stub ( TRANSACTION , 'end' ) . callsFake ( done ) ;
186+ sandbox . stub ( transaction , 'end' ) . callsFake ( done ) ;
174187 table . createReadStream ( REQUEST ) . on ( 'error' , done ) ;
175188 fakeReadStream . end ( ) ;
176189 } ) ;
@@ -219,9 +232,9 @@ describe('Table', () => {
219232
220233 it ( 'should delete the rows via transaction' , done => {
221234 const stub =
222- sandbox . stub ( TRANSACTION , 'deleteRows' ) . withArgs ( table . name , KEYS ) ;
235+ sandbox . stub ( transaction , 'deleteRows' ) . withArgs ( table . name , KEYS ) ;
223236
224- sandbox . stub ( TRANSACTION , 'commit' ) . callsFake ( callback => callback ( ) ) ;
237+ sandbox . stub ( transaction , 'commit' ) . callsFake ( callback => callback ( ) ) ;
225238
226239 table . deleteRows ( KEYS , err => {
227240 assert . ifError ( err ) ;
@@ -263,7 +276,7 @@ describe('Table', () => {
263276
264277 it ( 'should insert via transaction' , done => {
265278 const stub =
266- sandbox . stub ( TRANSACTION , 'insert' ) . withArgs ( table . name , ROW ) ;
279+ sandbox . stub ( transaction , 'insert' ) . withArgs ( table . name , ROW ) ;
267280
268281 table . insert ( ROW , err => {
269282 assert . ifError ( err ) ;
@@ -354,7 +367,7 @@ describe('Table', () => {
354367
355368 it ( 'should replace via transaction' , done => {
356369 const stub =
357- sandbox . stub ( TRANSACTION , 'replace' ) . withArgs ( table . name , ROW ) ;
370+ sandbox . stub ( transaction , 'replace' ) . withArgs ( table . name , ROW ) ;
358371
359372 table . replace ( ROW , err => {
360373 assert . ifError ( err ) ;
@@ -381,7 +394,7 @@ describe('Table', () => {
381394
382395 it ( 'should update via transaction' , done => {
383396 const stub =
384- sandbox . stub ( TRANSACTION , 'update' ) . withArgs ( table . name , ROW ) ;
397+ sandbox . stub ( transaction , 'update' ) . withArgs ( table . name , ROW ) ;
385398
386399 table . update ( ROW , err => {
387400 assert . ifError ( err ) ;
@@ -408,7 +421,7 @@ describe('Table', () => {
408421
409422 it ( 'should upsert via transaction' , done => {
410423 const stub =
411- sandbox . stub ( TRANSACTION , 'upsert' ) . withArgs ( table . name , ROW ) ;
424+ sandbox . stub ( transaction , 'upsert' ) . withArgs ( table . name , ROW ) ;
412425
413426 table . upsert ( ROW , err => {
414427 assert . ifError ( err ) ;
0 commit comments