generated from kestra-io/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(queries): add additional tests (#440)
tests for Queries : - Redshift - Snowflake - Sybase - Vertica
- Loading branch information
Showing
21 changed files
with
557 additions
and
25 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
plugin-jdbc-redshift/src/test/java/io/kestra/plugin/jdbc/redshift/RedshiftQueriesTest.java
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,103 @@ | ||
package io.kestra.plugin.jdbc.redshift; | ||
|
||
import io.kestra.core.junit.annotations.KestraTest; | ||
import io.kestra.core.models.property.Property; | ||
import io.kestra.core.runners.RunContext; | ||
import io.kestra.plugin.jdbc.AbstractJdbcQueries; | ||
import io.kestra.plugin.jdbc.AbstractRdbmsTest; | ||
import io.micronaut.context.annotation.Value; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
import java.net.URISyntaxException; | ||
import java.sql.SQLException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static io.kestra.core.models.tasks.common.FetchType.FETCH; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
@KestraTest | ||
@Disabled("no server for unit test") | ||
public class RedshiftQueriesTest extends AbstractRdbmsTest { | ||
@Value("${redshift.url}") | ||
protected String url; | ||
|
||
|
||
@Value("${redshift.user}") | ||
protected String user; | ||
|
||
@Value("${redshift.password}") | ||
protected String password; | ||
|
||
@Test | ||
void testMultiSelectWithParameters() throws Exception { | ||
RunContext runContext = runContextFactory.of(Collections.emptyMap()); | ||
|
||
Map<String, Object> parameters = Map.of( | ||
"age", 40, | ||
"brand", "Apple", | ||
"cpu_frequency", 1.5 | ||
); | ||
|
||
Queries taskGet = Queries.builder() | ||
.url(getUrl()) | ||
.username(getUsername()) | ||
.password(getPassword()) | ||
.fetchType(FETCH) | ||
.timeZoneId("Europe/Paris") | ||
.sql(""" | ||
SELECT firstName, lastName, age FROM employee where age > :age and age < :age + 10; | ||
SELECT brand, model FROM laptop where brand = :brand and cpu_frequency > :cpu_frequency; | ||
""") | ||
.parameters(Property.of(parameters)) | ||
.build(); | ||
|
||
AbstractJdbcQueries.MultiQueryOutput runOutput = taskGet.run(runContext); | ||
assertThat(runOutput.getOutputs().size(), is(2)); | ||
|
||
List<Map<String, Object>> employees = runOutput.getOutputs().getFirst().getRows(); | ||
assertThat("employees", employees, notNullValue()); | ||
assertThat("employees", employees.size(), is(1)); | ||
assertThat("employee selected", employees.getFirst().get("age"), is(BigDecimal.valueOf(45))); | ||
assertThat("employee selected", employees.getFirst().get("firstname"), is("John")); | ||
assertThat("employee selected", employees.getFirst().get("lastname"), is("Doe")); | ||
|
||
List<Map<String, Object>>laptops = runOutput.getOutputs().getLast().getRows(); | ||
assertThat("laptops", laptops, notNullValue()); | ||
assertThat("laptops", laptops.size(), is(1)); | ||
assertThat("selected laptop", laptops.getFirst().get("brand"), is("Apple")); | ||
} | ||
|
||
@Override | ||
protected String getUrl() { | ||
return this.url; | ||
} | ||
|
||
@Override | ||
protected String getUsername() { | ||
return this.user; | ||
} | ||
|
||
@Override | ||
protected String getPassword() { | ||
return this.password; | ||
} | ||
|
||
@Override | ||
@BeforeEach | ||
public void init() throws IOException, URISyntaxException, SQLException { | ||
initDatabase(); | ||
} | ||
|
||
@Override | ||
protected void initDatabase() throws SQLException, FileNotFoundException, URISyntaxException { | ||
executeSqlScript("scripts/redshift_queries.sql"); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
plugin-jdbc-redshift/src/test/resources/scripts/redshift_queries.sql
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 @@ | ||
-- Create table employee | ||
DROP TABLE IF EXISTS employee; | ||
CREATE TABLE employee ( | ||
firstName VARCHAR(100), | ||
lastName VARCHAR(100), | ||
age INTEGER | ||
); | ||
|
||
INSERT INTO employee(firstName, lastName, age) VALUES ('John', 'Doe', 45); | ||
INSERT INTO employee(firstName, lastName, age) VALUES ('Bryan', 'Grant', 33); | ||
INSERT INTO employee(firstName, lastName, age) VALUES ('Jude', 'Philips', 25); | ||
INSERT INTO employee(firstName, lastName, age) VALUES ('Michael', 'Page', 62); | ||
|
||
|
||
-- Create table laptop | ||
DROP TABLE IF EXISTS laptop; | ||
CREATE TABLE laptop | ||
( | ||
brand VARCHAR(100), | ||
model VARCHAR(100), | ||
cpu_frequency DECIMAL | ||
); | ||
|
||
INSERT INTO laptop (brand, model, cpu_frequency) VALUES ('Apple', 'MacBookPro M1 13', 2.2); | ||
INSERT INTO laptop (brand, model, cpu_frequency) VALUES ('Apple', 'MacBookPro M3 16', 1.5); | ||
INSERT INTO laptop (brand, model, cpu_frequency) VALUES ('LG', 'Gram', 1.95); | ||
INSERT INTO laptop (brand, model, cpu_frequency) VALUES ('Lenovo', 'ThinkPad', 1.05); | ||
|
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
122 changes: 122 additions & 0 deletions
122
...in-jdbc-snowflake/src/test/java/io/kestra/plugin/jdbc/snowflake/SnowflakeQueriesTest.java
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,122 @@ | ||
package io.kestra.plugin.jdbc.snowflake; | ||
|
||
import io.kestra.core.junit.annotations.KestraTest; | ||
import io.kestra.core.models.property.Property; | ||
import io.kestra.core.runners.RunContext; | ||
import io.kestra.plugin.jdbc.AbstractJdbcQueries; | ||
import io.kestra.plugin.jdbc.AbstractRdbmsTest; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
import java.net.URISyntaxException; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import static io.kestra.core.models.tasks.common.FetchType.FETCH; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
/** | ||
* After creating a Snowflake account, run this SQL query to obtain the host: | ||
* use role acountadmin; | ||
* select system$allowlist(); | ||
* Then find "type":"SNOWFLAKE_DEPLOYMENT" and the associated "host" will be | ||
* like <account_id>.snowflakecomputing.com under | ||
*/ | ||
@KestraTest | ||
@Disabled("Create a Snowflake account for unit testing") | ||
public class SnowflakeQueriesTest extends AbstractRdbmsTest { | ||
protected String host = ""; | ||
protected String username = ""; | ||
protected String password = ""; | ||
protected String database = "KESTRA"; | ||
|
||
@Test | ||
void testMultiSelectWithParameters() throws Exception { | ||
RunContext runContext = runContextFactory.of(Collections.emptyMap()); | ||
|
||
Map<String, Object> parameters = Map.of( | ||
"age", 40, | ||
"brand", "Apple", | ||
"cpu_frequency", 1.5 | ||
); | ||
|
||
Queries taskGet = Queries.builder() | ||
.url(getUrl()) | ||
.username(getUsername()) | ||
.password(getPassword()) | ||
.warehouse("COMPUTE_WH") | ||
.database(database) | ||
.fetchType(FETCH) | ||
.timeZoneId("Europe/Paris") | ||
.sql(""" | ||
SELECT firstName, lastName, age FROM employee where age > :age and age < :age + 10; | ||
SELECT brand, model FROM laptop where brand = :brand and cpu_frequency > :cpu_frequency; | ||
""") | ||
.parameters(Property.of(parameters)) | ||
.build(); | ||
|
||
AbstractJdbcQueries.MultiQueryOutput runOutput = taskGet.run(runContext); | ||
assertThat(runOutput.getOutputs().size(), is(2)); | ||
|
||
List<Map<String, Object>> employees = runOutput.getOutputs().getFirst().getRows(); | ||
assertThat("employees", employees, notNullValue()); | ||
assertThat("employees", employees.size(), is(1)); | ||
assertThat("employee selected", employees.getFirst().get("AGE"), is(BigDecimal.valueOf(45))); | ||
assertThat("employee selected", employees.getFirst().get("FIRSTNAME"), is("John")); | ||
assertThat("employee selected", employees.getFirst().get("LASTNAME"), is("Doe")); | ||
|
||
List<Map<String, Object>>laptops = runOutput.getOutputs().getLast().getRows(); | ||
assertThat("laptops", laptops, notNullValue()); | ||
assertThat("laptops", laptops.size(), is(1)); | ||
assertThat("selected laptop", laptops.getFirst().get("BRAND"), is("Apple")); | ||
} | ||
|
||
@Override | ||
protected String getUrl() { | ||
return "jdbc:snowflake://" + this.host + "/?loginTimeout=3"; | ||
} | ||
|
||
@Override | ||
protected String getUsername() { | ||
return this.username; | ||
} | ||
|
||
@Override | ||
protected String getPassword() { | ||
return this.password; | ||
} | ||
|
||
@Override | ||
protected Connection getConnection() throws SQLException { | ||
Properties properties = new Properties(); | ||
properties.put("user", getUsername()); | ||
properties.put("password", getPassword()); | ||
properties.put("warehouse", "COMPUTE_WH"); | ||
properties.put("db", "UNITTEST"); | ||
properties.put("schema", "public"); | ||
|
||
return DriverManager.getConnection(getUrl(), properties); | ||
} | ||
|
||
@Override | ||
@BeforeEach | ||
public void init() throws IOException, URISyntaxException, SQLException { | ||
initDatabase(); | ||
} | ||
|
||
@Override | ||
protected void initDatabase() throws SQLException, FileNotFoundException, URISyntaxException { | ||
executeSqlScript("scripts/snowflake_queries.sql"); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...in-jdbc-snowflake/src/test/java/io/kestra/plugin/jdbc/snowflake/SnowflakeTriggerTest.java
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
27 changes: 27 additions & 0 deletions
27
plugin-jdbc-snowflake/src/test/resources/scripts/snowflake_queries.sql
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,27 @@ | ||
-- Create table employee | ||
USE KESTRA; | ||
CREATE OR REPLACE TABLE employee ( | ||
firstName VARCHAR(100), | ||
lastName VARCHAR(100), | ||
age INTEGER | ||
); | ||
|
||
INSERT INTO employee(firstName, lastName, age) VALUES ('John', 'Doe', 45); | ||
INSERT INTO employee(firstName, lastName, age) VALUES ('Bryan', 'Grant', 33); | ||
INSERT INTO employee(firstName, lastName, age) VALUES ('Jude', 'Philips', 25); | ||
INSERT INTO employee(firstName, lastName, age) VALUES ('Michael', 'Page', 62); | ||
|
||
|
||
-- Create table laptop | ||
CREATE OR REPLACE TABLE laptop | ||
( | ||
brand VARCHAR(100), | ||
model VARCHAR(100), | ||
cpu_frequency DOUBLE PRECISION | ||
); | ||
|
||
INSERT INTO laptop (brand, model, cpu_frequency) VALUES ('Apple', 'MacBookPro M1 13', 2.2); | ||
INSERT INTO laptop (brand, model, cpu_frequency) VALUES ('Apple', 'MacBookPro M3 16', 1.5); | ||
INSERT INTO laptop (brand, model, cpu_frequency) VALUES ('LG', 'Gram', 1.95); | ||
INSERT INTO laptop (brand, model, cpu_frequency) VALUES ('Lenovo', 'ThinkPad', 1.05); | ||
|
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
Oops, something went wrong.