From ec6faf5476d7165d8e5c7dff0cbed741e9c24efa Mon Sep 17 00:00:00 2001 From: Marc Gravell Date: Sat, 2 May 2015 10:54:48 +0100 Subject: [PATCH] Fixes for CoreCLR and DNX --- Dapper NET40/SqlMapper.cs | 26 +-- Dapper.DNX.Tests/Program.cs | 37 ---- Dapper.DNX.Tests/project.json | 21 +- Dapper.DNX.Tests/project.lock.json | 56 +++++- Dapper.DNX.sln | 5 +- Dapper/project.json | 16 +- DapperTests NET35/DapperTests NET35.csproj | 5 +- DapperTests NET45/DapperTests NET45.csproj | 4 +- Tests/Assert.cs | 4 + Tests/DapperTests NET40.csproj | 5 +- Tests/Program.cs | 46 ++++- Tests/Tests.cs | 224 ++++++++++++++++----- 12 files changed, 332 insertions(+), 117 deletions(-) delete mode 100644 Dapper.DNX.Tests/Program.cs diff --git a/Dapper NET40/SqlMapper.cs b/Dapper NET40/SqlMapper.cs index 1a2e8a2..98a52a4 100644 --- a/Dapper NET40/SqlMapper.cs +++ b/Dapper NET40/SqlMapper.cs @@ -7,16 +7,16 @@ */ #if DNXCORE50 -using IDbDataParameter = System.Data.Common.DbParameter; -using IDataParameter = System.Data.Common.DbParameter; -using IDbTransaction = System.Data.Common.DbTransaction; -using IDbConnection = System.Data.Common.DbConnection; -using IDbCommand = System.Data.Common.DbCommand; -using IDataReader = System.Data.Common.DbDataReader; -using IDataRecord = System.Data.Common.DbDataReader; -using IDataParameterCollection = System.Data.Common.DbParameterCollection; -using DataException = System.InvalidOperationException; -using ApplicationException = System.InvalidOperationException; +using IDbDataParameter = global::System.Data.Common.DbParameter; +using IDataParameter = global::System.Data.Common.DbParameter; +using IDbTransaction = global::System.Data.Common.DbTransaction; +using IDbConnection = global::System.Data.Common.DbConnection; +using IDbCommand = global::System.Data.Common.DbCommand; +using IDataReader = global::System.Data.Common.DbDataReader; +using IDataRecord = global::System.Data.Common.DbDataReader; +using IDataParameterCollection = global::System.Data.Common.DbParameterCollection; +using DataException = global::System.InvalidOperationException; +using ApplicationException = global::System.InvalidOperationException; #endif using System; @@ -4907,10 +4907,10 @@ public DynamicParameters Output(T target, Expression> express #if DNXCORE50 lock (cache) { -#endif - setter = (Action)cache[lookup]; -#if DNXCORE50 + if(!cache.TryGetValue(lookup, out setter)) setter = null; } +#else + setter = (Action)cache[lookup]; #endif if (setter != null) goto MAKECALLBACK; diff --git a/Dapper.DNX.Tests/Program.cs b/Dapper.DNX.Tests/Program.cs deleted file mode 100644 index 34f650a..0000000 --- a/Dapper.DNX.Tests/Program.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Linq; -using System.Data.SqlClient; -using System.Threading.Tasks; -using System.Reflection; - -namespace Dapper.DNX.Tests -{ - public class Program - { - public void Main() - { -#if DNXCORE50 - Console.WriteLine("From: {0}", typeof(int).AssemblyQualifiedName); -#else - Console.WriteLine("Version: {0}", Environment.Version); -#endif - const string connectionString = "Data Source=.;Initial Catalog=tempdb;Integrated Security=True"; - using (var conn = new SqlConnection(connectionString)) - { - conn.Open(); - var row = conn.Query("select @a as X", new { a = 123 }).Single(); - Console.WriteLine(row.X); - - var methods = typeof(Dapper.SqlMapper).GetMethods().Where(x => x.Name == "QueryAsync").ToList(); -#if ASYNC - row = conn.QueryAsync("select @a as X", new { a = 123 }).Result.Single(); -#endif - Console.WriteLine(row.X); - } - } - class Foo - { - public int X { get; set; } - } - } -} diff --git a/Dapper.DNX.Tests/project.json b/Dapper.DNX.Tests/project.json index 7fd9aac..63c2cfa 100644 --- a/Dapper.DNX.Tests/project.json +++ b/Dapper.DNX.Tests/project.json @@ -6,26 +6,32 @@ "commands": { "Dapper.DNX.Tests": "Dapper.DNX.Tests" }, + "compile": [ "../Tests/Tests.cs", "../Tests/Program.cs", "../Tests/Assert.cs" ], + "compilationOptions": { "define": [ "NOEXTERNALS" ] }, "frameworks": { "net45": { "compilationOptions": { "define": [ "ASYNC" ], "warningsAsErrors": true }, "dependencies": { + "System.Threading.Thread": "4.0.0-beta-22816" }, "frameworkAssemblies": { - "System.Data": "4.0.0.0" + "System.Data": "4.0.0.0", + "System.Xml": "4.0.0.0" } }, "net40": { "dependencies": { }, "frameworkAssemblies": { - "System.Data": "4.0.0.0" + "System.Data": "4.0.0.0", + "System.Xml": "4.0.0.0" } }, "dnx451": { "compilationOptions": { "define": [ "ASYNC" ], "warningsAsErrors": true }, "frameworkAssemblies": { - "System.Data": "4.0.0.0" + "System.Data": "4.0.0.0", + "System.Xml": "4.0.0.0" } }, @@ -33,7 +39,14 @@ "compilationOptions": { "define": [ ], "warningsAsErrors": true }, "dependencies": { "System.Console": "4.0.0-beta-*", - "System.Reflection": "4.0.10-beta-*" + "System.Reflection": "4.0.10-beta-*", + "System.Linq": "4.0.0-beta-*", + "System.Data.Common": "4.0.0-beta-*", + "System.Data.SqlClient": "4.0.0-beta-*", + "System.Threading": "4.0.10-beta-*", + "System.Threading.Thread": "4.0.0-beta-*", + "System.Reflection.TypeExtensions": "4.0.0-beta-*" + //"System.Xml": "4.0.10-beta-*" } } } diff --git a/Dapper.DNX.Tests/project.lock.json b/Dapper.DNX.Tests/project.lock.json index 2bc1a72..42d9d7b 100644 --- a/Dapper.DNX.Tests/project.lock.json +++ b/Dapper.DNX.Tests/project.lock.json @@ -16,6 +16,20 @@ "runtime": [ "lib/net45/System.Runtime.dll" ] + }, + "System.Threading.Thread/4.0.0-beta-22816": { + "dependencies": { + "System.Runtime": "4.0.20-beta-22816" + }, + "frameworkAssemblies": [ + "mscorlib" + ], + "compile": [ + "lib/net45/System.Threading.Thread.dll" + ], + "runtime": [ + "lib/net45/System.Threading.Thread.dll" + ] } }, ".NETFramework,Version=v4.0": {}, @@ -325,6 +339,17 @@ "lib/aspnetcore50/System.Threading.Tasks.dll" ] }, + "System.Threading.Thread/4.0.0-beta-22816": { + "dependencies": { + "System.Runtime": "4.0.20-beta-22816" + }, + "compile": [ + "lib/contract/System.Threading.Thread.dll" + ], + "runtime": [ + "lib/aspnetcore50/System.Threading.Thread.dll" + ] + }, "System.Xml.ReaderWriter/4.0.10-beta-22816": { "dependencies": { "System.IO": "4.0.10-beta-22816", @@ -669,6 +694,19 @@ "lib/portable-wpa81+wp80+win80+net45+aspnetcore50/System.Threading.Tasks.dll" ] }, + "System.Threading.Thread/4.0.0-beta-22816": { + "sha512": "hq1pNoES0jEKslftDhBeJnRUBSjEepiya+39oH/7yCvOp4xMnXHlWe9G7ZS/dg2n4k+3VY21AUifisybcFjcCQ==", + "files": [ + "License.rtf", + "System.Threading.Thread.4.0.0-beta-22816.nupkg", + "System.Threading.Thread.4.0.0-beta-22816.nupkg.sha512", + "System.Threading.Thread.nuspec", + "lib/aspnetcore50/System.Threading.Thread.dll", + "lib/contract/System.Threading.Thread.dll", + "lib/net45/System.Threading.Thread.dll", + "lib/portable-wpa81+wp80+win80+net45+aspnetcore50/System.Threading.Thread.dll" + ] + }, "System.Xml.ReaderWriter/4.0.10-beta-22816": { "sha512": "G0aLPtC/phTfiJPwe0VA3tB3x8YFQ1dHFuE1xaHNr9eQm/AfBp4Pk+fn3s7ABJDus/T89EtIHQ9C+O6VmqXIQQ==", "files": [ @@ -688,17 +726,27 @@ "Dapper >= 1.41-*" ], ".NETFramework,Version=v4.5": [ - "framework/System.Data >= 4.0.0.0" + "System.Threading.Thread >= 4.0.0-beta-22816", + "framework/System.Data >= 4.0.0.0", + "framework/System.Xml >= 4.0.0.0" ], ".NETFramework,Version=v4.0": [ - "framework/System.Data >= 4.0.0.0" + "framework/System.Data >= 4.0.0.0", + "framework/System.Xml >= 4.0.0.0" ], "DNX,Version=v4.5.1": [ - "framework/System.Data >= 4.0.0.0" + "framework/System.Data >= 4.0.0.0", + "framework/System.Xml >= 4.0.0.0" ], "DNXCore,Version=v5.0": [ "System.Console >= 4.0.0-beta-*", - "System.Reflection >= 4.0.10-beta-*" + "System.Reflection >= 4.0.10-beta-*", + "System.Linq >= 4.0.0-beta-*", + "System.Data.Common >= 4.0.0-beta-*", + "System.Data.SqlClient >= 4.0.0-beta-*", + "System.Threading >= 4.0.10-beta-*", + "System.Threading.Thread >= 4.0.0-beta-*", + "System.Reflection.TypeExtensions >= 4.0.0-beta-*" ] } } \ No newline at end of file diff --git a/Dapper.DNX.sln b/Dapper.DNX.sln index 5af841a..4c105e3 100644 --- a/Dapper.DNX.sln +++ b/Dapper.DNX.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.22728.1 +VisualStudioVersion = 14.0.22823.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Dapper", "Dapper\Dapper.xproj", "{088D8CC4-E71E-44B6-9B87-4060B043983D}" EndProject @@ -9,8 +9,11 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Dapper.DNX.Tests", "Dapper. EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{03340C6E-4176-4C42-9C76-D5DFC79D1A22}" ProjectSection(SolutionItems) = preProject + Tests\Assert.cs = Tests\Assert.cs + Tests\Program.cs = Tests\Program.cs Dapper NET40\SqlMapper.cs = Dapper NET40\SqlMapper.cs Dapper NET45\SqlMapperAsync.cs = Dapper NET45\SqlMapperAsync.cs + Tests\Tests.cs = Tests\Tests.cs EndProjectSection EndProject Global diff --git a/Dapper/project.json b/Dapper/project.json index eac6663..9b297ef 100644 --- a/Dapper/project.json +++ b/Dapper/project.json @@ -1,8 +1,14 @@ { "authors": [ "Sam Saffron", "Marc Gravell" ], + "owners": [ "Sam Saffron", "Marc Gravell" ], + "projectUrl": "https://github.com/StackExchange/dapper-dot-net", + "licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0", + "summary": "A high performance Micro-ORM", "description": "A high performance Micro-ORM supporting Sql Server, MySQL, Sqlite, SqlCE, Firebird etc..", - "version": "1.41-beta", + "version": "1.41-beta2", "compile": [ "../Dapper NET40/*.cs", "../Dapper NET45/*.cs" ], + "title": "Dapper dot net", + "tags": [ "orm", "sql", "micro-orm" ], "frameworks": { "net45": { "compilationOptions": { "define": [ "ASYNC" ], "warningsAsErrors": true }, @@ -21,6 +27,14 @@ "System.Data": "4.0.0.0" } }, + //"net35": { + // "compilationOptions": { "warningsAsErrors": true, "languageVersion": "csharp3", "define": ["CSHARP30"] }, + // "dependencies": { + // }, + // "frameworkAssemblies": { + // "System.Data": "4.0.0.0" + // } + //}, "dnx451": { "compilationOptions": { "define": [ "ASYNC" ], "warningsAsErrors": true }, "dependencies": { diff --git a/DapperTests NET35/DapperTests NET35.csproj b/DapperTests NET35/DapperTests NET35.csproj index ad6a3d6..b745959 100644 --- a/DapperTests NET35/DapperTests NET35.csproj +++ b/DapperTests NET35/DapperTests NET35.csproj @@ -21,7 +21,7 @@ full false bin\Debug\ - TRACE;DEBUG;NET35 + TRACE;DEBUG;NET35 EXTERNALS prompt 4 @@ -30,9 +30,10 @@ pdbonly true bin\Release\ - TRACE;NET35 + TRACE;NET35 EXTERNALS prompt 4 + true diff --git a/DapperTests NET45/DapperTests NET45.csproj b/DapperTests NET45/DapperTests NET45.csproj index 70c1d87..60e8940 100644 --- a/DapperTests NET45/DapperTests NET45.csproj +++ b/DapperTests NET45/DapperTests NET45.csproj @@ -19,7 +19,7 @@ full false bin\Debug\ - DEBUG;TRACE + TRACE;DEBUG;EXTERNALS prompt 4 @@ -28,7 +28,7 @@ pdbonly true bin\Release\ - TRACE + TRACE;EXTERNALS prompt 4 diff --git a/Tests/Assert.cs b/Tests/Assert.cs index f9f19a9..96493f2 100644 --- a/Tests/Assert.cs +++ b/Tests/Assert.cs @@ -3,6 +3,10 @@ using System.Linq; using System.Text; +#if DNXCORE50 +using ApplicationException = global::System.InvalidOperationException; +#endif + namespace SqlMapper { static class Assert diff --git a/Tests/DapperTests NET40.csproj b/Tests/DapperTests NET40.csproj index 57d06d9..21b6540 100644 --- a/Tests/DapperTests NET40.csproj +++ b/Tests/DapperTests NET40.csproj @@ -22,7 +22,7 @@ full false bin\Debug\ - DEBUG;TRACE + TRACE;DEBUG;EXTERNALS PERF prompt 4 false @@ -32,10 +32,11 @@ pdbonly true bin\Release\ - TRACE + TRACE;EXTERNALS PERF prompt 4 false + false diff --git a/Tests/Program.cs b/Tests/Program.cs index 49193bf..b932a58 100644 --- a/Tests/Program.cs +++ b/Tests/Program.cs @@ -6,11 +6,15 @@ namespace SqlMapper { +#if EXTERNALS [ServiceStack.DataAnnotations.Alias("Posts")] [Soma.Core.Table(Name = "Posts")] +#endif public class Post { +#if EXTERNALS [Soma.Core.Id(Soma.Core.IdKind.Identity)] +#endif public int Id { get; set; } public string Text { get; set; } public DateTime CreationDate { get; set; } @@ -42,10 +46,14 @@ public static SqlConnection GetOpenConnection() static void RunPerformanceTests() { +#if PERF var test = new PerformanceTests(); const int iterations = 500; Console.WriteLine("Running {0} iterations that load up a post entity", iterations); test.Run(iterations); +#else + Console.WriteLine("Performance tests have not been built; add the PERF symbol"); +#endif } static void Main() @@ -53,13 +61,18 @@ static void Main() #if DEBUG RunTests(); -#else +#else EnsureDBSetup(); RunPerformanceTests(); #endif - Console.WriteLine("(end of tests; press any key)"); +#if DNXCORE50 + Console.WriteLine("(end of tests; press return)"); + Console.ReadLine(); +#else + Console.WriteLine("(end of tests; press any key)"); Console.ReadKey(); +#endif } private static void EnsureDBSetup() @@ -110,27 +123,46 @@ insert Posts ([Text],CreationDate, LastChangeDate) values (replicate('x', 2000), cmd.ExecuteNonQuery(); } } + private static bool HasAttribute(MemberInfo member) where T : Attribute + { +#if DNXCORE50 + return member.CustomAttributes.Any(x => x.AttributeType == typeof(T)); +#else + return Attribute.IsDefined(member, typeof(T), true); +#endif + } private static void RunTests() { var tester = new Tests(); - int fail = 0; + int fail = 0, skip = 0, pass = 0; MethodInfo[] methods = typeof(Tests).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); - var activeTests = methods.Where(m => Attribute.IsDefined(m, typeof(ActiveTestAttribute))).ToArray(); + var activeTests = methods.Where(m => HasAttribute(m)).ToArray(); if (activeTests.Length != 0) methods = activeTests; List failNames = new List(); foreach (var method in methods) { + if (HasAttribute(method)) + { + Console.Write("Skipping " + method.Name); + skip++; + continue; + } Console.Write("Running " + method.Name); try { method.Invoke(tester, null); Console.WriteLine(" - OK!"); + pass++; } catch(TargetInvocationException tie) { fail++; Console.WriteLine(" - " + tie.InnerException.Message); failNames.Add(method.Name); + if(tie.InnerException is TypeInitializationException) + { + Console.WriteLine("> " + tie.InnerException.InnerException.Message); + } }catch (Exception ex) { @@ -139,13 +171,15 @@ private static void RunTests() } } Console.WriteLine(); + + Console.WriteLine("Passed: {0}, Failed: {1}, Skipped: {2}", pass, fail, skip); if(fail == 0) { Console.WriteLine("(all tests successful)"); } else { - Console.WriteLine("#### FAILED: {0}", fail); + Console.WriteLine("Failures:"); foreach(var failName in failNames) { Console.WriteLine(failName); @@ -156,5 +190,7 @@ private static void RunTests() [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public sealed class ActiveTestAttribute : Attribute {} + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public sealed class SkipTestAttribute : Attribute { } } diff --git a/Tests/Tests.cs b/Tests/Tests.cs index f615ce0..d1ac091 100644 --- a/Tests/Tests.cs +++ b/Tests/Tests.cs @@ -1,10 +1,18 @@ //#define POSTGRESQL // uncomment to run postgres tests + +#if DNXCORE50 +using IDbCommand = global::System.Data.Common.DbCommand; +using IDbDataParameter = global::System.Data.Common.DbParameter; +using IDbConnection = global::System.Data.Common.DbConnection; +using IDbTransaction = global::System.Data.Common.DbTransaction; +using IDataReader = global::System.Data.Common.DbDataReader; +#endif + using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using Dapper; -using System.Data.SqlServerCe; using System.IO; using System.Data; using System.Collections; @@ -15,14 +23,46 @@ using System.Data.Common; using System.Globalization; using System.Threading; -using System.Data.Entity.Spatial; -using Microsoft.SqlServer.Types; using System.Data.SqlTypes; -using FirebirdSql.Data.FirebirdClient; using System.Diagnostics; + +#if EXTERNALS +using FirebirdSql.Data.FirebirdClient; +using System.Data.Entity.Spatial; +using Microsoft.SqlServer.Types; +using System.Data.SqlServerCe; #if POSTGRESQL using Npgsql; #endif +#endif + +#if DNXCORE50 +namespace System.ComponentModel { + public sealed class DescriptionAttribute : Attribute { + public DescriptionAttribute(string description) + { + Description = description; + } + public string Description {get;private set;} + } +} +namespace System +{ + public enum GenericUriParserOptions + { + Default + } + public class GenericUriParser + { + private GenericUriParserOptions options; + + public GenericUriParser(GenericUriParserOptions options) + { + this.options = options; + } + } +} +#endif namespace SqlMapper { @@ -222,7 +262,7 @@ public void TestNoDefaultConstructorWithEnum() nodef.NE1.IsEqualTo(ShortEnum.Five); nodef.NE2.IsEqualTo(null); } - +#if EXTERNALS class NoDefaultConstructorWithBinary { public System.Data.Linq.Binary Value { get; set; } @@ -232,7 +272,6 @@ public NoDefaultConstructorWithBinary(System.Data.Linq.Binary val) Value = val; } } - public void TestNoDefaultConstructorBinary() { byte[] orig = new byte[20]; @@ -241,7 +280,7 @@ public void TestNoDefaultConstructorBinary() var output = connection.Query("select @input as val", new { input }).First().Value; output.ToArray().IsSequenceEqualTo(orig); } - +#endif // http://stackoverflow.com/q/8593871 public void TestAbstractInheritance() { @@ -515,7 +554,7 @@ public void TestExtraFields() dog.First().Id .IsEqualTo(guid); } - +#if EXTERNALS // see http://stackoverflow.com/q/18847510/23354 public void TestOleDbParameters() { @@ -537,7 +576,7 @@ System.Data.OleDb.OleDbConnection ConnectViaOledb() conn.Open(); return conn; } - +#endif public void TestStrongType() { var guid = Guid.NewGuid(); @@ -1109,7 +1148,7 @@ public void TestFieldsAndPrivates() } - +#if EXTERNALS public void ExecuteReader() { var dt = new DataTable(); @@ -1121,7 +1160,7 @@ public void ExecuteReader() ((int)dt.Rows[0][0]).IsEqualTo(3); ((int)dt.Rows[0][1]).IsEqualTo(4); } - +#endif private class TestFieldCaseAndPrivatesEntity { public int a { get; set; } @@ -1235,7 +1274,7 @@ public class AuthorCE public int ID { get; set; } public string Name { get; set; } } - +#if EXTERNALS public void MultiRSSqlCE() { if (File.Exists("Test.sdf")) @@ -1264,7 +1303,7 @@ public void MultiRSSqlCE() cnn.Close(); } } - +#endif enum TestEnum : byte { Bla = 1 @@ -1486,14 +1525,14 @@ public void TestDbString() { var obj = connection.Query("select datalength(@a) as a, datalength(@b) as b, datalength(@c) as c, datalength(@d) as d, datalength(@e) as e, datalength(@f) as f", new - { - a = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true }, - b = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = false }, - c = new DbString { Value = "abcde", IsFixedLength = false, Length = 10, IsAnsi = true }, - d = new DbString { Value = "abcde", IsFixedLength = false, Length = 10, IsAnsi = false }, - e = new DbString { Value = "abcde", IsAnsi = true }, - f = new DbString { Value = "abcde", IsAnsi = false }, - }).First(); + { + a = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true }, + b = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = false }, + c = new DbString { Value = "abcde", IsFixedLength = false, Length = 10, IsAnsi = true }, + d = new DbString { Value = "abcde", IsFixedLength = false, Length = 10, IsAnsi = false }, + e = new DbString { Value = "abcde", IsAnsi = true }, + f = new DbString { Value = "abcde", IsAnsi = false }, + }).First(); ((int)obj.a).IsEqualTo(10); ((int)obj.b).IsEqualTo(20); ((int)obj.c).IsEqualTo(5); @@ -1656,7 +1695,7 @@ public void AddParameters(IDbCommand command, Dapper.SqlMapper.Identity identity } } - +#if EXTERNALS // SQL Server specific test to demonstrate TVP public void TestTVP() { @@ -1755,7 +1794,7 @@ public void TestTVPWithAdditionalParams() } } } - +#endif class IntCustomParam : Dapper.SqlMapper.ICustomQueryParameter { IEnumerable numbers; @@ -1789,7 +1828,7 @@ public void AddParameter(IDbCommand command, string name) p.Value = number_list; } } - +#if EXTERNALS public void TestTVPWithAnonymousObject() { try @@ -1816,6 +1855,7 @@ public void TestTVPWithAnonymousObject() } } } +#endif class Parent { @@ -1864,6 +1904,7 @@ class WithBizarreData public GenericUriParser Foo { get; set; } public int Bar { get; set; } } + public void TestUnexpectedDataMessage() { string msg = null; @@ -1878,6 +1919,7 @@ public void TestUnexpectedDataMessage() } msg.IsEqualTo("The member Foo of type System.GenericUriParser cannot be used as a parameter value"); } + public void TestUnexpectedButFilteredDataMessage() { int i = connection.Query("select @Bar", new WithBizarreData { Foo = new GenericUriParser(GenericUriParserOptions.Default), Bar = 23 }).Single(); @@ -2094,6 +2136,8 @@ public void TestMultiMapperIsNotConfusedWithUnorderedCols() result.Item2.BarId.IsEqualTo(3); result.Item2.Name.IsEqualTo("a"); } + +#if EXTERNALS public void TestLinqBinaryToClass() { byte[] orig = new byte[20]; @@ -2120,7 +2164,7 @@ class WithBinary { public System.Data.Linq.Binary Value { get; set; } } - +#endif class WithPrivateConstructor { @@ -2489,7 +2533,7 @@ public void TestCustomTypeMap() // custom mapping var map = new CustomPropertyTypeMap(typeof(TypeWithMapping), - (type, columnName) => type.GetProperties().Where(prop => prop.GetCustomAttributes(false).OfType().Any(attr => attr.Description == columnName)).FirstOrDefault()); + (type, columnName) => type.GetProperties().Where(prop => GetDescriptionFromAttribute(prop) == columnName).FirstOrDefault()); Dapper.SqlMapper.SetTypeMap(typeof(TypeWithMapping), map); item = connection.Query("Select 'AVal' as A, 'BVal' as B").Single(); @@ -2502,7 +2546,17 @@ public void TestCustomTypeMap() item.A.IsEqualTo("AVal"); item.B.IsEqualTo("BVal"); } - + static string GetDescriptionFromAttribute(MemberInfo member) + { + if (member == null) return null; +#if DNXCORE50 + var data = member.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(DescriptionAttribute)); + return data == null ? null : (string)data.ConstructorArguments.Single().Value; +#else + var attrib = (DescriptionAttribute)Attribute.GetCustomAttribute(member, typeof(DescriptionAttribute), false); + return attrib == null ? null : attrib.Description; +#endif + } public class TypeWithMapping { [Description("B")] @@ -2872,7 +2926,70 @@ public void TestChangingDefaultStringTypeMappingToAnsiString() Dapper.SqlMapper.PurgeQueryCache(); Dapper.SqlMapper.AddTypeMap(typeof(string), DbType.String); // Restore Default to Unicode String } +#if DNXCORE50 + class TransactedConnection : IDbConnection + { + IDbConnection _conn; + IDbTransaction _tran; + + public TransactedConnection(IDbConnection conn, IDbTransaction tran) + { + _conn = conn; + _tran = tran; + } + + public override string ConnectionString { get { return _conn.ConnectionString; } set { _conn.ConnectionString = value; } } + public override int ConnectionTimeout { get { return _conn.ConnectionTimeout; } } + public override string Database { get { return _conn.Database; } } + public override ConnectionState State { get { return _conn.State; } } + + protected override IDbTransaction BeginDbTransaction(IsolationLevel isolationLevel) + { + return _tran; + } + + public override void ChangeDatabase(string databaseName) + { + _conn.ChangeDatabase(databaseName); + } + public override string DataSource + { + get + { + return _conn.DataSource; + } + } + public override string ServerVersion + { + get + { + return _conn.ServerVersion; + } + } + public override void Close() + { + _conn.Close(); + } + protected override IDbCommand CreateDbCommand() + { + // The command inherits the "current" transaction. + var command = _conn.CreateCommand(); + command.Transaction = _tran; + return command; + } + protected override void Dispose(bool disposing) + { + if(disposing) _conn.Dispose(); + base.Dispose(disposing); + } + + public override void Open() + { + _conn.Open(); + } + } +#else class TransactedConnection : IDbConnection { IDbConnection _conn; @@ -2927,7 +3044,7 @@ public void Open() _conn.Open(); } } - +#endif public void TestDapperTableMetadataRetrieval() { // Test for a bug found in CS 51509960 where the following sequence would result in an InvalidOperationException being @@ -3004,20 +3121,29 @@ public void TestDoubleDecimalConversions_SO18228523_Nulls() row.C.Equals(0.0M); row.D.IsNull(); } - + private static CultureInfo ActiveCulture + { +#if DNXCORE50 + get { return CultureInfo.CurrentCulture; } + set { CultureInfo.CurrentCulture = value; } +#else + get { return Thread.CurrentThread.CurrentCulture; } + set { Thread.CurrentThread.CurrentCulture = value; } +#endif + } public void TestParameterInclusionNotSensitiveToCurrentCulture() { // note this might fail if your database server is case-sensitive - CultureInfo current = Thread.CurrentThread.CurrentCulture; + CultureInfo current = ActiveCulture; try { - Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR"); + ActiveCulture = new CultureInfo("tr-TR"); connection.Query("select @pid", new { PId = 1 }).Single(); } finally { - Thread.CurrentThread.CurrentCulture = current; + ActiveCulture = current; } } public void LiteralReplacement() @@ -3173,7 +3299,7 @@ class HasDoubleDecimal public decimal C { get; set; } public decimal? D { get; set; } } - +#if EXTERNALS public void DataTableParameters() { try { connection.Execute("drop proc #DataTableParameters"); } @@ -3277,7 +3403,7 @@ public void SupportInit() obj.Value.Equals("abc"); obj.Flags.Equals(31); } - +#endif public void GuidIn_SO_24177902() { // invent and populate @@ -3304,7 +3430,7 @@ public void GuidIn_SO_24177902() rows[1].i.Equals(3); rows[1].g.Equals(c); } - +#if EXTERNALS class HazGeo { public int Id { get; set; } @@ -3372,7 +3498,7 @@ public class HazSqlHierarchy public int Id { get; set; } public SqlHierarchyId Path { get; set; } } - +#endif public void TypeBasedViaDynamic() { Type type = GetSomeType(); @@ -3432,7 +3558,7 @@ public class SomeType public int A { get; set; } public string B { get; set; } } - +#if !DNXCORE50 class WithInit : ISupportInitialize { public string Value { get; set; } @@ -3448,7 +3574,7 @@ void ISupportInitialize.EndInit() Flags += 30; } } - +#endif public void SO24607639_NullableBools() { var obj = connection.Query( @@ -3482,7 +3608,9 @@ public void SO24605346_ProcsAndStrings() class PracticeRebateOrders { public string fTaxInvoiceNumber; +#if EXTERNALS [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] +#endif public string TaxInvoiceNumber { get { return fTaxInvoiceNumber; } set { fTaxInvoiceNumber = value; } } } @@ -3498,7 +3626,7 @@ public override RatingValue Parse(object value) throw new FormatException("Invalid conversion to RatingValue"); } - public override void SetValue(System.Data.IDbDataParameter parameter, RatingValue value) + public override void SetValue(IDbDataParameter parameter, RatingValue value) { // ... null, range checks etc ... parameter.DbType = System.Data.DbType.Int32; @@ -3566,10 +3694,12 @@ public void Issue22_ExecuteScalar() int? k = connection.ExecuteScalar("select @i", new { i = default(int?) }); k.IsNull(); +#if EXTERNALS Dapper.EntityFramework.Handlers.Register(); var geo = DbGeography.LineFromText("LINESTRING(-122.360 47.656, -122.343 47.656 )", 4326); var geo2 = connection.ExecuteScalar("select @geo", new { geo }); geo2.IsNotNull(); +#endif } public void Issue142_FailsNamedStatus() @@ -3955,7 +4085,7 @@ public void Issue178_SqlServer() Assert.IsFalse(reader2.NextResult()); } } - +#if EXTERNALS public void Issue178_Firebird() // we expect this to fail due to a bug in Firebird; a PR to fix it has been submitted { var cs = @"initial catalog=localhost:database;user id=SYSDBA;password=masterkey"; @@ -4000,6 +4130,7 @@ public void PseudoPositionalParameters_Simple() value.IsEqualTo(9); } } + public void PseudoPositionalParameters_Dynamic() { using (var connection = ConnectViaOledb()) @@ -4013,6 +4144,7 @@ public void PseudoPositionalParameters_Dynamic() value.IsEqualTo(9); } } + public void PseudoPositionalParameters_ReusedParameter() { using (var connection = ConnectViaOledb()) @@ -4058,7 +4190,7 @@ public void PseudoPositionalParameters_ExecMulti() sum.IsEqualTo(10); } } - +#endif public void QueryBasicWithoutQuery() { int? i = connection.Query("print 'not a query'").FirstOrDefault(); @@ -4210,7 +4342,7 @@ public void BasicDecimals() var c = connection.Query("select @c", new { c = 11.884M }).Single(); c.IsEqualTo(11.884M); } - + [SkipTest] public void Issue263_Timeout() { var watch = Stopwatch.StartNew(); @@ -4220,7 +4352,7 @@ public void Issue263_Timeout() var minutes = watch.ElapsedMilliseconds / 1000 / 60; Assert.IsTrue(minutes >= 0.95 && minutes <= 1.05); } - +#if EXTERNALS public void SO29596645_TvpProperty() { try { connection.Execute("CREATE TYPE SO29596645_ReminderRuleType AS TABLE (id int NOT NULL)"); } @@ -4234,7 +4366,7 @@ public void SO29596645_TvpProperty() val.IsEqualTo(20); } - +#endif public void Issue268_ReturnQueryMultiple() { connection.Execute(@"create proc #TestProc268 (@a int, @b int, @c int)as @@ -4257,7 +4389,7 @@ select @b var retVal = p.Get("RetVal"); retVal.IsEqualTo(3); } - +#if EXTERNALS class SO29596645_RuleTableValuedParameters : Dapper.SqlMapper.IDynamicParameters { private string parameterName; @@ -4289,7 +4421,7 @@ public SO29596645_OrganisationDTO() Rules = new SO29596645_RuleTableValuedParameters("@Rules"); } } - +#endif #if POSTGRESQL class Cat -- GitLab