Code Cleanup and test refactoring

This commit is contained in:
Sharad Chandran R 2023-05-03 19:28:48 +05:30
parent a4ab0ffc7f
commit 99955e8a90
13 changed files with 70 additions and 44 deletions

View File

@ -265,7 +265,7 @@ class AqQueue {
}
nodbUtil.wrap_fns(AqQueue.prototype,
nodbUtil.wrapFns(AqQueue.prototype,
"deqOne",
"deqMany",
"enqOne",

View File

@ -1615,7 +1615,7 @@ class Connection extends EventEmitter {
// NOTE: breakExecution() should not be serialized
Connection.prototype.break =
nodbUtil.callbackify(Connection.prototype.breakExecution);
nodbUtil.wrap_fns(Connection.prototype,
nodbUtil.wrapFns(Connection.prototype,
"changePassword",
"close",
"commit",

View File

@ -1,4 +1,4 @@
// Copyright (c) 2019, 2022, Oracle and/or its affiliates.
// Copyright (c) 2019, 2023, Oracle and/or its affiliates.
//
//----------------------------------------------------------------------------
//
@ -381,6 +381,41 @@ class BaseDbObject {
}
// method for transforming the error
function transformErr(func) {
return function() {
try {
return func.apply(this, arguments);
} catch (err) {
throw errors.transformErr(err, errors.transformErr);
}
};
}
// method for wrapping the functions so that any errors thrown are transformed
function wrapFns(proto) {
for (let i = 1; i < arguments.length; i++) {
const name = arguments[i];
proto[name] = transformErr(proto[name]);
}
}
wrapFns(BaseDbObject.prototype,
"_getAttrValue",
"_setAttrValue",
"append",
"deleteElement",
"getElement",
"getKeys",
"getFirstIndex",
"getLastIndex",
"getNextIndex",
"getPrevIndex",
"getValues",
"hasElement",
"setElement",
"trim"
);
// define proxy handler used for collections
BaseDbObject._collectionProxyHandler = {

View File

@ -117,13 +117,18 @@ const ERR_FETCH_TYPE_HANDLER_RETURN_VALUE = 120;
const ERR_FETCH_TYPE_HANDLER_TYPE = 121;
const ERR_FETCH_TYPE_HANDLER_CONVERTER = 122;
const ERR_CALL_TIMEOUT_EXCEEDED = 123;
const ERR_CONNECTION_CLOSED = 500;
// define mapping for ODPI-C errors that need to be wrapped with NJS errors
const adjustErrorXref = new Map();
adjustErrorXref.set("DPI-1010", ERR_CONNECTION_CLOSED);
adjustErrorXref.set("DPI-1040", ERR_LOB_CLOSED);
adjustErrorXref.set("DPI-1055", ERR_NAN_VALUE);
adjustErrorXref.set("DPI-1063", ERR_EXEC_MODE_ONLY_FOR_DML);
adjustErrorXref.set("DPI-1067", [ERR_CALL_TIMEOUT_EXCEEDED, "call timeout of ([0-9]+) ms"]);
adjustErrorXref.set("DPI-1080", ERR_CONNECTION_CLOSED);
adjustErrorXref.set("ORA-00028", ERR_CONNECTION_CLOSED);
adjustErrorXref.set("ORA-00600", ERR_CONNECTION_CLOSED);
adjustErrorXref.set("ORA-24338", ERR_INVALID_REF_CURSOR);
adjustErrorXref.set("ORA-24422", ERR_POOL_HAS_BUSY_CONNECTIONS);
adjustErrorXref.set("ORA-25708", ERR_TOKEN_HAS_EXPIRED);
@ -309,6 +314,8 @@ messages.set(ERR_FETCH_TYPE_HANDLER_CONVERTER,
'fetchTypeHandler return value attribute "converter" must be a function');
messages.set(ERR_CALL_TIMEOUT_EXCEEDED,
'call timeout of %d ms exceeded');
messages.set(ERR_CONNECTION_CLOSED,
'connection to the Oracle Database was broken. End-of-file on communication channel');
//-----------------------------------------------------------------------------
// assert()
@ -597,6 +604,7 @@ module.exports = {
ERR_FETCH_TYPE_HANDLER_TYPE,
ERR_FETCH_TYPE_HANDLER_CONVERTER,
ERR_CALL_TIMEOUT_EXCEEDED,
ERR_CONNECTION_CLOSED,
assert,
assertArgCount,
assertParamPropBool,

View File

@ -229,7 +229,7 @@ class Lob extends Duplex {
}
nodbUtil.wrap_fns(Lob.prototype, errors.ERR_BUSY_LOB,
nodbUtil.wrapFns(Lob.prototype, errors.ERR_BUSY_LOB,
"close",
"getData");
Lob.prototype._serializedRead = nodbUtil.serialize(Lob.prototype._readData);

View File

@ -360,7 +360,7 @@ class ResultSet {
}
nodbUtil.wrap_fns(ResultSet.prototype, errors.ERR_BUSY_RS,
nodbUtil.wrapFns(ResultSet.prototype, errors.ERR_BUSY_RS,
"close",
"getRow",
"getRows");

View File

@ -1,4 +1,4 @@
// Copyright (c) 2018, 2022, Oracle and/or its affiliates.
// Copyright (c) 2018, 2023, Oracle and/or its affiliates.
//-----------------------------------------------------------------------------
//
@ -253,7 +253,7 @@ class SodaCollection {
}
nodbUtil.wrap_fns(SodaCollection.prototype,
nodbUtil.wrapFns(SodaCollection.prototype,
"createIndex",
"drop",
"dropIndex",

View File

@ -1,4 +1,4 @@
// Copyright (c) 2018, 2022, Oracle and/or its affiliates.
// Copyright (c) 2018, 2023, Oracle and/or its affiliates.
//-----------------------------------------------------------------------------
//
@ -140,7 +140,7 @@ class SodaDatabase {
}
nodbUtil.wrap_fns(SodaDatabase.prototype,
nodbUtil.wrapFns(SodaDatabase.prototype,
"createCollection",
"getCollectionNames",
"openCollection");

View File

@ -1,4 +1,4 @@
// Copyright (c) 2018, 2022, Oracle and/or its affiliates.
// Copyright (c) 2018, 2023, Oracle and/or its affiliates.
//-----------------------------------------------------------------------------
//
@ -62,7 +62,7 @@ class SodaDocCursor {
}
nodbUtil.wrap_fns(SodaDocCursor.prototype,
nodbUtil.wrapFns(SodaDocCursor.prototype,
"close",
"getNext");

View File

@ -1,4 +1,4 @@
// Copyright (c) 2018, 2022, Oracle and/or its affiliates.
// Copyright (c) 2018, 2023, Oracle and/or its affiliates.
//-----------------------------------------------------------------------------
//
@ -207,7 +207,7 @@ class SodaOperation {
}
nodbUtil.wrap_fns(SodaOperation.prototype,
nodbUtil.wrapFns(SodaOperation.prototype,
"count",
"getCursor",
"getDocuments",

View File

@ -178,12 +178,12 @@ function preventConcurrent(func, errorCode) {
};
}
// The wrap_fns() function is used to wrap the named methods on the prototype
// The wrapFns() function is used to wrap the named methods on the prototype
// in multiple ways (serialize, preventConcurrent and callbackify); the
// arguments following the formal arguments contain the names of methods to
// wrap on the prototype; if the first extra argument is an error code, it is
// used to wrap to prevent concurrent access
function wrap_fns(proto) {
function wrapFns(proto) {
let nameIndex = 1;
let preventConcurrentErrorCode;
if (typeof arguments[1] === 'number') {
@ -349,5 +349,5 @@ module.exports = {
preventConcurrent,
serialize,
verifySodaDoc,
wrap_fns
wrapFns
};

View File

@ -68,7 +68,6 @@ describe('242. dbObject18.js', () => {
it('242.1.4 set oracledb.dbObjectAsPojo to value of Boolean("false")', function() {
const value = Boolean("false");
console.dir(value);
oracledb.dbObjectAsPojo = value;
assert.strictEqual(oracledb.dbObjectAsPojo, true);
}); // 242.1.4
@ -306,11 +305,6 @@ describe('242. dbObject18.js', () => {
const row = result.rows[0];
assert.strictEqual(row.SPORTNAME, 'Frisbee');
assert.deepStrictEqual(row.TEAM, players);
// console.dir(row.TEAM, { depth: null }); -- Output should be: [
// { SHIRTNUMBER: 11, NAME: 'Elizabeth' },
// { SHIRTNUMBER: 22, NAME: 'Frank' }
// ]
}); // 242.2.1
@ -331,6 +325,10 @@ describe('242. dbObject18.js', () => {
conn = await oracledb.getConnection(dbConfig);
await conn.execute(testsUtil.sqlDropType(TEAM_T));
await conn.execute(testsUtil.sqlDropType(PLAYER_T));
await conn.execute(testsUtil.sqlDropTable(TABLE));
let sql = `
CREATE OR REPLACE TYPE ${PLAYER_T} AS OBJECT (
shirtnumber NUMBER,
@ -353,20 +351,11 @@ describe('242. dbObject18.js', () => {
}); // before()
after(async () => {
let sql = testsUtil.sqlDropTable(TABLE);
await conn.execute(sql);
sql = testsUtil.sqlDropType(TEAM_T);
await conn.execute(sql);
sql = testsUtil.sqlDropType(PLAYER_T);
await conn.execute(sql);
await conn.execute(testsUtil.sqlDropType(TEAM_T));
await conn.execute(testsUtil.sqlDropType(PLAYER_T));
await conn.execute(testsUtil.sqlDropTable(TABLE));
await conn.close();
oracledb.dbObjectAsPojo = false;
}); // after()
it('242.3.1 set oracledb.dbObjectAsPojo = true and dbObjectAsPojo:false in bind option', async () => {
@ -397,7 +386,6 @@ describe('242. dbObject18.js', () => {
assert.strictEqual(row.TEAM[0]['NAME'], 'Elizabeth');
assert.strictEqual(row.TEAM[1]['SHIRTNUMBER'], 22);
assert.strictEqual(row.TEAM[1]['NAME'], 'Frank');
// console.dir(row.TEAM, { depth: null }); -- Output should be: DbObject [SYSTEM.NODB_TYP_TEAM_18] {}
}); // 242.3.1
@ -430,13 +418,10 @@ describe('242. dbObject18.js', () => {
const row = result.rows[0];
assert.strictEqual(row.SPORTNAME, 'Frisbee');
await assert.rejects(
async () => { //eslint-disable-line
const x = row.TEAM[0]; //eslint-disable-line
},
/DPI-1010/
assert.throws(
() => row.TEAM[0],
/NJS-500:/
);
// DPI-1010: not connected
// restore the connection
conn = await oracledb.getConnection(dbConfig);
@ -523,7 +508,6 @@ describe('242. dbObject18.js', () => {
const row = result.rows[0];
assert.strictEqual(row.SPORTNAME, 'Frisbee');
assert.deepStrictEqual(row.TEAM, players);
// console.dir(row.TEAM, { depth: null });
}); // 242.4.1
@ -557,7 +541,6 @@ describe('242. dbObject18.js', () => {
const row = result.rows[0];
assert.strictEqual(row.SPORTNAME, 'Frisbee');
assert.deepStrictEqual(row.TEAM, players);
// console.dir(row.TEAM, { depth: null });
// restore the connection
conn = await oracledb.getConnection(dbConfig);

View File

@ -102,7 +102,7 @@ describe('170. poolDrain.js', () => {
await assert.rejects(
async () => await conn.execute('select (7+8) from dual'),
/DPI-1010:/
/NJS-500:/
);
}); // 170.5