-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Dabrowski
authored and
Andrew Dabrowski
committed
Sep 30, 2017
1 parent
213fa34
commit 1c760b1
Showing
2 changed files
with
206 additions
and
0 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,129 @@ | ||
'use strict'; | ||
|
||
const SqlCommandWorker = require('../lib/SqlCommandWorker.js'); | ||
|
||
describe('SqlCommandWorker tests', () => { | ||
test('beginTransaction failed', () => { | ||
const connection = { | ||
beginTransaction: jest.fn((callback) => { | ||
callback('transaction error'); | ||
}), | ||
}; | ||
const sqlCommandWorker = new SqlCommandWorker(); | ||
const commandList = [ | ||
'INSERT INTO idea_db.job (name) values ("foo");', | ||
'INSERT INTO idea_db.job (name) values ("bar");', | ||
]; | ||
|
||
return sqlCommandWorker.executeCommandsAsync(connection, commandList) | ||
.then((result) => { | ||
expect(result).toBe(); | ||
}) | ||
.catch((err) => { | ||
expect(err).toBe('transaction error'); | ||
}); | ||
}); | ||
test('command failed', () => { | ||
const connection = { | ||
beginTransaction: jest.fn((callback) => { | ||
callback(undefined); | ||
}), | ||
query: jest.fn((query, callback) => callback('command error')), | ||
rollback: jest.fn((callback) => { | ||
callback(); | ||
}), | ||
}; | ||
const sqlCommandWorker = new SqlCommandWorker(); | ||
const commandList = [ | ||
'INSERT INTO idea_db.job (name) values ("foo");', | ||
'INSERT INTO idea_db.job (name) values ("bar");', | ||
]; | ||
|
||
return sqlCommandWorker.executeCommandsAsync(connection, commandList) | ||
.then((result) => { | ||
expect(result).toBe(); | ||
}) | ||
.catch((err) => { | ||
expect(err).toBe('command error'); | ||
}); | ||
}); | ||
test('commit failed', () => { | ||
const fakeResult = { | ||
affectedRows: 1, | ||
changedRows: 1, | ||
insertId: 'hello', | ||
}; | ||
const connection = { | ||
beginTransaction: jest.fn((callback) => { | ||
callback(undefined); | ||
}), | ||
commit: jest.fn((callback) => { | ||
callback('commit failed'); | ||
}), | ||
rollback: jest.fn((callback) => { | ||
callback(); | ||
}), | ||
query: jest.fn((query, callback) => { | ||
callback(undefined, fakeResult); | ||
}), | ||
}; | ||
const sqlCommandWorker = new SqlCommandWorker(); | ||
const commandList = [ | ||
'INSERT INTO idea_db.job (name) values ("foo");', | ||
'INSERT INTO idea_db.job (name) values ("bar");', | ||
]; | ||
|
||
return sqlCommandWorker.executeCommandsAsync(connection, commandList) | ||
.then((results) => { | ||
expect(results).toBe(); | ||
}) | ||
.catch((err) => { | ||
expect(err).toBe('commit failed'); | ||
}); | ||
}); | ||
test('commands succeeded', () => { | ||
const fakeResult = { | ||
affectedRows: 1, | ||
changedRows: 1, | ||
insertId: 'hello', | ||
}; | ||
const connection = { | ||
beginTransaction: jest.fn((callback) => { | ||
callback(undefined); | ||
}), | ||
commit: jest.fn((callback) => { | ||
callback(undefined); | ||
}), | ||
query: jest.fn((query, callback) => { | ||
callback(undefined, fakeResult); | ||
}), | ||
}; | ||
const sqlCommandWorker = new SqlCommandWorker(); | ||
const commandList = [ | ||
'INSERT INTO idea_db.job (name) values ("foo");', | ||
'INSERT INTO idea_db.job (name) values ("bar");', | ||
]; | ||
|
||
return sqlCommandWorker.executeCommandsAsync(connection, commandList) | ||
.then((results) => { | ||
expect(results).toBeDefined(); | ||
expect(results.length).toBe(2); | ||
for (let result of results) { | ||
let commandResult; | ||
if (result.Command1) { | ||
commandResult = result.Command1; | ||
} else if (result.Command2) { | ||
commandResult = result.Command2; | ||
} else { | ||
expect(commandResult).toBeDefined(); | ||
} | ||
expect(commandResult.affectedRows).toBe(1); | ||
expect(commandResult.changedRows).toBe(1); | ||
expect(commandResult.insertedId).toBe('hello'); | ||
} | ||
}) | ||
.catch((err) => { | ||
expect(err).toBe(); | ||
}); | ||
}); | ||
}); |
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,77 @@ | ||
'use strict'; | ||
|
||
const SqlConnectionManager = require('../lib/SqlConnectionManager.js'); | ||
|
||
describe('SqlConnectionManager tests', () => { | ||
test('open connection failed', () => { | ||
const connection = { | ||
connect: jest.fn((callback) => callback('connection error')), | ||
}; | ||
const connectionFactory = { | ||
createInstance: jest.fn(() => connection), | ||
}; | ||
const sqlConnectionManager = new SqlConnectionManager(connectionFactory); | ||
const sqlFuncPromise = ()=> Promise.resolve('hello'); | ||
return sqlConnectionManager.executeAsync(sqlFuncPromise) | ||
.then((result) => { | ||
expect(result).toBe(); | ||
}) | ||
.catch((err) => { | ||
expect(err).toBe('connection error'); | ||
}); | ||
}); | ||
test('close connection failed', () => { | ||
const connection = { | ||
connect: jest.fn((callback) => callback(undefined)), | ||
end: jest.fn((callback) => callback('connection error')), | ||
}; | ||
const connectionFactory = { | ||
createInstance: jest.fn(() => connection), | ||
}; | ||
const sqlConnectionManager = new SqlConnectionManager(connectionFactory); | ||
const sqlFuncPromise = ()=> Promise.resolve('hello'); | ||
return sqlConnectionManager.executeAsync(sqlFuncPromise) | ||
.then((result) => { | ||
expect(result).toBe(); | ||
}) | ||
.catch((err) => { | ||
expect(err).toBe('connection error'); | ||
}); | ||
}); | ||
test('query failed', () => { | ||
const connection = { | ||
connect: jest.fn((callback) => callback(undefined)), | ||
end: jest.fn((callback) => callback(undefined)), | ||
}; | ||
const connectionFactory = { | ||
createInstance: jest.fn(() => connection), | ||
}; | ||
const sqlConnectionManager = new SqlConnectionManager(connectionFactory); | ||
const sqlFuncPromise = ()=> Promise.reject('query error'); | ||
return sqlConnectionManager.executeAsync(sqlFuncPromise) | ||
.then((result) => { | ||
expect(result).toBe(); | ||
}) | ||
.catch((err) => { | ||
expect(err).toBe('query error'); | ||
}); | ||
}); | ||
test('query succeeded', () => { | ||
const connection = { | ||
connect: jest.fn((callback) => callback(undefined)), | ||
end: jest.fn((callback) => callback(undefined)), | ||
}; | ||
const connectionFactory = { | ||
createInstance: jest.fn(() => connection), | ||
}; | ||
const sqlConnectionManager = new SqlConnectionManager(connectionFactory); | ||
const sqlFuncPromise = ()=> Promise.resolve('query succeeded'); | ||
return sqlConnectionManager.executeAsync(sqlFuncPromise) | ||
.then((result) => { | ||
expect(result).toBe('query succeeded'); | ||
}) | ||
.catch((err) => { | ||
expect(err).toBe(); | ||
}); | ||
}); | ||
}); |