Skip to content
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
80 changes: 62 additions & 18 deletions TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
<?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\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.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>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -20,6 +29,9 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>SA1208</NoWarn>
<DocumentationFile>bin\Debug\AnyCompany.Tests.xml</DocumentationFile>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -28,26 +40,58 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.1</LangVersion>
</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="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.4.0\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.1.4.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.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="CustomerServiceTests.cs" />
<Compile Include="MockRepository\MockCustomerRepository.cs" />
<Compile Include="MockRepository\MockOrderRepository.cs" />
<Compile Include="OrderServiceTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\stylecop.json">
<Link>stylecop.json</Link>
</AdditionalFiles>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\StyleCop.Analyzers.1.1.118\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.1.118\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AnyCompany\AnyCompany.csproj">
<Project>{C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}</Project>
<Name>AnyCompany</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<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\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets')" />
</Project>
12 changes: 0 additions & 12 deletions TechTest/AnyCompany.Tests/Class1.cs

This file was deleted.

98 changes: 98 additions & 0 deletions TechTest/AnyCompany.Tests/CustomerServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// <copyright file="CustomerServiceTests.cs" company="Investec Bank">
// Copyright © Investec Bank 2018
// </copyright>

namespace AnyCompany.Tests
{
using AnyCompany.Service;
using AnyCompany.Tests.MockRepository;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

/// <summary>
/// Provides unit tests for the <see cref="CustomerService"/> class.
/// </summary>
[TestClass]
public class CustomerServiceTests
{
/// <summary>
/// Load all customers with orders
/// Some customers exist without orders
/// Get back the customers, no orders.
/// </summary>
[TestMethod]
public void LoadAllCustomerWithOrders_ExistingCustomersNoOrders_GetCustomersNoOrders()
{
// Arrange
var customerRepository = new MockCustomerRepository();
var orderRepository = new MockOrderRepository();
var customerService = new CustomerService(customerRepository, orderRepository);

// Act
var customersWithOrders = customerService.LoadAllCustomersWithOrders();

// Assert
Assert.IsTrue(customersWithOrders.Count() == 2);
foreach (var (customer, orders) in customersWithOrders)
{
Assert.IsTrue(orders.Count() == 0);
}
}

/// <summary>
/// Load all customers with orders
/// Some customers exist with orders
/// Get back the customers, with correct number of orders.
/// </summary>
[TestMethod]
public void LoadAllCustomerWithOrders_ExistingCustomersWithOrders_GetCustomersCorrectOrderNumbers()
{
// Arrange
var customerRepository = new MockCustomerRepository();
var orderRepository = new MockOrderRepository();
var customerService = new CustomerService(customerRepository, orderRepository);
var allCustomers = customerRepository.LoadAll().ToList();

orderRepository.Save(new Order()
{
OrderId = default,
Amount = 10,
VAT = default,
CustomerId = allCustomers[0].CustomerId,
});

orderRepository.Save(new Order()
{
OrderId = default,
Amount = 20,
VAT = default,
CustomerId = allCustomers[0].CustomerId,
});

orderRepository.Save(new Order()
{
OrderId = default,
Amount = 30,
VAT = default,
CustomerId = allCustomers[1].CustomerId,
});

// Act
var customersWithOrders = customerService.LoadAllCustomersWithOrders();

// Assert
Assert.IsTrue(customersWithOrders.Count() == 2);
foreach (var (customer, orders) in customersWithOrders)
{
if (customer.CustomerId == allCustomers[0].CustomerId)
{
Assert.IsTrue(orders.Count() == 2);
}
else if (customer.CustomerId == allCustomers[1].CustomerId)
{
Assert.IsTrue(orders.Count() == 1);
}
}
}
}
}
59 changes: 59 additions & 0 deletions TechTest/AnyCompany.Tests/MockRepository/MockCustomerRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// <copyright file="MockCustomerRepository.cs" company="Investec Bank">
// Copyright © Investec Bank 2018
// </copyright>

