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
2 changes: 1 addition & 1 deletion Refactoring/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Purchase(string productId, int quantity)
throw new OutOfStockException();
}

product.Quantity = product.Quantity - quantity+1;
product.Quantity = product.Quantity - quantity;
user.Balance = user.Balance - product.Price * quantity;

dataManager.SaveUser(user);
Expand Down
52 changes: 52 additions & 0 deletions UnitTestProject/AuthenticatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json;
using NUnit.Framework;
using Refactoring;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace UnitTestProject
{
[TestFixture]
class AuthenticatorTests
{
private User createTestUser(string name, string password, double balance)
{
var testUser = new User {Name = name, Password = password, Balance = balance};

return testUser;
}

[Test]
public void Test_ShouldAuthenticateUser()
{
//Arrange
var users = new List<User> { createTestUser("Unit Test", "", 100.00) };
var authenticate = new Authenticator(users);

//Act
var user = authenticate.Authenticate(users.ToList().Select(r => r.Name).First(),
users.ToList().Select(r => r.Password).First());

//Assert
Assert.IsNotNull(user);
}

[Test]
public void Test_ShouldReturnNull()
{
//Arrange
var users = new List<User> { createTestUser("Unit Test", "", 100.00) };
var authenticate = new Authenticator(users);

//Act
var user = authenticate.Authenticate("",
users.ToList().Select(r => r.Password).First());

//Assert
Assert.IsNull(user);
}
}
}
2 changes: 1 addition & 1 deletion UnitTestProject/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace UnitTestProject
{
[TestFixture]
//[Ignore("Disable integration tests")]
[Ignore("Disable integration tests")]
public class IntegrationTests
{
private List<User> users;
Expand Down
101 changes: 87 additions & 14 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,14 @@ class StoreTests
{
private User createTestUser(string name, string password, double balance)
{
User testUser = new User();
testUser.Name = name;
testUser.Password = password;
testUser.Balance = balance;
var testUser = new User {Name = name, Password = password, Balance = balance};

return testUser;
}

private Product createTestProduct(string id, string name, double price, int quantity)
{
Product testProduct = new Product();
testProduct.Id = id;
testProduct.Name = name;
testProduct.Price = price;
testProduct.Quantity = quantity;
var testProduct = new Product {Id = id, Name = name, Price = price, Quantity = quantity};

return testProduct;
}
Expand Down Expand Up @@ -60,36 +53,116 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
const string TEST_PRODUCT_ID = "2";

var users = new List<User> {createTestUser("Unit Test", "", 100.00)};
var products = new List<Product> {createTestProduct(TEST_PRODUCT_ID, "MyProduct", 5.00, 10)};
var dataManager = new DataManager(users, products);
var store = new Store(users.ToList().First(), dataManager);

//Act
store.Purchase(TEST_PRODUCT_ID,9);

//Assert
//(choose the appropriate statement(s))
//Assert.AreEqual(1, products[0].Quantity);
Assert.AreEqual(1, products[0].Quantity);
//Assert.AreSame(1, products[0].Quantity);
//Assert.IsTrue(products[0].Quantity == 1);
Assert.IsTrue(products[0].Quantity == 1);
}

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange
const string TEST_PRODUCT_ID = "2";
const string INSUFFICIENT_FUND_EXCEPTION =
"Exception of type 'Refactoring.InsufficientFundsException' was thrown.";

//Act
var users = new List<User> { createTestUser("Unit Test", "", 1.00) };
var products = new List<Product> { createTestProduct(TEST_PRODUCT_ID, "MyProduct", 5.00, 10) };
var dataManager = new DataManager(users, products);
var store = new Store(users.ToList().First(), dataManager);

try
{
//Act
store.Purchase(TEST_PRODUCT_ID, 9);
}
catch (Exception e)
{
Assert.AreEqual(INSUFFICIENT_FUND_EXCEPTION, e.Message);
}

//Assert
}

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
{
//Arrange
const string TEST_PRODUCT_ID = "2";

var users = new List<User> { createTestUser("Unit Test", "", 1.00) };
var products = new List<Product> { createTestProduct(TEST_PRODUCT_ID, "MyProduct", 5.00, 1) };
var dataManager = new DataManager(users, products);
var store = new Store(users.ToList().First(), dataManager);

//Act
Assert.Throws<InsufficientFundsException>(() => store.Purchase(TEST_PRODUCT_ID, 9));
}


[Test]
public void Test_ShouldThrowOutOfStockException()
{
//Arrange
Store store;
var TEST_PRODUCT_ID = InitialSetup(out store);
//Act
Assert.Throws<OutOfStockException>(() => store.Purchase(TEST_PRODUCT_ID, 3));

//Assert
}

[Test]
public void Test_ShouldReturnProductList()
{
//Arrange
Store store;
var TEST_PRODUCT_ID = InitialSetup(out store);
//Act
Assert.IsNotNullOrEmpty(store.GetProductList());
}

[Test]
public void Test_ShouldReturnNumberOfProducts()
{
//Arrange
Store store;
var TEST_PRODUCT_ID = InitialSetup(out store);
//Act
Assert.IsNotNullOrEmpty(store.NumberOfProducts().ToString());
}

[Test]
public void Test_ShouldContainsProduct()
{
Store store;
var TEST_PRODUCT_ID = InitialSetup(out store);

//Act
Assert.IsTrue(store.ContainsProduct(TEST_PRODUCT_ID));
}

private string InitialSetup(out Store store)
{
//Arrange
const string TEST_PRODUCT_ID = "2";

var users = new List<User> {createTestUser("Unit Test", "", 100.00)};
var products = new List<Product> {createTestProduct(TEST_PRODUCT_ID, "MyProduct", 5.00, 1)};
var dataManager = new DataManager(users, products);
store = new Store(users.ToList().First(), dataManager);
return TEST_PRODUCT_ID;
}

// THE BELOW CODE IS REQUIRED TO PREVENT THE TESTS FROM MODIFYING THE USERS/PRODUCTS ON FILE
// This is not a good unit testing pattern - the unit test dependency on the file system should
Expand Down
1 change: 1 addition & 0 deletions UnitTestProject/UnitTestProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="AuthenticatorTests.cs" />
<Compile Include="StoreTests.cs" />
<Compile Include="IntegrationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down