-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy patheditionTest.js
606 lines (536 loc) · 19.8 KB
/
editionTest.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* Copyright (c) 2018, 2025, 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
* 160. editionTest.js
*
* DESCRIPTION
* Test Edition Based Redefinition.
*
*****************************************************************************/
'use strict';
const oracledb = require('oracledb');
const assert = require('assert');
const dbConfig = require('./dbconfig.js');
const testsUtil = require('./testsUtil.js');
const timestamp = +new Date();
const edition1 = "edition_1_" + timestamp;
const edition2 = "edition_2_" + timestamp;
const schemaEdition = "schema_" + timestamp;
function generateRandomPassword(length) {
let result = "";
const choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (let i = 0; i < length; i++) {
result += choices.charAt(Math.floor(Math.random() * choices.length));
}
return result;
}
describe('160. editionTest.js', function() {
let dbaConn;
const nodbSchemaEditionPassword = generateRandomPassword(6);
const editionList = [];
const getEdition = async function(connection) {
const sql = "begin nodb_proc_edition(:out); end;";
const binds = { out: { type: oracledb.STRING, dir: oracledb.BIND_OUT } };
const result = await connection.execute(sql, binds);
return result.outBinds.out;
};
before(async function() {
let isRunnable = dbConfig.test.DBA_PRIVILEGE && !dbConfig.test.drcp
&& !(await testsUtil.cmanTdmCheck());
if (isRunnable) {
const connection = await oracledb.getConnection(dbConfig);
if (connection.oracleServerVersion < 1201000200) {
isRunnable = false;
}
await connection.close();
}
if (!isRunnable) {
this.skip();
}
//SYSDBA connection
let credential = {
user: dbConfig.test.DBA_user,
password: dbConfig.test.DBA_password,
connectionString: dbConfig.connectString,
privilege: oracledb.SYSDBA
};
dbaConn = await oracledb.getConnection(credential);
//Create the editions
let sql = "BEGIN \n" +
" DECLARE \n" +
" e_edition_missing EXCEPTION; \n" +
" PRAGMA EXCEPTION_INIT(e_edition_missing, -38802); \n" +
" BEGIN \n" +
` EXECUTE IMMEDIATE('DROP EDITION ${edition1} CASCADE'); \n` +
" EXCEPTION \n" +
" WHEN e_edition_missing \n" +
" THEN NULL; \n" +
" END; \n" +
" EXECUTE IMMEDIATE (' \n" +
` CREATE EDITION ${edition1}\n` +
" '); \n" +
"END; ";
await dbaConn.execute(sql);
sql = "BEGIN \n" +
" DECLARE \n" +
" e_edition_missing EXCEPTION; \n" +
" PRAGMA EXCEPTION_INIT(e_edition_missing, -38802); \n" +
" BEGIN \n" +
` EXECUTE IMMEDIATE('DROP EDITION ${edition2} CASCADE'); \n` +
" EXCEPTION \n" +
" WHEN e_edition_missing \n" +
" THEN NULL; \n" +
" END; \n" +
" EXECUTE IMMEDIATE (' \n" +
` CREATE EDITION ${edition2}\n` +
" '); \n" +
"END; ";
await dbaConn.execute(sql);
sql = `SELECT EDITION_NAME, PARENT_EDITION_NAME FROM ALL_EDITIONS`;
const result = await dbaConn.execute(sql);
if (result && result.rows) {
let parentEdition = "ORA$BASE";
for (let i = 1; i < result.rows.length; i++) {
for (let j = 0; j < result.rows.length; j++) {
if (result.rows[j][1] === parentEdition) {
editionList.push(result.rows[j][0]);
parentEdition = result.rows[j][0];
break;
}
}
}
}
// Create user
sql = "BEGIN \n" +
" DECLARE \n" +
" e_user_missing EXCEPTION; \n" +
" PRAGMA EXCEPTION_INIT(e_user_missing, -01918); \n" +
" BEGIN \n" +
` EXECUTE IMMEDIATE('DROP USER ${schemaEdition} CASCADE'); \n` +
" EXCEPTION \n" +
" WHEN e_user_missing \n" +
" THEN NULL; \n" +
" END; \n" +
" EXECUTE IMMEDIATE (' \n" +
` CREATE USER ${schemaEdition} IDENTIFIED BY ${nodbSchemaEditionPassword}\n` +
" '); \n" +
"END; ";
await dbaConn.execute(sql);
sql = `GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, \
CREATE VIEW, CREATE PROCEDURE, CREATE TRIGGER to ${schemaEdition}`;
await dbaConn.execute(sql);
// Allow editions for user
sql = `alter user ${schemaEdition} enable editions`;
await dbaConn.execute(sql);
sql = `grant use on edition ${edition1} to ${schemaEdition}`;
await dbaConn.execute(sql);
sql = `grant use on edition ${edition2} to ${schemaEdition}`;
await dbaConn.execute(sql);
// Get user connection
credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
const conn = await oracledb.getConnection(credential);
// Create procedure (without any editions)
let proc = "CREATE OR REPLACE PROCEDURE nodb_proc_edition (str OUT STRING) \n" +
"AS \n" +
"BEGIN \n" +
" str := 'E0'; \n" +
"END nodb_proc_edition;";
await conn.execute(proc);
// Change to Edition e1
sql = `alter session set edition = ${edition1}`;
await conn.execute(sql);
// Create procedure in context of Edition E1
proc = "CREATE OR REPLACE PROCEDURE nodb_proc_edition (str OUT STRING) \n" +
"AS \n" +
"BEGIN \n" +
" str := 'E1'; \n" +
"END nodb_proc_edition;";
await conn.execute(proc);
// Change to Edition e2
sql = `alter session set edition = ${edition2}`;
await conn.execute(sql);
// Create procedure in context of Edition E2
proc = "CREATE OR REPLACE PROCEDURE nodb_proc_edition (str OUT STRING) \n" +
"AS \n" +
"BEGIN \n" +
" str := 'E2'; \n" +
"END nodb_proc_edition;";
await conn.execute(proc);
//Close the connection
await conn.close();
}); // before()
after(async function() {
if (dbaConn) {
try {
for (let i = editionList.length - 1; i >= 0 ; i--) {
await dbaConn.execute(`DROP EDITION ${editionList[i]} CASCADE`);
}
} catch (err) {
// ORA-38810: Implementation restriction: cannot drop edition that has a parent and a child
assert.match(err.message, /^ORA-38810/);
} finally {
await dbaConn.execute(`DROP USER ${schemaEdition} CASCADE`);
await dbaConn.close();
}
}
}); // after()
it('160.1 Default. No edition. Direct connection.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
const connection = await oracledb.getConnection(credential);
assert.strictEqual(await getEdition(connection), 'E0');
await connection.close();
}); // 160.1
it('160.2 Default. No edition. Pooled connection.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
const pool = await oracledb.createPool(credential);
const connection = await pool.getConnection();
assert.strictEqual(await getEdition(connection), 'E0');
await connection.close();
await pool.close();
}); // 160.2
it('160.3 Direct connection. Set edition at getting connection.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: edition2
};
const connection = await oracledb.getConnection(credential);
assert.strictEqual(await getEdition(connection), 'E2');
await connection.close();
}); // 160.3
it('160.4 Pooled connection. Set edition at creating pool.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: edition1
};
const pool = await oracledb.createPool(credential);
const connection = await pool.getConnection();
assert.strictEqual(await getEdition(connection), 'E1');
await connection.close();
await pool.close();
}); // 160.4
it('160.5 Direct connection. Change session edition.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: edition2
};
const connection = await oracledb.getConnection(credential);
// Change to Edition e1
const sql = `alter session set edition = ${edition1}`;
await connection.execute(sql);
assert.strictEqual(await getEdition(connection), 'E1');
await connection.close();
}); // 160.5
it('160.6 Pooled connection. Change session edition.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: edition1
};
const pool = await oracledb.createPool(credential);
const connection = await pool.getConnection();
// Change to Edition default
const sql = "alter session set edition = ora$base";
await connection.execute(sql);
assert.strictEqual(await getEdition(connection), 'E0');
await connection.close();
await pool.close();
}); // 160.6
it('160.7 sets edition globally. Direct connection.', async function() {
oracledb.edition = edition2;
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
const connection = await oracledb.getConnection(credential);
assert.strictEqual(await getEdition(connection), 'E2');
oracledb.edition = '';
// This global property only takes effect at connection creation.
assert.strictEqual(await getEdition(connection), 'E2');
await connection.close();
}); // 160.7
it('160.8 sets edition globally. Pooled connection.', async function() {
oracledb.edition = edition2;
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
const pool = await oracledb.createPool(credential);
const connection = await pool.getConnection();
assert.strictEqual(await getEdition(connection), 'E2');
oracledb.edition = '';
assert.strictEqual(await getEdition(connection), 'E2');
const conn2 = await pool.getConnection();
assert.strictEqual(await getEdition(conn2), 'E2');
await conn2.close();
await connection.close();
await pool.close();
}); // 160.8
it('160.9 Negative - sets nonexistent edition globally', async function() {
oracledb.edition = 'nonexistence';
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
await assert.rejects(
async () => await oracledb.getConnection(credential),
// ORA-38802: edition does not exist
/ORA-38802:/
);
oracledb.edition = '';
}); // 160.9
it('160.10 Direct connection. Set nonexistent edition.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: "nonexistence"
};
await assert.rejects(
async () => await oracledb.getConnection(credential),
// ORA-38802: edition does not exist
/ORA-38802:/
);
}); // 160.10
it('160.11 Pooled connection. Set nonexistent edition.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: "nonexistence"
};
const pool = await oracledb.createPool(credential);
await assert.rejects(
async () => await pool.getConnection(),
// ORA-38802: edition does not exist
/ORA-38802:/
);
await pool.close();
}); // 160.11
it('160.12 sets to ora$base with direct connection', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: "ora$base"
};
const connection = await oracledb.getConnection(credential);
assert.strictEqual(await getEdition(connection), 'E0');
await connection.close();
}); // 160.12
it('160.13 resets to ora$base in direct connection', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: edition2
};
const connection = await oracledb.getConnection(credential);
// Change to Edition e0
const sql = "alter session set edition = ora$base";
await connection.execute(sql);
assert.strictEqual(await getEdition(connection), 'E0');
await connection.close();
}); // 160.13
it('160.14 sets to ora$base with pooled connection', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: "ora$base"
};
const pool = await oracledb.createPool(credential);
const connection = await pool.getConnection();
assert.strictEqual(await getEdition(connection), 'E0');
await connection.close();
await pool.close();
}); // 160.14
it('160.15 sets to ora$base globally', async function() {
oracledb.edition = 'ora$base';
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
const connection = await oracledb.getConnection(credential);
assert.strictEqual(await getEdition(connection), 'E0');
// This global property only takes effect at connection creation.
oracledb.edition = edition2;
assert.strictEqual(await getEdition(connection), 'E0');
await connection.close();
}); // 160.15
it('160.16 overrides the global setting. Direct connection', async function() {
oracledb.edition = edition1;
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: edition2
};
const connection = await oracledb.getConnection(credential);
assert.strictEqual(await getEdition(connection), 'E2');
oracledb.edition = '';
await connection.close();
}); // 160.16
it('160.17 sets to empty string. Direct connection.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: ""
};
const connection = await oracledb.getConnection(credential);
assert.strictEqual(await getEdition(connection), 'E0');
await connection.close();
}); // 160.17
it('160.18 Negative - invalid type. Direct connection.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: 123
};
await assert.rejects(
async () => await oracledb.getConnection(credential),
/NJS-007:/
);
}); // 160.18
it('160.19 Negative - invalid type. Pooled connection.', async function() {
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: 123
};
await assert.rejects(
async () => await oracledb.createPool(credential),
/NJS-007:/
);
}); // 160.19
it('160.20 sets ORA_EDITION. Direct connection.', async function() {
process.env.ORA_EDITION = edition1;
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
const connection = await oracledb.getConnection(credential);
assert.strictEqual(await getEdition(connection), 'E1');
await connection.close();
delete process.env.ORA_EDITION;
}); // 160.20
it('160.21 sets ORA_EDITION. Pooled connection.', async function() {
process.env.ORA_EDITION = edition2;
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
const pool = await oracledb.createPool(credential);
const connection = await pool.getConnection();
assert.strictEqual(await getEdition(connection), 'E2');
await connection.close();
await pool.close();
delete process.env.ORA_EDITION;
}); // 160.21
it('160.22 sets ORA_EDITION. Direct connection. Set edition at getting connection.', async function() {
process.env.ORA_EDITION = edition1;
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: edition2
};
const connection = await oracledb.getConnection(credential);
assert.strictEqual(await getEdition(connection), 'E2');
await connection.close();
delete process.env.ORA_EDITION;
}); // 160.22
it('160.23 sets ORA_EDITION. Pooled connection. Set edition at creating pool.', async function() {
process.env.ORA_EDITION = edition2;
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString,
edition: edition1
};
const pool = await oracledb.createPool(credential);
const connection = await pool.getConnection();
assert.strictEqual(await getEdition(connection), 'E1');
await connection.close();
await pool.close();
delete process.env.ORA_EDITION;
}); // 160.23
it('160.24 Negative - Sets ORA_EDITION with nonexistent value. Direct connection.', async function() {
process.env.ORA_EDITION = 'nonexistence';
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
await assert.rejects(
async () => await oracledb.getConnection(credential),
// ORA-38802: edition does not exist
/ORA-38802:/
);
delete process.env.ORA_EDITION;
}); // 160.24
it('160.25 Negative - Sets ORA_EDITION with nonexistent value. Pooled connection.', async function() {
process.env.ORA_EDITION = 'nonexistence';
const credential = {
user: schemaEdition,
password: nodbSchemaEditionPassword,
connectionString: dbConfig.connectString
};
const pool = await oracledb.createPool(credential);
await assert.rejects(
async () => await pool.getConnection(),
// ORA-38802: edition does not exist
/ORA-38802:/
);
await pool.close();
delete process.env.ORA_EDITION;
}); // 160.25
});