From febed63d5baff74dd9c05db4561a30bca3b14c91 Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-lf <115584722+sfc-gh-ext-simba-lf@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:28:53 -0700 Subject: [PATCH] SNOW-723810: Add coverage for SnowflakeDb classes (#786) ### Description Regarding issue 113 The PR adds tests for SnowflakeDb classes to increase code coverage ### Checklist - [x] Code compiles correctly - [x] Code is formatted according to [Coding Conventions](../CodingConventions.md) - [x] Created tests which fail without the change (if possible) - [x] All tests passing (`dotnet test`) - [x] Extended the README / documentation, if necessary - [x] Provide JIRA issue id (if possible) or GitHub issue id in PR name --- .../IntegrationTests/SFDbDataReaderIT.cs | 116 ++++++++++++++++++ .../IntegrationTests/SFDbTransactionIT.cs | 36 ++++++ .../UnitTests/SFDbCommandTest.cs | 43 +++++++ 3 files changed, 195 insertions(+) create mode 100644 Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs diff --git a/Snowflake.Data.Tests/IntegrationTests/SFDbDataReaderIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFDbDataReaderIT.cs index f2613acf6..11d08e721 100755 --- a/Snowflake.Data.Tests/IntegrationTests/SFDbDataReaderIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFDbDataReaderIT.cs @@ -594,6 +594,47 @@ public void testGetBoolean() } } + [Test] + public void testGetByte() + { + using (IDbConnection conn = new SnowflakeDbConnection()) + { + // Arrange + conn.ConnectionString = ConnectionString; + conn.Open(); + + CreateOrReplaceTable(conn, TableName, new[] + { + "col1 BINARY", + }); + + byte[] testBytes = Encoding.UTF8.GetBytes("TEST_GET_BINARAY"); + + IDbCommand cmd = conn.CreateCommand(); + + var p1 = cmd.CreateParameter(); + p1.ParameterName = "1"; + p1.DbType = DbType.Binary; + p1.Value = testBytes; + + cmd.Parameters.Add(p1); + cmd.CommandText = $"insert into {TableName} values (?)"; + cmd.ExecuteNonQuery(); + cmd.CommandText = $"select * from {TableName}"; + + // Act + using (IDataReader reader = cmd.ExecuteReader()) + { + int index = 0; + while (reader.Read()) + { + // Assert + Assert.AreEqual(testBytes[index++], reader.GetByte(0)); + } + } + } + } + [Test] public void testGetBinary() { @@ -737,6 +778,37 @@ public void testGetBinary() } } + [Test] + public void testGetChar() + { + using (IDbConnection conn = new SnowflakeDbConnection()) + { + // Arrange + conn.ConnectionString = ConnectionString; + conn.Open(); + + CreateOrReplaceTable(conn, TableName, new[] + { + "col1 VARCHAR(50)", + }); + + char testChar = 'T'; + + IDbCommand cmd = conn.CreateCommand(); + cmd.CommandText = $"insert into {TableName} values ('{testChar}')"; + cmd.ExecuteNonQuery(); + cmd.CommandText = $"select * from {TableName}"; + + // Act + using (IDataReader reader = cmd.ExecuteReader()) + { + // Assert + Assert.IsTrue(reader.Read()); + Assert.AreEqual(testChar, reader.GetChar(0)); + } + } + } + [Test] public void testGetChars() { @@ -882,6 +954,50 @@ public void testGetChars() } } + [Test] + public void testGetDataTypeName() + { + using (IDbConnection conn = new SnowflakeDbConnection()) + { + // Arrange + conn.ConnectionString = ConnectionString; + conn.Open(); + + CreateOrReplaceTable(conn, TableName, new[] + { + "col1 VARCHAR(50)", + "col2 BINARY", + "col3 DOUBLE" + }); + + string testChars = "TEST_GET_CHARS"; + byte[] testBytes = Encoding.UTF8.GetBytes("TEST_GET_BINARY"); + double testDouble = 1.2345678; + + IDbCommand cmd = conn.CreateCommand(); + + var p1 = cmd.CreateParameter(); + p1.ParameterName = "1"; + p1.DbType = DbType.Binary; + p1.Value = testBytes; + + cmd.Parameters.Add(p1); + cmd.CommandText = $"insert into {TableName} values ('{testChars}', ?, {testDouble.ToString()})"; + cmd.ExecuteNonQuery(); + cmd.CommandText = $"select * from {TableName}"; + + // Act + using (DbDataReader reader = (DbDataReader)cmd.ExecuteReader()) + { + // Assert + Assert.IsTrue(reader.Read()); + Assert.AreEqual("TEXT", reader.GetDataTypeName(0)); + Assert.AreEqual("BINARY", reader.GetDataTypeName(1)); + Assert.AreEqual("REAL", reader.GetDataTypeName(2)); + } + } + } + [Test] public void testGetStream() { diff --git a/Snowflake.Data.Tests/IntegrationTests/SFDbTransactionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFDbTransactionIT.cs index c0b571f98..2786d8226 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFDbTransactionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFDbTransactionIT.cs @@ -15,6 +15,42 @@ namespace Snowflake.Data.Tests.IntegrationTests [TestFixture] class SFDbTransactionIT : SFBaseTest { + [Test] + public void TestTransactionDbConnection() + { + using (var conn = new SnowflakeDbConnection()) + { + // Arrange + conn.ConnectionString = ConnectionString; + conn.Open(); + + // Act + using (IDbTransaction t1 = conn.BeginTransaction()) + { + // Assert + Assert.AreEqual(conn, t1.Connection); + } + } + } + + [Test] + public void TestTransactionIsolationLevel() + { + using (var conn = new SnowflakeDbConnection()) + { + // Arrange + conn.ConnectionString = ConnectionString; + conn.Open(); + + // Act + using (IDbTransaction t1 = conn.BeginTransaction()) + { + // Assert + Assert.AreEqual(IsolationLevel.ReadCommitted, t1.IsolationLevel); + } + } + } + [Test] // Test that when a transaction is disposed, rollback would be sent out public void TestTransactionDispose() diff --git a/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs b/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs new file mode 100644 index 000000000..e4350cd61 --- /dev/null +++ b/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. + */ + +namespace Snowflake.Data.Tests.UnitTests +{ + using NUnit.Framework; + using Snowflake.Data.Client; + using System; + + [TestFixture] + class SFDbCommandTest + { + SnowflakeDbCommand command; + + [SetUp] + public void BeforeTest() + { + command = new SnowflakeDbCommand(); + } + + [Test] + public void TestCommandWithConnectionAndCommandText() + { + // Arrange + SnowflakeDbConnection conn = new SnowflakeDbConnection(); + string commandText = "select 1"; + + // Act + command = new SnowflakeDbCommand(conn, commandText); + + // Assert + Assert.AreEqual(conn, command.Connection); + Assert.AreEqual(commandText, command.CommandText); + } + + [Test] + public void TestCommandPrepareThrowsNotImplemented() + { + Assert.Throws(() => command.Prepare()); + } + } +}