node-oracledb/test/soda11.js

713 lines
21 KiB
JavaScript
Raw Normal View History

2019-02-20 07:26:52 +08:00
/* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. */
2018-12-04 13:55:35 +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.
*
* The node-oracledb test suite uses 'mocha', 'should' and 'async'.
* See LICENSE.md for relevant licenses.
*
* NAME
* 179. soda11.js
*
* DESCRIPTION
* SODA tests which use modified collections.
* Oracle Cloud currently does not support collection modification.
* So this file should be out of Cloud test suite.
*
*****************************************************************************/
'use strict';
2019-02-08 09:51:05 +08:00
const oracledb = require('oracledb');
const should = require('should');
const dbconfig = require('./dbconfig.js');
const sodaUtil = require('./sodaUtil.js');
const testsUtil = require('./testsUtil.js');
2018-12-04 13:55:35 +08:00
describe('179. soda11.js', () => {
before(async function() {
2019-02-08 09:51:05 +08:00
const runnable = await testsUtil.checkPrerequisites();
2019-01-23 05:00:54 +08:00
if (!runnable) {
this.skip();
return;
}
2018-12-04 13:55:35 +08:00
await sodaUtil.cleanup();
});
it('179.1 create collection with metadata', async () => {
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let sd = conn.getSodaDatabase();
2019-02-20 07:26:52 +08:00
2018-12-04 13:55:35 +08:00
let t_tablename = "myTableName";
let t_metadata = {
"schemaName" : dbconfig.user.toUpperCase(),
"tableName" : t_tablename,
"keyColumn" :
{
"name" : "ID",
"sqlType" : "VARCHAR2",
"maxLength" : 255,
"assignmentMethod" : "UUID"
},
"contentColumn" :
{
"name" : "JSON_DOCUMENT",
"sqlType" : "BLOB",
"compress" : "NONE",
"cache" : true,
"encrypt" : "NONE",
"validation" : "STANDARD"
},
"versionColumn" :
{
"name" : "VERSION",
"method" : "SHA256"
},
"lastModifiedColumn" :
{
"name" : "LAST_MODIFIED"
},
"creationTimeColumn" :
{
"name" : "CREATED_ON"
},
"readOnly" : true
};
let t_collname = "soda_test_179_1";
let options = { metaData: t_metadata };
collection = await sd.createCollection(t_collname, options);
await conn.commit();
should.strictEqual(collection.name, t_collname);
should.strictEqual(typeof(collection.metaData), "object");
should.deepEqual(collection.metaData, t_metadata);
2018-12-04 13:55:35 +08:00
} catch(err) {
should.not.exist(err);
} finally {
await conn.commit();
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
}); // 179.1
it('179.2 Negative - create collection with an invalid metadata', async () => {
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let sd = conn.getSodaDatabase();
let t_metadata = {
"schemaName" : "nonexistent",
"tableName" : "bar",
"keyColumn" :
{
"name" : "ID",
"sqlType" : "VARCHAR2",
"maxLength" : 255,
"assignmentMethod" : "UUID"
},
"contentColumn" :
{
"name" : "JSON_DOCUMENT",
"sqlType" : "BLOB",
"compress" : "NONE",
"cache" : true,
"encrypt" : "NONE",
"validation" : "STANDARD"
},
"versionColumn" :
{
"name" : "VERSION",
"method" : "SHA256"
},
"lastModifiedColumn" :
{
"name" : "LAST_MODIFIED"
},
"creationTimeColumn" :
{
"name" : "CREATED_ON"
},
"readOnly" : false
};
let t_collname = "soda_test_179_2";
let options = { metaData: t_metadata };
2019-02-08 09:51:05 +08:00
await testsUtil.assertThrowsAsync(
2018-12-04 13:55:35 +08:00
async () => await sd.createCollection(t_collname, options),
/ORA-01918:/
);
// ORA-01918: user \'nonexistent\' does not exist
} catch(err) {
should.not.exist(err);
} finally {
await conn.commit();
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
}); // 179.2
it('179.3 throw error when creating collection with the existing name and different metadata', async () => {
let conn;
let collection1;
try {
conn = await oracledb.getConnection(dbconfig);
let sd = conn.getSodaDatabase();
let t_metadata1 = {
"schemaName" : dbconfig.user.toUpperCase(),
"tableName" : "nodb_tab_179_3",
"keyColumn" :
{
"name" : "ID",
"sqlType" : "VARCHAR2",
"maxLength" : 255,
"assignmentMethod" : "UUID"
},
"contentColumn" :
{
"name" : "JSON_DOCUMENTS",
"sqlType" : "BLOB",
"compress" : "NONE",
"cache" : true,
"encrypt" : "NONE",
"validation" : "STRICT"
},
"versionColumn" :
{
"name" : "VERSION",
"type":"String",
"method":"SHA256"
},
"lastModifiedColumn" :
{
"name":"LAST_MODIFIED"
},
"creationTimeColumn":
{
"name":"CREATED_ON"
},
"readOnly": false
};
let t_collname = "soda_test_179_3";
let options = { metaData: t_metadata1 };
collection1 = await sd.createCollection(t_collname, options);
let t_metadata2 = {
"schemaName" : "foo",
"tableName" : "bar",
"keyColumn" :
{
"name" : "ID",
"sqlType" : "VARCHAR2",
"maxLength" : 255,
"assignmentMethod" : "UUID"
},
"contentColumn" :
{
"name" : "JSON_DOCUMENTS",
"sqlType" : "BLOB",
"compress" : "NONE",
"cache" : true,
"encrypt" : "NONE",
"validation" : "STRICT"
},
"versionColumn" :
{
"name" : "VERSION",
"type":"String",
"method":"SHA256"
},
"lastModifiedColumn" :
{
"name":"LAST_MODIFIED"
},
"creationTimeColumn":
{
"name":"CREATED_ON"
},
"readOnly": true
};
let options2 = { metaData: t_metadata2 };
2019-02-08 09:51:05 +08:00
await testsUtil.assertThrowsAsync(
2018-12-04 13:55:35 +08:00
async () => await sd.createCollection(t_collname, options2),
/ORA-40669:/
);
// ORA-40669: Collection create failed: collection with same name but different metadata exists.
} catch(err) {
should.not.exist(err);
} finally {
if (collection1) {
try {
await collection1.drop();
} catch(err) {
should.not.exist(err);
}
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
}); // 179.3
it('179.4 customize the key value, String value', async () => {
let conn;
try {
conn = await oracledb.getConnection(dbconfig);
let sd = conn.getSodaDatabase();
let collectionName = 'soda_test_179_4';
let testMetaData = {
"schemaName" : dbconfig.user.toUpperCase(),
"tableName" : collectionName,
"keyColumn" :
{
"name" : "ID",
"sqlType" : "NUMBER",
"assignmentMethod" : "CLIENT"
},
"contentColumn" :
{
"name" : "JSON_DOCUMENTS",
"sqlType" : "BLOB",
"compress" : "NONE",
"cache" : true,
"encrypt" : "NONE",
"validation" : "STRICT"
},
"versionColumn" :
{
"name" : "VERSION",
"type":"String",
"method":"SHA256"
},
"lastModifiedColumn" :
{
"name":"LAST_MODIFIED"
},
"creationTimeColumn":
{
"name":"CREATED_ON"
},
"readOnly": false
};
let coll = await sd.createCollection(collectionName, { metaData: testMetaData});
let testContent = {
name: "Shelly",
address: {city: "Shenzhen", country: "China"}
};
2019-02-20 07:26:52 +08:00
/* The key must always be a string and is always returned a string as
2018-12-04 13:55:35 +08:00
well -- even if the "type" in the database is numeric. */
let testKey = '86755';
let testDoc = sd.createDocument(testContent, { key: testKey } );
should.strictEqual(testDoc.key, testKey);
await coll.insertOne(testDoc);
// Fetch it back
let docGot = await coll.find().key(testKey).getOne();
let contentGot = docGot.getContent();
should.strictEqual(contentGot.name, testContent.name);
should.strictEqual(
2019-02-20 07:26:52 +08:00
contentGot.address.country,
2018-12-04 13:55:35 +08:00
testContent.address.country
);
await conn.commit();
let res = await coll.drop();
should.strictEqual(res.dropped, true);
} catch(err) {
should.not.exist(err);
} finally {
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
}); // 179.4
// A variation of 179.4
it('179.5 Negative - customize the key value, numeric value', async () => {
let conn, coll;
try {
conn = await oracledb.getConnection(dbconfig);
let sd = conn.getSodaDatabase();
let collectionName = 'soda_test_179_5';
let testMetaData = {
"schemaName" : dbconfig.user.toUpperCase(),
"tableName" : collectionName,
"keyColumn" :
{
"name" : "ID",
"sqlType" : "NUMBER",
"assignmentMethod" : "CLIENT"
},
"contentColumn" :
{
"name" : "JSON_DOCUMENTS",
"sqlType" : "BLOB",
"compress" : "NONE",
"cache" : true,
"encrypt" : "NONE",
"validation" : "STRICT"
},
"versionColumn" :
{
"name" : "VERSION",
"type":"String",
"method":"SHA256"
},
"lastModifiedColumn" :
{
"name":"LAST_MODIFIED"
},
"creationTimeColumn":
{
"name":"CREATED_ON"
},
"readOnly": false
};
coll = await sd.createCollection(collectionName, { metaData: testMetaData});
let testContent = {
name: "Shelly",
address: {city: "Shenzhen", country: "China"}
};
2019-02-20 07:26:52 +08:00
/* The key must always be a string and is always returned a string as
2018-12-04 13:55:35 +08:00
well -- even if the "type" in the database is numeric. */
let testKey = 86755;
2019-02-08 09:51:05 +08:00
await testsUtil.assertThrowsAsync(
2018-12-04 13:55:35 +08:00
async () => await sd.createDocument(testContent, { key: testKey } ),
/NJS-007: invalid value for "key" in parameter 2/
2018-12-04 13:55:35 +08:00
);
} catch(err) {
should.not.exist(err);
} finally {
if (coll) {
try {
let res = await coll.drop();
should.strictEqual(res.dropped, true);
} catch(err) {
should.not.exist(err);
}
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
}); // 179.5
it('179.6 customize the value of mediaType', async () => {
let conn, coll;
try {
conn = await oracledb.getConnection(dbconfig);
let sd = conn.getSodaDatabase();
let collectionName = 'soda_test_179_6';
let testMetaData = {
"schemaName" : dbconfig.user.toUpperCase(),
"tableName" : collectionName,
"keyColumn" :
{
"name" : "ID",
"sqlType" : "NUMBER",
"assignmentMethod" : "CLIENT"
},
"mediaTypeColumn":
{
"name": "MediaType"
},
"contentColumn" :
{
"name" : "DOCUMENT",
"sqlType" : "BLOB",
"compress" : "NONE",
"cache" : true,
"encrypt" : "NONE",
"validation" : "STRICT"
},
"versionColumn" :
{
"name" : "VERSION",
"type":"String",
"method":"SHA256"
},
"lastModifiedColumn" :
{
"name":"LAST_MODIFIED"
},
"creationTimeColumn":
{
"name":"CREATED_ON"
},
"readOnly": false
};
coll = await sd.createCollection(collectionName, { metaData: testMetaData});
// Insert a new document
let testContent = {};
let testMediaType = 'image/png';
let testKey = '86755';
let testDoc = sd.createDocument(
2019-02-20 07:26:52 +08:00
testContent,
{ mediaType: testMediaType, key: testKey }
2018-12-04 13:55:35 +08:00
);
should.strictEqual(testDoc.mediaType, testMediaType);
let myKey = testDoc.key;
await coll.insertOne(testDoc);
// Fetch the document back
let myDoc = await coll.find().key(myKey).getOne();
should.strictEqual(myDoc.mediaType, testMediaType);
} catch(err) {
should.not.exist(err);
} finally {
await conn.commit();
if (coll) {
try {
let res = await coll.drop();
should.strictEqual(res.dropped, true);
} catch(err) {
should.not.exist(err);
}
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
}); // 179.6
it('179.7 Negative - customize mediaType, invalid type, numeric value', async () => {
let conn, coll;
try {
conn = await oracledb.getConnection(dbconfig);
let sd = conn.getSodaDatabase();
let collectionName = 'soda_test_179_7';
let testMetaData = {
"schemaName" : dbconfig.user.toUpperCase(),
"tableName" : collectionName,
"keyColumn" :
{
"name" : "ID",
"sqlType" : "NUMBER",
"assignmentMethod" : "CLIENT"
},
"mediaTypeColumn":
{
"name": "MediaType"
},
"contentColumn" :
{
"name" : "DOCUMENT",
"sqlType" : "BLOB",
"compress" : "NONE",
"cache" : true,
"encrypt" : "NONE",
"validation" : "STRICT"
},
"versionColumn" :
{
"name" : "VERSION",
"type":"String",
"method":"SHA256"
},
"lastModifiedColumn" :
{
"name":"LAST_MODIFIED"
},
"creationTimeColumn":
{
"name":"CREATED_ON"
},
"readOnly": false
};
coll = await sd.createCollection(collectionName, { metaData: testMetaData});
// Insert a new document
let testContent = {};
2019-02-20 07:26:52 +08:00
2018-12-04 13:55:35 +08:00
/* Negative value */
let testMediaType = 65432;
let testKey = '86755';
2019-02-08 09:51:05 +08:00
await testsUtil.assertThrowsAsync(
2018-12-04 13:55:35 +08:00
async () => await sd.createDocument(
2019-02-20 07:26:52 +08:00
testContent,
2018-12-04 13:55:35 +08:00
{ mediaType: testMediaType, key: testKey }
),
/NJS-007: invalid value for "mediaType" in parameter 2/
2018-12-04 13:55:35 +08:00
);
} catch(err) {
should.not.exist(err);
} finally {
await conn.commit();
if (coll) {
try {
let res = await coll.drop();
should.strictEqual(res.dropped, true);
} catch(err) {
should.not.exist(err);
}
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
}); // 179.7
it('179.8 insert an empty document with customized metadata', async () => {
let conn, coll;
try {
conn = await oracledb.getConnection(dbconfig);
let sd = conn.getSodaDatabase();
let collectionName = 'soda_test_179_8';
let testMetaData = {
"schemaName" : dbconfig.user.toUpperCase(),
"tableName" : collectionName,
"keyColumn" :
{
"name" : "ID",
"sqlType" : "NUMBER",
"assignmentMethod" : "CLIENT"
},
"mediaTypeColumn":
{
"name": "MediaType"
},
"contentColumn" :
{
"name" : "DOCUMENT",
"sqlType" : "BLOB",
"compress" : "NONE",
"cache" : true,
"encrypt" : "NONE",
"validation" : "STRICT"
},
"versionColumn" :
{
"name" : "VERSION",
"type":"String",
"method":"SHA256"
},
"lastModifiedColumn" :
{
"name":"LAST_MODIFIED"
},
"creationTimeColumn":
{
"name":"CREATED_ON"
},
"readOnly": false
};
coll = await sd.createCollection(collectionName, { metaData: testMetaData });
let testContent = {};
let testKey = '86755';
let testDoc = sd.createDocument(testContent, { key: testKey } );
let outDocument = await coll.insertOneAndGet(testDoc);
should.exist(outDocument);
} catch(err) {
should.not.exist(err);
} finally {
await conn.commit();
if (coll) {
try {
let res = await coll.drop();
should.strictEqual(res.dropped, true);
} catch(err) {
should.not.exist(err);
}
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
}); // 179.8
2019-02-20 07:26:52 +08:00
});