Commit a67ed727 authored by johandanforth's avatar johandanforth

Cleaned up shared files in Contrib and tests. Also added tests for transactions in Contrib

parent c8bd264d
...@@ -44,7 +44,10 @@ ...@@ -44,7 +44,10 @@
<Compile Include="..\Dapper.Contrib\Properties\AssemblyInfo.cs"> <Compile Include="..\Dapper.Contrib\Properties\AssemblyInfo.cs">
<Link>AssemblyInfo.cs</Link> <Link>AssemblyInfo.cs</Link>
</Compile> </Compile>
<Compile Include="SqlMapperExtensions.cs" /> <Compile Include="..\Dapper.Contrib\SqlMapperExtensions.cs">
<Link>SqlMapperExtensions.cs</Link>
</Compile>
<Compile Include="SqlMapperExtensionsAsync.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Properties\" /> <Folder Include="Properties\" />
......
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Dapper.Contrib.Tests_NET45
{
/// <summary>
/// Assert extensions borrowed from Sam's code in DapperTests
/// </summary>
static class Assert
{
public static void IsEqualTo<T>(this T obj, T other)
{
if (!obj.Equals(other))
{
throw new ApplicationException(string.Format("{0} should be equals to {1}", obj, other));
}
}
public static void IsSequenceEqualTo<T>(this IEnumerable<T> obj, IEnumerable<T> other)
{
if (!obj.SequenceEqual(other))
{
throw new ApplicationException(string.Format("{0} should be equals to {1}", obj, other));
}
}
public static void IsFalse(this bool b)
{
if (b)
{
throw new ApplicationException("Expected false");
}
}
public static void IsTrue(this bool b)
{
if (!b)
{
throw new ApplicationException("Expected true");
}
}
public static void IsNull(this object obj)
{
if (obj != null)
{
throw new ApplicationException("Expected null");
}
}
}
}
\ No newline at end of file
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
<ProjectGuid>{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}</ProjectGuid> <ProjectGuid>{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}</ProjectGuid>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Dapper.Contrib.Tests_NET45</RootNamespace> <RootNamespace>Dapper.Contrib.Tests</RootNamespace>
<AssemblyName>Dapper.Contrib.Tests NET45</AssemblyName> <AssemblyName>Dapper.Contrib.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
</PropertyGroup> </PropertyGroup>
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL" /> <Reference Include="System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL" />
<Reference Include="System.Transactions" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
...@@ -42,10 +43,14 @@ ...@@ -42,10 +43,14 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Assert.cs" /> <Compile Include="..\Dapper.Contrib.Tests\Assert.cs">
<Link>Assert.cs</Link>
</Compile>
<Compile Include="..\Dapper.Contrib.Tests\Tests.cs">
<Link>Tests.cs</Link>
</Compile>
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tests.cs" />
<Compile Include="TestsAsync.cs" /> <Compile Include="TestsAsync.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
using System; using System;
using System.Collections.Generic;
using System.Data.SqlServerCe; using System.Data.SqlServerCe;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Dapper.Contrib.Tests_NET45 namespace Dapper.Contrib.Tests
{ {
class Program class Program
{ {
...@@ -47,50 +43,21 @@ private static void RunTests() ...@@ -47,50 +43,21 @@ private static void RunTests()
var tester = new Tests(); var tester = new Tests();
foreach (var method in typeof(Tests).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) foreach (var method in typeof(Tests).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{ {
if (method.ReturnType != typeof(Task)) Console.Write("Running " + method.Name);
{ method.Invoke(tester, null);
Console.Write("Running " + method.Name); Console.WriteLine(" - OK!");
method.Invoke(tester, null);
Console.WriteLine(" - OK!");
}
} }
} }
private static void RunAsyncTests() private static void RunAsyncTests()
{ {
var tester = new TestsAsync(); var tester = new TestsAsync();
foreach (var method in typeof(TestsAsync).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
Console.Write("Running TableNameAsync"); {
Task.WaitAll(tester.TableNameAsync()); Console.Write("Running " + method.Name);
Console.WriteLine(" - OK!"); Task.WaitAll((Task)method.Invoke(tester, null));
Console.WriteLine(" - OK!");
Console.Write("Running TestSimpleGetAsync"); }
Task.WaitAll(tester.TestSimpleGetAsync());
Console.WriteLine(" - OK!");
Console.Write("Running InsertGetUpdateAsync");
Task.WaitAll(tester.InsertGetUpdateAsync());
Console.WriteLine(" - OK!");
Console.Write("Running InsertCheckKeyAsync");
Task.WaitAll(tester.InsertCheckKeyAsync());
Console.WriteLine(" - OK!");
Console.Write("Running BuilderSelectClauseAsync");
Task.WaitAll(tester.BuilderSelectClauseAsync());
Console.WriteLine(" - OK!");
Console.Write("Running BuilderTemplateWOCompositionAsync");
Task.WaitAll(tester.BuilderTemplateWOCompositionAsync());
Console.WriteLine(" - OK!");
Console.Write("Running InsertFieldWithReservedNameAsync");
Task.WaitAll(tester.InsertFieldWithReservedNameAsync());
Console.WriteLine(" - OK!");
Console.Write("Running DeleteAllAsync");
Task.WaitAll(tester.DeleteAllAsync());
Console.WriteLine(" - OK!");
} }
} }
} }
using System.Data;
using System.Data.SqlServerCe;
using System.IO;
using System.Linq;
using System.Reflection;
using Dapper.Contrib.Extensions;
using System.Collections.Generic;
using System;
using Dapper;
namespace Dapper.Contrib.Tests_NET45
{
public interface IUser
{
[Key]
int Id { get; set; }
string Name { get; set; }
int Age { get; set; }
}
public class User : IUser
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
[Table("Automobiles")]
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
}
[Table("Results")]
public class Result
{
public int Id { get; set; }
public string Name { get; set; }
public int Order { get; set; }
}
public class Tests
{
private IDbConnection GetOpenConnection()
{
var projLoc = Assembly.GetAssembly(GetType()).Location;
var projFolder = Path.GetDirectoryName(projLoc);
var connection = new SqlCeConnection("Data Source = " + projFolder + "\\Test.sdf;");
connection.Open();
return connection;
}
public void TableName()
{
using (var connection = GetOpenConnection())
{
// tests against "Automobiles" table (Table attribute)
connection.Insert(new Car {Name = "Volvo"});
connection.Get<Car>(1).Name.IsEqualTo("Volvo");
connection.Update(new Car() {Id = 1, Name = "Saab"}).IsEqualTo(true);
connection.Get<Car>(1).Name.IsEqualTo("Saab");
connection.Delete(new Car() {Id = 1}).IsEqualTo(true);
connection.Get<Car>(1).IsNull();
}
}
public void TestSimpleGet()
{
using (var connection = GetOpenConnection())
{
var id = connection.Insert(new User { Name = "Adama", Age = 10 });
var user = connection.Get<User>(id);
user.Id.IsEqualTo((int)id);
user.Name.IsEqualTo("Adama");
connection.Delete(user);
}
}
public void InsertGetUpdate()
{
using (var connection = GetOpenConnection())
{
connection.Get<User>(3).IsNull();
var id = connection.Insert(new User {Name = "Adam", Age = 10});
//get a user with "isdirty" tracking
var user = connection.Get<IUser>(id);
user.Name.IsEqualTo("Adam");
connection.Update(user).IsEqualTo(false); //returns false if not updated, based on tracking
user.Name = "Bob";
connection.Update(user).IsEqualTo(true); //returns true if updated, based on tracking
user = connection.Get<IUser>(id);
user.Name.IsEqualTo("Bob");
//get a user with no tracking
var notrackedUser = connection.Get<User>(id);
notrackedUser.Name.IsEqualTo("Bob");
connection.Update(notrackedUser).IsEqualTo(true); //returns true, even though user was not changed
notrackedUser.Name = "Cecil";
connection.Update(notrackedUser).IsEqualTo(true);
connection.Get<User>(id).Name.IsEqualTo("Cecil");
connection.Query<User>("select * from Users").Count().IsEqualTo(1);
connection.Delete(user).IsEqualTo(true);
connection.Query<User>("select * from Users").Count().IsEqualTo(0);
connection.Update(notrackedUser).IsEqualTo(false); //returns false, user not found
}
}
public void InsertCheckKey()
{
using (var connection = GetOpenConnection())
{
connection.Get<IUser>(3).IsNull();
User user = new User { Name = "Adamb", Age = 10 };
int id = (int)connection.Insert(user);
user.Id.IsEqualTo(id);
}
}
public void BuilderSelectClause()
{
using (var connection = GetOpenConnection())
{
var rand = new Random(8675309);
var data = new List<User>();
for (int i = 0; i < 100; i++)
{
var nU = new User { Age = rand.Next(70), Id = i, Name = Guid.NewGuid().ToString() };
data.Add(nU);
nU.Id = (int)connection.Insert<User>(nU);
}
var builder = new SqlBuilder();
var justId = builder.AddTemplate("SELECT /**select**/ FROM Users");
var all = builder.AddTemplate("SELECT Name, /**select**/, Age FROM Users");
builder.Select("Id");
var ids = connection.Query<int>(justId.RawSql, justId.Parameters);
var users = connection.Query<User>(all.RawSql, all.Parameters);
foreach (var u in data)
{
if (!ids.Any(i => u.Id == i)) throw new Exception("Missing ids in select");
if (!users.Any(a => a.Id == u.Id && a.Name == u.Name && a.Age == u.Age)) throw new Exception("Missing users in select");
}
}
}
public void BuilderTemplateWOComposition()
{
var builder = new SqlBuilder();
var template = builder.AddTemplate("SELECT COUNT(*) FROM Users WHERE Age = @age", new {age = 5});
if (template.RawSql == null) throw new Exception("RawSql null");
if (template.Parameters == null) throw new Exception("Parameters null");
using (var connection = GetOpenConnection())
{
connection.Insert(new User { Age = 5, Name = "Testy McTestington" });
if (connection.Query<int>(template.RawSql, template.Parameters).Single() != 1)
throw new Exception("Query failed");
}
}
public void InsertFieldWithReservedName()
{
using (var conneciton = GetOpenConnection())
{
var id = conneciton.Insert(new Result() { Name = "Adam", Order = 1 });
var result = conneciton.Get<Result>(id);
result.Order.IsEqualTo(1);
}
}
public void DeleteAll()
{
using (var connection = GetOpenConnection())
{
var id1 = connection.Insert(new User() { Name = "Alice", Age = 32 });
var id2 = connection.Insert(new User() { Name = "Bob", Age = 33 });
connection.DeleteAll<User>();
connection.Get<User>(id1).IsNull();
connection.Get<User>(id2).IsNull();
}
}
}
}
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Dapper.Contrib.Tests_NET45 namespace Dapper.Contrib.Tests
{ {
public class TestsAsync public class TestsAsync
{ {
......
...@@ -44,10 +44,8 @@ ...@@ -44,10 +44,8 @@
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL"> <Reference Include="System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL" />
<SpecificVersion>False</SpecificVersion> <Reference Include="System.Transactions" />
<HintPath>Dependencies\System.Data.SqlServerCe.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
......
using System; using System;
using System.Collections.Generic;
using System.Data.SqlServerCe; using System.Data.SqlServerCe;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text;
namespace Dapper.Contrib.Tests namespace Dapper.Contrib.Tests
{ {
...@@ -15,6 +11,8 @@ static void Main(string[] args) ...@@ -15,6 +11,8 @@ static void Main(string[] args)
{ {
Setup(); Setup();
RunTests(); RunTests();
Console.WriteLine("Press any key...");
Console.ReadKey();
} }
private static void Setup() private static void Setup()
...@@ -46,9 +44,6 @@ private static void RunTests() ...@@ -46,9 +44,6 @@ private static void RunTests()
method.Invoke(tester, null); method.Invoke(tester, null);
Console.WriteLine(" - OK!"); Console.WriteLine(" - OK!");
} }
Console.WriteLine("(end of tests; press any key)");
Console.ReadKey();
} }
} }
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Transactions;
using Dapper.Contrib.Extensions; using Dapper.Contrib.Extensions;
using System.Collections.Generic; using System.Collections.Generic;
using System; using System;
using Dapper;
namespace Dapper.Contrib.Tests namespace Dapper.Contrib.Tests
...@@ -31,6 +31,8 @@ public class Car ...@@ -31,6 +31,8 @@ public class Car
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
[Computed]
public string Computed { get; set; }
} }
[Table("Results")] [Table("Results")]
...@@ -53,16 +55,25 @@ private IDbConnection GetOpenConnection() ...@@ -53,16 +55,25 @@ private IDbConnection GetOpenConnection()
return connection; return connection;
} }
private IDbConnection GetConnection()
{
var projLoc = Assembly.GetAssembly(GetType()).Location;
var projFolder = Path.GetDirectoryName(projLoc);
var connection = new SqlCeConnection("Data Source = " + projFolder + "\\Test.sdf;");
return connection;
}
public void TableName() public void TableName()
{ {
using (var connection = GetOpenConnection()) using (var connection = GetOpenConnection())
{ {
// tests against "Automobiles" table (Table attribute) // tests against "Automobiles" table (Table attribute)
connection.Insert(new Car {Name = "Volvo"}); connection.Insert(new Car { Name = "Volvo" }).IsEqualTo(1);
connection.Get<Car>(1).Name.IsEqualTo("Volvo"); connection.Get<Car>(1).Name.IsEqualTo("Volvo");
connection.Update(new Car() {Id = 1, Name = "Saab"}).IsEqualTo(true); connection.Update(new Car() { Id = 1, Name = "Saab" }).IsEqualTo(true);
connection.Get<Car>(1).Name.IsEqualTo("Saab"); connection.Get<Car>(1).Name.IsEqualTo("Saab");
connection.Delete(new Car() {Id = 1}).IsEqualTo(true); connection.Delete(new Car() { Id = 1 }).IsEqualTo(true);
connection.Get<Car>(1).IsNull(); connection.Get<Car>(1).IsNull();
} }
} }
...@@ -85,7 +96,10 @@ public void InsertGetUpdate() ...@@ -85,7 +96,10 @@ public void InsertGetUpdate()
{ {
connection.Get<User>(3).IsNull(); connection.Get<User>(3).IsNull();
var id = connection.Insert(new User {Name = "Adam", Age = 10}); //insert with computed attribute that should be ignored
connection.Insert(new Car { Name = "Volvo", Computed = "this property should be ignored" });
var id = connection.Insert(new User { Name = "Adam", Age = 10 });
//get a user with "isdirty" tracking //get a user with "isdirty" tracking
var user = connection.Get<IUser>(id); var user = connection.Get<IUser>(id);
...@@ -112,6 +126,41 @@ public void InsertGetUpdate() ...@@ -112,6 +126,41 @@ public void InsertGetUpdate()
} }
} }
public void Transactions()
{
using (var connection = GetOpenConnection())
{
var id = connection.Insert(new Car { Name = "one car" }); //insert outside transaction
var tran = connection.BeginTransaction();
var car = connection.Get<Car>(id, tran);
var orgName = car.Name;
car.Name = "Another car";
connection.Update(car, tran);
tran.Rollback();
car = connection.Get<Car>(id); //updates should have been rolled back
car.Name.IsEqualTo(orgName);
}
}
public void TransactionScope()
{
using (var connection = GetConnection())
{
using (var txscope = new TransactionScope())
{
connection.Open(); //connection MUST be opened inside the transactionscope
var id = connection.Insert(new Car { Name = "one car" }); //inser car within transaction
txscope.Dispose(); //rollback
connection.Get<Car>(id).IsNull(); //returns null - car with that id should not exist
}
}
}
public void InsertCheckKey() public void InsertCheckKey()
{ {
using (var connection = GetOpenConnection()) using (var connection = GetOpenConnection())
...@@ -156,7 +205,7 @@ public void BuilderSelectClause() ...@@ -156,7 +205,7 @@ public void BuilderSelectClause()
public void BuilderTemplateWOComposition() public void BuilderTemplateWOComposition()
{ {
var builder = new SqlBuilder(); var builder = new SqlBuilder();
var template = builder.AddTemplate("SELECT COUNT(*) FROM Users WHERE Age = @age", new {age = 5}); var template = builder.AddTemplate("SELECT COUNT(*) FROM Users WHERE Age = @age", new { age = 5 });
if (template.RawSql == null) throw new Exception("RawSql null"); if (template.RawSql == null) throw new Exception("RawSql null");
if (template.Parameters == null) throw new Exception("Parameters null"); if (template.Parameters == null) throw new Exception("Parameters null");
...@@ -188,11 +237,11 @@ public void DeleteAll() ...@@ -188,11 +237,11 @@ public void DeleteAll()
{ {
var id1 = connection.Insert(new User() { Name = "Alice", Age = 32 }); var id1 = connection.Insert(new User() { Name = "Alice", Age = 32 });
var id2 = connection.Insert(new User() { Name = "Bob", Age = 33 }); var id2 = connection.Insert(new User() { Name = "Bob", Age = 33 });
connection.DeleteAll<User>(); connection.DeleteAll<User>().IsTrue();
connection.Get<User>(id1).IsNull(); connection.Get<User>(id1).IsNull();
connection.Get<User>(id2).IsNull(); connection.Get<User>(id2).IsNull();
} }
} }
} }
} }
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment