node-oracledb/test/v8Getter.js

422 lines
12 KiB
JavaScript

/* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. */
/******************************************************************************
*
* You may not use the identified files except in compliance with the Apache
* License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
* The node-oracledb test suite uses 'mocha', 'should' and 'async'.
* See LICENSE.md for relevant licenses.
*
* NAME
* 140. v8Getter.js
*
* DESCRIPTION
* Test v8 object getter functions. These cases overwrite the getter()
* functions.
*
* NUMBERING RULE
* Test numbers follow this numbering rule:
* 1 - 20 are reserved for basic functional tests
* 21 - 50 are reserved for data type supporting tests
* 51 onwards are for other tests
*
*****************************************************************************/
'use strict';
var oracledb = require('oracledb');
var should = require('should');
var async = require('async');
var dbConfig = require('./dbconfig.js');
describe('140. v8Getter.js', function() {
var connection = null;
var tableName = "nodb_tab_v8getter";
before(function(done) {
async.series([
function(cb) {
oracledb.getConnection(dbConfig, function(err, conn) {
should.not.exist(err);
connection = conn;
cb();
});
},
function(cb) {
var proc = "BEGIN \n" +
" DECLARE \n" +
" e_table_missing EXCEPTION; \n" +
" PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \n" +
" BEGIN \n" +
" EXECUTE IMMEDIATE('DROP TABLE " + tableName + " PURGE'); \n" +
" EXCEPTION \n" +
" WHEN e_table_missing \n" +
" THEN NULL; \n" +
" END; \n" +
" EXECUTE IMMEDIATE (' \n" +
" CREATE TABLE " + tableName + " ( \n" +
" department_id NUMBER, \n" +
" department_name VARCHAR2(50) \n" +
" ) \n" +
" '); \n" +
"END; ";
connection.execute(proc, function(err) {
should.not.exist(err);
cb();
});
},
function(cb) {
var sql = "INSERT INTO " + tableName + " VALUES(23, 'Jonh Smith')";
connection.execute(sql, function(err) {
should.not.exist(err);
cb();
});
}
], done);
}); // before
after(function(done) {
async.series([
function(cb) {
var sql = "DROP TABLE " + tableName + " PURGE";
connection.execute(sql, function(err) {
should.not.exist(err);
cb();
});
},
function(cb) {
connection.release(function(err) {
should.not.exist(err);
cb();
});
}
], done);
}); // after
describe('140.1 Negative: overwrite the getter() function of bind in objects', function() {
var sql = "select * from " + tableName + " where department_id = :id";
var bindObj = {id: 23};
Object.defineProperty(bindObj, 'id', {
get: function() {
throw 'Nope';
}
});
it('140.1.1 ProcessBindsByName()', function(done) {
connection.execute(
sql,
bindObj,
function(err, result) {
should.exist(err);
should.strictEqual(
err.message,
"Uncaught Nope"
);
should.not.exist(result);
done();
}
);
});
it('140.1.2 ProcessBindsByPos()', function(done) {
connection.execute(
sql,
[bindObj],
function(err, result) {
should.exist(err);
should.strictEqual(
err.message,
"NJS-044: named JSON object is not expected in this context"
);
should.not.exist(result);
done();
}
);
});
}); // 140.1
describe('140.2 Negative (ProcessBind): OUT bind with properties altered', function() {
it('140.2.1 ', function(done) {
async.series([
function doCreateProcedure(cb) {
var proc = "CREATE OR REPLACE PROCEDURE nodb_proc_v8_out (p_in IN VARCHAR2, p_out OUT VARCHAR2) \n" +
"AS \n" +
"BEGIN \n" +
" p_out := 'OUT: ' || p_in; \n" +
"END; ";
connection.execute(proc, function(err) {
should.not.exist(err);
cb();
});
},
function doTest(cb) {
var foo = { dir: oracledb.BIND_OUT, type: oracledb.STRING, maxSize: 200 };
Object.defineProperty(foo, 'dir', {
get: function() {
throw 'No Dir';
}
});
Object.defineProperty(foo, 'type', {
get: function() {
throw 'No Type';
}
});
Object.defineProperty(foo, 'maxSize', {
get: function() {
throw 'No maxSize';
}
});
connection.execute(
"begin nodb_proc_v8_out(:i, :o); end;",
{
i: "Changjie",
o: foo
},
function(err, result) {
should.exist(err);
should.strictEqual(
err.message,
"Uncaught No Dir"
);
should.not.exist(result);
cb();
}
);
},
function doDropProcedure(cb) {
var sql = "drop procedure nodb_proc_v8_out";
connection.execute(sql, function(err) {
should.not.exist(err);
cb();
});
}
], done);
});
}); // 140.2
describe('140.3 Negative: PL/SQL Indexed Table', function() {
before(function(done) {
async.series([
function doCreateTable(cb) {
var proc = "BEGIN \n" +
" DECLARE \n" +
" e_table_missing EXCEPTION; \n" +
" PRAGMA EXCEPTION_INIT(e_table_missing, -00942);\n " +
" BEGIN \n" +
" EXECUTE IMMEDIATE ('DROP TABLE nodb_tab_waveheight PURGE'); \n" +
" EXCEPTION \n" +
" WHEN e_table_missing \n" +
" THEN NULL; \n" +
" END; \n" +
" EXECUTE IMMEDIATE (' \n" +
" CREATE TABLE nodb_tab_waveheight (beach VARCHAR2(50), depth NUMBER) \n" +
" '); \n" +
"END; ";
connection.execute(
proc,
function(err) {
should.not.exist(err);
cb();
}
);
},
function doCreatePkg(cb) {
var proc = "CREATE OR REPLACE PACKAGE nodb_v8pkg IS \n" +
" TYPE beachType IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER;\n" +
" TYPE depthType IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; \n" +
" PROCEDURE array_in(beaches IN beachType, depths IN depthType); \n" +
"END; ";
connection.execute(
proc,
function(err) {
should.not.exist(err);
cb();
}
);
},
function doCreatePkgbody(cb) {
var proc = "CREATE OR REPLACE PACKAGE BODY nodb_v8pkg IS \n" +
" PROCEDURE array_in(beaches IN beachType, depths IN depthType) IS \n" +
" BEGIN \n" +
" IF beaches.COUNT <> depths.COUNT THEN \n" +
" RAISE_APPLICATION_ERROR(-20000, 'Array lengths must match for this example.');" +
" END IF; \n"+
" FORALL i IN INDICES OF beaches \n" +
" INSERT INTO nodb_tab_waveheight (beach, depth) VALUES (beaches(i), depths(i)); \n" +
" END; \n" +
"END;";
connection.execute(
proc,
function(err) {
should.not.exist(err);
cb();
}
);
}
], done);
}); // before
after(function(done) {
async.series([
function doDropPkg(cb) {
connection.execute(
"drop package nodb_v8pkg",
function(err) {
should.not.exist(err);
cb();
}
);
},
function doDropTable(cb) {
connection.execute(
"drop table nodb_tab_waveheight",
function(err) {
should.not.exist(err);
cb();
}
);
}
], done);
}); // after
it('140.3.1 bind an element being altered-JSON object', function(done) {
var foo =
{
beach_in: { type: oracledb.STRING, dir: oracledb.BIND_IN, val: ["Malibu Beach", "Bondi Beach", "Waikiki Beach"] },
depth_in: { type: oracledb.NUMBER, dir: oracledb.BIND_IN, val: [45, 30, 67] }
};
Object.defineProperty(foo, 'depth_in', {
get: function() {
throw 'No type';
}
});
connection.execute(
"BEGIN nodb_v8pkg.array_in(:beach_in, :depth_in); END;",
foo,
function(err) {
should.exist(err);
should.strictEqual(
err.message,
"Uncaught No type"
);
done();
}
);
}); // 140.3.1
it('140.3.2 GetBindTypeAndSizeFromValue()', function(done) {
var foo = { type: oracledb.NUMBER, dir: oracledb.BIND_IN, val: [45, 30, 67] };
Object.defineProperty(foo, 'type', {
get: function() {
throw 'No type';
}
});
connection.execute(
"BEGIN nodb_v8pkg.array_in(:beach_in, :depth_in); END;",
{
beach_in: { type: oracledb.STRING,
dir: oracledb.BIND_IN,
val: ["Malibu Beach", "Bondi Beach", "Waikiki Beach"] },
depth_in: foo
},
function(err, result) {
should.exist(err);
should.strictEqual(
err.message,
"Uncaught No type"
);
should.not.exist(result);
done();
}
);
}); // 140.3.2
}); // 140.3
describe('140.4 Negative: fetchInfo', function() {
it('140.4.1 changes getter() of fetchInfo itself', function(done) {
var foo = {
outFormat: oracledb.OBJECT,
fetchInfo : { "TS_DATE": { type : oracledb.STRING } }
};
Object.defineProperty(foo, 'fetchInfo', {
get: function() {
throw 'No fetchInfo';
}
});
connection.execute(
"SELECT TO_DATE('2017-01-09', 'YYYY-DD-MM') AS TS_DATE FROM DUAL",
[],
foo,
function(err, result) {
should.exist(err);
should.strictEqual(
err.message,
"Uncaught No fetchInfo"
);
should.not.exist(result);
done();
}
);
}); // 140.4.1
it('140.4.2 changes getter() of the value of fetchInfo object', function(done) {
var foo = { type : oracledb.STRING };
Object.defineProperty(foo, 'type', {
get: function() {
throw 'No type';
}
});
var option = {
outFormat: oracledb.OBJECT,
fetchInfo : { "TS_DATE": foo }
};
connection.execute(
"SELECT TO_DATE('2017-01-09', 'YYYY-DD-MM') AS TS_DATE FROM DUAL",
[],
option,
function(err, result) {
should.exist(err);
should.strictEqual(
err.message,
"Uncaught No type"
);
should.not.exist(result);
done();
}
);
}); // 140.4.2
}); // 140.4
});