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
79 changes: 79 additions & 0 deletions UnitTestProject/AuthenticatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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]
class AuthenticatorTests
{
private Authenticator Authenticator;

[SetUp]
public void Test_Initialize()
{
List<User> users = new List<User>(5);
users.Add(new User() { Name = "User1", Password = "PW1", Balance = 0.00 });
users.Add(new User() { Name = "User2", Password = "PW2", Balance = 5.00 });
users.Add(new User() { Name = "User3", Password = "PW3", Balance = 10.00 });
users.Add(new User() { Name = "User4", Password = "PW4", Balance = 20.00 });
users.Add(new User() { Name = "User5", Password = "PW5", Balance = 30.00 });

Authenticator = new Authenticator(users);
}

[Test]
public void AuthenticateNullUser()
{
Assert.IsNull(Authenticator.Authenticate(null, ""));
}

[Test]
public void AuthenticateNullPassword()
{
Assert.IsNull(Authenticator.Authenticate("", null));
}

[Test]
public void AuthenticateBadPassword()
{
Assert.IsNull(Authenticator.Authenticate("User1", ""));
}

[Test]
public void AuthenticateUserDoesntExist()
{
Assert.IsNull(Authenticator.Authenticate("User6", "PW6"));
}

[Test]
public void AuthenticateFirstUser()
{
var user = Authenticator.Authenticate("User1", "PW1");
Assert.IsNotNull(user);
Assert.AreEqual("User1", user.Name);
}

[Test]
public void AuthenticateLastUser()
{
var user = Authenticator.Authenticate("User5", "PW5");
Assert.IsNotNull(user);
Assert.AreEqual("User5", user.Name);
}

[Test]
public void AuthenticateMiddleUser()
{
var user = Authenticator.Authenticate("User3", "PW3");
Assert.IsNotNull(user);
Assert.AreEqual("User3", user.Name);
}
}
}
57 changes: 50 additions & 7 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,28 @@ private Product createTestProduct(string id, string name, double price, int quan
return testProduct;
}

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

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

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

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

return store;
}

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
var store = InitializeStore(TEST_PRODUCT_ID, 9.99, 10, 99.99);

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

Expand All @@ -60,22 +67,30 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
var store = InitializeStore(TEST_PRODUCT_ID, 9.99, 10, 99.99);
var product = store.GetProductById(TEST_PRODUCT_ID);

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

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

[Test]
[ExpectedException(typeof(InsufficientFundsException))]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
var store = InitializeStore(TEST_PRODUCT_ID, 1.01, 1, 1.00);

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

//Assert
}
Expand All @@ -84,10 +99,38 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
var store = InitializeStore(TEST_PRODUCT_ID, 1.01, 1, 1.00);

//Act
TestDelegate delegateThatThrows = new TestDelegate(() => store.Purchase(TEST_PRODUCT_ID, 1));

//Assert
Assert.Throws<InsufficientFundsException>(delegateThatThrows);
}

[Test]
[ExpectedException(typeof(OutOfStockException))]
public void Test_PurchaseThrowsExceptionWhenOutOfStock()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
var store = InitializeStore(TEST_PRODUCT_ID, 1.00, 0, 1.00);

//Act
store.Purchase(TEST_PRODUCT_ID, 1);
}

[Test]
[ExpectedException(typeof(OutOfStockException))]
public void Test_PurchaseThrowsExceptionWhenNotEnoughStock()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
var store = InitializeStore(TEST_PRODUCT_ID, 1.00, 2, 3.00);

//Act
store.Purchase(TEST_PRODUCT_ID, 3);
}


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