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
10 changes: 5 additions & 5 deletions Refactoring/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ public Store(User user, DataManager dataManager)
this.dataManager = dataManager;
}

public void Purchase(string productId, int quantity)
public void Purchase(string productId, int quantityPurchased)
{
Product product = this.GetProductById(productId);

if (!UserHasFundsForPurchase(product, quantity))
if (!UserHasFundsForPurchase(product, quantityPurchased))
{
throw new InsufficientFundsException();
}

if (!StoreHasStockForPurchase(product, quantity))
if (!StoreHasStockForPurchase(product, quantityPurchased))
{
throw new OutOfStockException();
}

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

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

namespace UnitTestProject
{
/// <summary>
/// Summary description for UnitTest1
/// </summary>
[TestFixture]
public class AuthenticatorTests
{
const string TEST_USER_NAME = "Bob";
const string TEST_USER_PASSWORD = "pass123";
const string TEST_USER_INCORRECT_PASSWORD = "totallynotthepassword";
const double ALL_TEST_USERS_BALANCE = 1.00;

private User createTestUser(string name, string password)
{
User testUser = new User();
testUser.Name = name;
testUser.Password = password;
testUser.Balance = ALL_TEST_USERS_BALANCE;

return testUser;
}

private List<User> CreateTestUserList(params string[] userNamesAndPasswords)
{
var userNameAndPasswordListCountIsOdd = (userNamesAndPasswords.Length % 2 == 1);

if (userNameAndPasswordListCountIsOdd)
{
throw new Exception("Test user list needs matching set of usernames and passwords.");
}

var userList = new List<User>();

for (int i = 0; i < userNamesAndPasswords.Length; i = i + 2)
{
var userName = userNamesAndPasswords[i];
var password = userNamesAndPasswords[i + 1];

userList.Add(createTestUser(userName, password));
}

return userList;
}

private Authenticator CreateTestAuthenticator()
{
var userList = CreateTestUserList("Sarah", "12345", TEST_USER_NAME, TEST_USER_PASSWORD, "John", "supersecretpassword", "Dog", "dogpassword2");

return new Authenticator(userList);
}

[Test]
public void Test_AuthenticationWorksWithCorrectPassword()
{
var authenticator = CreateTestAuthenticator();

var user = authenticator.Authenticate(TEST_USER_NAME, TEST_USER_PASSWORD);

Assert.IsNotNull(user);
Assert.AreEqual(TEST_USER_NAME, user.Name);
Assert.AreEqual(TEST_USER_PASSWORD, user.Password);
}

[Test]
public void Test_AuthenticationFailsWithIncorrectPassword()
{
var authenticator = CreateTestAuthenticator();

var user = authenticator.Authenticate(TEST_USER_NAME, TEST_USER_INCORRECT_PASSWORD);

Assert.IsNull(user);
}

[Test]
public void Test_AuthenticationFailsWithBlankUserName()
{
var authenticator = CreateTestAuthenticator();

var user = authenticator.Authenticate("", TEST_USER_PASSWORD);

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
54 changes: 33 additions & 21 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace UnitTestProject
[TestFixture]
class StoreTests
{
const string TEST_PRODUCT_ID = "1";

private User createTestUser(string name, string password, double balance)
{
User testUser = new User();
Expand All @@ -34,20 +36,23 @@ private Product createTestProduct(string id, string name, double price, int quan
return testProduct;
}

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
private Store CreateTestStore(double userBalance, double productPrice, int productQuantity)
{
//Arrange
const string TEST_PRODUCT_ID = "1";

var users = new List<User>();
users.Add(createTestUser("Test User", "", 99.99));
users.Add(createTestUser("Test User", "", userBalance));

var products = new List<Product>();
products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10));
products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", productPrice, productQuantity));

var dataManager = new DataManager(users, products);
var store = new Store(users[0], dataManager);

return new Store(users[0], dataManager);
}

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
var store = CreateTestStore(99.99, 9.99, 10);

//Act
store.Purchase(TEST_PRODUCT_ID, 10);
Expand All @@ -60,36 +65,44 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
var store = CreateTestStore(99.99, 9.99, 10);

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

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

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow_OneItem()
{
//Arrange
var store = CreateTestStore(1.00, 1.01, 10);

//Act

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

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow_MultipleItems()
{
//Arrange
var store = CreateTestStore(5.00, 1.00, 10);

//Act

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

[Test]
public void Test_PurchaseThrowsExceptionWhenStoreDoesntHaveEnoughStock()
{
//Arrange
var store = CreateTestStore(99.99, 1.00, 10);

//Act & Assert
Assert.Throws<OutOfStockException>(() => store.Purchase(TEST_PRODUCT_ID, 11));
}

// 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 All @@ -107,7 +120,6 @@ public void Test_Initialize()
originalProducts = JsonConvert.DeserializeObject<List<Product>>(File.ReadAllText(@"Data/Products.json"));
}


[TearDown]
public void Test_Cleanup()
{
Expand Down
1 change: 1 addition & 0 deletions UnitTestProject/UnitTestProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="StoreTests.cs" />
<Compile Include="IntegrationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AuthenticatorTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Refactoring\Tusc.csproj">
Expand Down