From 0876b7ad61c3993a1892502c77db5e8dbb841b36 Mon Sep 17 00:00:00 2001 From: cwquick Date: Fri, 29 Apr 2016 13:48:01 -0500 Subject: [PATCH 1/6] Exercise #1 - Completing Test_PurchaseRemovesProductFromStore, fixing bug in Store.cs --- Refactoring/Store.cs | 2 +- UnitTestProject/StoreTests.cs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Refactoring/Store.cs b/Refactoring/Store.cs index d9a7c78..48fcad6 100644 --- a/Refactoring/Store.cs +++ b/Refactoring/Store.cs @@ -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); diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 9f0b866..678583f 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -60,14 +60,25 @@ public void Test_PurchaseThrowsNoErrorForValidFunds() public void Test_PurchaseRemovesProductFromStore() { //Arrange + const string TEST_PRODUCT_ID = "1"; + + var users = new List(); + users.Add(createTestUser("Test User", "", 99.99)); + + var products = new List(); + 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.AreEqual(1, products[0].Quantity); //Assert.AreSame(1, products[0].Quantity); - //Assert.IsTrue(products[0].Quantity == 1); + //Assert.IsTrue(products[0].Quantity == 1); Functional and correct, but the first assert is more appropriate } [Test] From 748813ff1bb9022b39a1587828b6c9a59fe7d225 Mon Sep 17 00:00:00 2001 From: cwquick Date: Fri, 29 Apr 2016 13:55:10 -0500 Subject: [PATCH 2/6] Exercise #2 - Exception Tests (two versions) --- UnitTestProject/StoreTests.cs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 678583f..6125ad7 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -82,23 +82,53 @@ public void Test_PurchaseRemovesProductFromStore() } [Test] + [ExpectedException(typeof(InsufficientFundsException))] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange + const string TEST_PRODUCT_ID = "1"; + + var users = new List(); + users.Add(createTestUser("Test User", "", 1.00)); + + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); //Act + store.Purchase(TEST_PRODUCT_ID, 1); //Assert + 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 + const string TEST_PRODUCT_ID = "1"; - //Act + var users = new List(); + users.Add(createTestUser("Test User", "", 1.00)); - //Assert + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); + + //Act + 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); + } } From 368aee928368ee0c7b1b9304eb9ad02308fe8663 Mon Sep 17 00:00:00 2001 From: cwquick Date: Fri, 29 Apr 2016 13:59:46 -0500 Subject: [PATCH 3/6] Exercise #3 - Add new unit test to fully test the Purchase() function in Store.cs ([Ignore] attribute in IntegrationTest has been commented out again for test execution completeness) --- UnitTestProject/StoreTests.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 6125ad7..69c05ba 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -132,6 +132,28 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() } + [Test] + [ExpectedException(typeof(OutOfStockException))] + public void Test_ProductOutOfStock() + { + //Arrange + const string TEST_PRODUCT_ID = "1"; + + var users = new List(); + users.Add(createTestUser("Test User", "", 100.00)); + + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 5, 10)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); + + //Act + 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 + } + + // 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. From 7a4852898958d12b729c4daab1a7b62727ba689b Mon Sep 17 00:00:00 2001 From: cwquick Date: Fri, 29 Apr 2016 14:07:45 -0500 Subject: [PATCH 4/6] Exercise #4 - Basic Refactoring (improvements to be added as time permits) --- UnitTestProject/StoreTests.cs | 94 ++++++++++++----------------------- 1 file changed, 31 insertions(+), 63 deletions(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 69c05ba..c8e7b45 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -13,6 +13,12 @@ namespace UnitTestProject [TestFixture] class StoreTests { + const string TEST_PRODUCT_ID = "1"; + List users; + List products; + DataManager dataManager; + Store store; + private User createTestUser(string name, string password, double balance) { User testUser = new User(); @@ -34,50 +40,35 @@ private Product createTestProduct(string id, string name, double price, int quan return testProduct; } - [Test] - public void Test_PurchaseThrowsNoErrorForValidFunds() + [SetUp] + public void Test_CodeSetup() { - //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); + // 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(); users.Add(createTestUser("Test User", "", 99.99)); - var products = new List(); + products = new List(); products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10)); + } - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + [Test] + public void Test_PurchaseThrowsNoErrorForValidFunds() + { + dataManager = new DataManager(users, products); + store = new Store(users[0], dataManager); - //Act store.Purchase(TEST_PRODUCT_ID, 10); - - //Assert Assert.Pass("No assertion really necessary here"); } [Test] public void Test_PurchaseRemovesProductFromStore() { - //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - users.Add(createTestUser("Test User", "", 99.99)); + dataManager = new DataManager(users, products); + store = new Store(users[0], dataManager); - var products = new List(); - 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); Functional and correct, but the first assert is more appropriate } @@ -85,41 +76,25 @@ public void Test_PurchaseRemovesProductFromStore() [ExpectedException(typeof(InsufficientFundsException))] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { - //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - users.Add(createTestUser("Test User", "", 1.00)); - - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10)); + users[0].Balance = 1.00; + products[0].Price = 1.01; - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + dataManager = new DataManager(users, products); + store = new Store(users[0], dataManager); - //Act store.Purchase(TEST_PRODUCT_ID, 1); - - //Assert 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 - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - users.Add(createTestUser("Test User", "", 1.00)); - - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10)); + users[0].Balance = 1.00; + products[0].Price = 1.01; - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + dataManager = new DataManager(users, products); + store = new Store(users[0], dataManager); - //Act try { store.Purchase(TEST_PRODUCT_ID, 1); @@ -136,19 +111,12 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() [ExpectedException(typeof(OutOfStockException))] public void Test_ProductOutOfStock() { - //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - users.Add(createTestUser("Test User", "", 100.00)); - - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 5, 10)); + users[0].Balance = 100.00; + products[0].Price = 5; - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + dataManager = new DataManager(users, products); + store = new Store(users[0], dataManager); - //Act 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 } From a0f432ade8e9b70be8979489cccf38ea92baf6b7 Mon Sep 17 00:00:00 2001 From: cwquick Date: Fri, 29 Apr 2016 14:25:21 -0500 Subject: [PATCH 5/6] Exercise #5 - Add unit tests for Authenticator Class (may refactor and add additional if time permits) --- UnitTestProject/AuthenticatorTests.cs | 85 ++++++++++++++++++++++++++ UnitTestProject/UnitTestProject.csproj | 2 + 2 files changed, 87 insertions(+) create mode 100644 UnitTestProject/AuthenticatorTests.cs diff --git a/UnitTestProject/AuthenticatorTests.cs b/UnitTestProject/AuthenticatorTests.cs new file mode 100644 index 0000000..a654d72 --- /dev/null +++ b/UnitTestProject/AuthenticatorTests.cs @@ -0,0 +1,85 @@ +using System; +using NUnit.Framework; +using Refactoring; +using System.Collections.Generic; + +namespace UnitTestProject +{ + [TestFixture] + public class AuthenticatorTests + { + List 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(); + 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); + } + } +} diff --git a/UnitTestProject/UnitTestProject.csproj b/UnitTestProject/UnitTestProject.csproj index bcf3847..c695f54 100644 --- a/UnitTestProject/UnitTestProject.csproj +++ b/UnitTestProject/UnitTestProject.csproj @@ -35,6 +35,7 @@ 4 + ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll True @@ -73,6 +74,7 @@ + From 027951cf86c9a80b4d23d27ed6693b2604334253 Mon Sep 17 00:00:00 2001 From: cwquick Date: Fri, 29 Apr 2016 15:17:00 -0500 Subject: [PATCH 6/6] Slight formatting to Store instantiation --- UnitTestProject/StoreTests.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index c8e7b45..9cabcc9 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -40,6 +40,13 @@ private Product createTestProduct(string id, string name, double price, int quan return testProduct; } + private Store createStore(List users, List products) + { + DataManager dataManager = new DataManager(users, products); + Store store = new Store(users[0], dataManager); + return store; + } + [SetUp] public void Test_CodeSetup() { @@ -54,9 +61,7 @@ public void Test_CodeSetup() [Test] public void Test_PurchaseThrowsNoErrorForValidFunds() { - dataManager = new DataManager(users, products); - store = new Store(users[0], dataManager); - + store = createStore(users, products); store.Purchase(TEST_PRODUCT_ID, 10); Assert.Pass("No assertion really necessary here"); } @@ -64,9 +69,7 @@ public void Test_PurchaseThrowsNoErrorForValidFunds() [Test] public void Test_PurchaseRemovesProductFromStore() { - dataManager = new DataManager(users, products); - store = new Store(users[0], dataManager); - + 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 @@ -79,8 +82,7 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() users[0].Balance = 1.00; products[0].Price = 1.01; - dataManager = new DataManager(users, products); - store = new Store(users[0], dataManager); + store = createStore(users, products); 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 @@ -92,8 +94,7 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() users[0].Balance = 1.00; products[0].Price = 1.01; - dataManager = new DataManager(users, products); - store = new Store(users[0], dataManager); + store = createStore(users, products); try { @@ -114,8 +115,7 @@ public void Test_ProductOutOfStock() users[0].Balance = 100.00; products[0].Price = 5; - dataManager = new DataManager(users, products); - store = new Store(users[0], dataManager); + store = createStore(users, products); 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