node-oracledb/test/soda11.js

644 lines
20 KiB
JavaScript
Raw Normal View History

2023-05-03 18:32:09 +08:00
/* Copyright (c) 2018, 2023, Oracle and/or its affiliates. */
2018-12-04 13:55:35 +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.
2018-12-04 13:55:35 +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.
2018-12-04 13:55:35 +08:00
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
2018-12-04 13:55:35 +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.
2018-12-04 13:55:35 +08:00
* See the License for the specific language governing permissions and
* limitations under the License.
*
* 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');
2022-08-12 12:13:25 +08:00
const assert = require('assert');
2023-02-21 12:04:16 +08:00
const dbConfig = require('./dbconfig.js');
2019-02-08 09:51:05 +08:00
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() {
const runnable = await testsUtil.isSodaRunnable();
2019-01-23 05:00:54 +08:00
if (!runnable) {
this.skip();
}
2018-12-04 13:55:35 +08:00
await sodaUtil.cleanup();
});
it('179.1 create collection with metadata', async () => {
let conn, collection;
try {
2023-02-21 12:04:16 +08:00
conn = await oracledb.getConnection(dbConfig);
2023-08-17 16:11:49 +08:00
const sd = conn.getSodaDatabase();
2019-02-20 07:26:52 +08:00
2023-08-17 16:11:49 +08:00
const t_tablename = "myTableName";
const t_metadata = {
"schemaName": dbConfig.user.toUpperCase(),
"tableName": t_tablename,
"keyColumn":
2018-12-04 13:55:35 +08:00
{
"name": "ID",
"sqlType": "VARCHAR2",
"maxLength": 255,
"assignmentMethod": "UUID"
2018-12-04 13:55:35 +08:00
},
"contentColumn":
2018-12-04 13:55:35 +08:00
{
"name": "JSON_DOCUMENT",
"sqlType": "BLOB",
"compress": "NONE",
"cache": true,
"encrypt": "NONE",
"validation": "STANDARD"
2018-12-04 13:55:35 +08:00
},
"versionColumn":
2018-12-04 13:55:35 +08:00
{
"name": "VERSION",
"method": "SHA256"
2018-12-04 13:55:35 +08:00
},
"lastModifiedColumn":
2018-12-04 13:55:35 +08:00
{
"name": "LAST_MODIFIED"
2018-12-04 13:55:35 +08:00
},
"creationTimeColumn":
2018-12-04 13:55:35 +08:00
{
"name": "CREATED_ON"
2018-12-04 13:55:35 +08:00
},
"readOnly": true
2018-12-04 13:55:35 +08:00
};
2023-08-17 16:11:49 +08:00
const t_collname = "soda_test_179_1";
const options = { metaData: t_metadata };
2018-12-04 13:55:35 +08:00
collection = await sd.createCollection(t_collname, options);
await conn.commit();
2022-08-12 12:13:25 +08:00
assert.strictEqual(collection.name, t_collname);
2018-12-04 13:55:35 +08:00
2022-08-12 12:13:25 +08:00
assert.strictEqual(typeof (collection.metaData), "object");
2023-05-03 18:32:09 +08:00
assert.deepStrictEqual(collection.metaData, t_metadata);
2018-12-04 13:55:35 +08:00
} finally {
await conn.commit();
if (collection) {
2023-08-17 16:11:49 +08:00
const res = await collection.drop();
2022-08-12 12:13:25 +08:00
assert.strictEqual(res.dropped, true);
2018-12-04 13:55:35 +08:00
}
2023-02-21 11:20:36 +08:00
await conn.close();
2018-12-04 13:55:35 +08:00
}
}); // 179.1
it('179.2 Negative - create collection with an invalid metadata', async () => {
let conn, collection;
try {
2023-02-21 12:04:16 +08:00
conn = await oracledb.getConnection(dbConfig);
2023-08-17 16:11:49 +08:00
const sd = conn.getSodaDatabase();
2018-12-04 13:55:35 +08:00
2023-08-17 16:11:49 +08:00
const t_metadata = {
"schemaName": "nonexistent",
"tableName": "bar",
"keyColumn":
2018-12-04 13:55:35 +08:00
{
"name": "ID",
"sqlType": "VARCHAR2",
"maxLength": 255,
"assignmentMethod": "UUID"
2018-12-04 13:55:35 +08:00
},
"contentColumn":
2018-12-04 13:55:35 +08:00
{
"name": "JSON_DOCUMENT",
"sqlType": "BLOB",
"compress": "NONE",
"cache": true,
"encrypt": "NONE",
"validation": "STANDARD"
2018-12-04 13:55:35 +08:00
},
"versionColumn":
2018-12-04 13:55:35 +08:00
{
"name": "VERSION",
"method": "SHA256"
2018-12-04 13:55:35 +08:00
},
"lastModifiedColumn":
2018-12-04 13:55:35 +08:00
{
"name": "LAST_MODIFIED"
2018-12-04 13:55:35 +08:00
},
"creationTimeColumn":
2018-12-04 13:55:35 +08:00
{
"name": "CREATED_ON"
2018-12-04 13:55:35 +08:00
},
"readOnly": false
2018-12-04 13:55:35 +08:00
};
2023-08-17 16:11:49 +08:00
const t_collname = "soda_test_179_2";
const options = { metaData: t_metadata };
2023-02-21 11:45:10 +08:00
await assert.rejects(
2018-12-04 13:55:35 +08:00
async () => await sd.createCollection(t_collname, options),
/ORA-01918:/
);
// ORA-01918: user \'nonexistent\' does not exist
} finally {
await conn.commit();
if (collection) {
2023-08-17 16:11:49 +08:00
const res = await collection.drop();
2022-08-12 12:13:25 +08:00
assert.strictEqual(res.dropped, true);
2018-12-04 13:55:35 +08:00
}
2023-02-21 11:20:36 +08:00
await conn.close();
2018-12-04 13:55:35 +08:00
}
}); // 179.2
it('179.3 throw error when creating collection with the existing name and different metadata', async () => {
let conn;
let collection1;
try {
2023-02-21 12:04:16 +08:00
conn = await oracledb.getConnection(dbConfig);
2023-08-17 16:11:49 +08:00
const sd = conn.getSodaDatabase();
2018-12-04 13:55:35 +08:00
2023-08-17 16:11:49 +08:00
const t_metadata1 = {
"schemaName": dbConfig.user.toUpperCase(),
"tableName": "nodb_tab_179_3",
"keyColumn":
2018-12-04 13:55:35 +08:00
{
"name": "ID",
"sqlType": "VARCHAR2",
"maxLength": 255,
"assignmentMethod": "UUID"
2018-12-04 13:55:35 +08:00
},
"contentColumn":
2018-12-04 13:55:35 +08:00
{
"name": "JSON_DOCUMENTS",
"sqlType": "BLOB",
"compress": "NONE",
"cache": true,
"encrypt": "NONE",
"validation": "STRICT"
2018-12-04 13:55:35 +08:00
},
"versionColumn":
2018-12-04 13:55:35 +08:00
{
"name": "VERSION",
"type": "String",
"method": "SHA256"
2018-12-04 13:55:35 +08:00
},
"lastModifiedColumn":
2018-12-04 13:55:35 +08:00
{
"name": "LAST_MODIFIED"
2018-12-04 13:55:35 +08:00
},
"creationTimeColumn":
{
"name": "CREATED_ON"
2018-12-04 13:55:35 +08:00
},
"readOnly": false
};
2023-08-17 16:11:49 +08:00
const t_collname = "soda_test_179_3";
const options = { metaData: t_metadata1 };
2018-12-04 13:55:35 +08:00
collection1 = await sd.createCollection(t_collname, options);
2023-08-17 16:11:49 +08:00
const t_metadata2 = {
"schemaName": "foo",
"tableName": "bar",
"keyColumn":
2018-12-04 13:55:35 +08:00
{
"name": "ID",
"sqlType": "VARCHAR2",
"maxLength": 255,
"assignmentMethod": "UUID"
2018-12-04 13:55:35 +08:00
},
"contentColumn":
2018-12-04 13:55:35 +08:00
{
"name": "JSON_DOCUMENTS",
"sqlType": "BLOB",
"compress": "NONE",
"cache": true,
"encrypt": "NONE",
"validation": "STRICT"
2018-12-04 13:55:35 +08:00
},
"versionColumn":
2018-12-04 13:55:35 +08:00
{
"name": "VERSION",
"type": "String",
"method": "SHA256"
2018-12-04 13:55:35 +08:00
},
"lastModifiedColumn":
2018-12-04 13:55:35 +08:00
{
"name": "LAST_MODIFIED"
2018-12-04 13:55:35 +08:00
},
"creationTimeColumn":
{
"name": "CREATED_ON"
2018-12-04 13:55:35 +08:00
},
"readOnly": true
};
2023-08-17 16:11:49 +08:00
const options2 = { metaData: t_metadata2 };
2018-12-04 13:55:35 +08:00
2023-02-21 11:45:10 +08:00
await assert.rejects(
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.
} finally {
if (collection1) {
2023-02-21 11:20:36 +08:00
await collection1.drop();
2018-12-04 13:55:35 +08:00
}
if (conn) {
2023-02-21 11:20:36 +08:00
await conn.close();
2018-12-04 13:55:35 +08:00
}
}
}); // 179.3
it('179.4 customize the key value, String value', async () => {
let conn;
try {
2023-02-21 12:04:16 +08:00
conn = await oracledb.getConnection(dbConfig);
2023-08-17 16:11:49 +08:00
const sd = conn.getSodaDatabase();
const collectionName = 'soda_test_179_4';
const testMetaData = {
"schemaName": dbConfig.user.toUpperCase(),
"tableName": collectionName,
"keyColumn":
2018-12-04 13:55:35 +08:00
{
"name": "ID",
"sqlType": "NUMBER",
"assignmentMethod": "CLIENT"
2018-12-04 13:55:35 +08:00
},
"contentColumn":
2018-12-04 13:55:35 +08:00
{
"name": "JSON_DOCUMENTS",
"sqlType": "BLOB",
"compress": "NONE",
"cache": true,
"encrypt": "NONE",
"validation": "STRICT"
2018-12-04 13:55:35 +08:00
},
"versionColumn":
2018-12-04 13:55:35 +08:00
{
"name": "VERSION",
"type": "String",
"method": "SHA256"
2018-12-04 13:55:35 +08:00
},
"lastModifiedColumn":
2018-12-04 13:55:35 +08:00
{
"name": "LAST_MODIFIED"
2018-12-04 13:55:35 +08:00
},
"creationTimeColumn":
{
"name": "CREATED_ON"
2018-12-04 13:55:35 +08:00
},
"readOnly": false
};
2023-08-17 16:11:49 +08:00
const coll = await sd.createCollection(collectionName, { metaData: testMetaData});
2018-12-04 13:55:35 +08:00
2023-08-17 16:11:49 +08:00
const testContent = {
name: "Shelly",
2018-12-04 13:55:35 +08:00
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. */
2023-08-17 16:11:49 +08:00
const testKey = '86755';
const testDoc = sd.createDocument(testContent, { key: testKey });
2022-08-12 12:13:25 +08:00
assert.strictEqual(testDoc.key, testKey);
2018-12-04 13:55:35 +08:00
await coll.insertOne(testDoc);
// Fetch it back
2023-08-17 16:11:49 +08:00
const docGot = await coll.find().key(testKey).getOne();
const contentGot = docGot.getContent();
2022-08-12 12:13:25 +08:00
assert.strictEqual(contentGot.name, testContent.name);
assert.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();
2023-08-17 16:11:49 +08:00
const res = await coll.drop();
2022-08-12 12:13:25 +08:00
assert.strictEqual(res.dropped, true);
2018-12-04 13:55:35 +08:00
} finally {
if (conn) {
2023-02-21 11:20:36 +08:00
await conn.close();
2018-12-04 13:55:35 +08:00
}
}
}); // 179.4
// A variation of 179.4
it('179.5 Negative - customize the key value, numeric value', async () => {
let conn, coll;
try {
2023-02-21 12:04:16 +08:00
conn = await oracledb.getConnection(dbConfig);
2023-08-17 16:11:49 +08:00
const sd = conn.getSodaDatabase();
const collectionName = 'soda_test_179_5';
const testMetaData = {
"schemaName": dbConfig.user.toUpperCase(),
"tableName": collectionName,
"keyColumn":
2018-12-04 13:55:35 +08:00
{
"name": "ID",
"sqlType": "NUMBER",
"assignmentMethod": "CLIENT"
2018-12-04 13:55:35 +08:00
},
"contentColumn":
2018-12-04 13:55:35 +08:00
{
"name": "JSON_DOCUMENTS",
"sqlType": "BLOB",
"compress": "NONE",
"cache": true,
"encrypt": "NONE",
"validation": "STRICT"
2018-12-04 13:55:35 +08:00
},
"versionColumn":
2018-12-04 13:55:35 +08:00
{
"name": "VERSION",
"type": "String",
"method": "SHA256"
2018-12-04 13:55:35 +08:00
},
"lastModifiedColumn":
2018-12-04 13:55:35 +08:00
{
"name": "LAST_MODIFIED"
2018-12-04 13:55:35 +08:00
},
"creationTimeColumn":
{
"name": "CREATED_ON"
2018-12-04 13:55:35 +08:00
},
"readOnly": false
};
coll = await sd.createCollection(collectionName, { metaData: testMetaData});
2023-08-17 16:11:49 +08:00
const testContent = {
name: "Shelly",
2018-12-04 13:55:35 +08:00
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. */
2023-08-17 16:11:49 +08:00
const testKey = 86755;
2023-02-21 11:45:10 +08:00
await assert.rejects(
2021-04-01 12:48:37 +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
);
} finally {
if (coll) {
2023-08-17 16:11:49 +08:00
const res = await coll.drop();
2023-02-21 11:20:36 +08:00
assert.strictEqual(res.dropped, true);
2018-12-04 13:55:35 +08:00
}
if (conn) {
2023-02-21 11:20:36 +08:00
await conn.close();
2018-12-04 13:55:35 +08:00
}
}
}); // 179.5
it('179.6 customize the value of mediaType', async () => {
let conn, coll;
try {
2023-02-21 12:04:16 +08:00
conn = await oracledb.getConnection(dbConfig);
2023-08-17 16:11:49 +08:00
const sd = conn.getSodaDatabase();
const collectionName = 'soda_test_179_6';
const testMetaData = {
"schemaName": dbConfig.user.toUpperCase(),
"tableName": collectionName,
"keyColumn":
2018-12-04 13:55:35 +08:00
{
"name": "ID",
"sqlType": "NUMBER",
"assignmentMethod": "CLIENT"
2018-12-04 13:55:35 +08:00
},
"mediaTypeColumn":
{
"name": "MediaType"
},
"contentColumn":
2018-12-04 13:55:35 +08:00
{
"name": "DOCUMENT",
"sqlType": "BLOB",
"compress": "NONE",
"cache": true,
"encrypt": "NONE",
"validation": "STRICT"
2018-12-04 13:55:35 +08:00
},
"versionColumn":
2018-12-04 13:55:35 +08:00
{
"name": "VERSION",
"type": "String",
"method": "SHA256"
2018-12-04 13:55:35 +08:00
},
"lastModifiedColumn":
2018-12-04 13:55:35 +08:00
{
"name": "LAST_MODIFIED"
2018-12-04 13:55:35 +08:00
},
"creationTimeColumn":
{
"name": "CREATED_ON"
2018-12-04 13:55:35 +08:00
},
"readOnly": false
};
coll = await sd.createCollection(collectionName, { metaData: testMetaData});
// Insert a new document
2023-08-17 16:11:49 +08:00
const testContent = {};
const testMediaType = 'image/png';
const testKey = '86755';
const testDoc = sd.createDocument(
2019-02-20 07:26:52 +08:00
testContent,
{ mediaType: testMediaType, key: testKey }
2018-12-04 13:55:35 +08:00
);
2022-08-12 12:13:25 +08:00
assert.strictEqual(testDoc.mediaType, testMediaType);
2018-12-04 13:55:35 +08:00
2023-08-17 16:11:49 +08:00
const myKey = testDoc.key;
2018-12-04 13:55:35 +08:00
await coll.insertOne(testDoc);
// Fetch the document back
2023-08-17 16:11:49 +08:00
const myDoc = await coll.find().key(myKey).getOne();
2018-12-04 13:55:35 +08:00
2022-08-12 12:13:25 +08:00
assert.strictEqual(myDoc.mediaType, testMediaType);
2018-12-04 13:55:35 +08:00
} finally {
await conn.commit();
if (coll) {
2023-08-17 16:11:49 +08:00
const res = await coll.drop();
2023-02-21 11:20:36 +08:00
assert.strictEqual(res.dropped, true);
2018-12-04 13:55:35 +08:00
}
if (conn) {
2023-02-21 11:20:36 +08:00
await conn.close();
2018-12-04 13:55:35 +08:00
}
}
}); // 179.6
it('179.7 Negative - customize mediaType, invalid type, numeric value', async () => {
let conn, coll;
try {
2023-02-21 12:04:16 +08:00
conn = await oracledb.getConnection(dbConfig);
2023-08-17 16:11:49 +08:00
const sd = conn.getSodaDatabase();
const collectionName = 'soda_test_179_7';
const testMetaData = {
"schemaName": dbConfig.user.toUpperCase(),
"tableName": collectionName,
"keyColumn":
2018-12-04 13:55:35 +08:00
{
"name": "ID",
"sqlType": "NUMBER",
"assignmentMethod": "CLIENT"
2018-12-04 13:55:35 +08:00
},
"mediaTypeColumn":
{
"name": "MediaType"
},
"contentColumn":
2018-12-04 13:55:35 +08:00
{
"name": "DOCUMENT",
"sqlType": "BLOB",
"compress": "NONE",
"cache": true,
"encrypt": "NONE",
"validation": "STRICT"
2018-12-04 13:55:35 +08:00
},
"versionColumn":
2018-12-04 13:55:35 +08:00
{
"name": "VERSION",
"type": "String",
"method": "SHA256"
2018-12-04 13:55:35 +08:00
},
"lastModifiedColumn":
2018-12-04 13:55:35 +08:00
{
"name": "LAST_MODIFIED"
2018-12-04 13:55:35 +08:00
},
"creationTimeColumn":
{
"name": "CREATED_ON"
2018-12-04 13:55:35 +08:00
},
"readOnly": false
};
coll = await sd.createCollection(collectionName, { metaData: testMetaData});
// Insert a new document
2023-08-17 16:11:49 +08:00
const testContent = {};
2019-02-20 07:26:52 +08:00
2018-12-04 13:55:35 +08:00
/* Negative value */
2023-08-17 16:11:49 +08:00
const testMediaType = 65432;
const testKey = '86755';
2023-02-21 11:45:10 +08:00
await assert.rejects(
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
);
} finally {
await conn.commit();
if (coll) {
2023-08-17 16:11:49 +08:00
const res = await coll.drop();
2023-02-21 11:20:36 +08:00
assert.strictEqual(res.dropped, true);
2018-12-04 13:55:35 +08:00
}
if (conn) {
2023-02-21 11:20:36 +08:00
await conn.close();
2018-12-04 13:55:35 +08:00
}
}
}); // 179.7
it('179.8 insert an empty document with customized metadata', async () => {
let conn, coll;
try {
2023-02-21 12:04:16 +08:00
conn = await oracledb.getConnection(dbConfig);
2023-08-17 16:11:49 +08:00
const sd = conn.getSodaDatabase();
const collectionName = 'soda_test_179_8';
const testMetaData = {
"schemaName": dbConfig.user.toUpperCase(),
"tableName": collectionName,
"keyColumn":
2018-12-04 13:55:35 +08:00
{
"name": "ID",
"sqlType": "NUMBER",
"assignmentMethod": "CLIENT"
2018-12-04 13:55:35 +08:00
},
"mediaTypeColumn":
{
"name": "MediaType"
},
"contentColumn":
2018-12-04 13:55:35 +08:00
{
"name": "DOCUMENT",
"sqlType": "BLOB",
"compress": "NONE",
"cache": true,
"encrypt": "NONE",
"validation": "STRICT"
2018-12-04 13:55:35 +08:00
},
"versionColumn":
2018-12-04 13:55:35 +08:00
{
"name": "VERSION",
"type": "String",
"method": "SHA256"
2018-12-04 13:55:35 +08:00
},
"lastModifiedColumn":
2018-12-04 13:55:35 +08:00
{
"name": "LAST_MODIFIED"
2018-12-04 13:55:35 +08:00
},
"creationTimeColumn":
{
"name": "CREATED_ON"
2018-12-04 13:55:35 +08:00
},
"readOnly": false
};
coll = await sd.createCollection(collectionName, { metaData: testMetaData });
2023-08-17 16:11:49 +08:00
const testContent = {};
const testKey = '86755';
const testDoc = sd.createDocument(testContent, { key: testKey });
2018-12-04 13:55:35 +08:00
2023-08-17 16:11:49 +08:00
const outDocument = await coll.insertOneAndGet(testDoc);
2022-08-12 12:13:25 +08:00
assert(outDocument);
2018-12-04 13:55:35 +08:00
} finally {
await conn.commit();
if (coll) {
2023-08-17 16:11:49 +08:00
const res = await coll.drop();
2023-02-21 11:20:36 +08:00
assert.strictEqual(res.dropped, true);
2018-12-04 13:55:35 +08:00
}
if (conn) {
2023-02-21 11:20:36 +08:00
await conn.close();
2018-12-04 13:55:35 +08:00
}
}
}); // 179.8
2019-02-20 07:26:52 +08:00
});