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
1 change: 1 addition & 0 deletions Refactoring/InsufficientFundsException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ namespace Refactoring
[Serializable]
public class InsufficientFundsException : Exception
{
public static object i { get; set; }
}
}
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
74 changes: 74 additions & 0 deletions UnitTestProject/AuthenticatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using Refactoring;
using NUnit.Framework;

namespace UnitTestProject
{
[TestFixture]
public class AuthenticatorTests
{
#region Variables
private const string FirstUserName = "John";
private const string FirstUserPassword = "";

private const string EmptyUserName = "";
private const string EmptyUserPassword = "";
#endregion Variables

#region TestSetUp
private User CreateEmptyTestUser()
{
User testUser = new User();
testUser.Name = EmptyUserName;
testUser.Password = EmptyUserPassword;

return testUser;
}

private User CreateFirstTestUser()
{
User testUser = new User();
testUser.Name = FirstUserName;
testUser.Password = FirstUserPassword;

return testUser;
}

#endregion TestSetUp

#region TestMethod
[Test]
public void Test_NullUserName()
{
//Assign
var users = new List<User>();
users.Add(CreateEmptyTestUser());

//Act
var auth = new Authenticator(users);

//Assert
Assert.IsNull(auth.Authenticate(users[0].Name, users[0].Password));

}

[Test]
public void Test_ContainUserByUserName()
{
//Assign
var users = new List<User>();
users.Add(CreateFirstTestUser());

//Act
var auth = new Authenticator(users);

//Assert
Assert.AreEqual(FirstUserName, users[0].Name);

}
#endregion TestMethod

}
}
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
156 changes: 143 additions & 13 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ namespace UnitTestProject
[TestFixture]
class StoreTests
{
#region Variables
private const string FirstTestPrdId = "1";
private const string FirstTestPrdName = "Coke";
private const double FirstTestPrdPrice = 1.01;
private const int FirstTestPrdQty = 10;

private const string FirstUserName = "John";
private const string FirstUserPassword = "";
private const double FirstUserBalance = 99.99;


private const string SecondTestPrdID = "2";
private const string SecondTestPrdName = "Ice Tea";
private const double SecondTestPrdPrice = 1.50;
private const int SecondTestPrdQty = 5;

User _firstUser = new User();
Product _firstProduct = new Product();
Product _secondProduct = new Product();
#endregion Variables


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

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
private Store setUpFirstUserFirstProductPurchase()
{

var users = new List<User>();
_firstUser = createTestUser(FirstUserName, FirstUserPassword, FirstUserBalance);
users.Add(_firstUser);

var products = new List<Product>();
_firstProduct = createTestProduct(FirstTestPrdId, FirstTestPrdName, FirstTestPrdPrice, FirstTestPrdQty);
products.Add(_firstProduct);

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

}

private Store setUpTwoProducts()
{
var users = new List<User>();
users.Add(createTestUser("Test User", "", 99.99));
_firstUser = createTestUser(FirstUserName, FirstUserPassword, FirstUserBalance);
users.Add(_firstUser);

var products = new List<Product>();
products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10));
_firstProduct = createTestProduct(FirstTestPrdId, FirstTestPrdName, FirstTestPrdPrice, FirstTestPrdQty);
products.Add(_firstProduct);

_secondProduct = createTestProduct(SecondTestPrdID, SecondTestPrdName, SecondTestPrdPrice, SecondTestPrdQty);
products.Add(_secondProduct);

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

#endregion TestSetUp


#region Test
[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
//Arrange
int purchaseValidFunds = 1;
var store = setUpFirstUserFirstProductPurchase();

//Act
store.Purchase(TEST_PRODUCT_ID, 10);
store.Purchase(FirstTestPrdId, purchaseValidFunds);

//Assert
Assert.Pass("No assertion really necessary here");
Expand All @@ -60,36 +114,112 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
int removeQty = 4;
int equalCount = 6;
var store = setUpFirstUserFirstProductPurchase();

//Act

store.Purchase(FirstTestPrdId, removeQty);

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

[Test]
[ExpectedException(typeof(InsufficientFundsException))]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange

int purchaseInsufficientQty = 100;
var store = setUpFirstUserFirstProductPurchase();

//Act

//Assert
store.Purchase(FirstTestPrdId, purchaseInsufficientQty);

//Assert
}

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
{
//Arrange
int purchaseInsufficientQty = 100;
var store = setUpFirstUserFirstProductPurchase();

try
{
//Act
store.Purchase(FirstTestPrdId, purchaseInsufficientQty);

//Assert
Assert.Fail();
}
catch (Exception e)
{
Assert.IsTrue(e is InsufficientFundsException );
}
}

[Test]
public void Test_PurchaseThrowsExceptionWhenOutOfStock()
{
//Arrange
int purchaseInsufficientQty = 11;
var store = setUpFirstUserFirstProductPurchase();

try
{
//Act
store.Purchase(FirstTestPrdId, purchaseInsufficientQty);

//Assert
Assert.Fail();
}
catch (Exception e)
{
Assert.IsTrue(e is OutOfStockException);
}
}

[Test]
public void Test_NullWhenGetProductList()
{
//Arrange
var store = setUpFirstUserFirstProductPurchase();

Assert.NotNull(store.GetProductList());

}

[Test]
public void Test_ValidProductCount()
{
//Arrange
int equalCount = 2;

var store = setUpTwoProducts();

Assert.AreEqual(equalCount, store.NumberOfProducts());

}

[Test]
public void Test_ContainsProductByProductID()
{
//Arrange
var store = setUpTwoProducts();

//Act

//Assert
Assert.IsTrue(store.ContainsProduct(_firstProduct.Id));

}

#endregion Test

// 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