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

Added support for multiple schemas within a database, and support for get/delete for tables without an int primary key #9

Merged
merged 8 commits into from
Dec 19, 2013
8 changes: 6 additions & 2 deletions Dapper.SimpleCRUD.Tests/Dapper.SimpleCRUD.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Dapper.SimpleCRUD.Tests</RootNamespace>
<AssemblyName>Dapper.SimpleCRUD.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
<SccProjectName>
</SccProjectName>
Expand Down Expand Up @@ -48,6 +49,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
Expand All @@ -57,6 +59,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Dapper">
Expand Down Expand Up @@ -120,6 +123,7 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
35 changes: 29 additions & 6 deletions Dapper.SimpleCRUD.Tests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.IO;
using System.Reflection;
Expand All @@ -18,16 +19,26 @@ private static void Setup()
var projLoc = Assembly.GetAssembly(typeof(Program)).Location;
var projFolder = Path.GetDirectoryName(projLoc);

if (File.Exists(projFolder + "\\Test.sdf"))
File.Delete(projFolder + "\\Test.sdf");
var connectionString = "Data Source = " + projFolder + "\\Test.sdf;";
var engine = new SqlCeEngine(connectionString);
engine.CreateDatabase();
using (var connection = new SqlCeConnection(connectionString))
using (var connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;Initial Catalog=Master;Integrated Security=True"))
{
connection.Open();
try
{
connection.Execute(@" DROP DATABASE DapperSimpleCrudTestDb; ");
}
catch {}

connection.Execute(@" CREATE DATABASE DapperSimpleCrudTestDb; ");
}

using (var connection = new SqlConnection(@"Data Source = (LocalDB)\v11.0;Initial Catalog=DapperSimpleCrudTestDb;Integrated Security=True"))
{
connection.Open();
connection.Execute(@" create table Users (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, Age int not null, ScheduledDayOff int null) ");
connection.Execute(@" create table Car (CarId int IDENTITY(1,1) not null, Make nvarchar(100) not null, Model nvarchar(100) not null) ");
connection.Execute(@" create table City (Name nvarchar(100) not null, Population int not null) ");
connection.Execute(@" CREATE SCHEMA Log; ");
connection.Execute(@" create table Log.CarLog (Id int IDENTITY(1,1) not null, LogNotes nvarchar(100) NOT NULL) ");
}
Console.WriteLine("Created database");
}
Expand All @@ -41,6 +52,18 @@ private static void RunTests()
method.Invoke(tester, null);
Console.WriteLine(" - OK!");
}

using (var connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;Initial Catalog=Master;Integrated Security=True"))
{
connection.Open();
try
{
//drop any remaining connections, then drop the db.
connection.Execute(@" alter database DapperSimpleCrudTestDb set single_user with rollback immediate; DROP DATABASE DapperSimpleCrudTestDb; ");
}
catch {}
}

Console.ReadKey();
}

Expand Down
84 changes: 83 additions & 1 deletion Dapper.SimpleCRUD.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -50,14 +51,38 @@ public class Car

}

[System.ComponentModel.DataAnnotations.Schema.Table("CarLog", Schema = "Log")]
public class CarLog
{
public int Id { get; set; }
public string LogNotes { get; set; }
}

/// <summary>
/// This class should be used for failing tests, since no schema is specified and 'CarLog' is not on dbo
/// </summary>
[System.Data.Linq.Mapping.Table(Name = "CarLog")]
public class SchemalessCarLog
{
public int Id { get; set; }
public string LogNotes { get; set; }
}

public class City
{
[Key]
public string Name { get; set; }
public int Population { 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;");
var connection = new SqlConnection(@"Data Source = (LocalDB)\v11.0;Initial Catalog=DapperSimpleCrudTestDb;Integrated Security=True");
connection.Open();
return connection;
}
Expand Down Expand Up @@ -223,5 +248,62 @@ public void TestNullableSimpleTypesAreSaved()
}
}

public void TestInsertIntoDifferentSchema()
{
using (var connection = GetOpenConnection())
{
var id = connection.Insert(new CarLog { LogNotes = "blah blah blah"});
id.IsEqualTo(1);
}
}

public void TestGetFromDifferentSchema()
{
using (var connection = GetOpenConnection())
{
var carlog = connection.Get<CarLog>(1);
carlog.LogNotes.IsEqualTo("blah blah blah");
}
}

public void TestTryingToGetFromTableInSchemaWithoutDataAnnotationShouldFail()
{
using (var connection = GetOpenConnection())
{
try
{
connection.Get<SchemalessCarLog>(1);
}
catch (Exception)
{
//we expect to get an exception, so return
return;
}

//if we get here without throwing an exception, the test failed.
throw new ApplicationException("Expected exception");
}
}

public void TestGetFromTableWithNonIntPrimaryKey()
{
using (var connection = GetOpenConnection())
{
//note - there's not yet support for inserts without a non-int id, so drop down to a normal execute
connection.Execute("INSERT INTO CITY (NAME, POPULATION) VALUES ('Morgantown', 31000)");
var city = connection.Get<City>("Morgantown");
city.Population.IsEqualTo(31000);
}
}

public void TestDeleteFromTableWithNonIntPrimaryKey()
{
using (var connection = GetOpenConnection())
{
//note - there's not yet support for inserts without a non-int id, so drop down to a normal execute
connection.Execute("INSERT INTO CITY (NAME, POPULATION) VALUES ('Fairmont', 18737)");
connection.Delete<City>("Fairmont").IsEqualTo(1);
}
}
}
}
3 changes: 3 additions & 0 deletions Dapper.SimpleCRUD.Tests/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>