node-oracledb/test/soda5.js

616 lines
15 KiB
JavaScript
Raw Normal View History

2018-11-15 13:24:04 +08:00
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. */
/******************************************************************************
*
* 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
* 173. soda5.js
*
* DESCRIPTION
* More tests for sodaCollection class
*
*****************************************************************************/
'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-11-15 13:24:04 +08:00
2018-11-30 05:34:44 +08:00
const t_contents = sodaUtil.t_contents;
2018-11-15 13:24:04 +08:00
describe('173. soda5.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-11-15 13:24:04 +08:00
await sodaUtil.cleanup();
});
it('173.1 create index, basic case', async () => {
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let soda = conn.getSodaDatabase();
collection = await soda.createCollection("soda_test_173_1");
let indexSpec = {
"name": "OFFICE_IDX",
"fields": [
{
"path": "office",
"datatype": "string",
"order": "asc"
}
]
};
await collection.createIndex(indexSpec);
await Promise.all(
t_contents.map(function(content) {
return collection.insertOne(content);
})
);
2018-11-15 13:24:04 +08:00
// Fetch back
let empInShenzhen = await collection.find()
.filter({ "office": {"$like": "Shenzhen"} })
.count();
should.strictEqual(empInShenzhen.count, 2);
await conn.commit();
} catch(err) {
2018-11-26 11:35:19 +08:00
should.not.exist(err);
2018-11-15 13:24:04 +08:00
} finally {
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
}); // 173.1
it('173.2 query row not via indexSpec', async () => {
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let soda = conn.getSodaDatabase();
collection = await soda.createCollection("soda_test_173_2");
let indexSpec = {
"name": "OFFICE_IDX",
"fields": [
{
"path": "office",
"datatype": "string",
"order": "asc"
}
]
};
await collection.createIndex(indexSpec);
await Promise.all(
t_contents.map(function(content) {
return collection.insertOne(content);
})
);
} catch(err) {
should.not.exist(err);
}
try {
// Fetch back
let empInShenzhen = await collection.find()
.filter({ "name": {"$like": "Changjie"} })
.count();
should.strictEqual(empInShenzhen.count, 1);
} catch(err) {
should.not.exist(err);
}
2018-11-15 13:24:04 +08:00
try {
await conn.commit();
}
2018-11-15 13:24:04 +08:00
catch(err) {
should.not.exist(err);
}
2018-11-15 13:24:04 +08:00
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}); // 173.2
it('173.3 Negative - invalid indexSpec, invalid index property', async () => {
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let soda = conn.getSodaDatabase();
collection = await soda.createCollection("soda_test_173_3");
2018-11-15 13:24:04 +08:00
} catch(err) {
should.not.exist(err);
}
2018-11-26 11:35:19 +08:00
let indexSpec = { "foo": "bar" };
2019-02-08 09:51:05 +08:00
await testsUtil.assertThrowsAsync(
2018-11-26 11:35:19 +08:00
async () => await collection.createIndex(indexSpec),
2018-11-26 11:39:07 +08:00
// {
// errorNum: 40719,
// offset: 0,
// message: /^ORA-40719/
// }
/ORA-40719:/
2018-11-26 11:35:19 +08:00
);
2018-11-26 11:39:07 +08:00
2018-11-26 11:35:19 +08:00
// ORA-40719: invalid index property foo
2018-11-15 13:24:04 +08:00
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}); // 173.3
2018-11-26 11:33:02 +08:00
it('173.4 collection.drop(), basic case', async () => {
2018-11-15 13:24:04 +08:00
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let sd = conn.getSodaDatabase();
2018-11-26 11:33:02 +08:00
let collName = "soda_test_173_4";
2018-11-15 13:24:04 +08:00
collection = await sd.createCollection(collName);
} catch(err) {
should.not.exist(err);
} finally {
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
2018-11-26 11:33:02 +08:00
}); // 173.4
2018-11-15 13:24:04 +08:00
2018-11-26 11:33:02 +08:00
it('173.5 drop multiple times, no error thrown', async () => {
2018-11-15 13:24:04 +08:00
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let sd = conn.getSodaDatabase();
2018-11-26 11:33:02 +08:00
let collName = "soda_test_173_5";
2018-11-15 13:24:04 +08:00
collection = await sd.createCollection(collName);
let res = await collection.drop();
should.strictEqual(res.dropped, true);
res = await collection.drop();
should.strictEqual(res.dropped, false);
res = await collection.drop();
should.strictEqual(res.dropped, false);
} catch(err) {
should.not.exist(err);
} finally {
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
2018-11-26 11:33:02 +08:00
}); // 173.5
2018-11-15 13:24:04 +08:00
2018-11-26 11:33:02 +08:00
it('173.6 dropIndex(), basic case', async () => {
2018-11-15 13:24:04 +08:00
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let soda = conn.getSodaDatabase();
2018-11-26 11:33:02 +08:00
collection = await soda.createCollection("soda_test_173_6");
2018-11-15 13:24:04 +08:00
let indexSpec = {
"name": "OFFICE_IDX",
"fields": [
{
"path": "office",
"datatype": "string",
"order": "asc"
}
]
};
await collection.createIndex(indexSpec);
await Promise.all(
t_contents.map(function(content) {
return collection.insertOne(content);
})
);
2018-11-15 13:24:04 +08:00
// Fetch back
let empInShenzhen = await collection.find()
.filter({ "office": {"$like": "Shenzhen"} })
.count();
should.strictEqual(empInShenzhen.count, 2);
// drop index
let indexName = indexSpec.name;
await collection.dropIndex(indexName);
await conn.commit();
} catch(err) {
2018-11-26 11:35:19 +08:00
should.not.exist(err);
2018-11-15 13:24:04 +08:00
} finally {
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
2018-11-26 11:33:02 +08:00
}); // 173.6
2018-11-15 13:24:04 +08:00
2018-11-26 11:33:02 +08:00
it('173.7 dropping index does not impact query', async () => {
2018-11-15 13:24:04 +08:00
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let soda = conn.getSodaDatabase();
2018-11-26 11:33:02 +08:00
collection = await soda.createCollection("soda_test_173_7");
2018-11-15 13:24:04 +08:00
let indexSpec = {
"name": "OFFICE_IDX",
"fields": [
{
"path": "office",
"datatype": "string",
"order": "asc"
}
]
};
await collection.createIndex(indexSpec);
await Promise.all(
t_contents.map(function(content) {
return collection.insertOne(content);
})
);
// drop index
let indexName = indexSpec.name;
await collection.dropIndex(indexName);
2018-11-15 13:24:04 +08:00
// Fetch back
let empInShenzhen = await collection.find()
.filter({ "office": {"$like": "Shenzhen"} })
.count();
should.strictEqual(empInShenzhen.count, 2);
await conn.commit();
} catch(err) {
2018-11-26 11:35:19 +08:00
should.not.exist(err);
2018-11-15 13:24:04 +08:00
} finally {
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
2018-11-26 11:33:02 +08:00
}); // 173.7
2018-11-15 13:24:04 +08:00
2018-11-26 11:33:02 +08:00
it('173.8 The index is dropped regardless of the auto commit mode', async () => {
2018-11-15 13:24:04 +08:00
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let soda = conn.getSodaDatabase();
2018-11-26 11:33:02 +08:00
collection = await soda.createCollection("soda_test_173_8");
2018-11-15 13:24:04 +08:00
let indexSpec = {
"name": "OFFICE_IDX",
"fields": [
{
"path": "office",
"datatype": "string",
"order": "asc"
}
]
};
await collection.createIndex(indexSpec);
await Promise.all(
t_contents.map(function(content) {
return collection.insertOne(content);
})
);
2018-11-15 13:24:04 +08:00
// Fetch back
let empInShenzhen = await collection.find()
.filter({ "office": {"$like": "Shenzhen"} })
.count();
should.strictEqual(empInShenzhen.count, 2);
// drop index
let indexName = indexSpec.name;
await collection.dropIndex(indexName);
// Does not commit changes
// await conn.commit();
} catch(err) {
2018-11-26 11:35:19 +08:00
should.not.exist(err);
2018-11-15 13:24:04 +08:00
} finally {
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
2018-11-26 11:33:02 +08:00
}); // 173.8
2018-11-15 13:24:04 +08:00
2018-11-26 11:33:02 +08:00
it('173.9 Negative - dropIndex() no parameter', async () => {
2018-11-15 13:24:04 +08:00
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let soda = conn.getSodaDatabase();
2018-11-26 11:33:02 +08:00
collection = await soda.createCollection("soda_test_173_9");
2018-11-15 13:24:04 +08:00
let indexSpec = {
"name": "OFFICE_IDX",
"fields": [
{
"path": "office",
"datatype": "string",
"order": "asc"
}
]
};
await collection.createIndex(indexSpec);
await Promise.all(
t_contents.map(function(content) {
return collection.insertOne(content);
})
);
2018-11-15 13:24:04 +08:00
// Fetch back
let empInShenzhen = await collection.find()
.filter({ "office": {"$like": "Shenzhen"} })
.count();
should.strictEqual(empInShenzhen.count, 2);
// drop index multiple times
let indexName = indexSpec.name;
await collection.dropIndex(indexName);
await collection.dropIndex(indexName);
await collection.dropIndex(indexName);
await conn.commit();
} catch(err) {
2018-11-26 11:35:19 +08:00
should.not.exist(err);
2018-11-15 13:24:04 +08:00
} finally {
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
2018-11-26 11:33:02 +08:00
}); // 173.9
2018-11-26 11:33:02 +08:00
it('173.10 option object of dropIndex(), basic case', async () => {
let options = { "force" : true };
await dropIdxOpt(options);
2018-11-26 11:33:02 +08:00
}); // 173.10
2018-11-26 11:33:02 +08:00
it('173.11 option object of dropIndex(), boolean value is false', async () => {
let options = { "force" : false };
await dropIdxOpt(options);
2018-11-26 11:33:02 +08:00
}); // 173.11
it('173.12 getDataGuide(), basic case', async () => {
2018-11-30 05:36:47 +08:00
2018-11-30 05:34:44 +08:00
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let soda = conn.getSodaDatabase();
collection = await soda.createCollection('soda_test_173_12');
let myContent = { name: "Sally", address: {city: "Melbourne"} };
await collection.insertOne(myContent);
myContent = { name: "Matilda", address: {city: "Sydney"} };
await collection.insertOne(myContent);
myContent = { name: "May", address: {city: "London"} };
await collection.insertOne(myContent);
2018-11-30 05:36:47 +08:00
await conn.commit();
2018-11-30 05:34:44 +08:00
} catch(err) {
2018-11-30 05:36:47 +08:00
should.not.exist(err);
2018-11-30 05:34:44 +08:00
}
/*
* if isCreateIndexEligible >= 0: Can Create Index without error
* if isCreateIndexEligible < 0: Create Index will throw error ORA-00406
* if isCreateIndexEligible = undefined: Getting COMPATIBLE VERSION string failed, the following part is skipped
*/
const isCreateIndexEligible = testsUtil.versionStringCompare(await testsUtil.getDBCompatibleVersion(), '12.2.0.0.0');
2018-11-30 05:34:44 +08:00
try {
let indexSpec = {
"name": "TEST_IDX",
"search_on": "none",
"dataguide": "on"
};
if (isCreateIndexEligible >= 0) {
await collection.createIndex(indexSpec);
let outDocument = await collection.getDataGuide();
should.exist(outDocument);
} else if(isCreateIndexEligible < 0){
2019-02-08 09:51:05 +08:00
await testsUtil.assertThrowsAsync(async () => {await collection.createIndex(indexSpec);}, /ORA-00406:/);
}
2018-11-30 05:34:44 +08:00
} catch(err) {
2018-11-30 05:36:47 +08:00
should.not.exist(err);
2018-11-30 05:34:44 +08:00
}
if (isCreateIndexEligible >= 0) {
try {
let result = await collection.dropIndex('TEST_IDX');
should.strictEqual(result.dropped, true);
await conn.commit();
} catch(err) {
should.not.exist(err);
}
2018-11-30 05:34:44 +08:00
}
try {
if (collection) await collection.drop();
} catch (err) {
should.not.exist(err);
}
try {
if (conn) await conn.close();
} catch (err) {
should.not.exist(err);
}
2018-11-30 05:34:44 +08:00
}); // 173.12
});
const dropIdxOpt = async function(opts) {
let conn, collection;
try {
conn = await oracledb.getConnection(dbconfig);
let soda = conn.getSodaDatabase();
2018-11-30 05:34:44 +08:00
collection = await soda.createCollection("soda_test_173_10");
let indexSpec = {
"name": "OFFICE_IDX",
"fields": [
{
"path": "office",
"datatype": "string",
"order": "asc"
}
]
};
await collection.createIndex(indexSpec);
await Promise.all(
t_contents.map(function(content) {
return collection.insertOne(content);
})
);
// Fetch back
let empInShenzhen = await collection.find()
.filter({ "office": {"$like": "Shenzhen"} })
.count();
should.strictEqual(empInShenzhen.count, 2);
// drop index
let indexName = indexSpec.name;
await collection.dropIndex(indexName, opts);
await conn.commit();
} catch(err) {
2018-11-26 11:35:19 +08:00
should.not.exist(err);
} finally {
if (collection) {
let res = await collection.drop();
should.strictEqual(res.dropped, true);
}
if (conn) {
try {
await conn.close();
} catch(err) {
should.not.exist(err);
}
}
}
2018-11-26 11:35:19 +08:00
};