Skip to content

Commit

Permalink
SNOW-723810: Add coverage for SnowflakeDb classes (#786)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
sfc-gh-ext-simba-lf authored Oct 19, 2023
1 parent 459b170 commit febed63
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
116 changes: 116 additions & 0 deletions Snowflake.Data.Tests/IntegrationTests/SFDbDataReaderIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down
36 changes: 36 additions & 0 deletions Snowflake.Data.Tests/IntegrationTests/SFDbTransactionIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
43 changes: 43 additions & 0 deletions Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs
Original file line number Diff line number Diff line change
@@ -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<NotImplementedException>(() => command.Prepare());
}
}
}

0 comments on commit febed63

Please sign in to comment.