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
10 changes: 9 additions & 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 Expand Up @@ -76,6 +76,14 @@ public Product GetProductById(string productId)
return dataManager.Products.FirstOrDefault(p => p.Id.Equals(productId));
}

public int GetProductQuantity(string productId)
{
return dataManager.Products.FirstOrDefault(p => p.Id.Equals(productId)).Quantity;
//Product product;
//product = GetProductById(productId);
//return product.Quantity;
}

public bool ContainsProduct(string productId)
{
return dataManager.Products.Count(p => p.Id.Equals(productId)) > 0;
Expand Down
177 changes: 177 additions & 0 deletions UnitTestProject/AuthenticatorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
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 AuthenticatorTest
{

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_CanAuthenicateWithValidUserAndPassword()
{
//Arrange
var users = ArrangeThreeUsers();

var authenticator = new Authenticator(users);

//Act
var user = authenticator.Authenticate("Larry", "nuck");

//
Assert.IsNotNull(user);

}

[Test]
public void Test_CanAuthenicateWithValidUserAndPassword2()
{
//Arrange
var users = ArrangeThreeUsers();

var authenticator = new Authenticator(users);

//Act
var user = authenticator.Authenticate("Curly", "Flowbee");

//
Assert.IsNotNull(user);

}

[Test]
public void Test_CanAuthenicateWithWrongCasingOnPassword()
{
//Arrange
var users = ArrangeThreeUsers();

var authenticator = new Authenticator(users);

//Act
var user = authenticator.Authenticate("Curly", "flowbee");

//
Assert.IsNull(user);

}

[Test]
public void Test_CannotAuthenicateWithInvalidPassword()
{
//Arrange
var users = ArrangeThreeUsers();

var authenticator = new Authenticator(users);

//Act
var user = authenticator.Authenticate("Larry", "muck");

//
Assert.IsNull(user);

}

[Test]
public void Test_CannotAuthenicateWithInvalidUser()
{
//Arrange
var users = ArrangeThreeUsers();

var authenticator = new Authenticator(users);

//Act
var user = authenticator.Authenticate("Garry", "nuck");

//
Assert.IsNull(user);

}

[Test]
public void Test_CannotAuthenicateNullUser()
{
//Arrange
var users = ArrangeThreeUsers();

var authenticator = new Authenticator(users);

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

//
Assert.IsNull(user);

}

[Test]
public void Test_CannotAuthenicateEmptyStringUser()
{
//Arrange
var users = ArrangeThreeUsers();

var authenticator = new Authenticator(users);

//Act
var user = authenticator.Authenticate("", "nuck");

//
Assert.IsNull(user);

}

private List<User> ArrangeThreeUsers()
{
var users = new List<User>();
users.Add(createTestUser("Larry", "nuck", 10.00));
users.Add(createTestUser("Curly", "Flowbee", 10.00));
users.Add(createTestUser("Moe", "zoink", 10.00));
return users;
}

// 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.
private List<User> originalUsers;
private List<Product> originalProducts;

[SetUp]
public void Test_Initialize()
{
// Load users from data file
originalUsers = JsonConvert.DeserializeObject<List<User>>(File.ReadAllText(@"Data/Users.json"));

// Load products from data file
originalProducts = JsonConvert.DeserializeObject<List<Product>>(File.ReadAllText(@"Data/Products.json"));
}


[TearDown]
public void Test_Cleanup()
{
// Restore users
string json = JsonConvert.SerializeObject(originalUsers, Formatting.Indented);
File.WriteAllText(@"Data/Users.json", json);

// Restore products
string json2 = JsonConvert.SerializeObject(originalProducts, Formatting.Indented);
File.WriteAllText(@"Data/Products.json", json2);
}
}
}
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
60 changes: 51 additions & 9 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
{
const string TEST_PRODUCT_ID = "1";

private User createTestUser(string name, string password, double balance)
{
User testUser = new User();
Expand All @@ -34,20 +36,24 @@ private Product createTestProduct(string id, string name, double price, int quan
return testProduct;
}

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
public Store ArrangeForTesting(double userBalance, double productCost, 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", productCost, productQuantity));

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

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
//Arrange
var store = ArrangeForTesting(99.99, 9.99, 10);

//Act
store.Purchase(TEST_PRODUCT_ID, 10);
Expand All @@ -60,37 +66,73 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
var store = ArrangeForTesting(99.99, 9.99, 10);

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

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

[Test]
[ExpectedException(typeof(InsufficientFundsException))]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange
var store = ArrangeForTesting(99.99, 9.99, 10);

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

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

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

var store = ArrangeForTesting(99.99, 9.99, 10);

//Act

try
{
store.Purchase(TEST_PRODUCT_ID, 11);
Assert.Fail();
}
catch (Refactoring.InsufficientFundsException e)
{
//Assert.Throws(e, InsufficientFundsException);
//Assert.Throws<InsufficientFundsException>(e);
Assert.IsTrue(e is Refactoring.InsufficientFundsException);
}
catch (SystemException se)
{
Assert.Fail("Unexpection exception thrown of type " + se.GetType());
}
//Assert
}


[Test]
[ExpectedException(typeof(OutOfStockException))]
public void Test_PurchaseThrowsExceptionWhenNotEnoughStock()
{
//Arrange
var store = ArrangeForTesting(999.99, 9.99, 10);

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

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

// 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="AuthenticatorTest.cs" />
<Compile Include="StoreTests.cs" />
<Compile Include="IntegrationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down