Skip to content

Commit

Permalink
refactor: test for session-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
alkatrivedi committed Dec 27, 2024
1 parent 239ddb1 commit db372ba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/session-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export interface SessionFactoryInterface {
*
* @param {Database} database Database object.
* @param {String} name Name of the database.
* @param {SessionPoolOptions|SessionPoolInterface} options Session pool
* configuration options or custom pool interface.
* @param {SessionPoolOptions|SessionPoolConstructor} options Session pool
* configuration options or custom pool constructor.
*/
export class SessionFactory
extends common.GrpcServiceObject
Expand Down
53 changes: 27 additions & 26 deletions test/session-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {FakeTransaction} from './session-pool';
import {ReleaseError} from '../src/session-pool';

describe('SessionFactory', () => {
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'true';
let sessionFactory;
let fakeSession;
let fakeMuxSession;
Expand Down Expand Up @@ -78,35 +77,37 @@ describe('SessionFactory', () => {
});

describe('instantiation', () => {
before(() => {
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'false';
});
describe('when GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS is disabled', () => {
before(() => {
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'false';
});

it('should create a SessionPool object', () => {
assert(sessionFactory.pool_ instanceof SessionPool);
});
it('should create a SessionPool object', () => {
assert(sessionFactory.pool_ instanceof SessionPool);
});

it('should accept a custom Pool class', () => {
function FakePool() {}
FakePool.prototype.on = util.noop;
FakePool.prototype.open = util.noop;

const sessionFactory = new SessionFactory(
DATABASE,
NAME,
FakePool as {} as db.SessionPoolConstructor
);
assert(sessionFactory.pool_ instanceof FakePool);
});
it('should accept a custom Pool class', () => {
function FakePool() {}
FakePool.prototype.on = util.noop;
FakePool.prototype.open = util.noop;

it('should open the pool', () => {
const openStub = sandbox
.stub(SessionPool.prototype, 'open')
.callsFake(() => {});
const sessionFactory = new SessionFactory(
DATABASE,
NAME,
FakePool as {} as db.SessionPoolConstructor
);
assert(sessionFactory.pool_ instanceof FakePool);
});

new SessionFactory(DATABASE, NAME, POOL_OPTIONS);
it('should open the pool', () => {
const openStub = sandbox
.stub(SessionPool.prototype, 'open')
.callsFake(() => {});

assert.strictEqual(openStub.callCount, 1);
new SessionFactory(DATABASE, NAME, POOL_OPTIONS);

assert.strictEqual(openStub.callCount, 1);
});
});

describe('when GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS is enabled', () => {
Expand All @@ -120,7 +121,7 @@ describe('SessionFactory', () => {
);
});

it('should initiate the multiplexed session creation if GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS is enabled', () => {
it('should initiate the multiplexed session creation', () => {
const createSessionStub = sandbox
.stub(MultiplexedSession.prototype, 'createSession')
.callsFake(() => {});
Expand Down

0 comments on commit db372ba

Please sign in to comment.