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

test(csharp/test/Drivers/Snowflake): refactor snowflake test framework #1323

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
53 changes: 34 additions & 19 deletions csharp/test/Drivers/Snowflake/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void CanClientExecuteUpdate()
{
SnowflakeTestConfiguration testConfiguration = Utils.LoadTestConfiguration<SnowflakeTestConfiguration>(SnowflakeTestingUtils.SNOWFLAKE_TEST_CONFIG_VARIABLE);

using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnection(testConfiguration))
using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnectionUsingConnectionString(testConfiguration))
{
string[] queries = SnowflakeTestingUtils.GetQueries(testConfiguration);

Expand All @@ -68,7 +68,7 @@ public void CanClientExecuteUpdateUsingExecuteReader()
{
SnowflakeTestConfiguration testConfiguration = Utils.LoadTestConfiguration<SnowflakeTestConfiguration>(SnowflakeTestingUtils.SNOWFLAKE_TEST_CONFIG_VARIABLE);

using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnection(testConfiguration))
using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnectionUsingConnectionString(testConfiguration))
{
adbcConnection.Open();

Expand Down Expand Up @@ -104,7 +104,7 @@ public void CanClientGetSchema()
{
SnowflakeTestConfiguration testConfiguration = Utils.LoadTestConfiguration<SnowflakeTestConfiguration>(SnowflakeTestingUtils.SNOWFLAKE_TEST_CONFIG_VARIABLE);

using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnection(testConfiguration))
using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnectionUsingConnectionString(testConfiguration))
{
Tests.ClientTests.CanClientGetSchema(adbcConnection, testConfiguration);
}
Expand All @@ -119,7 +119,7 @@ public void CanClientExecuteQuery()
{
SnowflakeTestConfiguration testConfiguration = Utils.LoadTestConfiguration<SnowflakeTestConfiguration>(SnowflakeTestingUtils.SNOWFLAKE_TEST_CONFIG_VARIABLE);

using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnection(testConfiguration))
using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnectionUsingConnectionString(testConfiguration))
{
Tests.ClientTests.CanClientExecuteQuery(adbcConnection, testConfiguration);
}
Expand All @@ -136,7 +136,7 @@ public void CanClientExecuteQueryWithNoResults()
testConfiguration.Query = "SELECT * WHERE 0=1";
testConfiguration.ExpectedResultsCount = 0;

using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnection(testConfiguration))
using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnectionUsingConnectionString(testConfiguration))
{
Tests.ClientTests.CanClientExecuteQuery(adbcConnection, testConfiguration);
}
Expand All @@ -151,7 +151,7 @@ public void CanClientExecuteQueryUsingPrivateKey()
{
SnowflakeTestConfiguration testConfiguration = Utils.LoadTestConfiguration<SnowflakeTestConfiguration>(SnowflakeTestingUtils.SNOWFLAKE_TEST_CONFIG_VARIABLE);

ruowan marked this conversation as resolved.
Show resolved Hide resolved
using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnectionUsingConnectionString(testConfiguration))
using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnectionUsingConnectionString(testConfiguration, SnowflakeAuthentication.AuthJwt))
{
Tests.ClientTests.CanClientExecuteQuery(adbcConnection, testConfiguration);
}
Expand All @@ -166,7 +166,7 @@ public void VerifyTypesAndValues()
{
SnowflakeTestConfiguration testConfiguration = Utils.LoadTestConfiguration<SnowflakeTestConfiguration>(SnowflakeTestingUtils.SNOWFLAKE_TEST_CONFIG_VARIABLE);

using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnection(testConfiguration))
using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnectionUsingConnectionString(testConfiguration))
ruowan marked this conversation as resolved.
Show resolved Hide resolved
{
SampleDataBuilder sampleDataBuilder = SnowflakeData.GetSampleData();

Expand All @@ -179,7 +179,7 @@ public void VerifySchemaTables()
{
SnowflakeTestConfiguration testConfiguration = Utils.LoadTestConfiguration<SnowflakeTestConfiguration>(SnowflakeTestingUtils.SNOWFLAKE_TEST_CONFIG_VARIABLE);

using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnection(testConfiguration))
using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnectionUsingConnectionString(testConfiguration))
{
adbcConnection.Open();

Expand Down Expand Up @@ -218,7 +218,7 @@ public void VerifySchemaTables()
}
}

