node-oracledb/test/dataTypeBlob.js

157 lines
5.5 KiB
JavaScript
Raw Normal View History

2023-05-03 18:02:47 +08:00
/* Copyright (c) 2015, 2023, Oracle and/or its affiliates. */
2015-08-17 14:19:36 +08:00
/******************************************************************************
*
* 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.
2015-08-17 14:19:36 +08:00
*
* 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.
2015-08-17 14:19:36 +08:00
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
2015-08-17 14:19:36 +08:00
*
* 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.
2015-08-17 14:19:36 +08:00
* See the License for the specific language governing permissions and
* limitations under the License.
2016-03-24 14:09:53 +08:00
*
2015-08-17 14:19:36 +08:00
* NAME
* 41. dataTypeBlob.js
*
* DESCRIPTION
2016-03-24 14:09:53 +08:00
* Testing Oracle data type support - BLOB.
* This test corresponds to example files:
2015-08-17 14:19:36 +08:00
* blobinsert1.js, blobstream1.js and blobstream2.js
* Firstly, Loads an image data and INSERTs it into a BLOB column.
2016-03-24 14:09:53 +08:00
* Secondly, SELECTs the BLOB and pipes it to a file, blobstreamout.jpg
2015-08-17 14:19:36 +08:00
* Thirdly, SELECTs the BLOB and compares it with the original image
*
*****************************************************************************/
2016-03-24 14:15:35 +08:00
'use strict';
2015-09-02 20:33:00 +08:00
2023-02-21 14:11:40 +08:00
const oracledb = require('oracledb');
const fs = require('fs');
const assert = require('assert');
const dbConfig = require('./dbconfig.js');
const assist = require('./dataTypeAssist.js');
2015-08-17 14:19:36 +08:00
2023-02-21 14:11:40 +08:00
let inFileName = 'test/fuzzydinosaur.jpg'; // contains the image to be inserted
let outFileName = 'test/blobstreamout.jpg';
2015-08-17 14:19:36 +08:00
2018-11-15 13:21:56 +08:00
describe('41. dataTypeBlob.js', function() {
2015-09-02 20:33:00 +08:00
2023-02-21 14:11:40 +08:00
let connection = null;
let tableName = "nodb_myblobs";
2015-09-02 20:33:00 +08:00
2023-02-21 14:11:40 +08:00
before('get one connection', async function() {
connection = await oracledb.getConnection(dbConfig);
2017-06-14 09:54:15 +08:00
});
2016-03-24 14:09:53 +08:00
2023-02-21 14:11:40 +08:00
after('release connection', async function() {
await connection.close();
2017-06-14 09:54:15 +08:00
});
2015-09-02 20:33:00 +08:00
describe('41.1 testing BLOB data type', function() {
2023-02-21 14:11:40 +08:00
before('create table', async function() {
await connection.execute(assist.sqlCreateTable(tableName));
2017-06-14 09:54:15 +08:00
});
2015-09-02 20:33:00 +08:00
2023-02-21 14:11:40 +08:00
after(async function() {
await connection.execute("DROP table " + tableName + " PURGE");
2017-06-14 09:54:15 +08:00
});
2015-09-02 20:33:00 +08:00
2023-02-21 14:11:40 +08:00
it('41.1.1 stores BLOB value correctly', async function() {
let result = await connection.execute(
2023-05-03 18:32:09 +08:00
`INSERT INTO nodb_myblobs (num, content) VALUES (:n, EMPTY_BLOB()) RETURNING content INTO :lobbv`,
2023-02-21 14:11:40 +08:00
{ n: 2, lobbv: {type: oracledb.BLOB, dir: oracledb.BIND_OUT} },
{ autoCommit: false }); // a transaction needs to span the INSERT and pipe()
assert.strictEqual(result.rowsAffected, 1);
assert.strictEqual(result.outBinds.lobbv.length, 1);
let inStream = await fs.createReadStream(inFileName);
let lob = result.outBinds.lobbv[0];
await new Promise((resolve, reject) => {
inStream.on('error', reject);
lob.on('error', reject);
lob.on('finish', resolve);
inStream.pipe(lob);
});
await connection.commit();
result = await connection.execute(
"SELECT content FROM nodb_myblobs WHERE num = :n",
{ n: 2 });
lob = result.rows[0][0];
await new Promise((resolve, reject) => {
2023-05-03 18:32:09 +08:00
const outStream = fs.createWriteStream(outFileName);
2023-02-21 14:11:40 +08:00
lob.on('error', reject);
outStream.on('error', reject);
2023-05-03 18:32:09 +08:00
outStream.on('finish', resolve);
2023-02-21 14:11:40 +08:00
lob.pipe(outStream);
});
await connection.commit();
2023-05-03 18:32:09 +08:00
const originalData = await fs.promises.readFile(inFileName);
const generatedData = await fs.promises.readFile(outFileName);
assert.deepStrictEqual(originalData, generatedData);
2023-02-21 14:11:40 +08:00
result = await connection.execute(
"SELECT content FROM nodb_myblobs WHERE num = :n",
{ n: 2 });
lob = result.rows[0][0];
2023-05-03 18:32:09 +08:00
const blob = await lob.getData();
const data = await fs.promises.readFile(inFileName);
assert.deepStrictEqual(data, blob);
fs.unlinkSync(outFileName);
2017-06-14 09:54:15 +08:00
}); // 41.1.1
2023-02-21 14:11:40 +08:00
it('41.1.2 BLOB getData()', async function() {
let result = await connection.execute(
`INSERT INTO nodb_myblobs (num, content) ` +
`VALUES (:n, EMPTY_BLOB()) RETURNING content INTO :lobbv`,
{ n: 3, lobbv: {type: oracledb.BLOB, dir: oracledb.BIND_OUT} },
{ autoCommit: false }); // a transaction needs to span the INSERT and pipe()
assert.strictEqual(result.rowsAffected, 1);
assert.strictEqual(result.outBinds.lobbv.length, 1);
let inStream = fs.createReadStream(inFileName);
let lob = result.outBinds.lobbv[0];
2023-05-03 18:32:09 +08:00
await new Promise((resolve, reject) => {
lob.on('error', reject);
inStream.on('error', reject);
lob.on('finish', resolve);
2023-02-21 14:11:40 +08:00
inStream.pipe(lob); // pipes the data to the BLOB
});
2023-05-03 18:32:09 +08:00
await connection.commit();
2023-02-21 14:11:40 +08:00
result = await connection.execute(
"SELECT content FROM nodb_myblobs WHERE num = :n",
{ n: 3 });
lob = result.rows[0][0];
2023-05-03 18:32:09 +08:00
const data = await fs.promises.readFile(inFileName);
const blob = await lob.getData();
assert.deepStrictEqual(data, blob);
}); // 41.1.2
2017-06-14 09:54:15 +08:00
}); //41.1
2015-09-02 20:33:00 +08:00
describe('41.2 stores null value correctly', function() {
2023-02-21 14:11:40 +08:00
it('41.2.1 testing Null, Empty string and Undefined', async function() {
await assist.verifyNullValues(connection, tableName);
2017-06-14 09:54:15 +08:00
});
});
2015-09-02 20:33:00 +08:00
2017-06-14 09:54:15 +08:00
});