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
113 changes: 113 additions & 0 deletions UnitTestProject/AuthenticatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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 List<User> users;

[TestFixtureSetUp]
public void Class_Initialize()
{
users = new List<User>();
users.Add(createTestUser("Test1", "", 10));
users.Add(createTestUser("Test2", "t2", 20));
}

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

return testUser;
}

[Test]
public void Test_InitializeWithoutError()
{
//Arrange
//Act
Authenticator authenticator = new Authenticator(users);

//Assert
Assert.Pass("No assertion really necessary here");
}

[Test]
public void Test_AuthenticateUserSuccessfully()
{
//Arrange
Authenticator authenticator = new Authenticator(users);

//Act
User user = authenticator.Authenticate("Test1", "");

//Assert
Assert.IsTrue(user.Name == "Test1");
}

[Test]
public void Test_AuthenticationFailedIfUsernameIncorrect()
{
//Arrange
Authenticator authenticator = new Authenticator(users);

//Act
User user = authenticator.Authenticate("Test3", "");

//Assert
Assert.IsTrue(user == null);
}

[Test]
public void Test_AuthenticateUserSuccessfullyWithCorrectPassword()
{
//Arrange
Authenticator authenticator = new Authenticator(users);

//Act
User user = authenticator.Authenticate("Test2", "t2");

//Assert
Assert.IsTrue(user.Name == "Test2");
}

[Test]
public void Test_AuthenticationFailedIfPasswordIncorrect()
{
//Arrange
Authenticator authenticator = new Authenticator(users);

//Act
User user = authenticator.Authenticate("Test2", "test22");

//Assert
Assert.IsTrue(user == null);
}

[Test]
public void Test_AuthenticationFailedIfUsernameNull()
{
//Arrange
Authenticator authenticator = new Authenticator(users);

//Act
User user = authenticator.Authenticate(null, "");

//Assert
Assert.IsTrue(user == null);
}

}
}
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
52 changes: 39 additions & 13 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
{
private readonly 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 createTestStoreWithSingleUserAndProduct(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()
{
//Arrange
var store = createTestStoreWithSingleUserAndProduct(99.99, 9.99, 10);

//Act
store.Purchase(TEST_PRODUCT_ID, 10);
Expand All @@ -60,37 +65,58 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
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 dataManager = new DataManager(users, products);
var store = new Store(users[0], dataManager);
//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.IsTrue(products[0].Quantity == 1);
}

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

var store = createTestStoreWithSingleUserAndProduct(1.00, 1.01, 10);

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

//Assert
Assert.Fail("InsufficientFundsException should have been thrown.");
}

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
{
//Arrange
var store = createTestStoreWithSingleUserAndProduct(1.00, 1.01, 10);

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

//Act
[Test]
public void Test_PurchaseThrowsExceptionWhenStockIsTooLow()
{
//Arrange
var store = createTestStoreWithSingleUserAndProduct(10.00, 1.00, 1);

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


// 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
// actually be broken ... training on how to do this will be coming.
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