node-oracledb/test/soda8.js

310 lines
10 KiB
JavaScript
Raw Normal View History

2023-02-21 15:02:10 +08:00
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates. */
2018-11-30 05:34:44 +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
2023-02-21 15:02:10 +08:00
* 2.0 as shown at https://www.apache.org/licenses/LICENSE-2.0. You may choose
* either license.
2018-11-30 05:34:44 +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-11-30 05:34:44 +08:00
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
2018-11-30 05:34:44 +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-11-30 05:34:44 +08:00
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NAME
2023-02-21 11:20:36 +08:00
* 176. soda8.js
2018-11-30 05:34:44 +08:00
*
* DESCRIPTION
2023-02-21 11:20:36 +08:00
* More tests for terminal methods of SodaOperation class
2018-11-30 05:34:44 +08:00
*
*****************************************************************************/
'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-11-30 05:34:44 +08:00
const t_contents = sodaUtil.t_contents;
2023-02-21 15:02:10 +08:00
describe('176. soda8.js', () => {
2019-02-20 07:26:52 +08:00
2018-11-30 05:34:44 +08:00
before(async function() {
const runnable = await testsUtil.isSodaRunnable();
2019-01-23 05:00:54 +08:00
if (!runnable) {
this.skip();
return;
}
2018-11-30 05:34:44 +08:00
await sodaUtil.cleanup();
});
2023-02-21 11:20:36 +08:00
it('176.1 replaceOne(), basic case with document content', async () => {
2023-02-21 15:02:10 +08:00
const conn = await oracledb.getConnection(dbConfig);
const soda = await conn.getSodaDatabase();
const collection = await soda.createCollection("soda_test_176_1");
const myKeys = [];
for (let i = 0; i < t_contents.length; i++) {
const content = t_contents[i];
const doc = await collection.insertOneAndGet(content);
myKeys[i] = doc.key;
}
2023-02-21 11:20:36 +08:00
2023-02-21 15:02:10 +08:00
const inContent = { id: 2000, name: "Paul", office: "Singapore" };
let res = await collection.find().key(myKeys[1]).replaceOne(inContent);
assert.strictEqual(res.replaced, true);
// Fetch all back
const outDocuments = await collection.find().getDocuments();
const contents = [];
for (let i = 0; i < outDocuments.length; i++) {
contents[i] = outDocuments[i].getContent();
if (i == 1) {
assert.deepEqual(contents[i], inContent);
} else {
testsUtil.assertOneOf(t_contents, contents[i]);
2018-11-30 05:34:44 +08:00
}
}
2023-02-21 15:02:10 +08:00
await conn.commit();
res = await collection.drop();
assert.strictEqual(res.dropped, true);
await conn.close();
2023-02-21 11:20:36 +08:00
}); // 176.1
2018-11-30 05:34:44 +08:00
2023-02-21 11:20:36 +08:00
it('176.2 replaceOne(), basic case with document object', async () => {
2023-02-21 15:02:10 +08:00
const conn = await oracledb.getConnection(dbConfig);
const soda = await conn.getSodaDatabase();
const collection = await soda.createCollection("soda_test_176_2");
const myKeys = [];
for (let i = 0; i < t_contents.length; i++) {
const content = t_contents[i];
const doc = await collection.insertOneAndGet(content);
myKeys[i] = doc.key;
2018-11-30 05:34:44 +08:00
}
2023-02-21 15:02:10 +08:00
const inContent = { id: 2000, name: "Paul", office: "Singapore" };
const inDocument = soda.createDocument(inContent);
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
let res = await collection.find().key(myKeys[1]).replaceOne(inDocument);
assert.strictEqual(res.replaced, true);
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
// Fetch all back
const documents = await collection.find().getDocuments();
const contents = [];
for (let i = 0; i < documents.length; i++) {
contents[i] = documents[i].getContent();
if (i == 1) {
assert.deepEqual(contents[i], inContent);
} else {
2023-02-21 11:20:36 +08:00
testsUtil.assertOneOf(t_contents, contents[i]);
2018-11-30 05:34:44 +08:00
}
2022-08-12 12:13:25 +08:00
}
2023-02-21 15:02:10 +08:00
await conn.commit();
res = await collection.drop();
assert.strictEqual(res.dropped, true);
await conn.close();
}); // 176.2
2022-08-12 12:13:25 +08:00
2023-02-21 15:02:10 +08:00
it('175.3 replaceOne(), no error is reported if the query criteria do not match any document', async () => {
const conn = await oracledb.getConnection(dbConfig);
const soda = await conn.getSodaDatabase();
const collection = await soda.createCollection("soda_test_176_3");
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
const myKeys = [];
for (let i = 0; i < t_contents.length; i++) {
const content = t_contents[i];
const doc = await collection.insertOneAndGet(content);
myKeys[i] = doc.key;
2018-11-30 05:34:44 +08:00
}
2023-02-21 15:02:10 +08:00
const inContent = { id: 2000, name: "Paul", office: "Singapore" };
let res = await collection
.find()
.key('4A5AF2AAEB124FD4BFF80BC3630CB048')
.replaceOne(inContent);
assert.strictEqual(res.replaced, false);
// Fetch all back
const outDocuments = await collection.find().getDocuments();
const contents = [];
for (let i = 0; i < outDocuments.length; i++) {
contents[i] = outDocuments[i].getContent();
testsUtil.assertOneOf(t_contents, contents[i]);
2018-11-30 05:34:44 +08:00
}
2023-02-21 15:02:10 +08:00
await conn.commit();
res = await collection.drop();
assert.strictEqual(res.dropped, true);
await conn.close();
}); // 175.3
it('175.4 Negative - replaceOne(), the key() method must be used', async () => {
const conn = await oracledb.getConnection(dbConfig);
const soda = await conn.getSodaDatabase();
const collection = await soda.createCollection("soda_test_176_4");
const myKeys = [];
for (let i = 0; i < t_contents.length; i++) {
const content = t_contents[i];
const doc = await collection.insertOneAndGet(content);
myKeys[i] = doc.key;
}
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
const inContent = { id: 2000, name: "Paul", office: "Singapore" };
await assert.rejects(
async () => await collection.find().replaceOne(inContent),
/ORA-40734:/
);
// ORA-40734: key for the document to replace must be specified using the key attribute
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
// Fetch all back
const outDocuments = await collection.find().keys([myKeys[0]]).getDocuments();
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
const contents = [];
for (let i = 0; i < outDocuments.length; i++) {
contents[i] = outDocuments[i].getContent();
testsUtil.assertOneOf(t_contents, contents[i]);
2018-11-30 05:34:44 +08:00
}
2023-02-21 15:02:10 +08:00
await conn.commit();
const res = await collection.drop();
assert.strictEqual(res.dropped, true);
await conn.close();
}); // 175.4
it('175.5 replaceOneAndGet(), basic case with document content', async () => {
const conn = await oracledb.getConnection(dbConfig);
const soda = await conn.getSodaDatabase();
const collection = await soda.createCollection("soda_test_176_5");
const myKeys = [];
for (let i = 0; i < t_contents.length; i++) {
const content = t_contents[i];
const doc = await collection.insertOneAndGet(content);
myKeys[i] = doc.key;
}
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
const inContent = { id: 2000, name: "Paul", office: "Singapore" };
const updatedDocument = await collection.find().key(myKeys[1]).replaceOneAndGet(inContent);
assert(updatedDocument);
// Fetch all back
const outDocuments = await collection.find().getDocuments();
const contents = [];
for (let i = 0; i < outDocuments.length; i++) {
contents[i] = outDocuments[i].getContent();
if (i == 1) {
assert.deepEqual(contents[i], inContent);
} else {
testsUtil.assertOneOf(t_contents, contents[i]);
2023-02-21 11:20:36 +08:00
}
2023-02-21 15:02:10 +08:00
}
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
await conn.commit();
const res = await collection.drop();
assert.strictEqual(res.dropped, true);
await conn.close();
}); // 175.5
it('175.6 replaceOneAndGet(), basic case with document object', async () => {
const conn = await oracledb.getConnection(dbConfig);
const soda = await conn.getSodaDatabase();
const collection = await soda.createCollection("soda_test_176_6");
const myKeys = [];
for (let i = 0; i < t_contents.length; i++) {
let content = t_contents[i];
let doc = await collection.insertOneAndGet(content);
myKeys[i] = doc.key;
}
2022-08-12 12:13:25 +08:00
2023-02-21 15:02:10 +08:00
const inContent = { id: 2000, name: "Paul", office: "Singapore" };
const inDocument = soda.createDocument(inContent);
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
const updatedDocument = await collection.find().key(myKeys[1]).replaceOneAndGet(inDocument);
assert(updatedDocument);
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
// Fetch all back
const documents = await collection.find().getDocuments();
const contents = [];
for (let i = 0; i < documents.length; i++) {
contents[i] = documents[i].getContent();
if (i == 1) {
assert.deepEqual(contents[i], inContent);
} else {
testsUtil.assertOneOf(t_contents, contents[i]);
2018-11-30 05:34:44 +08:00
}
}
2023-02-21 15:02:10 +08:00
await conn.commit();
const res = await collection.drop();
assert.strictEqual(res.dropped, true);
await conn.close();
}); // 175.6
it('175.7 replaceOneAndGet(), updatedDocument does not have document content', async () => {
const conn = await oracledb.getConnection(dbConfig);
const soda = await conn.getSodaDatabase();
const collection = await soda.createCollection("soda_test_176_7");
const myKeys = [];
for (let i = 0; i < t_contents.length; i++) {
const content = t_contents[i];
const doc = await collection.insertOneAndGet(content);
myKeys[i] = doc.key;
}
2018-11-30 05:34:44 +08:00
2023-02-21 15:02:10 +08:00
const inContent = { id: 2000, name: "Paul", office: "Singapore" };
const updatedDocument = await collection.find().key(myKeys[1]).replaceOneAndGet(inContent);
assert(updatedDocument);
const outContent = updatedDocument.getContent();
assert.ifError(outContent);
await conn.commit();
const res = await collection.drop();
assert.strictEqual(res.dropped, true);
await conn.close();
}); // 175.7
it('175.8 replaceOneAndGet(), no error is reported if it does not match any document', async () => {
const conn = await oracledb.getConnection(dbConfig);
const soda = await conn.getSodaDatabase();
const collection = await soda.createCollection("soda_test_176_8");
const myKeys = [];
for (let i = 0; i < t_contents.length; i++) {
const content = t_contents[i];
const doc = await collection.insertOneAndGet(content);
myKeys[i] = doc.key;
2022-08-12 12:13:25 +08:00
}
2023-02-21 15:02:10 +08:00
const inContent = { id: 2000, name: "Paul", office: "Singapore" };
const updatedDocument = await collection
.find()
.key('4A5AF2AAEB124FD4BFF80BC3630CB048')
.replaceOneAndGet(inContent);
assert.ifError(updatedDocument);
await conn.commit();
const res = await collection.drop();
assert.strictEqual(res.dropped, true);
await conn.close();
}); // 175.8
2019-02-20 07:26:52 +08:00
});