-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from aashikam/testcov
Add tests for AWS Redshift connector
- Loading branch information
Showing
12 changed files
with
637 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Ballerina AWS Redshift Connector Test Module | ||
|
||
This test module is written to test the functionality of the Ballerina AWS Redshift Connector. | ||
|
||
## Docker Image | ||
|
||
For testing purposes, this [docker-pgredshift](https://github.com/HearthSim/docker-pgredshift/pkgs/container/docker-pgredshift) Docker image is used. It emulates AWS Redshift but with limited features, and it does not support SSL. | ||
|
||
## Emulated AWS Redshift Features | ||
|
||
The docker-pgredshift image provides a simulated environment resembling AWS Redshift. However, it is essential to be aware of the limited features and the absence of SSL support in this emulation. | ||
|
||
## Connection Details | ||
|
||
To establish a connection with the emulated AWS Redshift, the Ballerina AWS Redshift Connector uses the [Ballerina PostgreSQL Driver](https://github.com/ballerina-platform/module-ballerinax-postgresql.driver/) as the docker-pgredshift image is based on PostgreSQL and does not connect with the Redshift driver. | ||
|
||
## Usage | ||
|
||
Follow these steps to manually run the test module: | ||
|
||
1. Pull the docker-pgredshift image from [here](https://github.com/HearthSim/docker-pgredshift/pkgs/container/docker-pgredshift). | ||
2. Set up the docker-pgredshift container to emulate AWS Redshift. | ||
3. Use the Ballerina AWS Redshift Connector in your Ballerina programs to interact with the emulated AWS Redshift instance. | ||
|
||
## Reference Links | ||
|
||
- [docker-pgredshift Image](https://github.com/HearthSim/docker-pgredshift/pkgs/container/docker-pgredshift) | ||
- [Ballerina PostgreSQL Driver](https://github.com/ballerina-platform/module-ballerinax-postgresql.driver/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright (c) 2024 WSO2 LLC. (https://www.wso2.com) All Rights Reserved. | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with 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. | ||
|
||
import ballerina/sql; | ||
import ballerina/test; | ||
|
||
@test:Config { | ||
groups: ["batch-execute"] | ||
} | ||
function batchInsertIntoDataTable() returns error? { | ||
var data = [ | ||
{row_id: 12, longValue: 9223372036854774807, doubleValue: 123.34}, | ||
{row_id: 13, longValue: 9223372036854774807, doubleValue: 123.34}, | ||
{row_id: 14, longValue: 9223372036854774807, doubleValue: 123.34} | ||
]; | ||
sql:ParameterizedQuery[] sqlQueries = | ||
from var row in data | ||
select `INSERT INTO NumericTypes (int_type, bigint_type, double_type) VALUES (${row.row_id}, ${row.longValue}, ${row.doubleValue})`; | ||
validateBatchExecutionResult(check batchExecuteRedshiftClient(sqlQueries), [1, 1, 1] | ||
); | ||
} | ||
|
||
@test:Config { | ||
groups: ["batch-execute"], | ||
dependsOn: [batchInsertIntoDataTable] | ||
} | ||
function batchInsertIntoDataTable2() returns error? { | ||
int rowId = 15; | ||
int intValue = 5; | ||
sql:ParameterizedQuery sqlQuery = `INSERT INTO NumericTypes (row_id, int_type) VALUES(${rowId}, ${intValue})`; | ||
sql:ParameterizedQuery[] sqlQueries = [sqlQuery]; | ||
validateBatchExecutionResult(check batchExecuteRedshiftClient(sqlQueries), [1]); | ||
} | ||
|
||
@test:Config { | ||
groups: ["batch-execute"], | ||
dependsOn: [batchInsertIntoDataTable2] | ||
} | ||
function batchInsertIntoDataTableFailure() returns error? { | ||
var data = [ | ||
{row_id: 16, longValue: 9223372036854774807, doubleValue: 123.34}, | ||
{row_id: 17, longValue: 9223372036854774807, doubleValue: 123.34}, | ||
{row_id: 1, longValue: 9223372036854774807, doubleValue: 123.34}, | ||
{row_id: 18, longValue: 9223372036854774807, doubleValue: 123.34} | ||
]; | ||
sql:ParameterizedQuery[] sqlQueries = | ||
from var row in data | ||
select `INSERT INTO NumericTypes (row_id, bigint_type, double_type) VALUES (${row.row_id}, ${row.longValue}, ${row.doubleValue})`; | ||
sql:ExecutionResult[]|error result = batchExecuteRedshiftClient(sqlQueries); | ||
test:assertTrue(result is error); | ||
if result is sql:BatchExecuteError { | ||
sql:BatchExecuteErrorDetail errorDetails = result.detail(); | ||
test:assertEquals(errorDetails.executionResults.length(), 4); | ||
test:assertEquals(errorDetails.executionResults[0].affectedRowCount, -3); | ||
test:assertEquals(errorDetails.executionResults[1].affectedRowCount, -3); | ||
test:assertEquals(errorDetails.executionResults[2].affectedRowCount, -3); | ||
test:assertEquals(errorDetails.executionResults[3].affectedRowCount, -3); | ||
} else { | ||
test:assertFail("Database Error expected."); | ||
} | ||
} | ||
|
||
@test:Config { | ||
groups: ["batch-execute"], | ||
dependsOn: [batchInsertIntoDataTableFailure] | ||
} | ||
function batchInsertIntoCharacterTable() returns error? { | ||
var data = [ | ||
{row_id: 14, charValue: "This is char2", varcharValue: "This is varchar2"}, | ||
{row_id: 15, charValue: "This is char3", varcharValue: "This is varchar3"}, | ||
{row_id: 16, charValue: "This is char4", varcharValue: "This is varchar4"} | ||
]; | ||
sql:ParameterizedQuery[] sqlQueries = | ||
from var row in data | ||
select `INSERT INTO CharacterTypes (row_id, char_type, varchar_type) VALUES (${row.row_id}, ${row.charValue}, ${row.varcharValue})`; | ||
validateBatchExecutionResult(check batchExecuteRedshiftClient(sqlQueries), [1, 1, 1]); | ||
} | ||
|
||
@test:Config { | ||
groups: ["batch-execute"], | ||
dependsOn: [batchInsertIntoCharacterTable] | ||
} | ||
function batchUpdateCharacterTable() returns error? { | ||
var data = [ | ||
{row_id: 14, varcharValue: "Updated varchar2"}, | ||
{row_id: 15, varcharValue: "Updated varchar3"}, | ||
{row_id: 16, varcharValue: "Updated varchar4"} | ||
]; | ||
sql:ParameterizedQuery[] sqlQueries = | ||
from var row in data | ||
select `UPDATE CharacterTypes SET varchar_type = ${row.varcharValue} | ||
WHERE row_id = ${row.row_id}`; | ||
validateBatchExecutionResult(check batchExecuteRedshiftClient(sqlQueries), [1, 1, 1]); | ||
} | ||
|
||
isolated function validateBatchExecutionResult(sql:ExecutionResult[] results, int[] rowCount) { | ||
test:assertEquals(results.length(), rowCount.length()); | ||
} | ||
|
||
function batchExecuteRedshiftClient(sql:ParameterizedQuery[] sqlQueries) returns sql:ExecutionResult[]|error { | ||
Client dbClient = check new (jdbcUrl, user, password); | ||
sql:ExecutionResult[] result = check dbClient->batchExecute(sqlQueries); | ||
check dbClient.close(); | ||
return result; | ||
} |
Oops, something went wrong.