Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Apr 12, 2024
1 parent eb4a503 commit 3cf4518
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
7 changes: 6 additions & 1 deletion Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3926,7 +3926,12 @@ public static void ThrowDataException(Exception ex, int index, IDataReader reade
}
else
{
formattedValue = Convert.ToString(value) + " - " + Type.GetTypeCode(value.GetType());
formattedValue = Convert.ToString(value) + " - " + Identify(value.GetType());
}
static string Identify(Type type)
{
var tc = Type.GetTypeCode(type);
return tc == TypeCode.Object ? type.Name : tc.ToString();
}
}
catch (Exception valEx)
Expand Down
35 changes: 23 additions & 12 deletions tests/Dapper.Tests/TypeHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ public void TestChangingDefaultStringTypeMappingToAnsiString()

SqlMapper.PurgeQueryCache();

SqlMapper.AddTypeMap(typeof(string), DbType.AnsiString, true); // Change Default String Handling to AnsiString
var result02 = connection.Query<string>(sql, param).FirstOrDefault();
Assert.Equal("varchar", result02);
SqlMapper.AddTypeMap(typeof(string), DbType.AnsiString, false); // Change Default String Handling to AnsiString
try
{
var result02 = connection.Query<string>(sql, param).FirstOrDefault();
Assert.Equal("varchar", result02);

SqlMapper.PurgeQueryCache();
SqlMapper.AddTypeMap(typeof(string), DbType.String, true); // Restore Default to Unicode String
SqlMapper.PurgeQueryCache();
}
finally
{
SqlMapper.AddTypeMap(typeof(string), DbType.String, false); // Restore Default to Unicode String
}
}

[Fact]
Expand All @@ -46,13 +52,18 @@ public void TestChangingDefaultStringTypeMappingToAnsiStringFirstOrDefault()
Assert.Equal("nvarchar", result01);

SqlMapper.PurgeQueryCache();
SqlMapper.AddTypeMap(typeof(string), DbType.AnsiString, false); // Change Default String Handling to AnsiString
try
{
var result02 = connection.QueryFirstOrDefault<string>(sql, param);
Assert.Equal("varchar", result02);

SqlMapper.AddTypeMap(typeof(string), DbType.AnsiString, true); // Change Default String Handling to AnsiString
var result02 = connection.QueryFirstOrDefault<string>(sql, param);
Assert.Equal("varchar", result02);

SqlMapper.PurgeQueryCache();
SqlMapper.AddTypeMap(typeof(string), DbType.String, true); // Restore Default to Unicode String
SqlMapper.PurgeQueryCache();
}
finally
{
SqlMapper.AddTypeMap(typeof(string), DbType.String, false); // Restore Default to Unicode String
}
}

[Fact]
Expand Down Expand Up @@ -643,7 +654,7 @@ public void Issue149_TypeMismatch_SequentialAccess()
{
Guid guid = Guid.Parse("cf0ef7ac-b6fe-4e24-aeda-a2b45bb5654e");
var ex = Assert.ThrowsAny<Exception>(() => connection.Query<Issue149_Person>("select @guid as Id", new { guid }).First());
Assert.Equal("Error parsing column 0 (Id=n/a - Unable to cast object of type 'System.Guid' to type 'System.String'.)", ex.Message);
Assert.Equal("Error parsing column 0 (Id=cf0ef7ac-b6fe-4e24-aeda-a2b45bb5654e - Guid)", ex.Message);
}

public class Issue149_Person { public string? Id { get; set; } }
Expand Down

0 comments on commit 3cf4518

Please sign in to comment.