Skip to content

Commit

Permalink
Added testing for toml builder and snowflakedbconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jmartinezramirez committed Jul 18, 2024
1 parent 3a24629 commit 9c94717
Show file tree
Hide file tree
Showing 5 changed files with 345 additions and 36 deletions.
8 changes: 4 additions & 4 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2262,13 +2262,13 @@ public void TestConnectStringWithQueryTag()
}
}


[Test]
[Ignore("Ignore this test requires local connection.toml file. Can be run manually.")]
public void TestConnectStringReadFromToml()
[IgnoreInGithubActions("This test requires a valid connection string in the configuration file.")]
public void TestLocalDefaultConnectStringReadFromToml()
{
using (var conn = new SnowflakeDbConnection(true, "testconnection"))
using (var conn = new SnowflakeDbConnection())
{

conn.Open();
Assert.AreEqual(ConnectionState.Open, conn.State);
}
Expand Down
15 changes: 13 additions & 2 deletions Snowflake.Data.Tests/SFBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,14 @@ public class IgnoreOnEnvIsAttribute : Attribute, ITestAction
private readonly string _key;

private readonly string[] _values;
public IgnoreOnEnvIsAttribute(string key, string[] values)

private readonly string _reason;

public IgnoreOnEnvIsAttribute(string key, string[] values, string reason = null)
{
_key = key;
_values = values;
_reason = reason;
}

public void BeforeTest(ITest test)
Expand All @@ -433,7 +437,7 @@ public void BeforeTest(ITest test)
{
if (Environment.GetEnvironmentVariable(_key) == value)
{
Assert.Ignore("Test is ignored when environment variable {0} is {1} ", _key, value);
Assert.Ignore("Test is ignored when environment variable {0} is {1}. {2}", _key, value, _reason);
}
}
}
Expand All @@ -444,4 +448,11 @@ public void AfterTest(ITest test)

public ActionTargets Targets => ActionTargets.Test | ActionTargets.Suite;
}

public class IgnoreInGithubActions : IgnoreOnEnvIsAttribute
{
public IgnoreInGithubActions(string reason = null) : base("GITHUB_ACTIONS", new[] { "true" }, reason)
{
}
}
}
30 changes: 0 additions & 30 deletions Snowflake.Data.Tests/UnitTests/ConnectionTomlReaderTest.cs

This file was deleted.

61 changes: 61 additions & 0 deletions Snowflake.Data.Tests/UnitTests/SnowflakeDbConnectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@


namespace Snowflake.Data.Tests.UnitTests
{
using Core;
using Core.Tools;
using Moq;
using NUnit.Framework;
using Snowflake.Data.Client;

public class SnowflakeDbConnectionTest
{
[Test]
public void TestFillConnectionStringFromTomlConfig()
{
// Arrange
var mockFileOperations = new Mock<FileOperations>();
var mockEnvironmentOperations = new Mock<EnvironmentOperations>();
mockEnvironmentOperations.Setup(e => e.GetEnvironmentVariable(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string v, string d) => d);
mockFileOperations.Setup(f => f.Exists(It.IsAny<string>())).Returns(true);
mockFileOperations.Setup(f => f.ReadAllText(It.IsAny<string>()))
.Returns("[default]\naccount=\"testaccount\"\nuser=\"testuser\"\npassword=\"testpassword\"\n");
var tomlConnectionBuilder = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object);

// Act
using (var conn = new SnowflakeDbConnection(tomlConnectionBuilder))
{
conn.ConnectionString = "account=user1account;user=user1;password=user1password;";
conn.FillConnectionStringFromTomlConfigIfNotSet();
// Assert
Assert.AreNotEqual("account=testaccount;user=testuser;password=testpassword;", conn.ConnectionString);
Assert.AreNotEqual("account=testaccount;user=testuser;password=testpassword;", conn.ConnectionString);
}
}

[Test]
public void TestFillConnectionStringFromTomlConfigShouldNotBeExecutedIfAlreadySetConnectionString()
{
// Arrange
var connectionTest = "account=user1account;user=user1;password=user1password;";
var mockFileOperations = new Mock<FileOperations>();
var mockEnvironmentOperations = new Mock<EnvironmentOperations>();
mockEnvironmentOperations.Setup(e => e.GetEnvironmentVariable(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string v, string d) => d);
mockFileOperations.Setup(f => f.Exists(It.IsAny<string>())).Returns(true);
mockFileOperations.Setup(f => f.ReadAllText(It.IsAny<string>()))
.Returns("[default]\naccount=\"testaccount\"\nuser=\"testuser\"\npassword=\"testpassword\"\n");
var tomlConnectionBuilder = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object);

// Act
using (var conn = new SnowflakeDbConnection(tomlConnectionBuilder))
{
conn.ConnectionString = connectionTest;
conn.FillConnectionStringFromTomlConfigIfNotSet();
// Assert
Assert.AreEqual(connectionTest, conn.ConnectionString);
}
}
}
}
Loading

0 comments on commit 9c94717

Please sign in to comment.