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 #34

Open
wants to merge 5 commits 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
204 changes: 184 additions & 20 deletions TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions TechTest/AnyCompany.Tests/Class1.cs

This file was deleted.

14 changes: 14 additions & 0 deletions TechTest/AnyCompany.Tests/MockData/InMemoryAnyCompanyContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

namespace InvestecUnitTestsTests.MockData
{
using AnyCompany;
using AnyCompany.DAL;
using Microsoft.EntityFrameworkCore;
public class RepoAnyCompanyContext : AnyCompanyContext
{
public RepoAnyCompanyContext(DbContextOptions option) : base()
{

}
}
}
28 changes: 6 additions & 22 deletions TechTest/AnyCompany.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
using System.Reflection;
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("AnyCompany.Tests")]
[assembly: AssemblyTitle("InvestecUnitTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Investec Bank")]
[assembly: AssemblyProduct("AnyCompany.Tests")]
[assembly: AssemblyCopyright("Copyright © Investec Bank 2018")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("InvestecUnitTests")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[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("cd5d577e-bdc9-4dfc-ac6a-b1da474995f3")]
[assembly: Guid("60cfe149-cb3f-4227-8ead-4e16c0796535")]

// 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,96 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Data.Entity.Migrations;
using System.Linq;
using AnyCompany;
using AnyCompany.BUL.Services;
using AnyCompany.DAL;
using AnyCompany.DAL.Repositories;
using InvestecUnitTestsTests.MockData;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Moq;
using NUnit.Framework;

namespace InvestecUnitTests
{
[TestFixture]
public class CustomerRepositoryTests : TestsBase<ICustomerRepository>
{
[TestCase]
public void GIVEN_VALID_CUSTOMERID_RETURN_CUSTOMER()
{
//Arrange
Customer returnedCustomer = null;
int customerIdToGet = 1;

Customers.ToList().ForEach(i => RepoContext.Customers.AddOrUpdate(i));
RepoContext.SaveChanges();

//Act
CustomerRepositoryWrapper CustomerWrapper = new CustomerRepositoryWrapper();
returnedCustomer = CustomerWrapper.Get(customerIdToGet);

//Assert
Assert.True(returnedCustomer != null);
Assert.True(returnedCustomer.CustomerId == customerIdToGet);
}


[TestCase]
public void GIVEN_NONEXISTENT_CUSTOMERID_RETURN_NULL()
{
//Arrange
Customer returnedCustomer = null;
int customerIdToGet = -9999;

Customers.ToList().ForEach(i => RepoContext.Customers.AddOrUpdate(i));
RepoContext.SaveChanges();

//Act
CustomerRepositoryWrapper CustomerWrapper = new CustomerRepositoryWrapper();
returnedCustomer = CustomerWrapper.Get(customerIdToGet);

//Assert
Assert.True(returnedCustomer == null);
}

[TestCase]
public void RETURN_CUSTOMERS_WITH_LINKED_ORDERS_BY_DEFAULT()
{
//Arrange
CustomersWithOrders.ToList().ForEach(i => RepoContext.Customers.AddOrUpdate(i));
RepoContext.SaveChanges();

//Act
CustomerRepositoryWrapper customerWrapper = new CustomerRepositoryWrapper();
IEnumerable<Customer> returnedCustomers = customerWrapper.GetAll();
int orderCount = 0;
returnedCustomers.ToList().ForEach(customer => orderCount += customer.Orders.Count);

//Assert
Assert.True(orderCount > 0);
Assert.True(returnedCustomers.Count() > 0);
}

[TestCase]
public void GIVEN_FALSE_ARGUMENT_RETURN_CUSTOMERS_WITHOUT_LINKED_ORDERS()
{
//Arrange
CustomersWithOrders.ToList().ForEach(i => RepoContext.Customers.AddOrUpdate(i));
RepoContext.SaveChanges();

//Act
CustomerRepositoryWrapper customerWrapper = new CustomerRepositoryWrapper();
IEnumerable<Customer> returnedCustomers = customerWrapper.GetAll(false);
int orderCount = 0;
returnedCustomers.ToList().ForEach(customer => orderCount += customer.Orders.Count);

//Assert
Assert.True(orderCount == 0);
Assert.True(returnedCustomers.Count() > 0);
}
}
}
48 changes: 48 additions & 0 deletions TechTest/AnyCompany.Tests/RepositoryTests/OrderRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Linq;
using AnyCompany;
using AnyCompany.BUL.Services;
using AnyCompany.DAL;
using AnyCompany.DAL.Repositories;
using InvestecUnitTestsTests.MockData;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Moq;
using NUnit.Framework;

namespace InvestecUnitTests
{
[TestFixture]
public class OrderRepositoryTests : TestsBase<IOrderRepository>
{
[TestCase]
public void GIVEN_VALID_ORDER_RETURN_TRUE()
{
//Arrange

//Act
OrderRepositoryWrapper orderWrapper = new OrderRepositoryWrapper();
bool isSaved = orderWrapper.Save(ValidOrder);

//Assert
Assert.True(isSaved == true);
}


[TestCase]
public void GIVEN_INVALID_ORDER_RETURN_FALSE()
{
//Arrange

//Act
OrderRepositoryWrapper orderWrapper = new OrderRepositoryWrapper();
bool isSaved = orderWrapper.Save(InValidOrder);

//Assert
Assert.True(isSaved == false);
}
}
}
154 changes: 154 additions & 0 deletions TechTest/AnyCompany.Tests/ServiceTests/CustomerServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AnyCompany;
using AnyCompany.BUL.Helpers;
using AnyCompany.BUL.Services;
using AnyCompany.DAL;
using AnyCompany.DAL.Repositories;
using Moq;
using NUnit.Framework;

namespace InvestecUnitTests
{
[TestFixture]
public class CustomerServiceTests
{
private Mock<ICustomerRepository> _customerService = new Mock<ICustomerRepository>();
private CustomerService customerService;
private readonly List<Customer> customers = new List<Customer>
{
new Customer
{
Name = "Mike America", Country = "US", DateOfBirth = DateTime.Parse("1987-06-01"), CustomerId = 1
},
new Customer
{
Name = "Jonathan Canada", Country = "CN", DateOfBirth = DateTime.Parse("1925-06-01"), CustomerId = 2
},
new Customer
{
Name = "FROM THE UK CHINKU", Country = "UK", DateOfBirth = DateTime.Parse("2001-06-01"), CustomerId = 3
}
,
new Customer
{
Name = "Not in Database Adams", Country = "AG", DateOfBirth = DateTime.Parse("1982-12-12"), CustomerId = 50
}
};
private readonly List<Customer> customersWithOrders = new List<Customer>
{
new Customer
{
Name = "I Have Order too", Country = "NM", DateOfBirth = DateTime.Parse("1987-06-01"), CustomerId = 12
, Orders = new List<Order>()
{
new Order
{
Amount = 500,
OrderId = 12,
CustomerId = 1,
VAT = 15
}
}
},
new Customer
{
Name = "I Have Order", Country = "ZW", DateOfBirth = DateTime.Parse("1925-06-01"), CustomerId = 13
, Orders = new List<Order>()
{
new Order
{
Amount = 500,
OrderId = 12,
CustomerId = 1,
VAT = 15
}
}
}
};

[TestCase]
public void GIVEN_EXISTINGId_RETURNS_MATCHING_CUSTOMER()
{
//Arrange
Customer matchingCustomer = customers[2];
_customerService.Setup(x => x.Get(matchingCustomer.CustomerId)).Returns(matchingCustomer);

//Act
customerService = new CustomerService(_customerService.Object);
Customer returnedCustomer = customerService.RetrieveACustomer(matchingCustomer.CustomerId);

//Asset
Assert.AreSame(returnedCustomer, matchingCustomer);
Assert.AreEqual(returnedCustomer.CustomerId, matchingCustomer.CustomerId);
}

[TestCase]
public void GIVEN_WRONGId_RETURNS_NONMATCHING_CUSTOMER()
{
//Arrange
Customer customerToGet = customers[2];
Customer notMatchingCustomer = customers[0];
_customerService.Setup(x => x.Get(customerToGet.CustomerId)).Returns(notMatchingCustomer);

//Act
customerService = new CustomerService(_customerService.Object);
Customer returnedCustomer = customerService.RetrieveACustomer(customerToGet.CustomerId);

//Asset
Assert.AreNotSame(returnedCustomer, customerToGet);
}

[TestCase]
public void GIVEN_NONEXISTENTId_RETURNS_EMPTYCUSTOMEROBJECT()
{
//Arrange
int NonExtentId = 344;

_customerService.Setup(x => x.Get(NonExtentId)).Returns(new Customer());

//Act
customerService = new CustomerService(_customerService.Object);
Customer returnedCustomer = customerService.RetrieveACustomer(NonExtentId);

//Asset
Assert.True(returnedCustomer.CustomerId == 0);
}

[TestCase]
public void GIVEN_TRUE_ARGUMENT_RETURNS_CUSTOMERS_WITH_LINKED_ORDERS()
{
//Arrange
_customerService.Setup(x => x.GetAll(true)).Returns(customersWithOrders);

//Act
customerService = new CustomerService(_customerService.Object);
IEnumerable<Customer> returnedCustomers = customerService.RetrieveCustomers(true);
int orderCount = 0;
returnedCustomers.ToList().ForEach(i => orderCount += i.Orders.Count);

//Asset
Assert.AreSame(customersWithOrders, returnedCustomers);
Assert.True(orderCount > 0);
}

[TestCase]
public void GIVEN_FALSE_ARGUMENT_RETURNS_CUSTOMERS_WITHOUT_LINKED_ORDERS()
{
//Arrange
_customerService.Setup(x => x.GetAll(false)).Returns(customers);

//Act
customerService = new CustomerService(_customerService.Object);
IEnumerable<Customer> returnedCustomers = customerService.RetrieveCustomers(false);

int orderCount = 0;
returnedCustomers.ToList().ForEach(customer => orderCount += customer.Orders.Count);

//Asset
Assert.AreEqual(orderCount, 0);
Assert.AreNotSame(returnedCustomers, customersWithOrders);
}
}
}
Loading