Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destination Snowflake added integration test for NameTransformer #9065

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
93d53ac
fix for jdk 17
Dec 15, 2021
ede8d38
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 15, 2021
27be3fe
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 15, 2021
ca75033
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 16, 2021
72ab46f
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 16, 2021
aec3384
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 16, 2021
7efd5aa
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 20, 2021
6a773f9
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 21, 2021
fa18537
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 21, 2021
8b92a8d
updated SnowflakeDatabase jdbc connection params
Dec 21, 2021
dbb2d95
updated spec.json
Dec 21, 2021
6378294
updated spec.json
Dec 21, 2021
3b67e96
added integration test
Dec 22, 2021
b0ba37b
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 22, 2021
1a113b0
Merge branch 'master' into vmaltsev/destination-snowflake-update-sche…
Dec 22, 2021
888df78
fix file name
Dec 22, 2021
3ed9025
fix file name
Dec 22, 2021
01fccd9
updated integration tests
Dec 23, 2021
869898c
updated integration tests
Dec 23, 2021
94adebf
updated test method name
Dec 23, 2021
fa31a0d
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 23, 2021
67e0bd6
Merge branch 'master' of github.com:airbytehq/airbyte
Dec 24, 2021
a975619
Merge branch 'master' into vmaltsev/destination-snowflake-update-sche…
Dec 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2021 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.integrations.destination.snowflake;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.airbyte.commons.json.Jsons;
import io.airbyte.commons.string.Strings;
import io.airbyte.protocol.models.AirbyteConnectionStatus;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.*;

class SnowflakeDestinationIntegrationTest {
private final SnowflakeSQLNameTransformer namingResolver = new SnowflakeSQLNameTransformer();

@Test
void testCheckFailsWithInvalidPermissions() throws Exception {
// TODO(sherifnada) this test case is assumes config.json does not have permission to access the
// schema
// this connector should be updated with multiple credentials, each with a clear purpose (valid,
// invalid: insufficient permissions, invalid: wrong password, etc..)
final JsonNode credentialsJsonString = Jsons.deserialize(new String(Files.readAllBytes(Paths.get("secrets/config.json"))));
final AirbyteConnectionStatus check = new SnowflakeDestination().check(credentialsJsonString);
assertEquals(AirbyteConnectionStatus.Status.FAILED, check.getStatus());
}

@Test
public void testInvalidSchemaName() {
assertDoesNotThrow(this::syncWithNamingResolver);
assertThrows(SQLException.class, this::syncWithoutNamingResolver);

}

public void syncWithNamingResolver() throws IOException, SQLException {
final JsonNode config = getConfig();
final String createSchemaQuery = String.format("CREATE SCHEMA %s", namingResolver.getIdentifier(config.get("schema").asText()));
Connection connection =null;
try {
connection = SnowflakeDatabase.getConnection(config);
connection.createStatement().execute(createSchemaQuery);
}finally {
if (connection != null) {
final String dropSchemaQuery = String.format("DROP SCHEMA IF EXISTS %s", namingResolver.getIdentifier(config.get("schema").asText()));
connection.createStatement().execute(dropSchemaQuery);
connection.close();
}
}
}

private void syncWithoutNamingResolver() throws SQLException, IOException {
JsonNode config = getConfig();
String schemaName = config.get("schema").asText();
final String createSchemaQuery = String.format("CREATE SCHEMA %s", schemaName);

try (Connection connection = getConnection(config, false)) {
Statement statement = connection.createStatement();
statement.execute(createSchemaQuery);
}
}

public Connection getConnection(JsonNode config, boolean useNameTransformer) throws SQLException {

final String connectUrl = String.format("jdbc:snowflake://%s", config.get("host").asText());

final Properties properties = new Properties();

properties.put("user", config.get("username").asText());
properties.put("password", config.get("password").asText());
properties.put("warehouse", config.get("warehouse").asText());
properties.put("database", config.get("database").asText());
properties.put("role", config.get("role").asText());
properties.put("schema", useNameTransformer
? namingResolver.getIdentifier(config.get("schema").asText())
: config.get("schema").asText());

properties.put("JDBC_QUERY_RESULT_FORMAT", "JSON");

return DriverManager.getConnection(connectUrl, properties);
}

private JsonNode getConfig() throws IOException {
final JsonNode config = Jsons.deserialize(new String(Files.readAllBytes(Paths.get("secrets/insert_config.json"))));
final String schemaName = "schemaName with whitespace " + Strings.addRandomSuffix("integration_test", "_", 5);
((ObjectNode) config).put("schema", schemaName);
return config;
}
}

This file was deleted.