Skip to content

Commit e9f1d27

Browse files
committed
Refactor tests
1 parent 6f31c22 commit e9f1d27

File tree

2 files changed

+97
-191
lines changed

2 files changed

+97
-191
lines changed

test/clobStream.js

+82-180
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@
3232
'use strict';
3333

3434
const oracledb = require('oracledb');
35-
const should = require('should');
36-
const async = require('async');
35+
const assert = require('assert');
3736
const dbConfig = require('./dbconfig.js');
38-
const file = require('./file.js');
39-
const sql = require('./sql.js');
4037
const fs = require('fs');
38+
const fsPromises = require('fs/promises');
39+
const random = require('./random.js');
4140

4241
describe('128.clobStream.js', function() {
43-
let connection = null;
42+
let connection;
4443
const fileRoot = ".";
4544
let insertID = 1;
46-
let inFileName;
4745

4846
const proc_clob_prepare_tab = "BEGIN \n" +
4947
" DECLARE \n" +
@@ -63,148 +61,81 @@ describe('128.clobStream.js', function() {
6361
" '); \n" +
6462
"END; ";
6563

66-
before(function(done) {
67-
async.series([
68-
function(cb) {
69-
oracledb.getConnection(dbConfig, function(err, conn) {
70-
should.not.exist(err);
71-
connection = conn;
72-
cb();
73-
});
74-
},
75-
function(cb) {
76-
setupAllTable(cb);
77-
}
78-
], done);
79-
}); // before
64+
before(async function() {
65+
connection = await oracledb.getConnection(dbConfig);
66+
await connection.execute(proc_clob_prepare_tab);
67+
});
8068

81-
after(function(done) {
82-
async.series([
83-
function(cb) {
84-
dropAllTable(cb);
85-
},
86-
function(cb) {
87-
connection.release(function(err) {
88-
should.not.exist(err);
89-
cb();
90-
});
91-
}
92-
], done);
93-
}); // after
69+
after(async function() {
70+
await connection.execute("DROP TABLE nodb_tab_lobs_pre PURGE");
71+
await connection.close();
72+
});
9473

95-
beforeEach(function(done) {
74+
beforeEach(function() {
9675
insertID++;
97-
done();
9876
});
9977

10078
describe('128.1 stream txt file into CLOB column', function() {
101-
it('128.1.1 works with 64KB txt file', function(done) {
102-
inFileName = fileRoot + '/smallString.txt';
103-
let selectID = insertID + 200;
104-
let specialStr = '128.1.1';
105-
let fileSize = 65536;
106-
107-
bindIn_small(inFileName, fileSize, selectID, insertID, specialStr, done);
79+
it('128.1.1 works with 64KB txt file', async function() {
80+
const inFileName = fileRoot + '/smallString.txt';
81+
const selectID = insertID + 200;
82+
const specialStr = '128.1.1';
83+
const fileSize = 65536;
84+
await bindIn_small(inFileName, fileSize, selectID, insertID, specialStr);
10885
});
10986

110-
it('128.1.2 works with 64KB+1 txt file', function(done) {
111-
inFileName = fileRoot + '/smallString.txt';
112-
let selectID = insertID + 200;
113-
let specialStr = '128.1.2';
114-
let fileSize = 655376;
115-
116-
bindIn_small(inFileName, fileSize, selectID, insertID, specialStr, done);
87+
it('128.1.2 works with 64KB+1 txt file', async function() {
88+
const inFileName = fileRoot + '/smallString.txt';
89+
const selectID = insertID + 200;
90+
const specialStr = '128.1.2';
91+
const fileSize = 655376;
92+
await bindIn_small(inFileName, fileSize, selectID, insertID, specialStr);
11793
});
11894

119-
it('128.1.3 works with 1MB+1 txt file', function(done) {
120-
inFileName = fileRoot + '/smallString.txt';
121-
let selectID = insertID + 200;
122-
let specialStr = '128.1.3';
123-
let fileSize = 1 * 1024 * 1024;
124-
125-
bindIn_small(inFileName, fileSize, selectID, insertID, specialStr, done);
95+
it('128.1.3 works with 1MB+1 txt file', async function() {
96+
const inFileName = fileRoot + '/smallString.txt';
97+
const selectID = insertID + 200;
98+
const specialStr = '128.1.3';
99+
const fileSize = 1 * 1024 * 1024;
100+
await bindIn_small(inFileName, fileSize, selectID, insertID, specialStr);
126101
});
127102

128103
}); // 4.1
129104

130-
var bindIn_small = function(inFileName, fileSize, selectID, insertID, specialStr, callback) {
131-
async.series([
132-
function(cb) {
133-
file.createFileInKB(inFileName, fileSize, specialStr);
134-
cb();
135-
},
136-
function(cb) {
137-
insetTableWithClob(selectID, inFileName, cb);
138-
},
139-
function(cb) {
140-
verifyClob(selectID, insertID, fileSize, cb);
141-
},
142-
function(cb) {
143-
file.delete(inFileName);
144-
cb();
145-
}
146-
], callback);
147-
};
148-
149-
var setupAllTable = function(callback) {
150-
connection.execute(
151-
proc_clob_prepare_tab,
152-
function(err) {
153-
should.not.exist(err);
154-
callback();
155-
});
156-
};
157-
158-
var dropAllTable = function(callback) {
159-
connection.execute(
160-
"DROP TABLE nodb_tab_lobs_pre PURGE",
161-
function(err) {
162-
should.not.exist(err);
163-
callback();
164-
});
105+
const bindIn_small = async function(fileName, fileSize, selectID, insertID, specialStr) {
106+
const bigStr = random.getRandomString(fileSize, specialStr);
107+
await fsPromises.writeFile(fileName, bigStr);
108+
await insertTableWithClob(selectID, fileName);
109+
await verifyClob(selectID, insertID, fileSize);
110+
await fsPromises.unlink(fileName);
165111
};
166112

167-
var insetTableWithClob = function(id, inFileName, callback) {
168-
var sql = "INSERT INTO nodb_tab_lobs_pre (id, clob) VALUES (:i, EMPTY_CLOB()) RETURNING clob INTO :lobbv";
169-
var bindVar = { i: id, lobbv: { type: oracledb.CLOB, dir: oracledb.BIND_OUT } };
170-
171-
connection.execute(
172-
sql,
173-
bindVar,
174-
{ autoCommit: false }, // a transaction needs to span the INSERT and pipe()
175-
function(err, result) {
176-
should.not.exist(err);
177-
(result.rowsAffected).should.be.exactly(1);
178-
(result.outBinds.lobbv.length).should.be.exactly(1);
179-
180-
var inStream = fs.createReadStream(inFileName);
181-
var lob = result.outBinds.lobbv[0];
182-
183-
lob.on('error', function(err) {
184-
should.not.exist(err);
185-
});
186-
187-
inStream.on('error', function(err) {
188-
should.not.exist(err);
189-
});
190-
191-
lob.on('finish', function() {
192-
connection.commit(function(err) {
193-
should.not.exist(err);
194-
callback();
195-
});
196-
});
197-
198-
inStream.pipe(lob); // copies the text to the CLOB
199-
}
200-
);
113+
const insertTableWithClob = async function(id, fileName) {
114+
const sql = "INSERT INTO nodb_tab_lobs_pre (id, clob) VALUES (:i, EMPTY_CLOB()) RETURNING clob INTO :lobbv";
115+
const binds = {
116+
i: id,
117+
lobbv: { type: oracledb.CLOB, dir: oracledb.BIND_OUT }
118+
};
119+
// a transaction needs to span the INSERT and pipe()
120+
const options = { autoCommit: false };
121+
const result = await connection.execute(sql, binds, options);
122+
assert.strictEqual(result.rowsAffected, 1);
123+
assert.strictEqual(result.outBinds.lobbv.length, 1);
124+
const inStream = fs.createReadStream(fileName);
125+
const lob = result.outBinds.lobbv[0];
126+
await new Promise((resolve, reject) => {
127+
lob.on('error', reject);
128+
inStream.on('error', reject);
129+
lob.on('finish', resolve);
130+
inStream.pipe(lob);
131+
});
132+
await connection.commit();
201133
};
202134

203-
var verifyClob = function(selectID, insertID, lenExpected, callback) {
204-
var lob = {};
205-
var selectSql = "select clob from nodb_tab_lobs_pre where id = " + selectID;
206-
var insetSql = "INSERT INTO nodb_tab_lobs_pre (id, clob) VALUES (:i, :c)";
207-
var proc_compare_clob = "CREATE OR REPLACE PROCEDURE nodb_clob_compare(result OUT NUMBER, len OUT NUMBER) \n" +
135+
const verifyClob = async function(selectID, insertID, lenExpected) {
136+
const selectSql = "select clob from nodb_tab_lobs_pre where id = " + selectID;
137+
const insertSql = "INSERT INTO nodb_tab_lobs_pre (id, clob) VALUES (:i, :c)";
138+
const proc_compare_clob = "CREATE OR REPLACE PROCEDURE nodb_clob_compare(result OUT NUMBER, len OUT NUMBER) \n" +
208139
"IS \n" +
209140
" clob1 CLOB; \n" +
210141
" clob2 CLOB; \n" +
@@ -214,56 +145,27 @@ describe('128.clobStream.js', function() {
214145
" result := DBMS_LOB.COMPARE(clob1, clob2); \n" + // Zero if the comparison succeeds, nonzero if not.
215146
" len := length(clob1); \n" +
216147
"END nodb_clob_compare;";
217-
var sqlRunComparePorc = "begin nodb_clob_compare(:r, :l); end;";
218-
var sqlDropComparePorc = "DROP PROCEDURE nodb_clob_compare";
219-
220-
async.series([
221-
function(cb) {
222-
connection.execute(
223-
selectSql,
224-
function(err, result) {
225-
should.not.exist(err);
226-
lob = result.rows[0][0];
227-
should.exist(lob);
228-
cb();
229-
}
230-
);
231-
},
232-
function(cb) {
233-
var bindVar = { i: insertID, c: { val: lob, type: oracledb.CLOB, dir: oracledb.BIND_IN } };
234-
connection.execute(
235-
insetSql,
236-
bindVar,
237-
{ autoCommit: true },
238-
function(err) {
239-
should.not.exist(err);
240-
lob.close(cb);
241-
}
242-
);
243-
},
244-
function(cb) {
245-
sql.executeSql(connection, proc_compare_clob, {}, {}, cb);
246-
},
247-
function(cb) {
248-
var bindVar = {
249-
r: { type: oracledb.NUMBER, dir: oracledb.BIND_OUT },
250-
l: { type: oracledb.NUMBER, dir: oracledb.BIND_OUT }
251-
};
252-
connection.execute(
253-
sqlRunComparePorc,
254-
bindVar,
255-
function(err, result) {
256-
should.not.exist(err);
257-
result.outBinds.r.should.eql(0);
258-
should.strictEqual(result.outBinds.l, lenExpected);
259-
cb();
260-
});
261-
},
262-
function(cb) {
263-
sql.executeSql(connection, sqlDropComparePorc, {}, {}, cb);
264-
}
265-
], callback);
266-
148+
const sqlRunCompareProc = "begin nodb_clob_compare(:r, :l); end;";
149+
const sqlDropCompareProc = "DROP PROCEDURE nodb_clob_compare";
150+
151+
let result = await connection.execute(selectSql);
152+
const lob = result.rows[0][0];
153+
let binds = {
154+
i: insertID,
155+
c: { val: lob, type: oracledb.CLOB, dir: oracledb.BIND_IN }
156+
};
157+
const options = { autoCommit: true };
158+
await connection.execute(insertSql, binds, options);
159+
await lob.close();
160+
await connection.execute(proc_compare_clob);
161+
binds = {
162+
r: { type: oracledb.NUMBER, dir: oracledb.BIND_OUT },
163+
l: { type: oracledb.NUMBER, dir: oracledb.BIND_OUT }
164+
};
165+
result = await connection.execute(sqlRunCompareProc, binds);
166+
assert.strictEqual(result.outBinds.r, 0);
167+
assert.strictEqual(result.outBinds.l, lenExpected);
168+
await connection.execute(sqlDropCompareProc);
267169
};
268170

269171
});

test/nestedCursor05.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020, 2022,Oracle and/or its affiliates. */
1+
/* Copyright (c) 2020, 2023, Oracle and/or its affiliates. */
22

33
/******************************************************************************
44
*
@@ -222,22 +222,24 @@ describe('236. nestedCursor05.js', () => {
222222

223223
const stringMetaData = {
224224
name: 'DESCRIPTION',
225-
fetchType: 2001,
226-
dbType: 2001,
225+
fetchType: oracledb.DB_TYPE_VARCHAR,
226+
dbType: oracledb.DB_TYPE_VARCHAR,
227227
dbTypeName: 'VARCHAR2',
228228
nullable: true,
229229
byteSize: 200
230230
};
231231
assert.deepEqual(result.metaData[0], stringMetaData);
232232
assert.strictEqual(result.metaData[1].name, 'CHILDREN');
233-
assert.strictEqual(result.metaData[1].fetchType, 2021);
234-
assert.strictEqual(result.metaData[1].dbType, 2021);
233+
assert.strictEqual(result.metaData[1].fetchType, oracledb.DB_TYPE_CURSOR);
234+
assert.strictEqual(result.metaData[1].dbType, oracledb.DB_TYPE_CURSOR);
235235
assert.strictEqual(result.metaData[1].dbTypeName, 'CURSOR');
236236

237237
assert.deepEqual(result.metaData[1].metaData[0], stringMetaData);
238238
assert.strictEqual(result.metaData[1].metaData[1].name, 'GRANDCHILDREN');
239-
assert.strictEqual(result.metaData[1].metaData[1].fetchType, 2021);
240-
assert.strictEqual(result.metaData[1].metaData[1].dbType, 2021);
239+
assert.strictEqual(result.metaData[1].metaData[1].fetchType,
240+
oracledb.DB_TYPE_CURSOR);
241+
assert.strictEqual(result.metaData[1].metaData[1].dbType,
242+
oracledb.DB_TYPE_CURSOR);
241243
assert.strictEqual(result.metaData[1].metaData[1].dbTypeName, 'CURSOR');
242244

243245
assert.deepEqual(result.metaData[1].metaData[1].metaData[0], stringMetaData);
@@ -254,13 +256,15 @@ describe('236. nestedCursor05.js', () => {
254256
const result = await conn.execute(sqlOne, [], options);
255257

256258
assert.strictEqual(result.metaData[1].name, 'CHILDREN');
257-
assert.strictEqual(result.metaData[1].fetchType, 2021);
258-
assert.strictEqual(result.metaData[1].dbType, 2021);
259+
assert.strictEqual(result.metaData[1].fetchType, oracledb.DB_TYPE_CURSOR);
260+
assert.strictEqual(result.metaData[1].dbType, oracledb.DB_TYPE_CURSOR);
259261
assert.strictEqual(result.metaData[1].dbTypeName, 'CURSOR');
260262

261263
assert.strictEqual(result.metaData[1].metaData[1].name, 'GRANDCHILDREN');
262-
assert.strictEqual(result.metaData[1].metaData[1].fetchType, 2021);
263-
assert.strictEqual(result.metaData[1].metaData[1].dbType, 2021);
264+
assert.strictEqual(result.metaData[1].metaData[1].fetchType,
265+
oracledb.DB_TYPE_CURSOR);
266+
assert.strictEqual(result.metaData[1].metaData[1].dbType,
267+
oracledb.DB_TYPE_CURSOR);
264268
assert.strictEqual(result.metaData[1].metaData[1].dbTypeName, 'CURSOR');
265269

266270
assert.strictEqual(result.rows.length, LIMIT);

0 commit comments

Comments
 (0)