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

submit the solution for the tech test #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 66 additions & 18 deletions TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\EntityFramework.6.4.4\build\EntityFramework.props" Condition="Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.props')" />
<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>cd5d577e-bdc9-4dfc-ac6a-b1da474995f3</ProjectGuid>
<ProjectGuid>{CD5D577E-BDC9-4DFC-AC6A-B1DA474995F3}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AnyCompany.Tests</RootNamespace>
<AssemblyName>AnyCompany.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -30,24 +33,69 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>

<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.Net.Http"/>

<Reference Include="System.Xml"/>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.4.4.0\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.1.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.1.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="Moq, Version=4.14.0.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.14.5\lib\net45\Moq.dll</HintPath>
</Reference>
<Reference Include="NSubstitute, Version=4.2.0.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
<HintPath>..\packages\NSubstitute.4.2.2\lib\net46\NSubstitute.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="CustomerSpec.cs" />
<Compile Include="DbTestHelper.cs" />
<Compile Include="OrderServiceSpec.cs" />
<Compile Include="OrderSpec.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UnitofWorkSpec.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AnyCompany\AnyCompany.csproj">
<Project>{C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}</Project>
<Name>AnyCompany</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EntityFramework.6.4.4\build\EntityFramework.props'))" />
<Error Condition="!Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EntityFramework.6.4.4\build\EntityFramework.targets'))" />
</Target>
<Import Project="..\packages\EntityFramework.6.4.4\build\EntityFramework.targets" Condition="Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.targets')" />
</Project>
24 changes: 24 additions & 0 deletions TechTest/AnyCompany.Tests/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.0" newVersion="4.0.4.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
12 changes: 0 additions & 12 deletions TechTest/AnyCompany.Tests/Class1.cs

This file was deleted.

65 changes: 65 additions & 0 deletions TechTest/AnyCompany.Tests/CustomerSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using AnyCompany.DataAccessLayer;
using AnyCompany.Models;
using AnyCompany.RepositoryLayer;
using AnyCompany.RepositoryLayer.Contracts;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyCompany.Tests
{
[TestClass]
public class CustomerSpec
{
private Mock<CompanyContext> contextMock;

[TestInitialize]
public void Init()
{
contextMock = new Mock<CompanyContext>();

var mockCustomer = new List<Customer>
{
new Customer
{
Id = 1,
Name = "Senzo",
Surname = "Meyiwa",
Email = "[email protected]",
DateOfBirth = DateTime.Now,
Country = "UK"
}
}.AsQueryable();

contextMock.Setup(c => c.Customers).Returns((DbSet<Customer>)mockCustomer);
}

[TestMethod]
public void GivenAValidCustomerId_WhenRequestingCustomerDetails_ReturnCustomerDetails()
{

var customer = Substitute.For<ICustomerRepository<Customer>>();
var result = customer.GetCustomer(1);

Assert.AreEqual("Senzo",result.Name);
Assert.AreEqual("Meyiwa", result.Surname);
}

[TestMethod]
public void GivenAnInvalidCustomerId_WhenRequestingCustomerDetails_DontReturnCustomerDetails()
{
var customer = Substitute.For<ICustomerRepository<Customer>>();
var result = customer.GetCustomer(2);

Assert.AreEqual("", result.Name);
Assert.AreEqual("", result.Surname);
}

}
}
38 changes: 38 additions & 0 deletions TechTest/AnyCompany.Tests/DbTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Moq;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyCompany.Tests
{
public static class DbTestHelper
{
public static DbSet<T> ToDbSet<T>(this IEnumerable<T> data) where T : class
{
var fakeData = data.AsQueryable();
var mockSet = new Mock<DbSet<T>>();

mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(fakeData.Provider);
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(fakeData.Expression);
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(fakeData.ElementType);
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(fakeData.GetEnumerator());

return mockSet.Object;
}

public static Mock<DbSet<T>> ToDbMock<T>(this IEnumerable<T> data) where T : class
{
var fakeData = data.AsQueryable();
var mockSet = new Mock<DbSet<T>>();
mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(fakeData.Provider);
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(fakeData.Expression);
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(fakeData.ElementType);
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(fakeData.GetEnumerator());

return mockSet;
}
}
}
20 changes: 20 additions & 0 deletions TechTest/AnyCompany.Tests/OrderServiceSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyCompany.Tests
{
[TestClass]
class OrderServiceSpec
{
[TestInitialize]
public void Init()
{

}

}
}
111 changes: 111 additions & 0 deletions TechTest/AnyCompany.Tests/OrderSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using AnyCompany.DataAccessLayer;
using AnyCompany.Models;
using AnyCompany.RepositoryLayer.Contracts;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyCompany.Tests
{
[TestClass]
class OrderSpec
{
private Mock<CompanyContext> contextMock;
private Mock<IOrderRepository<Order>> mockOrderRepository;

[TestInitialize]
public void Init()
{
contextMock = new Mock<CompanyContext>();
mockOrderRepository = new Mock<IOrderRepository<Order>>(contextMock.Object);

var mockCustomer = new List<Customer>
{
new Customer
{
Id = 1,
Name = "Senzo",
Surname = "Meyiwa",
Email = "[email protected]",
DateOfBirth = DateTime.Now,
Country = "UK"
},
new Customer
{
Id = 2,
Name = "Sipho",
Surname = "Khumalo",
Email = "[email protected]",
DateOfBirth = DateTime.Now,
Country = "ZAF"
}
}.AsQueryable();

var mockOrder = new List<Order>
{
new Order
{
Id = 1,
Amount = 200,
OrderNumber = "ABCDEF1234",
VAT = 0
},
new Order
{
Id = 2,
Amount = 500,
OrderNumber = "ABCDEF0123",
VAT = 0
}
}.AsQueryable();

var mockCustomerOrder = new List<CustomerOrder>
{
new CustomerOrder
{
Id = 1,
CustomerId = 1,
OrderId = 1,
Completed = false
},
new CustomerOrder
{
Id = 1,
CustomerId = 1,
OrderId = 2,
Completed = false
}
}.AsQueryable();

contextMock.Setup(c => c.Customers).Returns((DbSet<Customer>)mockCustomer);
contextMock.Setup(c => c.Orders).Returns((DbSet<Order>)mockOrder);
contextMock.Setup(c => c.CustomerOrders).Returns((DbSet<CustomerOrder>)mockCustomerOrder);
}

[TestMethod]
public void GivenAValidCustomerId_WhenSearchingForCustomerOrders_ReturnCustomerOrders()
{
var order = Substitute.For<IOrderRepository<Order>>();
var result = order.GetCustomerOrders(1);

Assert.AreEqual("Senzo", result.Name);
Assert.AreEqual(2, result.Orders.Count());
}

[TestMethod]
public void GivenAnInvalidCustomerId_WhenSearchingForCustomerOrders_DoNotReturnCustomerOrders()
{
var order = Substitute.For<IOrderRepository<Order>>();
var result = order.GetCustomerOrders(3);

Assert.AreEqual("", result.Name);
Assert.AreEqual("", result.Email);
}
}
}
Loading