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
37 changes: 37 additions & 0 deletions UnitTestProject/AuthenticatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Newtonsoft.Json;
using NUnit.Framework;
using Refactoring;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnitTestProject
{
[TestFixture]
public class AuthenticatorTests
{
private User createTestUser(string name, string password)
{
User testUser = new User();
testUser.Name = name;
testUser.Password = password;
return testUser;
}

[Test]
public void Test_Authenticate()
{
//Arrange
var users = new List<User>();
users.Add(createTestUser("UnitTest", "UnitTest"));

var authenticate = new Authenticator(users);
//Act
authenticate.Authenticate("UnitTest", string.Empty);
//Assert
Assert.IsFalse(users[0].Password ==string.Empty);
}
}
}
69 changes: 53 additions & 16 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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,21 +35,24 @@ private Product createTestProduct(string id, string name, double price, int quan
return testProduct;
}

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
private DataManager PreArrange(string userName, string userPassword, double userBalance, string productName, double productPrice, int productQty )
{
//Arrange
const string TEST_PRODUCT_ID = "1";

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

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

products.Add(createTestProduct(TEST_PRODUCT_ID, productName, productPrice, productQty));
var dataManager = new DataManager(users, products);
var store = new Store(users[0], dataManager);
return dataManager;
}


[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
//Arrange
var dataManager = PreArrange("Test User", "", 99.99, "Product", 9.99, 10);
var store = new Store(dataManager.Users[0], dataManager);
//Act
store.Purchase(TEST_PRODUCT_ID, 10);

Expand All @@ -60,12 +64,13 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange

var dataManager = PreArrange("Test User", "", 99.99, "Product", 9.99, 10);
var store = new Store(dataManager.Users[0], dataManager);
//Act

store.Purchase(TEST_PRODUCT_ID, 9);
//Assert
//(choose the appropriate statement(s))
//Assert.AreEqual(1, products[0].Quantity);
Assert.AreEqual(1, store.GetProductById(TEST_PRODUCT_ID).Quantity);
//Assert.AreSame(1, products[0].Quantity);
//Assert.IsTrue(products[0].Quantity == 1);
}
Expand All @@ -74,22 +79,54 @@ public void Test_PurchaseRemovesProductFromStore()
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange

var dataManager = PreArrange("Test User", "", 1.00, "Product", 1.01, 10);
var store = new Store(dataManager.Users[0], dataManager);
//Act

try
{
store.Purchase(TEST_PRODUCT_ID, 1);
}
catch(Exception e)
{
Assert.AreEqual("Exception of type 'Refactoring.InsufficientFundsException' was thrown.", e.Message);
}
//Assert
}

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
{
//Arrange

var dataManager = PreArrange("Test User", "", 1.00, "Product", 1.01, 10);
var store = new Store(dataManager.Users[0], dataManager);
//Act

double dBal = dataManager.Users[0].Balance - store.GetProductById(TEST_PRODUCT_ID).Price;
//Assert
Assert.IsFalse(dBal > 0);
}
[Test]
public void Test_StoreHasStockForPurchase()
{
//Arrange
var dataManager = PreArrange("Test User", "", 10.00, "Product", 0.55, 10);
var store = new Store(dataManager.Users[0], dataManager);
//Act
try
{
store.Purchase(TEST_PRODUCT_ID, 12);
}
catch (Exception e)
{
if (e.GetType().Name == typeof(OutOfStockException).Name)
Assert.IsFalse((store.GetProductById(TEST_PRODUCT_ID).Quantity - 12) > 0);
if ( e.GetType().Name ==typeof(InsufficientFundsException).Name)
Assert.IsFalse(dataManager.Users[0].Balance - store.GetProductById(TEST_PRODUCT_ID).Price * 12 > 0);

}
//Assert


}

// 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
2 changes: 2 additions & 0 deletions UnitTestProject/UnitTestProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -73,6 +74,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