knex/test/unit/dialects/oracledb.js

212 lines
5.9 KiB
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
const expect = require('chai').expect;
const knex = require('../../../knex');
const config = require('../../knexfile');
const sinon = require('sinon');
2020-04-19 00:40:23 +02:00
describe('OracleDb externalAuth', function () {
const knexInstance = knex({
client: 'oracledb',
connection: {
user: 'user',
password: 'password',
connectString: 'connect-string',
externalAuth: true,
host: 'host',
database: 'database',
},
});
let spy;
2020-04-19 00:40:23 +02:00
before(function () {
spy = sinon.spy(knexInstance.client.driver, 'getConnection');
});
2020-04-19 00:40:23 +02:00
it('externalAuth and connectString should be sent to the getConnection', function () {
const connectionWithExternalAuth = {
connectString: 'connect-string',
externalAuth: true,
};
2020-04-19 00:40:23 +02:00
knexInstance.client.acquireRawConnection().then(
function (resolve) {},
function (reject) {}
);
expect(spy).to.have.callCount(1);
expect(spy).to.have.been.calledWith(connectionWithExternalAuth);
});
2020-04-19 00:40:23 +02:00
after(function () {
knexInstance.client.driver.getConnection.restore();
});
});
2020-04-19 00:40:23 +02:00
describe('OracleDb parameters', function () {
describe('with fetchAsString parameter ', function () {
let knexClient;
2020-04-19 00:40:23 +02:00
before(function () {
const conf = _.clone(config.oracledb);
conf.fetchAsString = ['number', 'DATE', 'cLOb', 'BUFFER'];
knexClient = knex(conf);
return knexClient;
});
2020-04-19 00:40:23 +02:00
it('on float', function () {
return knexClient
.raw('select 7.329 as "field" from dual')
2020-04-19 00:40:23 +02:00
.then(function (result) {
expect(result[0]).to.be.ok;
expect(result[0].field).to.be.a('string');
});
});
2020-04-19 00:40:23 +02:00
it('on date', function () {
return knexClient
.raw('select CURRENT_DATE as "field" from dual')
2020-04-19 00:40:23 +02:00
.then(function (result) {
expect(result[0]).to.be.ok;
expect(result[0].field).to.be.a('string');
});
});
2020-04-19 00:40:23 +02:00
it('on clob', function () {
2019-10-15 09:23:07 +03:00
return knexClient
.raw('select TO_CLOB(\'LONG CONTENT\') as "field" from dual')
2020-04-19 00:40:23 +02:00
.then(function (result) {
2019-10-15 09:23:07 +03:00
expect(result[0]).to.be.ok;
expect(result[0].field).to.be.equal('LONG CONTENT');
});
});
2020-04-19 00:40:23 +02:00
it('on raw', function () {
return knexClient
.raw('select UTL_RAW.CAST_TO_RAW(3) as "field" from dual')
2020-04-19 00:40:23 +02:00
.then(function (result) {
expect(result[0]).to.be.ok;
expect(result[0].field).to.be.equal('33');
});
});
2020-04-19 00:40:23 +02:00
after(function () {
return knexClient.destroy();
});
});
2020-04-19 00:40:23 +02:00
describe('without fetchAsString parameter', function () {
let knexClient;
2020-04-19 00:40:23 +02:00
before(function () {
knexClient = knex(config.oracledb);
return knexClient;
});
2020-04-19 00:40:23 +02:00
it('on float', function () {
return knexClient
.raw('select 7.329 as "field" from dual')
2020-04-19 00:40:23 +02:00
.then(function (result) {
expect(result[0]).to.be.ok;
expect(result[0].field).to.not.be.a('string');
});
});
2020-04-19 00:40:23 +02:00
it('on date', function () {
return knexClient
.raw('select CURRENT_DATE as "field" from dual')
2020-04-19 00:40:23 +02:00
.then(function (result) {
expect(result[0]).to.be.ok;
expect(result[0].field).to.not.be.a('string');
});
});
it('on blob', async () => {
const result = await knexClient.raw(
'select TO_BLOB(\'67c1a1acaaca11a1b36fa6636166709b\') as "field" from dual'
);
expect(result[0]).to.be.ok;
expect(result[0].field.toString('hex')).to.be.equal(
'67c1a1acaaca11a1b36fa6636166709b'
);
});
2020-04-19 00:40:23 +02:00
it('on raw', function () {
return knexClient
.raw('select UTL_RAW.CAST_TO_RAW(3) as "field" from dual')
2020-04-19 00:40:23 +02:00
.then(function (result) {
expect(result[0]).to.be.ok;
expect(result[0].field).to.be.instanceOf(Buffer);
});
});
2020-04-19 00:40:23 +02:00
after(function () {
return knexClient.destroy();
});
});
});
2020-04-19 00:40:23 +02:00
describe('OracleDb unit tests', function () {
let knexClient;
2020-04-19 00:40:23 +02:00
before(function () {
const conf = _.clone(config.oracledb);
conf.fetchAsString = ['number', 'DATE', 'cLOb', 'BUFFER'];
knexClient = knex(conf);
return knexClient;
});
2020-04-19 00:40:23 +02:00
it('disposes the connection on connection error', async function () {
let spy = sinon.spy();
// call the real acquireConnection but release the connection immediately to cause connection error
const acquireConnection = knexClient.client.acquireConnection;
sinon.stub(knexClient.client, 'acquireConnection').callsFake(async () => {
const conn = await acquireConnection.call(knexClient.client);
conn.release();
spy = sinon.spy(conn, 'close');
return conn;
});
let exception;
try {
await knexClient.raw('insert into DUAL values(1)');
} catch (e) {
exception = e;
}
expect(exception).not.to.equal(undefined);
expect(exception.message).to.include('NJS-003: invalid connection');
expect(spy.callCount).to.equal(1);
});
2020-04-19 00:40:23 +02:00
it('clears the connection from the pool on disconnect during commit', async function () {
const err = 'error message';
const spy = sinon.spy(knexClient.client, 'releaseConnection');
// call the real acquireConnection but ensure commitAsync fails simulating a disconnect
const acquireConnection = knexClient.client.acquireConnection;
sinon.stub(knexClient.client, 'acquireConnection').callsFake(async () => {
const conn = await acquireConnection.call(knexClient.client);
conn.commitAsync = () => Promise.reject(err);
return conn;
});
let exception;
try {
await knexClient.transaction(async (trx) => {
await trx('DUAL').select('*');
});
} catch (e) {
exception = e;
}
expect(spy.callCount).to.equal(1);
expect(exception).to.equal(err);
});
2020-04-19 00:40:23 +02:00
afterEach(function () {
knexClient.client.acquireConnection.restore();
});
2020-04-19 00:40:23 +02:00
after(function () {
return knexClient.destroy();
});
});