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] Improve SqlDataRecord DateOnly and TimeOnly coverage #3128

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public void SqlRecordFillTest()
new SqlMetaData("col11", SqlDbType.Real),
new SqlMetaData("col12", SqlDbType.Decimal),
new SqlMetaData("col13", SqlDbType.Money),
new SqlMetaData("col14", SqlDbType.Variant)
new SqlMetaData("col14", SqlDbType.Variant),
#if NET
new SqlMetaData("col15", SqlDbType.Date),
new SqlMetaData("col16", SqlDbType.Time),
#endif
};

SqlDataRecord record = new SqlDataRecord(metaData);
Expand Down Expand Up @@ -115,7 +119,13 @@ public void SqlRecordFillTest()

record.SetSqlMoney(12, SqlMoney.MaxValue);
Assert.Equal(SqlMoney.MaxValue, record.GetSqlMoney(12));
#if NET
record.SetValue(14, new DateOnly(2025, 11,28));
Assert.Equal(new DateOnly(2025, 11, 28), record.GetValue(14));

record.SetValue(15, new TimeOnly(1, 57, 58));
Assert.Equal(new TimeOnly(1, 57, 58), record.GetValue(15));
#endif

// Try adding different values to SqlVariant type
for (int i = 0; i < record.FieldCount - 1; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Data.SqlTypes;
using System.Threading;
using Xunit;
using System.Globalization;

#if !NETFRAMEWORK
using Microsoft.SqlServer.Types;
using Microsoft.Data.SqlClient.Server;
Expand Down Expand Up @@ -442,6 +444,75 @@ public static void TestDateOnlyTVPDataTable_CommandSP()
DataTestUtility.DropUserDefinedType(connection, tableTypeName);
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void TestDateOnlyTVPSqlDataRecord_CommandSP()
{
string tableTypeName = "[dbo]." + DataTestUtility.GetUniqueNameForSqlServer("UDTTTestDateOnlySqlDataRecordTVP");
string spName = DataTestUtility.GetUniqueNameForSqlServer("spTestDateOnlySqlDataRecordTVP");
SqlConnection connection = new(s_connString);
try
{
connection.Open();
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = $"CREATE TYPE {tableTypeName} AS TABLE ([DateColumn] date NULL, [TimeColumn] time NULL)";
cmd.ExecuteNonQuery();
cmd.CommandText = $"CREATE PROCEDURE {spName} (@dates {tableTypeName} READONLY) AS SELECT COUNT(*) FROM @dates";
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = spName;
cmd.CommandType = CommandType.StoredProcedure;

SqlMetaData[] metadata = new SqlMetaData[]
{
new SqlMetaData("DateColumn", SqlDbType.Date),
new SqlMetaData("TimeColumn", SqlDbType.Time)
};

SqlDataRecord record1 = new SqlDataRecord(metadata);
record1.SetValues(new DateOnly(2023, 11, 15), new TimeOnly(12, 30, 45));

SqlDataRecord record2 = new SqlDataRecord(metadata);
record2.SetValues(new DateOnly(2025, 11, 15), new TimeOnly(13, 31, 46));

IList<SqlDataRecord> featureInserts = new List<SqlDataRecord>
{
record1,
record2,
};

cmd.Parameters.Add(new SqlParameter
{
ParameterName = "@dates",
SqlDbType = SqlDbType.Structured,
TypeName = tableTypeName,
Value = featureInserts,
});

using var reader = cmd.ExecuteReader();

Assert.True(reader.HasRows);

int count = 0;
while (reader.Read())
{
Assert.NotNull(reader[0]);
count++;
}

Assert.Equal(1, count);
}
}
finally
{
DataTestUtility.DropStoredProcedure(connection, spName);
DataTestUtility.DropUserDefinedType(connection, tableTypeName);
}
}
#endif

#region Scaled Decimal Parameter & TVP Test
Expand Down Expand Up @@ -471,8 +542,8 @@ public static void SqlDecimalConvertToDecimal_TestInRange(string sqlDecimalValue
cmd.Connection = cnn;
using SqlDataReader rdr = cmd.ExecuteReader();
Assert.True(rdr.Read(), "SqlDataReader must have a value");
decimal retrunValue = rdr.GetDecimal(0);
Assert.Equal(expectedDecimalValue, retrunValue.ToString());
decimal returnValue = rdr.GetDecimal(0);
Assert.Equal(expectedDecimalValue, returnValue.ToString(CultureInfo.InvariantCulture));
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
Expand Down
Loading