namespace AnyCompany.Tests.MockRepository
{
using AnyCompany.Repository;
using System;
using System.Collections.Generic;

/// <summary>
/// Provides a mock implementation of ICustomerRepository for use in unit tests, that starts with two customers.
/// </summary>
internal class MockCustomerRepository : ICustomerRepository
{
private readonly List<Customer> customers = new List<Customer>();

/// <summary>
/// Initializes a new instance of the <see cref="MockCustomerRepository"/> class.
/// </summary>
public MockCustomerRepository()
{
this.customers.Add(new Customer
{
CustomerId = 1,
Country = "UK",
DateOfBirth = new DateTime(1990, 01, 01),
Name = "John Doe",
});

this.customers.Add(new Customer
{
CustomerId = 2,
Country = "USA",
DateOfBirth = new DateTime(1980, 01, 01),
Name = "Jane Doe",
});
}

/// <inheritdoc/>
public Customer Load(int customerId)
{
if (customerId > 0 && customerId <= 2)
{
return this.customers[customerId - 1];
}
else
{
throw new ApplicationException($"Customer not found with id: {customerId}");
}
}

/// <inheritdoc/>
public IEnumerable<Customer> LoadAll()
{
return this.customers;
}
}
}
29 changes: 29 additions & 0 deletions TechTest/AnyCompany.Tests/MockRepository/MockOrderRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// <copyright file="MockOrderRepository.cs" company="Investec Bank">
// Copyright © Investec Bank 2018
// </copyright>

namespace AnyCompany.Tests.MockRepository
{
using AnyCompany.Repository;
using System.Collections.Generic;

/// <summary>
/// Provides a mock implementation of IOrderRepository for use during unit testing, that starts out empty.
/// </summary>
internal class MockOrderRepository : IOrderRepository
{
private readonly List<Order> orders = new List<Order>();

/// <inheritdoc/>
public void Save(Order order)
{
this.orders.Add(order);
}

/// <inheritdoc/>
public IEnumerable<Order> LoadAll()
{
return this.orders;
}
}
}
78 changes: 78 additions & 0 deletions TechTest/AnyCompany.Tests/OrderServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// <copyright file="OrderServiceTests.cs" company="Investec Bank">
// Copyright © Investec Bank 2018
// </copyright>

namespace AnyCompany.Tests
{
using AnyCompany.Service;
using AnyCompany.Tests.MockRepository;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

/// <summary>
/// Provides unit tests for the <see cref="OrderService"/> class.
/// </summary>
[TestClass]
public class OrderServiceTests
{
/// <summary>
/// Save
/// An order linked to a UK customer
/// VAT is 0.2d.
/// </summary>
[TestMethod]
public void PlaceOrder_LinkedToUkCustomer_VatIsTwentyPercent()
{
// Arrange
var orderRepository = new MockOrderRepository();
var customerRepository = new MockCustomerRepository();
var orderService = new OrderService(orderRepository, customerRepository);
var ukCustomer = customerRepository.LoadAll().First();

// Act
var order = new Order()
{
OrderId = default,
Amount = 20,
VAT = default,
CustomerId = ukCustomer.CustomerId,
};

orderService.PlaceOrder(order);

// Assert
Assert.IsTrue(ukCustomer.Country == "UK");
Assert.IsTrue(order.VAT == 0.2d);
}

/// <summary>
/// Save
/// An order linked to a non-UK customer
/// VAT is 0.
/// </summary>
[TestMethod]
public void PlaceOrder_LinkedToNonUkCustomer_VatIsZero()
{
// Arrange
var orderRepository = new MockOrderRepository();
var customerRepository = new MockCustomerRepository();
var orderService = new OrderService(orderRepository, customerRepository);
var nonUkCustomer = customerRepository.LoadAll().Skip(1).First();

// Act
var order = new Order()
{
OrderId = default,
Amount = 20,
VAT = default,
CustomerId = nonUkCustomer.CustomerId,
};

orderService.PlaceOrder(order);

// Assert
Assert.IsTrue(nonUkCustomer.Country != "UK");
Assert.IsTrue(order.VAT == 0d);
}
}
}
Loading