-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added integration tests for transactions.
Added isolation levels to api.
- Loading branch information
1 parent
ee93373
commit 1476156
Showing
5 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
DROP TABLE IF EXISTS dbo.MutationTests; | ||
CREATE TABLE dbo.MutationTests( | ||
id int | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const { config } = testHelpers; | ||
|
||
const skwell = require( "src/" ); | ||
describe( "Transaction - Integration", () => { | ||
let sql; | ||
before( () => { | ||
sql = skwell.connect( config ); | ||
return sql.execute( sql.fromFile( "sql/transaction-setup.sql" ) ); | ||
} ); | ||
|
||
beforeEach( async () => { | ||
await sql.execute( "DELETE FROM MutationTests" ); | ||
} ); | ||
|
||
after( () => { | ||
return sql.dispose(); | ||
} ); | ||
|
||
describe( "isolation levels", () => { | ||
const isolationLevelQuery = ` | ||
SELECT CASE transaction_isolation_level | ||
WHEN 0 THEN 'Unspecified' | ||
WHEN 1 THEN 'ReadUncommitted' | ||
WHEN 2 THEN 'ReadCommitted' | ||
WHEN 3 THEN 'Repeatable' | ||
WHEN 4 THEN 'Serializable' | ||
WHEN 5 THEN 'Snapshot' END AS level | ||
FROM sys.dm_exec_sessions | ||
where session_id = @@SPID`; | ||
|
||
it( "should default to read committed isolation level", async () => { | ||
await sql.transaction( async tx => { | ||
return tx.queryValue( isolationLevelQuery ); | ||
} ).should.eventually.equal( "ReadCommitted" ); | ||
} ); | ||
|
||
it( "should set isolation level", async () => { | ||
await sql.transaction( sql.read_uncommitted, async tx => { | ||
return tx.queryValue( isolationLevelQuery ); | ||
} ).should.eventually.equal( "ReadUncommitted" ); | ||
} ); | ||
} ); | ||
|
||
describe( "rollback", () => { | ||
it( "should roll back transaction when sql fails", async () => { | ||
const expectedError = "Automatic Rollback. Failed Because: Invalid object name 'fake_table'."; | ||
|
||
await sql.transaction( async tx => { | ||
await tx.execute( "insert into MutationTests(id) values (1)" ); | ||
await tx.query( "select lol from fake_table" ); | ||
} ).should.eventually.be.rejectedWith( expectedError ); | ||
|
||
const vals = await sql.query( "select * from MutationTests" ); | ||
vals.length.should.equal( 0 ); | ||
} ); | ||
|
||
it( "should roll back transaction when an error is thrown", async () => { | ||
await sql.transaction( async tx => { | ||
await tx.execute( "insert into MutationTests(id) values (1)" ); | ||
throw new Error( "NOPE" ); | ||
} ).should.eventually.be.rejectedWith( "NOPE" ); | ||
|
||
const vals = await sql.query( "select * from MutationTests" ); | ||
vals.length.should.equal( 0 ); | ||
} ); | ||
|
||
it( "should roll back transaction when manually calling rollback", async () => { | ||
await sql.transaction( async tx => { | ||
await tx.execute( "insert into MutationTests(id) values (1)" ); | ||
await tx.rollback(); | ||
} ).should.eventually.be.rejectedWith( "Manual Rollback" ); | ||
|
||
const vals = await sql.query( "select * from MutationTests" ); | ||
vals.length.should.equal( 0 ); | ||
} ); | ||
} ); | ||
|
||
it( "should commit transaction when promise resolves", async () => { | ||
await sql.transaction( async tx => { | ||
await tx.execute( "insert into MutationTests(id) values (1)" ); | ||
} ); | ||
|
||
const vals = await sql.query( "select * from MutationTests" ); | ||
vals.should.deep.equal( [ { id: 1 } ] ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters