Skip to content

Commit

Permalink
test(queries): add additional tests
Browse files Browse the repository at this point in the history
tests for Queries :
- Redshift
- Snowflake
- Sybase
  • Loading branch information
anna-geller authored and mgabelle committed Nov 14, 2024
1 parent dddc145 commit a25512f
Show file tree
Hide file tree
Showing 15 changed files with 423 additions and 14 deletions.
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");
}
}
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);

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import lombok.experimental.SuperBuilder;
import net.snowflake.client.jdbc.SnowflakeConnection;
Expand All @@ -18,7 +19,6 @@
import java.io.InputStream;
import java.net.URI;
import java.sql.Connection;
import jakarta.validation.constraints.NotNull;

@SuperBuilder
@ToString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import lombok.experimental.SuperBuilder;
import net.snowflake.client.jdbc.SnowflakeConnection;
Expand All @@ -15,7 +16,6 @@
import java.io.InputStream;
import java.net.URI;
import java.sql.Connection;
import jakarta.validation.constraints.NotNull;

@SuperBuilder
@ToString
Expand Down
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");
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.kestra.plugin.jdbc.snowflake;

import com.google.common.collect.ImmutableMap;
import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.core.runners.RunContext;
import io.kestra.plugin.jdbc.AbstractJdbcQuery;
import io.kestra.plugin.jdbc.AbstractRdbmsTest;
import io.micronaut.context.annotation.Value;
import io.kestra.core.junit.annotations.KestraTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

Expand All @@ -15,7 +15,10 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.time.*;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.kestra.plugin.jdbc.snowflake;

import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.plugin.jdbc.AbstractJdbcTriggerTest;
import io.micronaut.context.annotation.Value;
import io.kestra.core.junit.annotations.KestraTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.core.runners.RunContext;
import io.kestra.core.runners.RunContextFactory;
import io.kestra.core.storages.StorageInterface;
import io.kestra.core.utils.IdUtils;
import io.micronaut.context.annotation.Value;
import io.kestra.core.junit.annotations.KestraTest;
import jakarta.inject.Inject;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Disabled;
Expand Down
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);

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import io.kestra.plugin.jdbc.AbstractJdbcQuery;
import io.kestra.plugin.jdbc.AutoCommitInterface;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

import java.sql.DriverManager;
Expand Down
Loading

0 comments on commit a25512f

Please sign in to comment.