From 79983dd61cfdb7ac34448a1efd8cf6f459c7760b Mon Sep 17 00:00:00 2001 From: Chris Pfohl Date: Fri, 19 Jan 2024 09:40:06 -0500 Subject: [PATCH 1/2] Add test that I _think_ verifies my expected behavior or demonstrates a documentation gap. --- tests/Dapper.Tests/MiscTests.cs | 74 ++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/tests/Dapper.Tests/MiscTests.cs b/tests/Dapper.Tests/MiscTests.cs index 9028919ea..9d76d65e8 100644 --- a/tests/Dapper.Tests/MiscTests.cs +++ b/tests/Dapper.Tests/MiscTests.cs @@ -74,6 +74,10 @@ private record PositionalCarRecord(int Age, Car.TrapEnum Trap, string? Name) public PositionalCarRecord() : this(default, default, default) { } } + private record SimpleRecord(int InternalId, int ExternalId, string? Name); + + private record SimpleRecordReordered(int InternalId, string? Name, Guid ExternalId); + private record NominalCarRecord { public int Age { get; init; } @@ -101,6 +105,46 @@ public void TestPositionalRecord() Assert.Equal(2, (int)car.Trap); } + [Fact] + public void TestSimpleRecord() + { + var simple = connection.Query("select 1 InternalId, 42 ExternalId, 'Ford' Name").First(); + + Assert.Equal(1, simple.InternalId); + Assert.Equal(42, simple.ExternalId); + Assert.Equal("Ford", simple.Name); + } + + [Fact] + public void TestSimpleRecordReordered() + { + var simple = connection.Query("select 1 InternalId, 42 ExternalId, 'Ford' Name").First(); + + Assert.Equal(1, simple.InternalId); + Assert.Equal(42, simple.ExternalId); + Assert.Equal("Ford", simple.Name); + } + + [Fact] + public void TestSimpleRecordWithUnderscoreMapping() + { + var simple = connection.Query("select 1 internal_id, 42 external_id, 'Ford' name").First(); + + Assert.Equal(1, simple.InternalId); + Assert.Equal(42, simple.ExternalId); + Assert.Equal("Ford", simple.Name); + } + + [Fact] + public void TestSimpleRecordWithUnderscoreMappingRearranged() + { + var simple = connection.Query("select 1 internal_id, 42 external_id, 'Ford' name").First(); + + Assert.Equal(1, simple.InternalId); + Assert.Equal(42, simple.ExternalId); + Assert.Equal("Ford", simple.Name); + } + [Fact] public void TestNominalRecord() { @@ -319,7 +363,7 @@ public void CheckComplexConcat() const string use_end_only = "CONCAT(@search_term, '%')"; const string use_both = "CONCAT('%', @search_term, '%')"; - // if true, slower query due to not being able to use indices, but will allow searching inside strings + // if true, slower query due to not being able to use indices, but will allow searching inside strings const bool allow_start_wildcards = false; string query = string.Format(formatted, allow_start_wildcards ? use_both : use_end_only); @@ -390,12 +434,12 @@ public void TestStringList() public void TestExecuteCommand() { Assert.Equal(2, connection.Execute(@" - set nocount on - create table #t(i int) - set nocount off - insert #t - select @a a union all select @b - set nocount on + set nocount on + create table #t(i int) + set nocount off + insert #t + select @a a union all select @b + set nocount on drop table #t", new { a = 1, b = 2 })); } @@ -681,7 +725,7 @@ public void DbStringNullHandling() [Fact] public void TestDbStringToString() { - Assert.Equal("Dapper.DbString (Value: 'abcde', Length: 10, IsAnsi: True, IsFixedLength: True)", + Assert.Equal("Dapper.DbString (Value: 'abcde', Length: 10, IsAnsi: True, IsFixedLength: True)", new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true }.ToString()); Assert.Equal("Dapper.DbString (Value: 'abcde', Length: 10, IsAnsi: False, IsFixedLength: True)", new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = false }.ToString()); @@ -1309,12 +1353,12 @@ public HazGetOnlyAndCtor(int idProperty, string nameProperty) IdProperty = idProperty; NameProperty = nameProperty; } - } - - [Fact] + } + + [Fact] public void Issue1164_OverflowExceptionForByte() { - const string sql = "select cast(200 as smallint) as [value]"; // 200 more than sbyte.MaxValue but less than byte.MaxValue + const string sql = "select cast(200 as smallint) as [value]"; // 200 more than sbyte.MaxValue but less than byte.MaxValue Issue1164Object obj = connection.QuerySingle>(sql); Assert.StrictEqual(200, obj.Value); } @@ -1322,7 +1366,7 @@ public void Issue1164_OverflowExceptionForByte() [Fact] public void Issue1164_OverflowExceptionForUInt16() { - const string sql = "select cast(40000 as bigint) as [value]"; // 40000 more than short.MaxValue but less than ushort.MaxValue + const string sql = "select cast(40000 as bigint) as [value]"; // 40000 more than short.MaxValue but less than ushort.MaxValue Issue1164Object obj = connection.QuerySingle>(sql); Assert.StrictEqual(40000, obj.Value); } @@ -1330,7 +1374,7 @@ public void Issue1164_OverflowExceptionForUInt16() [Fact] public void Issue1164_OverflowExceptionForUInt32() { - const string sql = "select cast(4000000000 as bigint) as [value]"; // 4000000000 more than int.MaxValue but less than uint.MaxValue + const string sql = "select cast(4000000000 as bigint) as [value]"; // 4000000000 more than int.MaxValue but less than uint.MaxValue Issue1164Object obj = connection.QuerySingle>(sql); Assert.StrictEqual(4000000000, obj.Value); } @@ -1338,7 +1382,7 @@ public void Issue1164_OverflowExceptionForUInt32() [Fact] public void Issue1164_OverflowExceptionForUInt64() { - const string sql = "select cast(10000000000000000000.0 as float) as [value]"; // 10000000000000000000 more than long.MaxValue but less than ulong.MaxValue + const string sql = "select cast(10000000000000000000.0 as float) as [value]"; // 10000000000000000000 more than long.MaxValue but less than ulong.MaxValue Issue1164Object obj = connection.QuerySingle>(sql); Assert.StrictEqual(10000000000000000000, obj.Value); } From 828167e58d41e05df20c12d5700a46b38122db13 Mon Sep 17 00:00:00 2001 From: Chris Pfohl Date: Fri, 19 Jan 2024 09:57:41 -0500 Subject: [PATCH 2/2] Fix a duh bug --- tests/Dapper.Tests/MiscTests.cs | 36 ++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/tests/Dapper.Tests/MiscTests.cs b/tests/Dapper.Tests/MiscTests.cs index 9d76d65e8..e542c8bcb 100644 --- a/tests/Dapper.Tests/MiscTests.cs +++ b/tests/Dapper.Tests/MiscTests.cs @@ -76,7 +76,7 @@ public PositionalCarRecord() : this(default, default, default) { } private record SimpleRecord(int InternalId, int ExternalId, string? Name); - private record SimpleRecordReordered(int InternalId, string? Name, Guid ExternalId); + private record SimpleRecordReordered(int InternalId, string? Name, int ExternalId); private record NominalCarRecord { @@ -128,21 +128,39 @@ public void TestSimpleRecordReordered() [Fact] public void TestSimpleRecordWithUnderscoreMapping() { - var simple = connection.Query("select 1 internal_id, 42 external_id, 'Ford' name").First(); + var matchVal = DefaultTypeMap.MatchNamesWithUnderscores; + try + { + DefaultTypeMap.MatchNamesWithUnderscores = true; + var simple = connection.Query("select 1 internal_id, 42 external_id, 'Ford' name").First(); - Assert.Equal(1, simple.InternalId); - Assert.Equal(42, simple.ExternalId); - Assert.Equal("Ford", simple.Name); + Assert.Equal(1, simple.InternalId); + Assert.Equal(42, simple.ExternalId); + Assert.Equal("Ford", simple.Name); + } + finally + { + DefaultTypeMap.MatchNamesWithUnderscores = matchVal; + } } [Fact] public void TestSimpleRecordWithUnderscoreMappingRearranged() { - var simple = connection.Query("select 1 internal_id, 42 external_id, 'Ford' name").First(); + var matchVal = DefaultTypeMap.MatchNamesWithUnderscores; + try + { + DefaultTypeMap.MatchNamesWithUnderscores = true; + var simple = connection.Query("select 1 internal_id, 42 external_id, 'Ford' name").First(); - Assert.Equal(1, simple.InternalId); - Assert.Equal(42, simple.ExternalId); - Assert.Equal("Ford", simple.Name); + Assert.Equal(1, simple.InternalId); + Assert.Equal(42, simple.ExternalId); + Assert.Equal("Ford", simple.Name); + } + finally + { + DefaultTypeMap.MatchNamesWithUnderscores = matchVal; + } } [Fact]