node-oracledb/test/autoCommit.js

398 lines
11 KiB
JavaScript
Raw Normal View History

/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
2015-07-20 12:42:12 +08:00
/******************************************************************************
*
* 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.
2016-03-24 14:09:53 +08:00
*
* The node-oracledb test suite uses 'mocha', 'should' and 'async'.
2015-07-20 12:42:12 +08:00
* See LICENSE.md for relevant licenses.
*
* NAME
* 7. autoCommit.js
*
* DESCRIPTION
* Testing general autoCommit feature.
*
* 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
2016-03-24 14:09:53 +08:00
*
2015-07-20 12:42:12 +08:00
*****************************************************************************/
'use strict';
2015-07-20 12:42:12 +08:00
var oracledb = require('oracledb');
var should = require('should');
var async = require('async');
var dbConfig = require('./dbconfig.js');
2015-07-20 12:42:12 +08:00
2015-08-17 14:19:36 +08:00
describe('7. autoCommit.js', function() {
var pool = null;
var connection = null;
before('create pool, get one connection, create table', function(done) {
2016-03-24 14:09:53 +08:00
var script =
2015-08-17 14:19:36 +08:00
"BEGIN \
DECLARE \
2017-06-14 09:54:15 +08:00
e_table_missing EXCEPTION; \
PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \
2015-08-17 14:19:36 +08:00
BEGIN \
2017-06-14 09:54:15 +08:00
EXECUTE IMMEDIATE ('DROP TABLE nodb_commit_dept purge'); \
2015-08-17 14:19:36 +08:00
EXCEPTION \
2017-06-14 09:54:15 +08:00
WHEN e_table_missing \
2015-08-17 14:19:36 +08:00
THEN NULL; \
END; \
EXECUTE IMMEDIATE (' \
2017-06-14 09:54:15 +08:00
CREATE TABLE nodb_commit_dept ( \
2015-08-17 14:19:36 +08:00
department_id NUMBER, \
department_name VARCHAR2(20) \
) \
'); \
END; ";
2015-07-20 12:42:12 +08:00
async.series([
2015-08-17 14:19:36 +08:00
function(callback) {
oracledb.createPool(
{
2016-05-16 07:57:53 +08:00
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString,
2015-08-17 14:19:36 +08:00
poolMin : 3,
poolMax : 7,
poolIncrement : 1
},
function(err, connectionPool) {
should.not.exist(err);
pool = connectionPool;
callback();
}
);
},
function(callback) {
pool.getConnection( function(err, conn) {
should.not.exist(err);
2015-07-20 12:42:12 +08:00
connection = conn;
callback();
2016-03-24 14:09:53 +08:00
});
2015-07-20 12:42:12 +08:00
},
2015-08-17 14:19:36 +08:00
function(callback) {
2016-03-24 14:09:53 +08:00
connection.execute(
2015-08-17 14:19:36 +08:00
script,
function(err) {
should.not.exist(err);
callback();
}
);
2015-07-20 12:42:12 +08:00
}
2015-08-17 14:19:36 +08:00
], done);
2017-06-14 09:54:15 +08:00
});
2016-03-24 14:09:53 +08:00
after('drop table, release connection, terminate pool', function(done) {
2015-07-20 12:42:12 +08:00
async.series([
2015-08-17 14:19:36 +08:00
function(callback) {
2015-07-20 12:42:12 +08:00
connection.execute(
2017-06-14 09:54:15 +08:00
"DROP TABLE nodb_commit_dept purge",
2015-08-17 14:19:36 +08:00
function(err) {
should.not.exist(err);
2015-07-20 12:42:12 +08:00
callback();
}
);
},
2015-08-17 14:19:36 +08:00
function(callback) {
connection.release( function(err) {
should.not.exist(err);
2015-07-20 12:42:12 +08:00
callback();
});
},
2015-08-17 14:19:36 +08:00
function(callback) {
pool.terminate(function(err) {
should.not.exist(err);
2015-07-20 12:42:12 +08:00
callback();
});
}
], done);
2017-06-14 09:54:15 +08:00
});
2015-08-17 14:19:36 +08:00
afterEach('truncate table, reset the oracledb properties', function(done) {
oracledb.autoCommit = false; /* Restore to default value */
connection.execute(
2017-06-14 09:54:15 +08:00
"TRUNCATE TABLE nodb_commit_dept",
2015-08-17 14:19:36 +08:00
function(err) {
should.not.exist(err);
done();
2016-03-24 14:09:53 +08:00
}
2015-08-17 14:19:36 +08:00
);
2017-06-14 09:54:15 +08:00
});
2016-03-24 14:09:53 +08:00
2015-08-17 14:19:36 +08:00
it('7.1 autoCommit takes effect when setting oracledb.autoCommit before connecting', function(done) {
var conn1 = null;
var conn2 = null;
oracledb.autoCommit = true;
2015-07-20 12:42:12 +08:00
async.series([
2015-08-17 14:19:36 +08:00
function(callback) {
pool.getConnection(
function(err, conn) {
should.not.exist(err);
conn1 = conn;
callback();
}
);
},
function(callback) {
conn1.execute(
2017-06-14 09:54:15 +08:00
"INSERT INTO nodb_commit_dept VALUES (82, 'Security')",
2015-08-17 14:19:36 +08:00
function(err) {
2015-07-20 12:42:12 +08:00
should.not.exist(err);
callback();
}
);
},
2015-08-17 14:19:36 +08:00
function(callback) { // get another connection
pool.getConnection(
function(err, conn) {
should.not.exist(err);
conn2 = conn;
callback();
}
);
},
function(callback) {
conn2.execute(
2017-06-14 09:54:15 +08:00
"SELECT department_id FROM nodb_commit_dept WHERE department_name = 'Security'",
2015-08-17 14:19:36 +08:00
[],
{ outFormat: oracledb.OBJECT },
function(err, result) {
2015-07-20 12:42:12 +08:00
should.not.exist(err);
2017-06-14 09:54:15 +08:00
result.rows[0].DEPARTMENT_ID.should.eql(82).and.be.a.Number();
2015-07-20 12:42:12 +08:00
callback();
}
);
2015-08-17 14:19:36 +08:00
},
function(callback) {
conn1.execute(
2017-06-14 09:54:15 +08:00
"UPDATE nodb_commit_dept SET department_id = 101 WHERE department_name = 'Security'",
2015-08-17 14:19:36 +08:00
function(err){
should.not.exist(err);
callback();
}
);
},
function(callback) {
conn2.execute(
2017-06-14 09:54:15 +08:00
"SELECT department_id FROM nodb_commit_dept WHERE department_name = 'Security'",
2015-08-17 14:19:36 +08:00
[],
{ outFormat: oracledb.OBJECT },
function(err, result) {
should.not.exist(err);
2017-06-14 09:54:15 +08:00
result.rows[0].DEPARTMENT_ID.should.eql(101).and.be.a.Number();
2015-08-17 14:19:36 +08:00
callback();
}
);
},
function(callback) {
conn1.release(function(err) {
should.not.exist(err);
callback();
});
},
function(callback) {
conn2.release(function(err) {
should.not.exist(err);
callback();
});
2015-07-20 12:42:12 +08:00
}
], done);
2017-06-14 09:54:15 +08:00
});
2015-08-17 14:19:36 +08:00
it('7.2 autoCommit takes effect when setting oracledb.autoCommit after connecting', function(done) {
var conn1 = null;
var conn2 = null;
2015-07-20 12:42:12 +08:00
async.series([
2015-08-17 14:19:36 +08:00
function(callback) {
pool.getConnection(
function(err, conn) {
should.not.exist(err);
conn1 = conn;
callback();
}
);
},
function(callback) {
oracledb.autoCommit = true; // change autoCommit after connection
conn1.execute(
2017-06-14 09:54:15 +08:00
"INSERT INTO nodb_commit_dept VALUES (82, 'Security')",
2015-08-17 14:19:36 +08:00
function(err) {
2015-07-20 12:42:12 +08:00
should.not.exist(err);
callback();
}
);
},
2016-03-24 14:09:53 +08:00
function(callback) {
2015-08-17 14:19:36 +08:00
pool.getConnection(
function(err, conn) {
should.not.exist(err);
conn2 = conn;
callback();
}
);
},
function(callback) {
conn2.execute(
2017-06-14 09:54:15 +08:00
"SELECT department_id FROM nodb_commit_dept WHERE department_name = 'Security'",
2015-08-17 14:19:36 +08:00
[],
{ outFormat: oracledb.OBJECT },
function(err, result) {
should.not.exist(err);
2017-06-14 09:54:15 +08:00
result.rows[0].DEPARTMENT_ID.should.eql(82).and.be.a.Number();
2015-08-17 14:19:36 +08:00
callback();
}
);
},
function(callback) {
conn1.execute(
2017-06-14 09:54:15 +08:00
"UPDATE nodb_commit_dept SET department_id = 101 WHERE department_name = 'Security'",
2015-07-20 12:42:12 +08:00
function(err){
should.not.exist(err);
callback();
}
);
},
2015-08-17 14:19:36 +08:00
function(callback) {
conn2.execute(
2017-06-14 09:54:15 +08:00
"SELECT department_id FROM nodb_commit_dept WHERE department_name = 'Security'",
2015-08-17 14:19:36 +08:00
[],
{ outFormat: oracledb.OBJECT },
function(err, result) {
2015-07-20 12:42:12 +08:00
should.not.exist(err);
2017-06-14 09:54:15 +08:00
result.rows[0].DEPARTMENT_ID.should.eql(101).and.be.a.Number();
2015-07-20 12:42:12 +08:00
callback();
}
);
2015-08-17 14:19:36 +08:00
},
function(callback) {
conn1.release(function(err) {
should.not.exist(err);
callback();
});
},
function(callback) {
conn2.release(function(err) {
should.not.exist(err);
callback();
});
2015-07-20 12:42:12 +08:00
}
], done);
2017-06-14 09:54:15 +08:00
});
2015-08-17 14:19:36 +08:00
it('7.3 autoCommit setting does not affect previous SQL result', function(done) {
var conn1 = null;
var conn2 = null;
2016-03-24 14:09:53 +08:00
2015-08-17 14:19:36 +08:00
async.series([
function(callback) {
pool.getConnection(
function(err, conn) {
should.not.exist(err);
conn1 = conn;
callback();
}
);
},
function(callback) {
conn1.execute(
2017-06-14 09:54:15 +08:00
"INSERT INTO nodb_commit_dept VALUES (82, 'Security')",
2015-08-17 14:19:36 +08:00
function(err) {
should.not.exist(err);
callback();
}
);
},
2016-03-24 14:09:53 +08:00
function(callback) {
2015-08-17 14:19:36 +08:00
pool.getConnection(
function(err, conn) {
should.not.exist(err);
conn2 = conn;
callback();
}
);
},
function(callback) {
oracledb.autoCommit = true; // change autoCommit after connection
conn2.execute(
2017-06-14 09:54:15 +08:00
"SELECT department_id FROM nodb_commit_dept WHERE department_name = 'Security'",
2015-08-17 14:19:36 +08:00
[],
{ outFormat: oracledb.OBJECT },
function(err, result) {
should.not.exist(err);
(result.rows).should.eql([]);
callback();
}
);
},
function(callback) {
conn2.execute(
2017-06-14 09:54:15 +08:00
"INSERT INTO nodb_commit_dept VALUES (99, 'Marketing')",
2015-08-17 14:19:36 +08:00
function(err) {
should.not.exist(err);
callback();
}
);
},
function(callback) {
conn2.execute(
2017-06-14 09:54:15 +08:00
"SELECT COUNT(*) as amount FROM nodb_commit_dept",
2015-08-17 14:19:36 +08:00
[],
{ outFormat: oracledb.OBJECT },
function(err, result) {
should.not.exist(err);
result.rows[0].AMOUNT.should.eql(1);
callback();
}
);
},
function(callback) {
conn1.execute(
2017-06-14 09:54:15 +08:00
"SELECT COUNT(*) as amount FROM nodb_commit_dept",
2015-08-17 14:19:36 +08:00
[],
{ outFormat: oracledb.OBJECT },
function(err, result) {
should.not.exist(err);
result.rows[0].AMOUNT.should.eql(2); // autoCommit for SELECT
callback();
}
);
},
function(callback) {
conn1.release(function(err) {
should.not.exist(err);
callback();
});
},
function(callback) {
conn2.release(function(err) {
should.not.exist(err);
callback();
});
}
], done);
2017-06-14 09:54:15 +08:00
});
2015-08-17 14:19:36 +08:00
2017-06-14 09:54:15 +08:00
});