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

Demonstrate Issue with Tests #2029

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 77 additions & 15 deletions tests/Dapper.Tests/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, int ExternalId);

private record NominalCarRecord
{
public int Age { get; init; }
Expand Down Expand Up @@ -101,6 +105,64 @@ public void TestPositionalRecord()
Assert.Equal(2, (int)car.Trap);
}

[Fact]
public void TestSimpleRecord()
{
var simple = connection.Query<SimpleRecord>("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<SimpleRecordReordered>("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 matchVal = DefaultTypeMap.MatchNamesWithUnderscores;
try
{
DefaultTypeMap.MatchNamesWithUnderscores = true;
var simple = connection.Query<SimpleRecord>("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);
}
finally
{
DefaultTypeMap.MatchNamesWithUnderscores = matchVal;
}
}

[Fact]
public void TestSimpleRecordWithUnderscoreMappingRearranged()
{
var matchVal = DefaultTypeMap.MatchNamesWithUnderscores;
try
{
DefaultTypeMap.MatchNamesWithUnderscores = true;
var simple = connection.Query<SimpleRecordReordered>("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);
}
finally
{
DefaultTypeMap.MatchNamesWithUnderscores = matchVal;
}
}

[Fact]
public void TestNominalRecord()
{
Expand Down Expand Up @@ -319,7 +381,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);
Expand Down Expand Up @@ -390,12 +452,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 }));
}

Expand Down Expand Up @@ -681,7 +743,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());
Expand Down Expand Up @@ -1309,36 +1371,36 @@ 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<byte> obj = connection.QuerySingle<Issue1164Object<byte>>(sql);
Assert.StrictEqual(200, obj.Value);
}

[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<ushort> obj = connection.QuerySingle<Issue1164Object<ushort>>(sql);
Assert.StrictEqual(40000, obj.Value);
}

[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<uint> obj = connection.QuerySingle<Issue1164Object<uint>>(sql);
Assert.StrictEqual(4000000000, obj.Value);
}

[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<ulong> obj = connection.QuerySingle<Issue1164Object<ulong>>(sql);
Assert.StrictEqual(10000000000000000000, obj.Value);
}
Expand Down