node-oracledb/test/dataTypeBlob.js

275 lines
8.5 KiB
JavaScript
Raw Normal View History

/* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. */
2015-08-17 14:19:36 +08:00
/******************************************************************************
*
* You may not use the identified files except in compliance with the Apache
* License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at
* http://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.
2016-03-24 14:09:53 +08:00
*
* The node-oracledb test suite uses 'mocha', 'should' and 'async'.
2015-08-17 14:19:36 +08:00
* See LICENSE.md for relevant licenses.
*
* 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
2015-08-17 14:19:36 +08:00
var oracledb = require('oracledb');
var fs = require('fs');
var async = require('async');
var should = require('should');
var dbConfig = require('./dbconfig.js');
2015-08-17 14:19:36 +08:00
var assist = require('./dataTypeAssist.js');
2018-11-15 13:21:56 +08:00
var inFileName = 'test/fuzzydinosaur.jpg'; // contains the image to be inserted
var 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
2015-08-17 14:19:36 +08:00
var connection = null;
var tableName = "nodb_myblobs";
2015-09-02 20:33:00 +08:00
before('get one connection', function(done) {
2017-06-14 09:54:15 +08:00
oracledb.getConnection(
{
user: dbConfig.user,
password: dbConfig.password,
connectString: dbConfig.connectString
},
function(err, conn) {
should.not.exist(err);
connection = conn;
done();
}
);
});
2016-03-24 14:09:53 +08:00
2015-09-02 20:33:00 +08:00
after('release connection', function(done) {
connection.release( function(err) {
should.not.exist(err);
done();
});
2017-06-14 09:54:15 +08:00
});
2015-09-02 20:33:00 +08:00
describe('41.1 testing BLOB data type', function() {
before('create table', function(done) {
assist.createTable(connection, tableName, done);
2017-06-14 09:54:15 +08:00
});
2015-09-02 20:33:00 +08:00
after(function(done) {
connection.execute(
2017-06-14 09:54:15 +08:00
"DROP table " + tableName + " PURGE",
2015-08-17 14:19:36 +08:00
function(err) {
2015-09-02 20:33:00 +08:00
should.not.exist(err);
2015-08-17 14:19:36 +08:00
done();
}
);
2017-06-14 09:54:15 +08:00
});
2015-09-02 20:33:00 +08:00
it('41.1.1 stores BLOB value correctly', function(done) {
2017-06-14 09:54:15 +08:00
connection.should.be.ok();
2015-09-02 20:33:00 +08:00
async.series([
function blobinsert1(callback) {
2016-03-24 14:09:53 +08:00
2015-09-02 20:33:00 +08:00
connection.execute(
"INSERT INTO nodb_myblobs (num, content) VALUES (:n, EMPTY_BLOB()) RETURNING content INTO :lobbv",
2015-09-02 20:33:00 +08:00
{ n: 2, lobbv: {type: oracledb.BLOB, dir: oracledb.BIND_OUT} },
{ autoCommit: false }, // a transaction needs to span the INSERT and pipe()
function(err, result) {
should.not.exist(err);
(result.rowsAffected).should.be.exactly(1);
(result.outBinds.lobbv.length).should.be.exactly(1);
2016-03-24 14:09:53 +08:00
2015-09-02 20:33:00 +08:00
var inStream = fs.createReadStream(inFileName);
inStream.on('error', function(err) {
should.not.exist(err, "inStream.on 'end' event");
});
2015-08-17 14:19:36 +08:00
2015-09-02 20:33:00 +08:00
var lob = result.outBinds.lobbv[0];
lob.on('error', function(err) {
should.not.exist(err, "lob.on 'error' event");
});
inStream.pipe(lob); // pipes the data to the BLOB
2018-11-15 13:21:56 +08:00
lob.on('close', function() {
connection.commit(function(err) {
should.not.exist(err);
2018-11-15 13:21:56 +08:00
callback();
});
});
2015-09-02 20:33:00 +08:00
}
);
},
function blobstream1(callback) {
connection.execute(
"SELECT content FROM nodb_myblobs WHERE num = :n",
2015-09-02 20:33:00 +08:00
{ n: 2 },
function(err, result) {
should.not.exist(err);
var lob = result.rows[0][0];
should.exist(lob);
lob.on('error', function(err) {
2015-09-25 15:21:55 +08:00
should.not.exist(err, "lob.on 'error' event");
2015-09-02 20:33:00 +08:00
});
2015-08-17 14:19:36 +08:00
2015-09-02 20:33:00 +08:00
var outStream = fs.createWriteStream(outFileName);
outStream.on('error', function(err) {
2015-09-25 15:21:55 +08:00
should.not.exist(err, "outStream.on 'error' event");
2015-08-17 14:19:36 +08:00
});
2015-09-02 20:33:00 +08:00
lob.pipe(outStream);
2018-11-15 13:21:56 +08:00
outStream.on('close', function() {
2015-09-02 20:33:00 +08:00
fs.readFile( inFileName, function(err, originalData) {
2015-08-17 14:19:36 +08:00
should.not.exist(err);
2016-03-24 14:09:53 +08:00
2015-09-02 20:33:00 +08:00
fs.readFile( outFileName, function(err, generatedData) {
should.not.exist(err);
originalData.should.eql(generatedData);
2018-11-15 13:21:56 +08:00
callback();
2015-09-02 20:33:00 +08:00
});
2015-08-17 14:19:36 +08:00
});
2015-09-02 20:33:00 +08:00
2018-11-15 13:21:56 +08:00
}); // close event
2015-09-02 20:33:00 +08:00
}
);
},
function blobstream2(callback) {
connection.execute(
"SELECT content FROM nodb_myblobs WHERE num = :n",
2015-09-02 20:33:00 +08:00
{ n: 2 },
function(err, result) {
should.not.exist(err);
2016-03-24 14:09:53 +08:00
var blob = Buffer.alloc(0);
2015-09-02 20:33:00 +08:00
var blobLength = 0;
var lob = result.rows[0][0];
should.exist(lob);
lob.on('error', function(err) {
2015-09-25 15:21:55 +08:00
should.not.exist(err, "lob.on 'error' event");
2015-08-17 14:19:36 +08:00
});
2015-09-02 20:33:00 +08:00
lob.on('data', function(chunk) {
blobLength = blobLength + chunk.length;
blob = Buffer.concat([blob, chunk], blobLength);
2015-08-17 14:19:36 +08:00
});
2016-03-24 14:09:53 +08:00
2018-11-15 13:21:56 +08:00
lob.on('close', function() {
2015-09-02 20:33:00 +08:00
fs.readFile( inFileName, function(err, data) {
should.not.exist(err);
data.length.should.be.exactly(blob.length);
data.should.eql(blob);
2018-11-15 13:21:56 +08:00
callback();
2015-09-02 20:33:00 +08:00
});
2018-11-15 13:21:56 +08:00
}); // close event
2015-08-17 14:19:36 +08:00
2015-09-02 20:33:00 +08:00
}
);
2016-03-24 14:15:35 +08:00
},
function deleteOutFile(callback) {
fs.unlink(outFileName, function(err) {
should.not.exist(err);
callback();
});
2015-09-02 20:33:00 +08:00
}
], done);
2017-06-14 09:54:15 +08:00
}); // 41.1.1
2019-06-03 10:09:08 +08:00
it('41.1.2 BLOB getData()', function(done) {
connection.should.be.ok();
async.series([
function blobinsert1(callback) {
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()
function(err, result) {
should.not.exist(err);
(result.rowsAffected).should.be.exactly(1);
(result.outBinds.lobbv.length).should.be.exactly(1);
var inStream = fs.createReadStream(inFileName);
inStream.on('error', function(err) {
should.not.exist(err, "inStream.on 'end' event");
});
var lob = result.outBinds.lobbv[0];
lob.on('error', function(err) {
should.not.exist(err, "lob.on 'error' event");
});
inStream.pipe(lob); // pipes the data to the BLOB
lob.on('close', function() {
connection.commit(function(err) {
should.not.exist(err);
callback();
});
});
}
);
},
function blobstream2(callback) {
connection.execute(
"SELECT content FROM nodb_myblobs WHERE num = :n",
{ n: 3 },
function(err, result) {
should.not.exist(err);
var lob = result.rows[0][0];
should.exist(lob);
fs.readFile( inFileName, function(err, data) {
should.not.exist(err);
2019-06-03 10:09:08 +08:00
lob.getData(function(err, blob) {
data.length.should.be.exactly(blob.length);
data.should.eql(blob);
callback();
});
});
});
}
], done);
}); // 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() {
it('41.2.1 testing Null, Empty string and Undefined', function(done) {
assist.verifyNullValues(connection, tableName, done);
2017-06-14 09:54:15 +08:00
});
});
2015-09-02 20:33:00 +08:00
2017-06-14 09:54:15 +08:00
});