@incdevco/framework
Version:
node.js lambda framework
139 lines (83 loc) • 2.9 kB
JavaScript
var Expect = require('chai').expect;
var Mock = require('../mock');
var Utilities = require('../utilities');
var PgPool = require('pg').Pool;
var Connection = require('./connection');
var Pg = require('./index');
var Transaction = require('./transaction');
describe('pg', function () {
'use strict';
var mock, pg;
beforeEach(function () {
pg = new Pg({});
mock = new Mock();
});
describe('connect', function () {
var client;
beforeEach(function() {
client = {};
pg.pool = {};
});
it('should call connect on pool, wrap the returned client in a new Connection instance and return it', function (done) {
mock.mock(pg).expect('getPool')
.willResolve(pg.pool);
mock.mock(pg.pool).expect('connect')
.willResolve(client);
pg.connect()
.then(function (actual) {
Expect(actual).to.be.an.instanceof(Connection, 'actual');
Expect(actual.client).to.deep.equal(client, 'client');
mock.done(done);
})
.catch(done);
});
});
describe('getPool', function () {
it('should create a pool and return a promise that resolves the pool', function(done) {
pg.getPool()
.then(function (result) {
Expect(result).to.be.instanceof(PgPool, 'pg.pool');
return mock.done(done);
})
.catch(done);
});
it('should decrypt the CiphertextBlob and then create the pool', function (done) {
var config = {
CiphertextBlob: 'CiphertextBlob',
EncryptionContext: 'EncryptionContext',
GrantTokens: 'GrantTokens'
};
pg = new Pg(Utilities.copy(config));
mock.mock(pg.kms).expect('decrypt')
.with(config)
.willReturnAwsPromiseResolve({
Plaintext: 'Plaintext'
});
pg.getPool()
.then(function (result) {
Expect(result).to.be.instanceof(PgPool, 'pool');
return mock.done(done);
})
.catch(done);
});
});
describe('startTransaction', function() {
var connection;
beforeEach(function() {
connection = {};
});
it('should call connect then call query on connection with BEGIN and return new Transaction', function (done) {
mock.mock(pg).expect('connect')
.willResolve(connection);
mock.mock(connection).expect('query')
.with('BEGIN')
.willResolve(true);
pg.startTransaction()
.then(function (actual) {
Expect(actual).to.be.an.instanceof(Transaction, 'actual');
mock.done(done);
})
.catch(done);
});
});
});