Skip to content

Commit

Permalink
Added unit tests for UnitOfWork & UnitOfWorkFactory.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wahid Shalaly committed Mar 12, 2014
1 parent 36b6e2c commit db17d20
Show file tree
Hide file tree
Showing 23 changed files with 363 additions and 67 deletions.
4 changes: 4 additions & 0 deletions src/.nuget/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="xunit.runners" version="1.9.2" />
</packages>
6 changes: 6 additions & 0 deletions src/ReliableUnitOfWork.SqlAzure.UnitTests.xunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<xunit>
<assemblies>
<assembly filename="ReliableUnitOfWork.SqlAzure.UnitTests\bin\Debug\ReliableUnitOfWork.SqlAzure.UnitTests.dll" shadow-copy="true" />
</assemblies>
</xunit>
13 changes: 13 additions & 0 deletions src/ReliableUnitOfWork.SqlAzure.UnitTests/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ReliableUnitOfWork.SqlAzure.UnitTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ReliableUnitOfWork.SqlAzure.UnitTests")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7d74f221-8745-4313-85e4-0310c58a3528")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{EA7B64E9-55F3-4987-BCAF-86B03C985009}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ReliableUnitOfWork.SqlAzure.UnitTests</RootNamespace>
<AssemblyName>ReliableUnitOfWork.SqlAzure.UnitTests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework">
<HintPath>..\packages\EntityFramework.6.0.2\lib\net45\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="EntityFramework.SqlServer">
<HintPath>..\packages\EntityFramework.6.0.2\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="xunit">
<HintPath>..\packages\xunit.1.9.2\lib\net20\xunit.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="UnitOfWorkFactoryFacts.cs" />
<Compile Include="UnitOfWorkFacts.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestObjects.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ReliableUnitOfWork.SqlAzure\ReliableUnitOfWork.SqlAzure.csproj">
<Project>{c450e55f-cde0-4048-bba5-40c06833640f}</Project>
<Name>ReliableUnitOfWork.SqlAzure</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
30 changes: 30 additions & 0 deletions src/ReliableUnitOfWork.SqlAzure.UnitTests/TestObjects.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace ReliableUnitOfWork.SqlAzure.UnitTests
{
public class TestContext : UnitDbContext
{
}

public class TestPlayer1 : UnitOfWorkPlayer<TestContext>
{
protected override void HandlePlayerJoinedUnit(object sender, EventArgs e)
{
}
}

public class TestPlayer2 : UnitOfWorkPlayer<TestContext>
{
public readonly TestPlayer1 TestPlayer1;

public TestPlayer2(TestPlayer1 testPlayer1)
{
TestPlayer1 = testPlayer1;
}

protected override void HandlePlayerJoinedUnit(object sender, EventArgs e)
{
TestPlayer1.Join(UnitOfWork);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using ReliableUnitOfWork.SqlAzure.Interfaces;
using Xunit;

namespace ReliableUnitOfWork.SqlAzure.UnitTests
{
public class UnitOfWorkFactoryFacts
{
public class StartNew_WithoutParameters
{
private readonly IUnitOfWorkFactory<TestContext> unitOfWorkFactory = new UnitOfWorkFactory<TestContext>();

[Fact]
public void ShouldCreateNewUnitOfWork()
{
var unitOfWork = unitOfWorkFactory.StartNew();
Assert.NotNull(unitOfWork);
Assert.NotNull(unitOfWork.DbContext);
Assert.IsType<UnitOfWork<TestContext>>(unitOfWork);
Assert.IsType<TestContext>(unitOfWork.DbContext);
}
}

public class StartNew_WithPlayerHasNoDependencies
{
private readonly IUnitOfWorkFactory<TestContext> unitOfWorkFactory = new UnitOfWorkFactory<TestContext>();

[Fact]
public void ShouldCreateNewUnitOfWork()
{
var testPlayer1 = new TestPlayer1();
var unitOfWork = unitOfWorkFactory.StartNew(testPlayer1);

Assert.NotNull(unitOfWork);
Assert.NotNull(unitOfWork.DbContext);
Assert.IsType<UnitOfWork<TestContext>>(unitOfWork);
Assert.IsType<TestContext>(unitOfWork.DbContext);
}

[Fact]
public void ShouldJoinPlayerToTheNewUnitOfWork()
{
var testPlayer1 = new TestPlayer1();
var unitOfWork = unitOfWorkFactory.StartNew(testPlayer1);

Assert.Equal(unitOfWork.UniqueId, testPlayer1.UnitOfWork.UniqueId);
}
}

public class StartNew_WithPlayerHasDependencies
{
private readonly IUnitOfWorkFactory<TestContext> unitOfWorkFactory = new UnitOfWorkFactory<TestContext>();

[Fact]
public void ShouldCreateNewUnitOfWork()
{
var testPlayer1 = new TestPlayer1();
var unitOfWork = unitOfWorkFactory.StartNew(testPlayer1);

Assert.NotNull(unitOfWork);
Assert.NotNull(unitOfWork.DbContext);
Assert.IsType<UnitOfWork<TestContext>>(unitOfWork);
Assert.IsType<TestContext>(unitOfWork.DbContext);
}

[Fact]
public void ShouldJoinPlayerAndDependenciesToTheNewUnitOfWork()
{
var testPlayer2 = new TestPlayer2(new TestPlayer1());
var unitOfWork = unitOfWorkFactory.StartNew(testPlayer2);

Assert.Equal(unitOfWork.UniqueId, testPlayer2.UnitOfWork.UniqueId);
Assert.Equal(unitOfWork.UniqueId, testPlayer2.TestPlayer1.UnitOfWork.UniqueId);
}
}
}
}
30 changes: 30 additions & 0 deletions src/ReliableUnitOfWork.SqlAzure.UnitTests/UnitOfWorkFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Xunit;

namespace ReliableUnitOfWork.SqlAzure.UnitTests
{
public class UnitOfWorkFacts
{
public class Constructor
{
[Fact]
public void ShouldCreateNewInstanceFromUnitDbContext()
{
var unitOfWork = new UnitOfWork<TestContext>();
Assert.NotNull(unitOfWork.DbContext);
}
}

public class Dispose
{
[Fact]
public void ShouldDisposeTheUnitDbContextToo()
{
var unitOfWork = new UnitOfWork<TestContext>();

unitOfWork.Dispose();

Assert.Null(unitOfWork.DbContext);
}
}
}
}
5 changes: 5 additions & 0 deletions src/ReliableUnitOfWork.SqlAzure.UnitTests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.0.2" targetFramework="net45" />
<package id="xunit" version="1.9.2" targetFramework="net45" />
</packages>
11 changes: 11 additions & 0 deletions src/ReliableUnitOfWork.SqlAzure.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\ReliableUnitOfWork.SqlAzure.nuspec = ..\ReliableUnitOfWork.SqlAzure.nuspec
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReliableUnitOfWork.SqlAzure.UnitTests", "ReliableUnitOfWork.SqlAzure.UnitTests\ReliableUnitOfWork.SqlAzure.UnitTests.csproj", "{EA7B64E9-55F3-4987-BCAF-86B03C985009}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{AD1EF0E1-F9E6-400C-B444-79C02D8EC4DC}"
ProjectSection(SolutionItems) = preProject
.nuget\packages.config = .nuget\packages.config
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +28,10 @@ Global
{C450E55F-CDE0-4048-BBA5-40C06833640F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C450E55F-CDE0-4048-BBA5-40C06833640F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C450E55F-CDE0-4048-BBA5-40C06833640F}.Release|Any CPU.Build.0 = Release|Any CPU
{EA7B64E9-55F3-4987-BCAF-86B03C985009}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA7B64E9-55F3-4987-BCAF-86B03C985009}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA7B64E9-55F3-4987-BCAF-86B03C985009}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA7B64E9-55F3-4987-BCAF-86B03C985009}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions src/ReliableUnitOfWork.SqlAzure/DomainService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ReliableUnitOfWork.SqlAzure.Interfaces;
using ReliableUnitOfWork.SqlAzure.Internals;
using Serilog;

