From 32ea6c201feb93976113d64c581ada66545bd411 Mon Sep 17 00:00:00 2001 From: dmcconnell37 Date: Fri, 29 Apr 2016 14:04:37 -0500 Subject: [PATCH 1/5] Exercise #1 --- Refactoring/Store.cs | 2 +- UnitTestProject/IntegrationTests.cs | 2 +- UnitTestProject/StoreTests.cs | 13 ++++++++++++- 3 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/IntegrationTests.cs b/UnitTestProject/IntegrationTests.cs index f526b8a..ddb4fb9 100644 --- a/UnitTestProject/IntegrationTests.cs +++ b/UnitTestProject/IntegrationTests.cs @@ -10,7 +10,7 @@ namespace UnitTestProject { [TestFixture] - //[Ignore("Disable integration tests")] + [Ignore("Disable integration tests")] public class IntegrationTests { private List users; diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 9f0b866..79ebfc1 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -60,12 +60,23 @@ 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); } From 88d6118f6736bb2a283d1403e7b9969548f7cdeb Mon Sep 17 00:00:00 2001 From: dmcconnell37 Date: Fri, 29 Apr 2016 14:38:46 -0500 Subject: [PATCH 2/5] Exercise #2 --- UnitTestProject/StoreTests.cs | 39 ++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 79ebfc1..ebf7af9 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -82,22 +82,59 @@ 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.Pass("No assertion really necessary here"); } [Test] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() { //Arrange + const string TEST_PRODUCT_ID = "1"; - //Act + 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 + try + { + store.Purchase(TEST_PRODUCT_ID, 1); + Assert.Fail(); + } + catch (Refactoring.InsufficientFundsException e) + { + //Assert.Throws(e, InsufficientFundsException); + //Assert.Throws(e); + Assert.IsTrue(e is Refactoring.InsufficientFundsException); + } + catch (SystemException se) + { + Assert.Fail("Unexpection exception thrown of type " + se.GetType()); + } //Assert } From 071f7996b40c8ab3a236d8c1cd843eabad8bb48c Mon Sep 17 00:00:00 2001 From: dmcconnell37 Date: Fri, 29 Apr 2016 14:42:48 -0500 Subject: [PATCH 3/5] Exercise #3 --- UnitTestProject/StoreTests.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index ebf7af9..d01409f 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -139,6 +139,29 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() } + [Test] + [ExpectedException(typeof(OutOfStockException))] + public void Test_PurchaseThrowsExceptionWhenNotEnoughStock() + { + //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", 1.01, 10)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); + + //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. From 458cd14931ad4350493e78f021bcfd7267b4b420 Mon Sep 17 00:00:00 2001 From: dmcconnell37 Date: Fri, 29 Apr 2016 15:23:18 -0500 Subject: [PATCH 4/5] Exercise #4 --- Refactoring/Store.cs | 8 ++++ UnitTestProject/StoreTests.cs | 71 +++++++++++------------------------ 2 files changed, 29 insertions(+), 50 deletions(-) diff --git a/Refactoring/Store.cs b/Refactoring/Store.cs index 48fcad6..f6a80b1 100644 --- a/Refactoring/Store.cs +++ b/Refactoring/Store.cs @@ -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; diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index d01409f..a1f27e0 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -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(); @@ -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(); - users.Add(createTestUser("Test User", "", 99.99)); + users.Add(createTestUser("Test User", "", userBalance)); var products = new List(); - 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); @@ -60,23 +66,15 @@ 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); + 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, products[0].Quantity); + Assert.AreEqual(1, store.GetProductQuantity(TEST_PRODUCT_ID)); //Assert.AreSame(1, products[0].Quantity); //Assert.IsTrue(products[0].Quantity == 1); } @@ -86,19 +84,10 @@ public void Test_PurchaseRemovesProductFromStore() 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); + var store = ArrangeForTesting(99.99, 9.99, 10); //Act - store.Purchase(TEST_PRODUCT_ID, 1); + store.Purchase(TEST_PRODUCT_ID, 11); //Assert Assert.Pass("No assertion really necessary here"); @@ -108,21 +97,12 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() 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)); - - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + var store = ArrangeForTesting(99.99, 9.99, 10); //Act try { - store.Purchase(TEST_PRODUCT_ID, 1); + store.Purchase(TEST_PRODUCT_ID, 11); Assert.Fail(); } catch (Refactoring.InsufficientFundsException e) @@ -144,16 +124,7 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() public void Test_PurchaseThrowsExceptionWhenNotEnoughStock() { //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", 1.01, 10)); - - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + var store = ArrangeForTesting(999.99, 9.99, 10); //Act store.Purchase(TEST_PRODUCT_ID, 11); From a7ad51deb3f9fd5343f94d8558bacaec18bdf8f8 Mon Sep 17 00:00:00 2001 From: dmcconnell37 Date: Fri, 29 Apr 2016 15:50:35 -0500 Subject: [PATCH 5/5] Exercise #5 --- UnitTestProject/AuthenticatorTest.cs | 177 +++++++++++++++++++++++++ UnitTestProject/UnitTestProject.csproj | 1 + 2 files changed, 178 insertions(+) create mode 100644 UnitTestProject/AuthenticatorTest.cs diff --git a/UnitTestProject/AuthenticatorTest.cs b/UnitTestProject/AuthenticatorTest.cs new file mode 100644 index 0000000..7a2bd0c --- /dev/null +++ b/UnitTestProject/AuthenticatorTest.cs @@ -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 ArrangeThreeUsers() + { + var users = new List(); + 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 originalUsers; + private List originalProducts; + + [SetUp] + public void Test_Initialize() + { + // Load users from data file + originalUsers = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Users.json")); + + // Load products from data file + originalProducts = JsonConvert.DeserializeObject>(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); + } + } + } diff --git a/UnitTestProject/UnitTestProject.csproj b/UnitTestProject/UnitTestProject.csproj index bcf3847..7c0385d 100644 --- a/UnitTestProject/UnitTestProject.csproj +++ b/UnitTestProject/UnitTestProject.csproj @@ -70,6 +70,7 @@ +