-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathautoCommit.js
270 lines (217 loc) · 8.14 KB
/
autoCommit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Copyright (c) 2015, 2023, Oracle and/or its affiliates. */
/******************************************************************************
*
* This software is dual-licensed to you under the Universal Permissive License
* (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
* 2.0 as shown at https://www.apache.org/licenses/LICENSE-2.0. You may choose
* either license.
*
* If you elect to accept the software under the Apache License, Version 2.0,
* the following applies:
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://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.
*
* NAME
* 7. autoCommit.js
*
* DESCRIPTION
* Testing general autoCommit feature.
*
*****************************************************************************/
'use strict';
const oracledb = require('oracledb');
const assert = require('assert');
const dbConfig = require('./dbconfig.js');
describe('7. autoCommit.js', function() {
let pool = null;
let connection = null;
before('create pool, get one connection, create table', async function() {
const script =
"BEGIN \
DECLARE \
e_table_missing EXCEPTION; \
PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \
BEGIN \
EXECUTE IMMEDIATE ('DROP TABLE nodb_commit_dept purge'); \
EXCEPTION \
WHEN e_table_missing \
THEN NULL; \
END; \
EXECUTE IMMEDIATE (' \
CREATE TABLE nodb_commit_dept ( \
department_id NUMBER, \
department_name VARCHAR2(20) \
) \
'); \
END; ";
pool = await oracledb.createPool(
{
...dbConfig,
poolMin: 3,
poolMax: 7,
poolIncrement: 1
});
connection = await pool.getConnection();
await connection.execute(script);
});
after('drop table, release connection, terminate pool', async function() {
await connection.execute("DROP TABLE nodb_commit_dept purge");
await connection.close();
await pool.terminate();
});
afterEach('truncate table, reset the oracledb properties', async function() {
oracledb.autoCommit = false; /* Restore to default value */
await connection.execute("TRUNCATE TABLE nodb_commit_dept");
});
it('7.1 autoCommit takes effect when setting oracledb.autoCommit before connecting', async function() {
let conn1 = null;
let conn2 = null;
let result = null;
oracledb.autoCommit = true;
conn1 = await pool.getConnection();
await conn1.execute("INSERT INTO nodb_commit_dept VALUES (82, 'Security')");
// get another connection
conn2 = await pool.getConnection();
result = await conn2.execute(
"SELECT department_id FROM nodb_commit_dept WHERE department_name = 'Security'",
[],
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.strictEqual(result.rows[0].DEPARTMENT_ID, 82);
assert.strictEqual(typeof (result.rows[0].DEPARTMENT_ID), "number");
await conn1.execute(
"UPDATE nodb_commit_dept SET department_id = 101 WHERE department_name = 'Security'");
result = await conn2.execute(
"SELECT department_id FROM nodb_commit_dept WHERE department_name = 'Security'",
[],
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.strictEqual(result.rows[0].DEPARTMENT_ID, 101);
assert.strictEqual(typeof (result.rows[0].DEPARTMENT_ID), "number");
await conn1.release();
await conn2.release();
});
it('7.2 autoCommit takes effect when setting oracledb.autoCommit after connecting', async function() {
let conn1 = null;
let conn2 = null;
let result = null;
conn1 = await pool.getConnection();
oracledb.autoCommit = true; // change autoCommit after connection
await conn1.execute("INSERT INTO nodb_commit_dept VALUES (82, 'Security')");
conn2 = await pool.getConnection();
result = await conn2.execute(
"SELECT department_id FROM nodb_commit_dept WHERE department_name = 'Security'",
[],
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.strictEqual(result.rows[0].DEPARTMENT_ID, 82);
assert.strictEqual(typeof (result.rows[0].DEPARTMENT_ID), "number");
await conn1.execute("UPDATE nodb_commit_dept SET department_id = 101 WHERE department_name = 'Security'");
result = await conn2.execute(
"SELECT department_id FROM nodb_commit_dept WHERE department_name = 'Security'",
[],
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.strictEqual(result.rows[0].DEPARTMENT_ID, 101);
assert.strictEqual(typeof (result.rows[0].DEPARTMENT_ID), "number");
await conn1.release();
await conn2.release();
});
it('7.3 autoCommit setting does not affect previous SQL result', async function() {
let conn1 = null;
let conn2 = null;
let result = null;
conn1 = await pool.getConnection();
await conn1.execute("INSERT INTO nodb_commit_dept VALUES (82, 'Security')");
conn2 = await pool.getConnection();
oracledb.autoCommit = true; // change autoCommit after connection
result = await conn2.execute(
"SELECT department_id FROM nodb_commit_dept WHERE department_name = 'Security'",
[],
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.deepStrictEqual(result.rows, []);
await conn2.execute(
"INSERT INTO nodb_commit_dept VALUES (99, 'Marketing')");
result = await conn2.execute(
"SELECT COUNT(*) as amount FROM nodb_commit_dept",
[],
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.strictEqual(result.rows[0].AMOUNT, 1);
result = await conn1.execute(
"SELECT COUNT(*) as amount FROM nodb_commit_dept",
[],
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.strictEqual(result.rows[0].AMOUNT, 2); // autoCommit for SELECT
await conn1.release();
await conn2.release();
});
describe('7.4 global option - oracledb.autoCommit', function() {
let defaultValue;
beforeEach(function() {
defaultValue = oracledb.autoCommit;
});
afterEach(function() {
oracledb.autoCommit = defaultValue;
});
it('7.4.1 Negative - 0', function() {
setAsGlobalOption(0);
});
it('7.4.2 Negative - negative number', function() {
setAsGlobalOption(-1);
});
it('7.4.3 Negative - positive number', function() {
setAsGlobalOption(-1);
});
it('7.4.4 Negative - NaN', function() {
setAsGlobalOption(NaN);
});
it('7.4.5 Negative - undefined', function() {
setAsGlobalOption(undefined);
});
const setAsGlobalOption = function(setValue) {
assert.throws(
function() {
oracledb.autoCommit = setValue;
},
/NJS-004:*/
);
};
});
describe('7.5 set autoCommit as an execute() option', function() {
it('7.5.1 Negative - 0', async function() {
await setAsExecOption(0);
});
it('7.5.2 Negative - negative number', async function() {
await setAsExecOption(-1);
});
it('7.5.3 Negative - positive number', async function() {
await setAsExecOption(-1);
});
it('7.5.4 Negative - NaN', async function() {
await setAsExecOption(NaN);
});
it("7.5.5 works as 'false' when setting to 'undefined'", async function() {
let result = null;
result = await connection.execute(
"select user from dual",
[],
{ autoCommit: undefined });
assert(result);
});
const setAsExecOption = async function(setValue) {
const sql = "select user from dual";
const binds = [];
const options = { autoCommit: setValue };
await assert.rejects(
async () => await connection.execute(sql, binds, options),
/NJS-007:/
);
};
});
});