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

tech-test refactored for review #37

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
59 changes: 41 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\MSTest.TestAdapter.2.1.2\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.2.1.2\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>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -30,24 +33,44 @@
<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="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="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" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Mocks\CustomerRepositoryMock.cs" />
<Compile Include="Mocks\OrderRepositoryMock.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestCustomerService.cs" />
<Compile Include="TestOrderService.cs" />
</ItemGroup>
<ItemGroup>
<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\MSTest.TestAdapter.2.1.2\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.1.2\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.1.2\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.1.2\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.2.1.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.2.1.2\build\net45\MSTest.TestAdapter.targets')" />
</Project>
66 changes: 66 additions & 0 deletions TechTest/AnyCompany.Tests/Mocks/CustomerRepositoryMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using AnyCompany.DAL;
using AnyCompany.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyCompany.Tests.Mocks
{
public class CustomerRepositoryMock : ICustomerRepository
{
public Customer GetCustomerById(int customerId)
{
var customer = new Customer();

if (customerId == 1)
{
customer = new Customer()
{
CustomerId = 1,
Country = new Country()
{
CountryId = 1,
Name = "UK",
VATRate = 0.2d
},
DateOfBirth = new DateTime(1982, 2, 26),
Name = "John"
};
}

return customer;
}

public IEnumerable<Customer> GetAllCustomers()
{
var customers = new List<Customer>();
customers.Add(new Customer()
{
CustomerId = 1,
CountryId = 1,
DateOfBirth = new DateTime(1982, 2, 26),
Name = "John"
});

customers.Add(new Customer()
{
CustomerId = 2,
CountryId = 1,
DateOfBirth = new DateTime(1983, 2, 26),
Name = "Jack"
});

customers.Add(new Customer()
{
CustomerId = 3,
CountryId = 1,
DateOfBirth = new DateTime(1984, 2, 26),
Name = "Jill"
});

return customers;
}
}
}
28 changes: 28 additions & 0 deletions TechTest/AnyCompany.Tests/Mocks/OrderRepositoryMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using AnyCompany.DAL;
using AnyCompany.Models;
using System.Collections.Generic;

namespace AnyCompany.Tests.Mocks
{
public class OrderRepositoryMock : IOrderRepository
{
public IEnumerable<Order> GetOrdersByCustomerId(int customerId)
{
var orders = new List<Order>();

if (customerId == 1)
{
orders.Add(new Order { OrderId = 1, Amount = 3, VAT = 0.2d, CustomerId = 1 });
orders.Add(new Order { OrderId = 2, Amount = 4, VAT = 0.2d, CustomerId = 1 });
orders.Add(new Order { OrderId = 3, Amount = 5, VAT = 0.2d, CustomerId = 1 });
}

return orders;
}

public bool Save(Order order)
{
return true;
}
}
}
60 changes: 60 additions & 0 deletions TechTest/AnyCompany.Tests/TestCustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using AnyCompany.Models;
using AnyCompany.Tests.Mocks;
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]
public class TestCustomerService
{
private readonly CustomerService _customerService;

public TestCustomerService()
{
_customerService = new CustomerService(
new CustomerRepositoryMock());
}

[TestMethod]
public void GetCustomerById_ValidCustomerId_ReturnsCustomer()
{
////----Act
var response = _customerService.GetCustomerById(1);

////----Assert
Assert.IsNotNull(response);
Assert.IsTrue(response is Customer);
Assert.AreEqual(1, response.CustomerId);
}

[TestMethod]
public void GetCustomerById_InvalidCustomerId_ReturnsEmptyCustomer()
{
////----Act
var response = _customerService.GetCustomerById(5);

////----Assert
Assert.IsNotNull(response);
Assert.IsTrue(response is Customer);
Assert.AreEqual(0, response.CustomerId);
}

[TestMethod]
public void GetAllCustomers_ReturnsListOfCustomers()
{
////----Act
var response = _customerService.GetAllCustomers();

////----Assert
Assert.IsNotNull(response);
Assert.IsTrue(response is List<Customer>);
Assert.IsTrue(response.Count() > 0);
}

}
}
93 changes: 93 additions & 0 deletions TechTest/AnyCompany.Tests/TestOrderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using AnyCompany.Models;
using AnyCompany.Tests.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace AnyCompany.Tests
{
[TestClass]
public class TestOrderService
{
private readonly OrderService _orderService;

public TestOrderService()
{
_orderService = new OrderService(
new OrderRepositoryMock());
}

[TestMethod]
public void PlaceOrder_OrderCreated()
{
//----Arrange
var customer = new Customer
{
CustomerId = 1,
Country = new Country()
{
CountryId = 1,
Name = "UK",
VATRate = 0.2d
},
Name = "John"
};

var order = new Order(10, customer);

////----Act
var response = _orderService.PlaceOrder(order);

////----Assert
Assert.IsTrue(response);
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void PlaceOrder_ZeroAmount_ThrowsException()
{
//----Arrange
var customer = new Customer
{
CustomerId = 1,
Country = new Country()
{
CountryId = 1,
Name = "UK",
VATRate = 0.2d
},
Name = "John"
};

var order = new Order(0, customer);

////----Act
var response = _orderService.PlaceOrder(order);
}

[TestMethod]
public void GetOrdersByCustomerId_ValidCustomerId_ReturnsCustomerOrders()
{
var response = _orderService.GetOrdersByCustomerId(1);

////----Assert
Assert.IsNotNull(response);
Assert.IsTrue(response is List<Order>);
Assert.IsTrue(response.Count() > 0);
}

[TestMethod]
public void GetOrdersByCustomerId_InvalidCustomerId_ReturnsEmptyList()
{
var response = _orderService.GetOrdersByCustomerId(5);

Assert.IsNotNull(response);
Assert.IsTrue(response is List<Order>);
Assert.IsTrue(response.Count() <= 0);
}
}
}
5 changes: 5 additions & 0 deletions TechTest/AnyCompany.Tests/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="MSTest.TestAdapter" version="2.1.2" targetFramework="net461" />
<package id="MSTest.TestFramework" version="2.1.2" targetFramework="net461" />
</packages>
14 changes: 10 additions & 4 deletions TechTest/AnyCompany/AnyCompany.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Customer.cs" />
<Compile Include="CustomerRepository.cs" />
<Compile Include="Order.cs" />
<Compile Include="OrderRepository.cs" />
<Compile Include="CustomerService.cs" />
<Compile Include="DAL\CustomerRepositoryWrapper.cs" />
<Compile Include="DAL\RepositoryBase.cs" />
<Compile Include="DAL\Interfaces\ICustomerRepository.cs" />
<Compile Include="DAL\Interfaces\IOrderRepository.cs" />
<Compile Include="Models\Country.cs" />
<Compile Include="Models\Customer.cs" />
<Compile Include="DAL\CustomerRepository.cs" />
<Compile Include="Models\Order.cs" />
<Compile Include="DAL\OrderRepository.cs" />
<Compile Include="OrderService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
26 changes: 26 additions & 0 deletions TechTest/AnyCompany/CustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using AnyCompany.DAL;
using AnyCompany.Models;
using System.Collections.Generic;

namespace AnyCompany
{
public class CustomerService
{
private readonly ICustomerRepository _customerRepository;

public CustomerService(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}

public Customer GetCustomerById(int customerId)
{
return _customerRepository.GetCustomerById(customerId);
}

public IEnumerable<Customer> GetAllCustomers()
{
return _customerRepository.GetAllCustomers();
}
}
}
Loading