@@ -225,6 +225,58 @@ describe('client.test.js', function () {
225225 } ) ;
226226 } ) ;
227227
228+ describe ( 'beginTransactionScope(scope)' , function ( ) {
229+ it ( 'should beginTransactionScope() error' , function * ( ) {
230+ let db = rds ( { } ) ;
231+ try {
232+ yield db . beginTransactionScope ( function * ( ) { } ) ;
233+ throw new Error ( 'should not run this' ) ;
234+ } catch ( err ) {
235+ assert . equal ( err . name , 'RDSClientGetConnectionError' ) ;
236+ }
237+ } ) ;
238+
239+ it ( 'should insert 2 rows in a transaction' , function * ( ) {
240+ let result = yield this . db . beginTransactionScope ( function * ( conn ) {
241+ yield conn . query ( 'insert into ??(name, email, gmt_create, gmt_modified) \
242+ values(?, ?, now(), now())' ,
243+ [ table , prefix + 'beginTransactionScope1' , prefix + 'm@beginTransactionScope1.com' ] ) ;
244+ yield conn . query ( 'insert into ??(name, email, gmt_create, gmt_modified) \
245+ values(?, ?, now(), now())' ,
246+ [ table , prefix + 'beginTransactionScope2' , prefix + 'm@beginTransactionScope1.com' ] ) ;
247+ return true ;
248+ } ) ;
249+
250+ assert . equal ( result , true ) ;
251+
252+ let rows = yield this . db . query ( 'select * from ?? where email=? order by id' ,
253+ [ table , prefix + 'm@beginTransactionScope1.com' ] ) ;
254+ assert . equal ( rows . length , 2 ) ;
255+ assert . equal ( rows [ 0 ] . name , prefix + 'beginTransactionScope1' ) ;
256+ assert . equal ( rows [ 1 ] . name , prefix + 'beginTransactionScope2' ) ;
257+ } ) ;
258+
259+ it ( 'should rollback when query fail' , function * ( ) {
260+ try {
261+ yield this . db . beginTransactionScope ( function * ( conn ) {
262+ yield conn . query ( 'insert into ??(name, email, gmt_create, gmt_modified) \
263+ values(?, ?, now(), now())' ,
264+ [ table , prefix + 'beginTransactionScope-fail1' , 'm@beginTransactionScope-fail.com' ] ) ;
265+ yield conn . query ( 'insert into ??(name, email, gmt_create, gmt_modified) \
266+ valuefail(?, ?, now(), now())' ,
267+ [ table , prefix + 'beginTransactionScope-fail12' , 'm@beginTransactionScope-fail.com' ] ) ;
268+ return true ;
269+ } ) ;
270+ } catch ( err ) {
271+ assert . equal ( err . code , 'ER_PARSE_ERROR' ) ;
272+ }
273+
274+ let rows = yield this . db . query ( 'select * from ?? where email=? order by id' ,
275+ [ table , prefix + 'm@beginTransactionScope-fail.com' ] ) ;
276+ assert . equal ( rows . length , 0 ) ;
277+ } ) ;
278+ } ) ;
279+
228280 describe ( 'get(table, obj, options), select(table, options)' , function ( ) {
229281 before ( function * ( ) {
230282 let result = yield this . db . insert ( table , {
0 commit comments