diff --git a/test/integration/testExecute.js b/test/integration/testExecute.js index 5f62b7a58..48f32357c 100644 --- a/test/integration/testExecute.js +++ b/test/integration/testExecute.js @@ -431,5 +431,70 @@ describe('Execute test - variant', function () { it(testCase.name, createItCallback(testCase, rowAsserts)); }); -}); + describe( 'connection.execute() Resubmitting requests using requestId and different connections', function () { + const createTable = 'create or replace table test_request_id(colA string)'; + let firstConnection; + let secondConnection; + before(async () => { + firstConnection = testUtil.createConnection(); + secondConnection = testUtil.createConnection(); + await testUtil.connectAsync(firstConnection); + await testUtil.connectAsync(secondConnection); + await testUtil.executeCmdAsync(firstConnection, createTable); + }); + + beforeEach(async () => { + await testUtil.executeCmdAsync(firstConnection, 'truncate table if exists test_request_id'); + }); + + after(async () => { + await testUtil.executeCmdAsync(firstConnection, 'drop table if exists test_request_id'); + await testUtil.destroyConnectionAsync(firstConnection); + await testUtil.destroyConnectionAsync(secondConnection); + }); + + it('Do not INSERT twice when the same request id and connection', async () => { + let result; + result = await testUtil.executeCmdAsyncWithAdditionalParameters(firstConnection, 'INSERT INTO test_request_id VALUES (\'testValue\');'); + const requestId = result.rowStatement.getRequestId(); + + result = await testUtil.executeCmdAsyncWithAdditionalParameters(firstConnection, + 'INSERT INTO test_request_id VALUES (\'testValue\');', + { requestId: requestId }); + assert.strictEqual(result.rowStatement.getRequestId(), requestId); + + result = await testUtil.executeCmdAsyncWithAdditionalParameters(firstConnection, 'SELECT * from test_request_id ;'); + assert.strictEqual(result.rows.length, 1); + }); + + it('Execute INSERT for the same request id and different connection', async () => { + let result; + result = await testUtil.executeCmdAsyncWithAdditionalParameters(firstConnection, 'INSERT INTO test_request_id VALUES (\'testValue\');'); + const requestId = result.rowStatement.getRequestId(); + + result = await testUtil.executeCmdAsyncWithAdditionalParameters(secondConnection, 'INSERT INTO test_request_id VALUES (\'testValue\');', { requestId: requestId }); + assert.strictEqual(result.rowStatement.getRequestId(), requestId); + + result = await testUtil.executeCmdAsyncWithAdditionalParameters(firstConnection, 'SELECT * from test_request_id ;'); + assert.strictEqual(result.rows.length, 2); + }); + + it('Execute SELECT for the same request id and different data', async () => { + await testUtil.executeCmdAsyncWithAdditionalParameters(firstConnection, 'INSERT INTO test_request_id VALUES (\'testValue\');'); + let result = await testUtil.executeCmdAsyncWithAdditionalParameters(firstConnection, 'SELECT * from test_request_id;'); + assert.strictEqual(result.rows.length, 1); + const requestId = result.rowStatement.getRequestId(); + + await testUtil.executeCmdAsyncWithAdditionalParameters(firstConnection, 'INSERT INTO test_request_id VALUES (\'testValue\');'); + result = await testUtil.executeCmdAsyncWithAdditionalParameters(firstConnection, 'SELECT * from test_request_id;', { requestId: requestId }); + assert.strictEqual(result.rows.length, 1); + + result = await testUtil.executeCmdAsyncWithAdditionalParameters(firstConnection, 'SELECT * from test_request_id ;'); + assert.strictEqual(result.rows.length, 2); + }); + }); + + + +}); diff --git a/test/integration/testUtil.js b/test/integration/testUtil.js index 9348a3275..c15b61449 100644 --- a/test/integration/testUtil.js +++ b/test/integration/testUtil.js @@ -96,6 +96,18 @@ const executeCmdAsync = function (connection, sqlText, binds = undefined) { module.exports.executeCmdAsync = executeCmdAsync; +const executeCmdAsyncWithAdditionalParameters = function (connection, sqlText, additionalParameters) { + return new Promise((resolve, reject) => { + const executeParams = { ...{ + sqlText: sqlText, + complete: (err, rowStatement, rows) => + err ? reject(err) : resolve({ rowStatement: rowStatement, rows: rows }) + }, ...additionalParameters }; + connection.execute(executeParams); + }); +}; + +module.exports.executeCmdAsyncWithAdditionalParameters = executeCmdAsyncWithAdditionalParameters; /** * Drop tables one by one if exist - any connection error is ignored * @param connection Connection