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 5 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
68 changes: 33 additions & 35 deletions csharp/test/Drivers/Snowflake/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ClientTests
{
public ClientTests()
{
Skip.IfNot(Utils.CanExecuteTestConfig(SnowflakeTestingUtils.SNOWFLAKE_TEST_CONFIG_VARIABLE));
Skip.IfNot(Utils.CanExecuteTestConfig(SnowflakeTestingUtils.SNOWFLAKE_TEST_CONFIG_VARIABLE));
}

/// <summary>
Expand All @@ -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,9 @@ 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))
Skip.If(testConfiguration.Authentication.SnowflakeJwt is null, "JWT authentication is not configured");

using (Adbc.Client.AdbcConnection adbcConnection = GetSnowflakeAdbcConnectionUsingConnectionString(testConfiguration, SnowflakeAuthentication.AuthJwt))
{
Tests.ClientTests.CanClientExecuteQuery(adbcConnection, testConfiguration);
}
Expand All @@ -166,7 +168,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 +181,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 +220,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,42 +230,38 @@ 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 privateKey = testConfiguration.Authentication.SnowflakeJwt.PrivateKey;
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_VALUE] = privateKey;
if(!string.IsNullOrEmpty(testConfiguration.Pkcs8Passcode))
{
builder[SnowflakeParameters.PKCS8_PASS] = testConfiguration.Pkcs8Passcode;
}
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
else if (string.IsNullOrEmpty(authType) || authType == SnowflakeAuthentication.AuthSnowflake)
{
builder[SnowflakeParameters.PASSWORD] = testConfiguration.Password;
// if no auth type is specified, use the snowflake auth
builder[SnowflakeParameters.AUTH_TYPE] = SnowflakeAuthentication.AuthSnowflake;
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)
{
AdbcDriver = snowflakeDriver
};
}
private Adbc.Client.AdbcConnection GetSnowflakeAdbcConnection(SnowflakeTestConfiguration testConfiguration)
{
Dictionary<string, string> parameters = new Dictionary<string, string>();

AdbcDriver snowflakeDriver = SnowflakeTestingUtils.GetSnowflakeAdbcDriver(testConfiguration, out parameters);

Adbc.Client.AdbcConnection adbcConnection = new Adbc.Client.AdbcConnection(
snowflakeDriver,
parameters: parameters,
options: new Dictionary<string, string>()
);

return adbcConnection;
}
}
}
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": ""
},
"auth_snowflake": {
"user": "",
"password": ""
}
},
"query": "",
"expectedResults": 0
}
70 changes: 54 additions & 16 deletions csharp/test/Drivers/Snowflake/SnowflakeTestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,65 @@ internal class SnowflakeTestConfiguration : TestConfiguration
public string Warehouse { get; set; }

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

/// <summary>
/// The file location of the authentication token (if using).
/// The snowflake Authentication
/// </summary>
[JsonPropertyName("authenticationTokenPath"), JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string AuthenticationTokenPath { get; set; }
[JsonPropertyName("authentication")]
public SnowflakeAuthentication Authentication { get; set; }

/// <summary>
/// The passcode to use if the JWT token is encrypted.
/// </summary>
[JsonPropertyName("pkcs8Passcode"), JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string Pkcs8Passcode { 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 AuthSnowflake = "auth_snowflake";

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

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

[JsonPropertyName(AuthSnowflake)]
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; }
}
}
9 changes: 8 additions & 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,10 +63,16 @@ 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() }
};

if(testConfiguration.Authentication.Default is not null)
{
parameters[SnowflakeParameters.AUTH_TYPE] = SnowflakeAuthentication.AuthSnowflake;
parameters[SnowflakeParameters.USERNAME] = testConfiguration.Authentication.Default.User;
parameters[SnowflakeParameters.PASSWORD] = testConfiguration.Authentication.Default.Password;
}

if(!string.IsNullOrWhiteSpace(testConfiguration.Host))
{
parameters[SnowflakeParameters.HOST] = testConfiguration.Host;
Expand Down
Loading