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
85 changes: 85 additions & 0 deletions UnitTestProject/AuthenticatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using NUnit.Framework;
using Refactoring;
using System.Collections.Generic;

namespace UnitTestProject
{
[TestFixture]
public class AuthenticatorTests
{
List<User> users;
Authenticator authenticator;

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

return testUser;
}

[SetUp]
public void Test_CodeSetup()
{
users = new List<User>();
users.Add(createTestUser("Test User A", "pass123", 99.99));
users.Add(createTestUser("Test User B", "Password01", 99.99));
users.Add(createTestUser("Test User C", "password", 99.99));
users.Add(createTestUser("Test User D", "hunter2", 99.99));
users.Add(createTestUser("Test User E", "f8W&3M9v=", 99.99));

authenticator = new Authenticator(users);
}

[Test]
public void Test_NullOrEmptyUsername()
{
User result;
result = authenticator.Authenticate("", "password");
Assert.AreEqual(null, result);
}

[Test]
public void Test_ValidUsername()
{
User result;
result = authenticator.Authenticate("Test User D", "hunter2");
Assert.AreEqual(users[3], result);
}

[Test]
public void Test_CannotFindUserWithIncorrectUsername()
{
User result;
result = authenticator.Authenticate("Test User X", "pass123");
Assert.AreEqual(null, result);
}

[Test]
public void Test_CannotFindUserWithIncorrectPassword()
{
User result;
result = authenticator.Authenticate("Test User C", "nopass");
Assert.AreEqual(null, result);
}

[Test]
public void Test_FindUserByCredentials()
{
User result;
result = authenticator.Authenticate("Test User C", "password");
Assert.AreEqual(users[2], result);
}

[Test]
public void Test_FindUserByCredentialsNullPassword()
{
User result;
result = authenticator.Authenticate("Test User E", null);
Assert.AreEqual(users[4], result);
}
}
}
85 changes: 58 additions & 27 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace UnitTestProject
[TestFixture]
class StoreTests
{
const string TEST_PRODUCT_ID = "1";
List<User> users;
List<Product> products;
DataManager dataManager;
Store store;

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

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
private Store createStore(List<User> users, List<Product> products)
{
//Arrange
const string TEST_PRODUCT_ID = "1";
DataManager dataManager = new DataManager(users, products);
Store store = new Store(users[0], dataManager);
return store;
}

var users = new List<User>();
[SetUp]
public void Test_CodeSetup()
{
// Set up variables here for use when testing. Subsequent tests will alter the specfics of this basic configuration for their individual tests
users = new List<User>();
users.Add(createTestUser("Test User", "", 99.99));

var products = new List<Product>();
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
[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
store = createStore(users, products);
store.Purchase(TEST_PRODUCT_ID, 10);

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

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

//Act

//Assert
//(choose the appropriate statement(s))
//Assert.AreEqual(1, products[0].Quantity);
//Assert.AreSame(1, products[0].Quantity);
//Assert.IsTrue(products[0].Quantity == 1);
store = createStore(users, products);
store.Purchase(TEST_PRODUCT_ID, 9);
Assert.AreEqual(1, products[0].Quantity);
//Assert.IsTrue(products[0].Quantity == 1); Functional and correct, but the first assert is more appropriate
}

[Test]
[ExpectedException(typeof(InsufficientFundsException))]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange
users[0].Balance = 1.00;
products[0].Price = 1.01;

//Act
store = createStore(users, products);

//Assert
store.Purchase(TEST_PRODUCT_ID, 1);
Assert.Fail("InsufficientFundsException was not thrown"); // Should not reach this point due to the [ExpectedException] attribute on this test
}

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
{
//Arrange
users[0].Balance = 1.00;
products[0].Price = 1.01;

store = createStore(users, products);

try
{
store.Purchase(TEST_PRODUCT_ID, 1);
Assert.Fail("InsufficientFundsException was not thrown"); // Should not reach this point due to an invalid balance
}
catch (InsufficientFundsException ex)
{
Assert.True(ex is InsufficientFundsException);
}
}


[Test]
[ExpectedException(typeof(OutOfStockException))]
public void Test_ProductOutOfStock()
{
users[0].Balance = 100.00;
products[0].Price = 5;

//Act
store = createStore(users, products);

//Assert
store.Purchase(TEST_PRODUCT_ID, 12);
Assert.Fail("OutOfStockException was not thrown"); // Should not reach this point due to the [ExpectedException] attribute on this test
}


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