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

[TestFixtureSetUp]
public void Initialize_Data()
{
users = new List<User>();
User testUser = new User();
testUser.Name = "Test User";
testUser.Password = "password";
testUser.Balance = 10.00;
users.Add(testUser);

authenticator = new Authenticator(users);
}

[Test]
public void Test_AuthenticateReturnsUserWithSuccessfulAuthenticate()
{
User returnUser;
//Act
returnUser = authenticator.Authenticate("Test User", "password");
//Assert
Assert.AreEqual(users[0], returnUser);
}

[Test]
public void Test_AuthenticateReturnsNullWithIncorrectName()
{
User returnUser;
//Act
returnUser = authenticator.Authenticate("User Test", "password");
//Assert
Assert.Null(returnUser);
}

[Test]
public void Test_AuthenticateReturnsNullWithWrongPassword()
{
User returnUser;
//Act
returnUser = authenticator.Authenticate("Test User", "");
//Assert
Assert.Null(returnUser);
}

[Test]
public void Test_AuthenticateReturnsNullWithNullName()
{
User returnUser;
//Act
returnUser = authenticator.Authenticate(null, "password");
//Assert
Assert.Null(returnUser);
}

[Test]
public void Test_AuthenticateReturnsUserWithNullPassword()
{
User returnUser;
//Act
returnUser = authenticator.Authenticate("Test User", null);
//Assert
Assert.AreEqual(users[0], returnUser);
}


// 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);
}
}
}
80 changes: 67 additions & 13 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,34 @@ private Product createTestProduct(string id, string name, double price, int quan
return testProduct;
}

private const string TEST_PRODUCT_ID = "1";
private List<User> users;
private List<Product> products;
private DataManager dataManager;
private Store store;

[SetUp]
public void Initialize_Data()
{
users = new List<User>();
products = new List<Product>();
}

private void CreateStore()
{
dataManager = new DataManager(users, products);
store = new Store(users[0], dataManager);
}

[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);
CreateStore();

//Act
store.Purchase(TEST_PRODUCT_ID, 10);
Expand All @@ -60,34 +74,74 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
users.Add(createTestUser("Test User", "", 99.99));

products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10));

CreateStore();

//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.AreEqual(1, products[0].Quantity);
}

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange
users.Add(createTestUser("Test User", "", 1.00));

//Act
products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10));

//Assert
CreateStore();

InsufficientFundsException expectedException = new InsufficientFundsException();

//Act
try
{
store.Purchase(TEST_PRODUCT_ID, 1);
Assert.Fail();
}
catch(Exception e)
{
Assert.AreEqual(expectedException.Message, e.Message);
}

}

[Test]
[ExpectedException(typeof(InsufficientFundsException))]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
{
//Arrange
users.Add(createTestUser("Test User", "", 1.00));

products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10));

CreateStore();

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

}

[Test]
[ExpectedException(typeof(OutOfStockException))]
public void Test_PurchaseThrowsExceptionWhenOutOfStock()
{
//Arrange
users.Add(createTestUser("Test User", "", 10.00));

products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.00, 0));

CreateStore();

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

//Assert
}


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