From 9c94717db2fa984047b6610bad9294825c6a6429 Mon Sep 17 00:00:00 2001 From: Juan Martinez Ramirez Date: Thu, 18 Jul 2024 16:34:23 -0600 Subject: [PATCH] Added testing for toml builder and snowflakedbconnection --- .../IntegrationTests/SFConnectionIT.cs | 8 +- Snowflake.Data.Tests/SFBaseTest.cs | 15 +- .../UnitTests/ConnectionTomlReaderTest.cs | 30 -- .../UnitTests/SnowflakeDbConnectionTest.cs | 61 ++++ .../SnowflakeTomlConnectionBuilderTest.cs | 267 ++++++++++++++++++ 5 files changed, 345 insertions(+), 36 deletions(-) delete mode 100644 Snowflake.Data.Tests/UnitTests/ConnectionTomlReaderTest.cs create mode 100644 Snowflake.Data.Tests/UnitTests/SnowflakeDbConnectionTest.cs create mode 100644 Snowflake.Data.Tests/UnitTests/SnowflakeTomlConnectionBuilderTest.cs diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs index 0ccbe3f9b..53ef034e7 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs @@ -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); } diff --git a/Snowflake.Data.Tests/SFBaseTest.cs b/Snowflake.Data.Tests/SFBaseTest.cs index a6929eed5..ca2e7f576 100755 --- a/Snowflake.Data.Tests/SFBaseTest.cs +++ b/Snowflake.Data.Tests/SFBaseTest.cs @@ -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) @@ -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); } } } @@ -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) + { + } + } } diff --git a/Snowflake.Data.Tests/UnitTests/ConnectionTomlReaderTest.cs b/Snowflake.Data.Tests/UnitTests/ConnectionTomlReaderTest.cs deleted file mode 100644 index 4b377fd1c..000000000 --- a/Snowflake.Data.Tests/UnitTests/ConnectionTomlReaderTest.cs +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2024 Snowflake Computing Inc. All rights reserved. - */ - -namespace Snowflake.Data.Tests.UnitTests -{ - using NUnit.Framework; - using Snowflake.Data.Core; - - [TestFixture, NonParallelizable] - class ConnectionTomlReaderTest - { - - [Test] - [Ignore("Pending to mock filesystem for testing")] - public void Test() - { - // Arrange - var reader = new SnowflakeTomlConnectionBuilder(); - - // Act - var connectionString = reader.GetConnectionStringFromToml("testconnection"); - - // Assert - Assert.AreEqual("", connectionString); - } - - } - -} diff --git a/Snowflake.Data.Tests/UnitTests/SnowflakeDbConnectionTest.cs b/Snowflake.Data.Tests/UnitTests/SnowflakeDbConnectionTest.cs new file mode 100644 index 000000000..c023b6aaf --- /dev/null +++ b/Snowflake.Data.Tests/UnitTests/SnowflakeDbConnectionTest.cs @@ -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(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations.Setup(e => e.GetEnvironmentVariable(It.IsAny(), It.IsAny())) + .Returns((string v, string d) => d); + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(true); + mockFileOperations.Setup(f => f.ReadAllText(It.IsAny())) + .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(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations.Setup(e => e.GetEnvironmentVariable(It.IsAny(), It.IsAny())) + .Returns((string v, string d) => d); + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(true); + mockFileOperations.Setup(f => f.ReadAllText(It.IsAny())) + .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); + } + } + } +} diff --git a/Snowflake.Data.Tests/UnitTests/SnowflakeTomlConnectionBuilderTest.cs b/Snowflake.Data.Tests/UnitTests/SnowflakeTomlConnectionBuilderTest.cs new file mode 100644 index 000000000..6a61d4c91 --- /dev/null +++ b/Snowflake.Data.Tests/UnitTests/SnowflakeTomlConnectionBuilderTest.cs @@ -0,0 +1,267 @@ +/* + * Copyright (c) 2024 Snowflake Computing Inc. All rights reserved. + */ + +namespace Snowflake.Data.Tests.UnitTests +{ + using System; + using Core.Tools; + using Moq; + using NUnit.Framework; + using Snowflake.Data.Core; + + [TestFixture] + class SnowflakeTomlConnectionBuilderTest + { + private readonly string _basicTomlConfig = @" +[default] +account = ""defaultaccountname"" +user = ""defaultusername"" +password = ""defaultpassword"" +[testconnection] +account = ""testaccountname"" +user = ""testusername"" +password = ""testpassword"" +[otherconnection] +account = ""otheraccountname"" +user = ""otherusername"" +password = ""otherpassword"""; + + [Test] + public void TestConnectionWithReadFromDefaultValuesInEnvironmentVariables() + { + // Arrange + var mockFileOperations = new Mock(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(It.IsAny(), It.IsAny())) + .Returns((string e, string s) => s); + + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(true); + mockFileOperations.Setup(f => f.ReadAllText(It.Is(p => p.Contains(".snowflake")))) + .Returns(_basicTomlConfig); + + var reader = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object); + + // Act + var connectionString = reader.GetConnectionStringFromToml(); + + // Assert + Assert.AreEqual("account=defaultaccountname;user=defaultusername;password=defaultpassword;", connectionString); + } + + [Test] + public void TestConnectionWithUserConnectionNameFromEnvVariable() + { + // Arrange + var mockFileOperations = new Mock(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeHome, It.IsAny())) + .Returns("/testpath"); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeDefaultConnectionName, It.IsAny())) + .Returns("testconnection"); + + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(true); + mockFileOperations.Setup(f => f.ReadAllText("/testpath/connections.toml")) + .Returns(_basicTomlConfig); + + var reader = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object); + + // Act + var connectionString = reader.GetConnectionStringFromToml(); + + // Assert + Assert.AreEqual("account=testaccountname;user=testusername;password=testpassword;", connectionString); + } + + [Test] + public void TestConnectionWithUserConnectionNameFromEnvVariableWithMultipleConnections() + { + // Arrange + var mockFileOperations = new Mock(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeHome, It.IsAny())) + .Returns("/testpath"); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeDefaultConnectionName, It.IsAny())) + .Returns("otherconnection"); + + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(true); + mockFileOperations.Setup(f => f.ReadAllText("/testpath/connections.toml")) + .Returns(_basicTomlConfig); + + var reader = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object); + + // Act + var connectionString = reader.GetConnectionStringFromToml(); + + // Assert + Assert.AreEqual("account=otheraccountname;user=otherusername;password=otherpassword;", connectionString); + } + + [Test] + public void TestConnectionWithUserConnectionName() + { + // Arrange + var mockFileOperations = new Mock(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeHome, It.IsAny())) + .Returns("/testpath"); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeDefaultConnectionName, It.IsAny())) + .Returns("otherconnection"); + + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(true); + mockFileOperations.Setup(f => f.ReadAllText("/testpath/connections.toml")) + .Returns(_basicTomlConfig); + + var reader = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object); + + // Act + var connectionString = reader.GetConnectionStringFromToml("testconnection"); + + // Assert + Assert.AreEqual("account=testaccountname;user=testusername;password=testpassword;", connectionString); + } + + + [Test] + [TestCase("database = \"mydb\"", "DB=mydb;")] + public void TestConnectionMapPropertiesFromTomlKeyValues(string tomlKeyValue, string connectionStringValue) + { + // Arrange + var mockFileOperations = new Mock(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeHome, It.IsAny())) + .Returns("/testpath"); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeDefaultConnectionName, It.IsAny())) + .Returns("default"); + + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(true); + mockFileOperations.Setup(f => f.ReadAllText("/testpath/connections.toml")) + .Returns($@" +[default] +account = ""defaultaccountname"" +user = ""defaultusername"" +password = ""defaultpassword"" +{tomlKeyValue} +"); + + var reader = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object); + + // Act + var connectionString = reader.GetConnectionStringFromToml(); + + // Assert + Assert.AreEqual($"account=defaultaccountname;user=defaultusername;password=defaultpassword;{connectionStringValue}", connectionString); + } + + [Test] + public void TestConnectionConfigurationFileDoesNotExistsShouldReturnEmpty() + { + // Arrange + var mockFileOperations = new Mock(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeHome, It.IsAny())) + .Returns("/testpath"); + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(false); + var reader = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object); + + // Act + var connectionString = reader.GetConnectionStringFromToml(); + + // Assert + Assert.AreEqual(string.Empty, connectionString); + } + + [Test] + public void TestConnectionWithInvalidConnectionName() + { + // Arrange + var mockFileOperations = new Mock(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeHome, It.IsAny())) + .Returns("/testpath"); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeDefaultConnectionName, It.IsAny())) + .Returns("wrongconnectionname"); + + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(true); + mockFileOperations.Setup(f => f.ReadAllText(It.Is(p => p.Contains(".snowflake")))) + .Returns(_basicTomlConfig); + + var reader = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object); + + // Act and assert + Assert.Throws(() => reader.GetConnectionStringFromToml(), "Specified connection name does not exist in connections.toml"); + } + + [Test] + public void TestConnectionWithNonExistingDefaultConnection() + { + // Arrange + var mockFileOperations = new Mock(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(It.IsAny(), It.IsAny())) + .Returns((string e, string s) => s); + + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(true); + mockFileOperations.Setup(f => f.ReadAllText(It.Is(p => p.Contains(".snowflake")))) + .Returns("[qa]\naccount = \"qaaccountname\"\nuser = \"qausername\"\npassword = \"qapassword\""); + + var reader = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object); + + // Act + var connectionString = reader.GetConnectionStringFromToml(); + + // Assert + Assert.AreEqual(string.Empty, connectionString); + } + + + [Test] + public void TestConnectionWithSpecifiedConnectionEmpty() + { + // Arrange + var mockFileOperations = new Mock(); + var mockEnvironmentOperations = new Mock(); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeHome, It.IsAny())) + .Returns("/testpath"); + mockEnvironmentOperations + .Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.SnowflakeDefaultConnectionName, It.IsAny())) + .Returns("testconnection1"); + + mockFileOperations.Setup(f => f.Exists(It.IsAny())).Returns(true); + mockFileOperations.Setup(f => f.ReadAllText("/testpath/connections.toml")) + .Returns(@" +[default] +account = ""defaultaccountname"" +user = ""defaultusername"" +password = ""defaultpassword"" +[testconnection1] +[testconnection2] +account = ""testaccountname"" +user = ""testusername"" +password = ""testpassword"""); + + var reader = new SnowflakeTomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object); + + // Act + var connectionString = reader.GetConnectionStringFromToml(); + + // Assert + Assert.AreEqual(string.Empty, connectionString); + } + } + +}