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
39 changes: 39 additions & 0 deletions UnitTestProject/AuthenticatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Newtonsoft.Json;
using NUnit.Framework;
using Refactoring;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;

namespace UnitTestProject
{
[TestFixture]
public class AuthenticatorTests
{
Authenticator authenticator;

[SetUp]
public void Test_Initialize()
{
var users = new List<User>();

var user = new User();
user.Name = "test";
user.Password = "password";

users.Add(user);

authenticator = new Authenticator(users);
}

[Test]
public void TestAuthenticate()
{
var user = authenticator.Authenticate("test", "password");

Assert.IsNotNull(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
144 changes: 135 additions & 9 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ private Product createTestProduct(string id, string name, double price, int quan
return testProduct;
}

private Store createStore(List<User> users, List<Product> products)
{
var dataManager = new DataManager(users, products);
return new Store(users[0], dataManager);
}

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
Expand All @@ -46,8 +52,7 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
var products = new List<Product>();
products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10));

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

//Act
store.Purchase(TEST_PRODUCT_ID, 10);
Expand All @@ -60,34 +65,155 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
const string TEST_PRODUCT_ID = "1";

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

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

var store = createStore(users, products);

//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, products[0].Quantity);
Assert.IsTrue(products[0].Quantity == 1);
}

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

//Act
var users = new List<User>();
users.Add(createTestUser("Test User", "", 1));

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

var store = createStore(users, products);

try
{
//Act
store.Purchase(TEST_PRODUCT_ID, 1);
Assert.Fail();
}
catch (InsufficientFundsException)
{
Assert.Pass();
}

//Assert
}

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

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

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

var store = createStore(users, products);

try
{
//Act
store.Purchase(TEST_PRODUCT_ID, 2);
Assert.Fail();
}
catch (InsufficientFundsException)
{
Assert.Pass();
}
}

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

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

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

var store = createStore(users, products);

try
{
//Act
store.Purchase(TEST_PRODUCT_ID, 1);
Assert.Fail();
}
catch (OutOfStockException)
{
Assert.Pass();
}
}

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

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

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

var store = createStore(users, products);

//Act
Assert.That(store.GetProductList().Contains("Type quit to exit the application"));
}

//Assert
[Test]
public void Test_NumberOfProducts()
{
//Arrange
const string TEST_PRODUCT_ID = "1";

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

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

var store = createStore(users, products);

//Act
Assert.That(store.NumberOfProducts() == 1);
}

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

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

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

var store = createStore(users, products);

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


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