Permalink
Browse files

Separate tests for begin/commit and begin/rollback transactions.

  • Loading branch information...
1 parent 61e7708 commit 618e7dfcf757a345c05ae2d69a438ca82f3159ab @pekim pekim committed Jul 9, 2012
Showing with 95 additions and 37 deletions.
  1. +95 −37 test/integration/transactions-test.coffee
View
132 test/integration/transactions-test.coffee
@@ -17,52 +17,110 @@ if (debug)
else
config.options.debug = {}
-exports.beginCommitTransaction = (test) ->
- test.expect(4)
+class Tester
+ constructor: (@test) ->
+ @connection = new Connection(config)
- connection = new Connection(config)
+ @connection.on('end', (info) =>
+ @test.done()
+ )
+
+ @connection.on('errorMessage', (error) =>
+ console.log("#{error.number} : #{error.message}")
+ )
+
+ @connection.on('debug', (message) =>
+ if (debug)
+ console.log(message)
+ )
+
+ createTable: (callback) =>
+ request = new Request('create table #temp (id int)', (err) =>
+ @test.ok(!err)
+ callback(err)
+ )
+
+ @connection.execSqlBatch(request)
+
+ insert: (callback) =>
+ request = new Request('insert into #temp (id) values(1)', (err) =>
+ @test.ok(!err)
+ callback(err)
+ )
+
+ @connection.execSqlBatch(request)
+
+ select: (callback, expectedRows) =>
+ request = new Request('select id from #temp', (err, rowCount) =>
+ @test.ok(!err)
+ @test.strictEqual(rowCount, expectedRows)
+ callback(err)
+ )
+
+ request.on('row', (columns) =>
+ @test.strictEqual(columns[0].value, 1)
+ )
+
+ @connection.execSqlBatch(request)
+
+ selectExpectZeroRows: (callback) =>
+ @select(callback, 0)
+
+ selectExpectOneRow: (callback) =>
+ @select(callback, 1)
+
+ beginTransaction: (callback) =>
+ @connection.beginTransaction((err, transactionDescriptor) =>
+ @test.ok(!err)
+ @test.ok(transactionDescriptor)
- beginTransaction = (callback) ->
- connection.beginTransaction((err, transactionDescriptor) ->
- test.ok(!err)
- test.ok(transactionDescriptor)
callback(err)
, 'abc')
- select = (callback) ->
- request = new Request('select 3', (err) ->
- test.ok(!err)
+ commitTransaction: (callback) =>
+ @connection.commitTransaction((err) =>
+ @test.ok(!err)
+
callback(err)
)
- connection.execSql(request)
+ rollbackTransaction: (callback) =>
+ @connection.rollbackTransaction((err) =>
+ @test.ok(!err)
- commitTransaction = (callback) ->
- connection.commitTransaction((err) ->
- test.ok(!err)
- connection.close()
callback(err)
)
- connection.on('connect', (err) ->
- async.series([
- beginTransaction
- select
- commitTransaction
- () ->
- connection.close()
- ]);
- )
-
- connection.on('end', (info) ->
- test.done()
- )
-
- connection.on('errorMessage', (error) ->
- console.log("#{error.number} : #{error.message}")
- )
-
- connection.on('debug', (message) ->
- if (debug)
- console.log(message)
- )
+ close: (callback) =>
+ @connection.close()
+
+ run: (actions) =>
+ @connection.on('connect', (err) =>
+ async.series(actions)
+ )
+
+exports.beginCommit = (test) ->
+ test.expect(8)
+
+ tester = new Tester(test)
+ tester.run([
+ tester.createTable
+ tester.beginTransaction
+ tester.insert
+ tester.commitTransaction
+ tester.selectExpectOneRow
+ tester.close
+ ])
+
+exports.beginRollback = (test) ->
+ test.expect(7)
+
+ tester = new Tester(test)
+ tester.run([
+ tester.createTable
+ tester.beginTransaction
+ tester.insert
+ tester.rollbackTransaction
+ tester.selectExpectZeroRows
+ tester.close
+ ])

0 comments on commit 618e7df

Please sign in to comment.