Skip to content

Commit 87610ef

Browse files
committed
Add JSON Relational Duality and Fetch Type Handler Tests
1 parent e97aae1 commit 87610ef

File tree

2 files changed

+113
-7
lines changed

2 files changed

+113
-7
lines changed

test/fetchTypeHandler.js

Lines changed: 107 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ describe('271. fetchTypeHandler.js', function() {
317317
if (connection.oracleServerVersion < 2100000000 || testsUtil.getClientVersion() < 2100000000) {
318318
this.skip();
319319
}
320-
320+
const TABLE = 'jsondata';
321321
oracledb.fetchTypeHandler = function() {
322322
const myConverter = (v) => {
323323
v.empId = 10;
@@ -326,21 +326,21 @@ describe('271. fetchTypeHandler.js', function() {
326326
return {converter: myConverter};
327327
};
328328

329-
const createTable = (`CREATE TABLE jsondata (
329+
const createTable = (`CREATE TABLE ${TABLE} (
330330
obj_data JSON
331331
)
332332
`);
333-
const plsql = testsUtil.sqlCreateTable('jsondata', createTable);
333+
const plsql = testsUtil.sqlCreateTable(TABLE, createTable);
334334
await connection.execute(plsql);
335335

336-
const sql = `INSERT into jsondata VALUES ('{"empId": 1, "empName": "Employee1", "city": "New City"}')`;
336+
const sql = `INSERT into ${TABLE} VALUES ('{"empId": 1, "empName": "Employee1", "city": "New City"}')`;
337337
await connection.execute(sql);
338338

339-
const result = await connection.execute("select * from jsondata");
339+
const result = await connection.execute(`select * from ${TABLE}`);
340340
assert.strictEqual(result.rows[0][0]["empId"], 10);
341341
assert.strictEqual(result.rows[0][0]["empName"], 'Employee1');
342342
assert.strictEqual(result.rows[0][0]["city"], 'New City');
343-
await connection.execute(`DROP TABLE jsondata PURGE`);
343+
await connection.execute(testsUtil.sqlDropTable(TABLE));
344344
});
345345

346346
/*
@@ -384,4 +384,104 @@ describe('271. fetchTypeHandler.js', function() {
384384
assert.strictEqual(result.rows[0].TS_NUM, numResults[numStrs.indexOf(element)]);
385385
}
386386
});
387-
});
387+
388+
it('271.20 setting a private property in the metadata', async function() {
389+
oracledb.fetchTypeHandler = function(metadata) {
390+
metadata._privateProp = 'I am a private property of ' + metadata.name;
391+
};
392+
393+
const sql = `select 5 as "MyId", 6 as "MyValue", 'A string' as "MyString" from dual`;
394+
const result = await connection.execute(sql);
395+
396+
assert.strictEqual(result.metaData[0]._privateProp, "I am a private property of MyId");
397+
assert.strictEqual(result.metaData[1]._privateProp, "I am a private property of MyValue");
398+
assert.strictEqual(result.metaData[2]._privateProp, "I am a private property of MyString");
399+
});
400+
401+
it('271.21 fetchTypeHandler for nulls with converter function', async function() {
402+
oracledb.fetchTypeHandler = function() {
403+
const myConverter = (v) => {
404+
return String(v);
405+
};
406+
return {converter: myConverter};
407+
};
408+
409+
const sql = `SELECT NULL FROM DUAL`;
410+
const result = await connection.execute(sql);
411+
assert.strictEqual(result.rows[0][0], "null");
412+
});
413+
414+
it('271.22 converter function to convert column val to string', async function() {
415+
const TABLE = 't';
416+
const sql = `CREATE TABLE ${TABLE} (n_col NUMBER)`;
417+
const plsql = testsUtil.sqlCreateTable(TABLE, sql);
418+
await connection.execute(plsql);
419+
const inssql = `INSERT INTO ${TABLE} (n_col) VALUES (:bv)`;
420+
await connection.execute(inssql, { bv: 123 });
421+
422+
function fetchTypeHandlerFunc(metadata) {
423+
if (metadata.dbType === oracledb.DB_TYPE_NUMBER) {
424+
return {converter: convertToString};
425+
}
426+
};
427+
428+
async function convertToString(val) {
429+
if (val !== null) {
430+
val = 'abc';
431+
}
432+
return val;
433+
}
434+
435+
const result = await connection.execute(
436+
`select * from ${TABLE}`,
437+
[],
438+
{
439+
fetchTypeHandler: fetchTypeHandlerFunc,
440+
outFormat: oracledb.OUT_FORMAT_OBJECT
441+
}
442+
);
443+
444+
assert.strictEqual(result.rows[0].N_COL, 'abc');
445+
await connection.execute(testsUtil.sqlDropTable(TABLE));
446+
});
447+
448+
it('271.23 converter function with multiple columns', async function() {
449+
await connection.execute("alter session set time_zone = '+0:00'");
450+
451+
oracledb.fetchTypeHandler = function(metadata) {
452+
if (metadata.dbTypeName === "TIMESTAMP") {
453+
return {type: oracledb.DATE};
454+
}
455+
else if(metadata.dbTypeName === "NUMBER") {
456+
return {type: oracledb.STRING};
457+
}
458+
};
459+
const TABLE = 'my_table';
460+
const sql = `CREATE TABLE ${TABLE} (
461+
id NUMBER,
462+
name VARCHAR2(50),
463+
age NUMBER,
464+
created_date TIMESTAMP
465+
)`;
466+
const plsql = testsUtil.sqlCreateTable(TABLE, sql);
467+
await connection.execute(plsql);
468+
469+
await connection.execute(`INSERT INTO ${TABLE} values (01, 'ABC', 23,
470+
TO_TIMESTAMP('2023-04-27 10:30:00', 'YYYY-MM-DD HH24:MI:SS'))`);
471+
const result = await connection.execute(`
472+
SELECT id, name, age,
473+
created_date AS TS_DATE FROM ${TABLE}`,
474+
[],
475+
{
476+
outFormat: oracledb.OUT_FORMAT_OBJECT
477+
}
478+
);
479+
480+
assert.deepEqual(Object.getOwnPropertyNames(result.rows[0]), ["ID","NAME","AGE","TS_DATE"]);
481+
assert.deepEqual(result.rows[0].ID, "1");
482+
assert.deepEqual(result.rows[0].NAME, "ABC");
483+
assert.deepEqual(result.rows[0].AGE, "23");
484+
assert.deepEqual(result.rows[0].TS_DATE, new Date('2023-04-27T10:30:00.000Z'));
485+
await connection.execute(testsUtil.sqlDropTable(TABLE));
486+
});
487+
});

test/opts/.mocharc.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,9 @@ spec:
254254
- test/poolTimeout.js
255255
- test/binding_buffer_string.js
256256
- test/fetchTypeHandler.js
257+
- test/jsonDualityViews1.js
258+
- test/jsonDualityViews2.js
259+
- test/jsonDualityViews3.js
260+
- test/jsonDualityViews4.js
261+
- test/jsonDualityViews5.js
262+
- test/jsonDualityViews6.js

0 commit comments

Comments
 (0)