node-oracledb/test/dataTypeBlob.js

246 lines
8.9 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');
2024-02-05 12:27:17 +08:00
const testsUtil = require(`./testsUtil.js`);
2015-08-17 14:19:36 +08:00
2023-08-17 16:11:49 +08:00
const inFileName = 'test/fuzzydinosaur.jpg'; // contains the image to be inserted
const 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;
2023-08-17 16:11:49 +08:00
const 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);
2023-08-17 16:11:49 +08:00
const inStream = await fs.createReadStream(inFileName);
2023-02-21 14:11:40 +08:00
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);
2023-08-17 16:11:49 +08:00
const inStream = fs.createReadStream(inFileName);
2023-02-21 14:11:40 +08:00
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
2024-02-05 12:27:17 +08:00
describe('41.3 OSON column metadata ', function() {
let isRunnable = false;
const TABLE = "nodb_myblobs_oson_col";
const createTable = (`CREATE TABLE ${TABLE} (
IntCol number(9) not null,
OsonCol blob not null,
blobCol blob not null,
constraint TestOsonCols_ck_1 check (OsonCol is json format oson)
)`
);
const plsql = testsUtil.sqlCreateTable(TABLE, createTable);
before('create table', async function() {
oracledb.fetchAsBuffer = [oracledb.BLOB];
2024-02-05 12:27:17 +08:00
if (testsUtil.getClientVersion() >= 2100000000 &&
connection.oracleServerVersion >= 2100000000) {
isRunnable = true;
}
if (!isRunnable) {
this.skip();
}
await connection.execute(plsql);
});
after(async function() {
oracledb.fetchAsBuffer = [];
2024-02-05 12:27:17 +08:00
await connection.execute(testsUtil.sqlDropTable(TABLE));
});
it('41.3.1 Verify isOson flag in column metadata', async function() {
const result = await connection.execute(`select * from ${TABLE}`);
assert.strictEqual(result.metaData[0].isOson, false);
assert.strictEqual(result.metaData[1].isOson, true);
assert.strictEqual(result.metaData[2].isOson, false);
}); // 41.3.1
it('41.3.2 Verify Basic encode/decode OSON on OSON format column', async function() {
const expectedObj1 = {key1: "val1"};
const expectedObj2 = {key2: "val2"};
const expectedObj3 = [new Float32Array([1, 2]), [1, 2]];
const byteBuf = Buffer.from(JSON.stringify((expectedObj1)));
// Insert Buffer into OSON format column and verify with decode.
let result = await connection.execute(`insert into ${TABLE}(IntCol, OsonCol, blobCol)
values (1, :1, :2) `,
[byteBuf, byteBuf]);
result = await connection.execute(`select OSONCOL from ${TABLE}`);
let generatedObj = connection.decodeOSON(result.rows[0][0]);
assert.deepStrictEqual(expectedObj1, generatedObj);
// Generate OSON bytes and insert these bytes and verify with decode.
const osonBytes = connection.encodeOSON(expectedObj2);
result = await connection.execute(`insert into ${TABLE}(IntCol, OsonCol, blobCol)
values (2, :1, :2) `,
[osonBytes, byteBuf]);
result = await connection.execute(`select OSONCOL from ${TABLE} where IntCol = 2`);
generatedObj = connection.decodeOSON(result.rows[0][0]);
assert.deepStrictEqual(expectedObj2, generatedObj);
// Verify vector inside OSON image for 23.4 server onwards.
if (connection.oracleServerVersion >= 2304000000) {
result = await connection.execute(`insert into ${TABLE}(IntCol, OsonCol, blobCol)
values (3, :1, :2) `,
[connection.encodeOSON(expectedObj3), byteBuf]);
result = await connection.execute(`select OSONCOL from ${TABLE} where IntCol = 3`);
generatedObj = connection.decodeOSON(result.rows[0][0]);
assert.deepStrictEqual(expectedObj3, generatedObj);
}
// Check invalid values to decodeOSON
assert.rejects(
() => connection.decodeOSON(Buffer.from('invalid')),
/NJS-113:/
);
assert.rejects(
() => connection.decodeOSON('invalid'),
/NJS-005:/
);
assert.rejects(
() => connection.decodeOSON(),
/NJS-009:/
);
}); // 41.3.2
2024-02-05 12:27:17 +08:00
}); //41.3
2017-06-14 09:54:15 +08:00
});