Skip to content

Commit

Permalink
[TEST] Improve SqlDataRecord DateOnly and TimeOnly coverage (#3128)
Browse files Browse the repository at this point in the history
* Improve SqlDataRecord DateOnly and TimeOnly coverage

Make SqlDecimalConvertToDecimal_TestInRange run on non-US systems

fixes #3125
related to #2258

* make tests actually pass :-(

* netfx test fix

---------

Co-authored-by: Ben Russell <[email protected]>
  • Loading branch information
ErikEJ and benrr101 authored Feb 25, 2025
1 parent cefeabc commit b5ce725
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
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 @@ -116,13 +120,22 @@ public void SqlRecordFillTest()
record.SetSqlMoney(12, SqlMoney.MaxValue);
Assert.Equal(SqlMoney.MaxValue, record.GetSqlMoney(12));

int offset = 1;
#if NET
offset = 3;
record.SetValue(14, new DateOnly(2025, 11,28));
Assert.Equal(new DateTime(2025, 11, 28), record.GetValue(14));

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

// Try adding different values to SqlVariant type
for (int i = 0; i < record.FieldCount - 1; ++i)
for (int i = 0; i < record.FieldCount - offset; ++i)
{
object valueToSet = record.GetSqlValue(i);
record.SetValue(record.FieldCount - 1, valueToSet);
object o = record.GetSqlValue(record.FieldCount - 1);
record.SetValue(record.FieldCount - offset, valueToSet);
object o = record.GetSqlValue(record.FieldCount - offset);

if (o is SqlBinary)
{
Expand All @@ -133,8 +146,8 @@ public void SqlRecordFillTest()
Assert.Equal(valueToSet, o);
}

record.SetDBNull(record.FieldCount - 1);
Assert.Equal(DBNull.Value, record.GetSqlValue(record.FieldCount - 1));
record.SetDBNull(record.FieldCount - offset);
Assert.Equal(DBNull.Value, record.GetSqlValue(record.FieldCount - offset));

record.SetDBNull(i);
Assert.Equal(DBNull.Value, record.GetValue(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

0 comments on commit b5ce725

Please sign in to comment.