private Adbc.Client.AdbcConnection GetSnowflakeAdbcConnectionUsingConnectionString(SnowflakeTestConfiguration testConfiguration)
private Adbc.Client.AdbcConnection GetSnowflakeAdbcConnectionUsingConnectionString(SnowflakeTestConfiguration testConfiguration, string authType=null)
{
// see https://arrow.apache.org/adbc/0.5.1/driver/snowflake.html

Expand All @@ -228,22 +228,37 @@ private Adbc.Client.AdbcConnection GetSnowflakeAdbcConnectionUsingConnectionStri
builder[SnowflakeParameters.HOST] = testConfiguration.Host;
builder[SnowflakeParameters.DATABASE] = testConfiguration.Database;
builder[SnowflakeParameters.USERNAME] = testConfiguration.User;
if (!string.IsNullOrEmpty(testConfiguration.AuthenticationTokenPath))
if (authType == SnowflakeAuthentication.AuthJwt)
{
builder[SnowflakeParameters.AUTH_TYPE] = testConfiguration.AuthenticationType;
string privateKey = File.ReadAllText(testConfiguration.AuthenticationTokenPath);
if (testConfiguration.AuthenticationType.Equals("auth_jwt", StringComparison.OrdinalIgnoreCase))
string privateKeyFile = testConfiguration.Authentication.SnowflakeJwt.PrivateKeyFile;
string privateKey = testConfiguration.Authentication.SnowflakeJwt.PrivateKey;

if (privateKeyFile != null)
{
builder[SnowflakeParameters.PKCS8_VALUE] = privateKey;
if(!string.IsNullOrEmpty(testConfiguration.Pkcs8Passcode))
{
builder[SnowflakeParameters.PKCS8_PASS] = testConfiguration.Pkcs8Passcode;
}
privateKey = File.ReadAllText(privateKeyFile);
}
builder[SnowflakeParameters.AUTH_TYPE] = SnowflakeAuthentication.AuthJwt;
ruowan marked this conversation as resolved.
Show resolved Hide resolved
builder[SnowflakeParameters.PKCS8_VALUE] = privateKey;
builder[SnowflakeParameters.USERNAME] = testConfiguration.Authentication.SnowflakeJwt.User;
if (!string.IsNullOrEmpty(testConfiguration.Authentication.SnowflakeJwt.PrivateKeyPassPhrase))
{
builder[SnowflakeParameters.PKCS8_PASS] = testConfiguration.Authentication.SnowflakeJwt.PrivateKeyPassPhrase;
}
}
else if (authType == SnowflakeAuthentication.AuthOAuth)
{
builder[SnowflakeParameters.AUTH_TYPE] = SnowflakeAuthentication.AuthOAuth;
builder[SnowflakeParameters.AUTH_TOKEN] = testConfiguration.Authentication.OAuth.Token;
if (testConfiguration.Authentication.OAuth.User != null)
{
builder[SnowflakeParameters.USERNAME] = testConfiguration.Authentication.OAuth.User;
}
}
else
{
builder[SnowflakeParameters.PASSWORD] = testConfiguration.Password;
// default basic auth
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to specify auth_snowflake (what is currently called Default)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

builder[SnowflakeParameters.USERNAME] = testConfiguration.Authentication.Default.User;
ruowan marked this conversation as resolved.
Show resolved Hide resolved
builder[SnowflakeParameters.PASSWORD] = testConfiguration.Authentication.Default.Password;
}
AdbcDriver snowflakeDriver = SnowflakeTestingUtils.GetSnowflakeAdbcDriver(testConfiguration);
return new Adbc.Client.AdbcConnection(builder.ConnectionString)
Expand Down
4 changes: 2 additions & 2 deletions csharp/test/Drivers/Snowflake/DriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ public void CanGetObjectsAll()
.FirstOrDefault();

Assert.True(columns != null, "Columns cannot be null");
Assert.Equal(testConfiguration.Metadata.ExpectedColumnCount, columns.Count);
Assert.Equal(_testConfiguration.Metadata.ExpectedColumnCount, columns.Count);

if (testConfiguration.UseHighPrecision)
if (_testConfiguration.UseHighPrecision)
{
IEnumerable<AdbcColumn> highPrecisionColumns = columns.Where(c => c.XdbcTypeName == "NUMBER");

Expand Down
21 changes: 15 additions & 6 deletions csharp/test/Drivers/Snowflake/Resources/snowflakeconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@
"account": "",
"host": "",
"database": "",
"user": "",
"password": "",
"warehouse": "",
"authenticationType": "",
"authenticationTokenPath": "",
"pkcs8Passcode": "",
"useHighPrecision": true,
"useHighPrecision": true,
"metadata": {
"catalog": "",
"schema": "",
"table": "",
"expectedColumnCount": 0
},
"authentication": {
"auth_oauth": {
"token": ""
},
"auth_jwt": {
"private_key_file": "",
"private_key_pwd": "",
"user": ""
},
"default": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"default" is not an auth type. This should be auth_snowflake

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

"user": "",
"password": ""
}
},
"query": "",
"expectedResults": 0
}
66 changes: 55 additions & 11 deletions csharp/test/Drivers/Snowflake/SnowflakeTestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,65 @@ internal class SnowflakeTestConfiguration : TestConfiguration
public string AuthenticationType { get; set; }
ruowan marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The file location of the authentication token (if using).
/// The Snowflake use high precision
/// </summary>
[JsonPropertyName("authenticationTokenPath"), JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string AuthenticationTokenPath { get; set; }
[JsonPropertyName("useHighPrecision")]
public bool UseHighPrecision { get; set; } = true;

/// <summary>
/// The passcode to use if the JWT token is encrypted.
/// The snowflake Authentication
/// </summary>
[JsonPropertyName("pkcs8Passcode"), JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string Pkcs8Passcode { get; set; }
[JsonPropertyName("authentication")]
public SnowflakeAuthentication Authentication { get; set; }

/// <summary>
/// The Snowflake authentication type.
/// </summary>
[JsonPropertyName("useHighPrecision")]
public bool UseHighPrecision { get; set; } = true;
}

public class SnowflakeAuthentication
{
public const string AuthOAuth = "auth_oauth";
public const string AuthJwt = "auth_jwt";
public const string AuthDefault = "default";
ruowan marked this conversation as resolved.
Show resolved Hide resolved

[JsonPropertyName(AuthOAuth)]
public OAuthAuthentication OAuth { get; set; }

[JsonPropertyName(AuthJwt)]
public JwtAuthentication SnowflakeJwt { get; set; }

[JsonPropertyName(AuthDefault)]
public DefaultAuthentication Default { get; set; }
}

public class OAuthAuthentication
{
[JsonPropertyName("token")]
public string Token { get; set; }

[JsonPropertyName("user")]
public string User { get; set; }
}

public class JwtAuthentication
{
[JsonPropertyName("private_key")]
public string PrivateKey { get; set; }

[JsonPropertyName("private_key_file")]
public string PrivateKeyFile { get; set; }

[JsonPropertyName("private_key_pwd")]
public string PrivateKeyPassPhrase{ get; set; }

[JsonPropertyName("user")]
public string User { get; set; }
}

public class DefaultAuthentication
{
[JsonPropertyName("user")]
public string User { get; set; }

[JsonPropertyName("password")]
public string Password { get; set; }
}
}
2 changes: 1 addition & 1 deletion csharp/test/Drivers/Snowflake/SnowflakeTestingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal class SnowflakeParameters
public const string PASSWORD = "password";
public const string WAREHOUSE = "adbc.snowflake.sql.warehouse";
public const string AUTH_TYPE = "adbc.snowflake.sql.auth_type";
public const string AUTH_TOKEN = "adbc.snowflake.sql.client_option.auth_token";
public const string HOST = "adbc.snowflake.sql.uri.host";
public const string PKCS8_VALUE = "adbc.snowflake.sql.client_option.jwt_private_key_pkcs8_value";
public const string PKCS8_PASS = "adbc.snowflake.sql.client_option.jwt_private_key_pkcs8_password";
Expand Down Expand Up @@ -62,7 +63,6 @@ out Dictionary<string, string> parameters
{ SnowflakeParameters.USERNAME, testConfiguration.User },
{ SnowflakeParameters.PASSWORD, testConfiguration.Password },
{ SnowflakeParameters.WAREHOUSE, testConfiguration.Warehouse },
{ SnowflakeParameters.AUTH_TYPE, testConfiguration.AuthenticationType },
ruowan marked this conversation as resolved.
Show resolved Hide resolved
{ SnowflakeParameters.USE_HIGH_PRECISION, testConfiguration.UseHighPrecision.ToString().ToLowerInvariant() }
};

Expand Down
Loading