-
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 specs testing behavior around client acquiring a connection.
- Loading branch information
1 parent
fefe9c8
commit 8a33f57
Showing
3 changed files
with
75 additions
and
9 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,67 @@ | ||
const { sinon } = testHelpers; | ||
const Client = require( "src/client" ); | ||
|
||
describe( "client", () => { | ||
describe( "when connection acquire succeeds", () => { | ||
let pool, result, action; | ||
const expectedConnection = "CONNECTION"; | ||
const expectedResult = "RESULT"; | ||
|
||
before( async () => { | ||
pool = { | ||
acquire: sinon.stub().resolves( expectedConnection ), | ||
release: sinon.stub().resolves() | ||
}; | ||
action = sinon.stub().resolves( expectedResult ); | ||
const client = new Client( pool ); | ||
result = await client.withConnection( action ); | ||
} ); | ||
|
||
it( "should return correct result", () => { | ||
result.should.equal( "RESULT" ); | ||
} ); | ||
|
||
it( "should acquire connection", () => { | ||
pool.acquire.should.be.calledOnce().and.calledWithExactly(); | ||
} ); | ||
|
||
it( "should call action with connection", () => { | ||
action.should.be.calledOnce().and.calledWithExactly( expectedConnection ); | ||
} ); | ||
|
||
it( "should release connection", async () => { | ||
pool.release.should.be.calledOnce().and.calledWithExactly( expectedConnection ); | ||
} ); | ||
} ); | ||
|
||
describe( "when connection acquire fails", () => { | ||
let pool, error, action; | ||
const expectedError = new Error( "Boom" ); | ||
|
||
before( async () => { | ||
pool = { | ||
acquire: sinon.stub().rejects( expectedError ), | ||
release: sinon.stub().resolves() | ||
}; | ||
action = sinon.stub().resolves(); | ||
const client = new Client( pool ); | ||
try { | ||
await client.withConnection( action ); | ||
} catch ( e ) { | ||
error = e; | ||
} | ||
} ); | ||
|
||
it( "should error", () => { | ||
error.should.equal( expectedError ); | ||
} ); | ||
|
||
it( "should acquire connection", () => { | ||
pool.acquire.should.be.calledOnce().and.calledWithExactly(); | ||
} ); | ||
|
||
it( "should not try to release connection", async () => { | ||
pool.release.should.not.be.called(); | ||
} ); | ||
} ); | ||
} ); |
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