import oracledb from 'oracledb';
import testConfig from "./config"; // 替换为实际路径
process.env.ORACLE_USER = testConfig.oracle.user ;
process.env.ORACLE_PASS = testConfig.oracle.password ;
process.env.ORACLE_CONNECTION_STRING = testConfig.oracle.connectString ;
process.env.ORACLE_HOME = testConfig.oracleHome ;
process.env.TNS_ADMIN = testConfig.tnsAdmin ;
import { executeQuery } from './index';
process.env.NODE_DEBUG = "oracledb";
let connection: oracledb.Connection;
jest.setTimeout(30000); // 设置超时时间为 30000 毫秒
beforeAll(async () => {
connection = await oracledb.getConnection(testConfig.oracle);
// 创建测试数据表
await connection.execute(`
CREATE TABLE test_table (
id NUMBER PRIMARY KEY,
name VARCHAR2(50)
)
`);
});
afterAll(async () => {
// 删除测试数据表
await connection.execute(`DROP TABLE test_table PURGE`);
await connection.close();
});
afterEach(async () => {
// 清理测试表中的数据
await connection.execute(`DELETE FROM test_table`);
});
describe('executeQuery', () => {
it('should execute a simple SELECT query', async () => {
await connection.execute(`SET TRANSACTION NAME 'transaction1'`);
// 插入测试数据
await connection.execute(`INSERT INTO test_table (id, name) VALUES (:id, :name)`, {
id: 1,
name: 'Test Name',
});
await connection.commit();
// 测试查询功能
const results = await executeQuery(
`SELECT * FROM test_table WHERE id = :id`,
[1],
);
expect(results).toEqual([{ ID: 1, NAME: 'Test Name' }]);
});
it('should handle no results gracefully', async () => {
const results = await executeQuery(`SELECT * FROM test_table WHERE id = :id`, [99]);
expect(results).toEqual([]);
});
});