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
8 changes: 8 additions & 0 deletions Refactoring/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ public class Product
public double Price;
[JsonProperty("Quantity")]
public int Quantity;

public Product(string Id, string Name, double Price, int Quantity)
{
this.Id = Id;
this.Name = Name;
this.Price = Price;
this.Quantity = Quantity;
}
}
}
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
7 changes: 7 additions & 0 deletions Refactoring/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@ public class User
public string Password;
[JsonProperty("Balance")]
public double Balance;

public User(string Name, string Password, double Balance)
{
this.Name = Name;
this.Password = Password;
this.Balance = Balance;
}
}
}
176 changes: 176 additions & 0 deletions UnitTestProject/AuthenticatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using NUnit.Framework;
using Refactoring;
using System.Collections.Generic;

namespace UnitTestProject
{
[TestFixture]
class AuthenticatorTests
{
[Test]
public void Test_EmptyUserList()
{
//Arrange
List<User> users = new List<User>();
Authenticator authenticator = new Authenticator(users);

//Act
User authenticatedUser = authenticator.Authenticate("username", "password");

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

[Test]
public void Test_ValidUser()
{
//Arrange
User user = new User("username", "password", 0);
List<User> users = new List<User>();
users.Add(user);

Authenticator authenticator = new Authenticator(users);

//Act
User authenticatedUser = authenticator.Authenticate("username", "password");

//Assert
Assert.NotNull(authenticatedUser);
Assert.AreEqual("username", authenticatedUser.Name);
}

[Test]
public void Test_MultipleUsers()
{
//Arrange
User user1 = new User("username1", "password1", 0);
User user2 = new User("username2", "password2", 0);
User user3 = new User("username3", "password3", 0);

List<User> users = new List<User>();
users.Add(user1);
users.Add(user2);
users.Add(user3);

Authenticator authenticator = new Authenticator(users);

//Act
User authenticatedUser = authenticator.Authenticate("username2", "password2");

//Assert
Assert.NotNull(authenticatedUser);
Assert.AreEqual("username2", authenticatedUser.Name);
}

[Test]
public void Test_DuplicateUser()
{
//Arrange
User user1 = new User("username", "password", 0);
User user2 = new User("username", "password", 0);

List<User> users = new List<User>();
users.Add(user1);
users.Add(user2);

Authenticator authenticator = new Authenticator(users);

//Act
User authenticatedUser = authenticator.Authenticate("username", "password");

//Assert
Assert.NotNull(authenticatedUser);
Assert.AreEqual("username", authenticatedUser.Name);
}

[Test]
public void Test_InvalidUsername()
{
//Arrange
User user = new User("username", "password", 0);
List<User> users = new List<User>();
users.Add(user);

Authenticator authenticator = new Authenticator(users);

//Act
User authenticatedUser = authenticator.Authenticate("wrong_username", "password");

//Assert
Assert.Null(authenticatedUser);
}

[Test]
public void Test_EmptyUsername()
{
//Arrange
User user = new User("username", "password", 0);
List<User> users = new List<User>();
users.Add(user);

Authenticator authenticator = new Authenticator(users);

//Act
User authenticatedUser = authenticator.Authenticate("", "password");

//Assert
Assert.Null(authenticatedUser);
}

[Test]
public void Test_NullUsername()
{
//Arrange
User user = new User("username", "password", 0);
List<User> users = new List<User>();
users.Add(user);

Authenticator authenticator = new Authenticator(users);

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

//Assert
Assert.Null(authenticatedUser);
}

[Test]
public void Test_InvalidPassword()
{
//Arrange
User user = new User("username", "password", 0);
List<User> users = new List<User>();
users.Add(user);

Authenticator authenticator = new Authenticator(users);

//Act
User authenticatedUser = authenticator.Authenticate("username", "wrong_password");

//Assert
Assert.Null(authenticatedUser);
}

[Test]
public void Test_NullPassword()
{
//Arrange
User user1 = new User("username1", "password1", 0);
User user2 = new User("username2", "password2", 0);

List<User> users = new List<User>();
users.Add(user1);
users.Add(user2);

Authenticator authenticator = new Authenticator(users);

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

//Assert
Assert.NotNull(authenticatedUser);
Assert.AreEqual("username2", authenticatedUser.Name);
}
}
}
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
86 changes: 50 additions & 36 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,47 @@ namespace UnitTestProject
[TestFixture]
class StoreTests
{
private User createTestUser(string name, string password, double balance)
{
User testUser = new User();
testUser.Name = name;
testUser.Password = password;
testUser.Balance = balance;

return testUser;
}
const string TEST_PRODUCT_ID = "Test Product ID";
const string TEST_PRODUCT_NAME = "Test Product Name";
const string TEST_USER_NAME = "Test User";
const string TEST_USER_PASSWORD = "";

private Product createTestProduct(string id, string name, double price, int quantity)
class SingleProductSingleUserStoreTestHelper
{
Product testProduct = new Product();
testProduct.Id = id;
testProduct.Name = name;
testProduct.Price = price;
testProduct.Quantity = quantity;

return testProduct;
private List<User> users;
private List<Product> products;
private Store store;

public SingleProductSingleUserStoreTestHelper(double userBalance, double productPrice, int productQuantity)
{
users = new List<User>();
users.Add(new User(TEST_USER_NAME, TEST_USER_PASSWORD, userBalance));

products = new List<Product>();
products.Add(new Product(TEST_PRODUCT_ID, TEST_PRODUCT_NAME, productPrice, productQuantity));

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

public void purchase(int quantity)
{
store.Purchase(TEST_PRODUCT_ID, quantity);
}

public int getProductQuantity()
{
return products[0].Quantity;
}
}

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
//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 dataManager = new DataManager(users, products);
var store = new Store(users[0], dataManager);
SingleProductSingleUserStoreTestHelper helper = new SingleProductSingleUserStoreTestHelper(99.9, 9.99, 10);

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

//Assert
Assert.Pass("No assertion really necessary here");
Expand All @@ -60,36 +63,47 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
SingleProductSingleUserStoreTestHelper helper = new SingleProductSingleUserStoreTestHelper(99.9, 9.99, 10);

//Act
helper.purchase(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, helper.getProductQuantity());
}

[Test]
[ExpectedException(typeof(Refactoring.InsufficientFundsException))]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange
SingleProductSingleUserStoreTestHelper helper = new SingleProductSingleUserStoreTestHelper(1.00, 1.01, 1);

//Act

//Assert
helper.purchase(1);
}

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

//Act
SingleProductSingleUserStoreTestHelper helper = new SingleProductSingleUserStoreTestHelper(1.00, 1.01, 1);

//Assert
Assert.Throws<Refactoring.InsufficientFundsException>(
delegate { helper.purchase(1); });
}

[Test]
public void Test_PurchaseThrowsExceptionWhenProductIsOutOfStock()
{
//Arrange
SingleProductSingleUserStoreTestHelper helper = new SingleProductSingleUserStoreTestHelper(100.00, 1.00, 1);

//Assert
Assert.Throws<Refactoring.OutOfStockException>(
delegate { helper.purchase(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
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