namespace ReliableUnitOfWork.SqlAzure
Expand Down
2 changes: 1 addition & 1 deletion src/ReliableUnitOfWork.SqlAzure/Interfaces/IRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ReliableUnitOfWork.SqlAzure.Interfaces
{
public interface IRepository<in TDbContext> : IUnitOfWorkPlayer<TDbContext>
public interface IRepository<TDbContext> : IUnitOfWorkPlayer<TDbContext>
where TDbContext : IDbContext
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ReliableUnitOfWork.SqlAzure.Interfaces
{
public interface IUnitOfWorkFactory<out TDbContext> : IUniqueIdHolder
public interface IUnitOfWorkFactory<TDbContext> : IUniqueIdHolder
where TDbContext : IDbContext
{
IUnitOfWork<TDbContext> StartNew(params IUnitOfWorkPlayer<TDbContext>[] players);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace ReliableUnitOfWork.SqlAzure.Interfaces
{
public interface IUnitOfWorkPlayer<in TDbContext> : IUniqueIdHolder
public interface IUnitOfWorkPlayer<TDbContext> : IUniqueIdHolder
where TDbContext : IDbContext
{
IUnitOfWork<TDbContext> UnitOfWork { get; }

void Join(IUnitOfWork<TDbContext> unitOfWork);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using ReliableUnitOfWork.SqlAzure.Interfaces;

namespace ReliableUnitOfWork.SqlAzure
{
public abstract class DomainServiceBase<TDbContext> : UniqueIdHolder, IDomainService<TDbContext>
where TDbContext : class, IDbContext, new()
{
public abstract IUnitOfWork<TDbContext> StartNewUnit();
}
using ReliableUnitOfWork.SqlAzure.Interfaces;

namespace ReliableUnitOfWork.SqlAzure.Internals
{
public abstract class DomainServiceBase<TDbContext> : UniqueIdHolder, IDomainService<TDbContext>
where TDbContext : class, IDbContext, new()
{
public abstract IUnitOfWork<TDbContext> StartNewUnit();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace ReliableUnitOfWork.SqlAzure.Interfaces
{
public abstract class UnitOfWorkBase<TDbContext> : UniqueIdHolder, IUnitOfWork<TDbContext>
where TDbContext : class, IDbContext, new()
{
public TDbContext DbContext { get; protected set; }

public abstract void Dispose();
}
using ReliableUnitOfWork.SqlAzure.Interfaces;

namespace ReliableUnitOfWork.SqlAzure.Internals
{
public abstract class UnitOfWorkBase<TDbContext> : UniqueIdHolder, IUnitOfWork<TDbContext>
where TDbContext : class, IDbContext, new()
{
public TDbContext DbContext { get; protected set; }

public abstract void Dispose();
}
}
Loading

0 comments on commit db17d20

Please sign in to comment.