From 085c579855c23246672d8d3928b626070f0e94de Mon Sep 17 00:00:00 2001 From: kderrett Date: Fri, 29 Apr 2016 13:57:41 -0500 Subject: [PATCH 1/6] Exercise 1 --- Refactoring/Store.cs | 2 +- UnitTestProject/StoreTests.cs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 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..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 93bcfdd1fdb96ae810884a0c9c2aae56115c4cf0 Mon Sep 17 00:00:00 2001 From: kderrett Date: Fri, 29 Apr 2016 14:38:07 -0500 Subject: [PATCH 2/6] Exercise 2 --- UnitTestProject/StoreTests.cs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 79ebfc1..aad6870 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -85,18 +85,49 @@ public void Test_PurchaseRemovesProductFromStore() public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange + const string TEST_PRODUCT_ID = "1"; + InsufficientFundsException expectedException = new InsufficientFundsException(); - //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(); + } + catch(Exception e) + { + Assert.AreEqual(expectedException.Message, e.Message); + } + } [Test] + [ExpectedException(typeof(InsufficientFundsException))] 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); //Act + store.Purchase(TEST_PRODUCT_ID, 1); //Assert } From 169d38f4f3374a5121cb412880c1aaa79bbbbfbe Mon Sep 17 00:00:00 2001 From: kderrett Date: Fri, 29 Apr 2016 14:45:58 -0500 Subject: [PATCH 3/6] Exercise 3 --- UnitTestProject/StoreTests.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index aad6870..b3ccb6b 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -129,7 +129,27 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() //Act store.Purchase(TEST_PRODUCT_ID, 1); - //Assert + } + + [Test] + [ExpectedException(typeof(OutOfStockException))] + public void Test_PurchaseThrowsExceptionWhenOutOfStock() + { + //Arrange + const string TEST_PRODUCT_ID = "1"; + + var users = new List(); + users.Add(createTestUser("Test User", "", 10.00)); + + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.00, 0)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); + + //Act + store.Purchase(TEST_PRODUCT_ID, 1); + } From 7f22494925681533879caaa0e72da839ac646c43 Mon Sep 17 00:00:00 2001 From: kderrett Date: Fri, 29 Apr 2016 15:03:27 -0500 Subject: [PATCH 4/6] Exercise 4 --- UnitTestProject/StoreTests.cs | 57 ++++++++++++++++------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index b3ccb6b..42f8099 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -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 users; + private List products; + private DataManager dataManager; + private Store store; + + [SetUp] + public void Initialize_Data() + { + users = new List(); + products = new List(); + } + + 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(); 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); + CreateStore(); //Act store.Purchase(TEST_PRODUCT_ID, 10); @@ -60,16 +74,11 @@ 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); + CreateStore(); //Act store.Purchase(TEST_PRODUCT_ID, 9); @@ -85,17 +94,13 @@ public void Test_PurchaseRemovesProductFromStore() public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange - const string TEST_PRODUCT_ID = "1"; - InsufficientFundsException expectedException = new InsufficientFundsException(); - - 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); + CreateStore(); + + InsufficientFundsException expectedException = new InsufficientFundsException(); //Act try @@ -115,16 +120,11 @@ 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); + CreateStore(); //Act store.Purchase(TEST_PRODUCT_ID, 1); @@ -136,16 +136,11 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() public void Test_PurchaseThrowsExceptionWhenOutOfStock() { //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); users.Add(createTestUser("Test User", "", 10.00)); - var products = new List(); products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.00, 0)); - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + CreateStore(); //Act store.Purchase(TEST_PRODUCT_ID, 1); From 999ea52c23ee685b3dc42d735a3f07c97030b82a Mon Sep 17 00:00:00 2001 From: kderrett Date: Fri, 29 Apr 2016 15:34:18 -0500 Subject: [PATCH 5/6] Exercise 5 --- UnitTestProject/StoreTests.cs | 3 --- UnitTestProject/UnitTestProject.csproj | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 42f8099..9b6df5c 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -84,10 +84,7 @@ public void Test_PurchaseRemovesProductFromStore() 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); } [Test] 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 b761c4cd4688a4a1b8f3c4271ba78bfd557c0ca4 Mon Sep 17 00:00:00 2001 From: kderrett Date: Fri, 29 Apr 2016 15:40:27 -0500 Subject: [PATCH 6/6] Added Test file --- UnitTestProject/AuthenticatorTests.cs | 112 ++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 UnitTestProject/AuthenticatorTests.cs diff --git a/UnitTestProject/AuthenticatorTests.cs b/UnitTestProject/AuthenticatorTests.cs new file mode 100644 index 0000000..bef19bf --- /dev/null +++ b/UnitTestProject/AuthenticatorTests.cs @@ -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 users; + private Authenticator authenticator; + + [TestFixtureSetUp] + public void Initialize_Data() + { + users = new List(); + 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 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); + } + } +}