From 2442961fc3832de1ab175ec3bc3289b9473688c4 Mon Sep 17 00:00:00 2001 From: Chris Jeffers Date: Fri, 29 Apr 2016 14:03:10 -0500 Subject: [PATCH 1/5] 1 --- Refactoring/Store.cs | 2 +- UnitTestProject/IntegrationTests.cs | 2 +- UnitTestProject/StoreTests.cs | 15 ++++++++++++++- 3 files changed, 16 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..4134c08 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -60,12 +60,25 @@ public void Test_PurchaseThrowsNoErrorForValidFunds() public void Test_PurchaseRemovesProductFromStore() { //Arrange + const string TEST_PRODUCT_ID = "1"; + + var users = new List(); + var user = createTestUser("Test User", "", 99.99); + users.Add(user); + + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10)); + + DataManager dataManager = new DataManager(users, products); + Store store = new Store(createTestUser("test", "", 100.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 c89cee24c7a93fe44d74d6d75bc4ae76c6858414 Mon Sep 17 00:00:00 2001 From: Chris Jeffers Date: Fri, 29 Apr 2016 14:13:00 -0500 Subject: [PATCH 2/5] 2 --- UnitTestProject/StoreTests.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 4134c08..44e1366 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -70,7 +70,7 @@ public void Test_PurchaseRemovesProductFromStore() products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10)); DataManager dataManager = new DataManager(users, products); - Store store = new Store(createTestUser("test", "", 100.0), dataManager); + Store store = new Store(user, dataManager); //Act @@ -84,13 +84,23 @@ public void Test_PurchaseRemovesProductFromStore() } [Test] + [ExpectedException(typeof(InsufficientFundsException))] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange + const string TEST_PRODUCT_ID = "1"; - //Act + var users = new List(); + var user = createTestUser("Test User", "", 1.00); + users.Add(user); - //Assert + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10)); + + DataManager dataManager = new DataManager(users, products); + Store store = new Store(user, dataManager); + + store.Purchase(TEST_PRODUCT_ID, 1); } [Test] From 4292cc4d8f1c1b143021ef7e962da8986b5bdeaa Mon Sep 17 00:00:00 2001 From: Chris Jeffers Date: Fri, 29 Apr 2016 14:17:16 -0500 Subject: [PATCH 3/5] 3 --- UnitTestProject/StoreTests.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 44e1366..0c60770 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -113,6 +113,33 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() //Assert } + [Test] + [ExpectedException(typeof(OutOfStockException))] + public void Test_PurchaseThrowsOutOfStockException() + { + //Arrange + const string TEST_PRODUCT_ID = "1"; + + var users = new List(); + var user = createTestUser("Test User", "", 99.99); + users.Add(user); + + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 1)); + + DataManager dataManager = new DataManager(users, products); + Store store = new Store(user, dataManager); + + //Act + + store.Purchase(TEST_PRODUCT_ID, 2); + + //Assert + //(choose the appropriate statement(s)) + Assert.AreEqual(1, products[0].Quantity); + //Assert.AreSame(1, products[0].Quantity); + //Assert.IsTrue(products[0].Quantity == 1); + } // 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 From 01be5c116f3a2a0f6eed731f0fc9c5373720eb66 Mon Sep 17 00:00:00 2001 From: Chris Jeffers Date: Fri, 29 Apr 2016 15:11:06 -0500 Subject: [PATCH 4/5] 4 --- UnitTestProject/StoreTests.cs | 112 ++++++++++++---------------------- 1 file changed, 39 insertions(+), 73 deletions(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 0c60770..cd302ae 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -13,6 +13,38 @@ namespace UnitTestProject [TestFixture] class StoreTests { + private const String TEST_PRODUCT_ID = "1"; + + private Store createStore(double balance, double product_cost, int quantity) + { + var users = getUserInList("Test User", "", balance); + var products = getProductInList(TEST_PRODUCT_ID, "Product", product_cost, quantity); + + var dataManager = new DataManager(users, products); + return new Store(users[0], dataManager); + } + + private Store createStoreWithProducts(double balance, List products) + { + var users = getUserInList("Test User", "", balance); + var dataManager = new DataManager(users, products); + return new Store(users[0], dataManager); + } + + private List getUserInList(string name, string password, double balance) + { + var users = new List(); + users.Add(createTestUser("Test User", "", balance)); + return users; + } + + private List getProductInList(string id, string name, double price, int quantity) + { + var products = new List(); + products.Add(createTestProduct(id, "Product", price, quantity)); + return products; + } + private User createTestUser(string name, string password, double balance) { User testUser = new User(); @@ -37,108 +69,40 @@ private Product createTestProduct(string id, string name, double price, int quan [Test] public void Test_PurchaseThrowsNoErrorForValidFunds() { - //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - users.Add(createTestUser("Test User", "", 99.99)); + var store = createStore(99.9, 9, 10); - 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, 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(); - var user = createTestUser("Test User", "", 99.99); - users.Add(user); - - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10)); - - DataManager dataManager = new DataManager(users, products); - Store store = new Store(user, dataManager); - - //Act + var products = getProductInList(TEST_PRODUCT_ID, "Product", 9.99, 10); + Store store = createStoreWithProducts(99.99, products); 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] [ExpectedException(typeof(InsufficientFundsException))] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { - //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - var user = createTestUser("Test User", "", 1.00); - users.Add(user); - - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10)); - - DataManager dataManager = new DataManager(users, products); - Store store = new Store(user, dataManager); + var store = createStore(1.00, 1.01, 10); store.Purchase(TEST_PRODUCT_ID, 1); } - [Test] - public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() - { - //Arrange - - //Act - - //Assert - } - [Test] [ExpectedException(typeof(OutOfStockException))] public void Test_PurchaseThrowsOutOfStockException() { - //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - var user = createTestUser("Test User", "", 99.99); - users.Add(user); - - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 1)); - - DataManager dataManager = new DataManager(users, products); - Store store = new Store(user, dataManager); - - //Act + var store = createStore(99.99, 9.99, 1); store.Purchase(TEST_PRODUCT_ID, 2); - - //Assert - //(choose the appropriate statement(s)) - Assert.AreEqual(1, products[0].Quantity); - //Assert.AreSame(1, products[0].Quantity); - //Assert.IsTrue(products[0].Quantity == 1); } // THE BELOW CODE IS REQUIRED TO PREVENT THE TESTS FROM MODIFYING THE USERS/PRODUCTS ON FILE @@ -155,6 +119,8 @@ public void Test_Initialize() // Load products from data file originalProducts = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Products.json")); + + } From 5b1f4572fa7c79abaa84ba48cc8de5b91363dc8f Mon Sep 17 00:00:00 2001 From: Chris Jeffers Date: Fri, 29 Apr 2016 15:20:57 -0500 Subject: [PATCH 5/5] 5 --- Refactoring.sln | 7 ++-- UnitTestProject/AuthenticatorTests.cs | 52 ++++++++++++++++++++++++++ UnitTestProject/UnitTestProject.csproj | 1 + 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 UnitTestProject/AuthenticatorTests.cs diff --git a/Refactoring.sln b/Refactoring.sln index 94e9f0e..0a92aec 100644 --- a/Refactoring.sln +++ b/Refactoring.sln @@ -1,8 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.21005.1 -MinimumVisualStudioVersion = 10.0.40219.1 +# Visual Studio 2012 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tusc", "Refactoring\Tusc.csproj", "{2D3F5D0E-4A6B-44C0-8F63-8E95243AC028}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestProject", "UnitTestProject\UnitTestProject.csproj", "{1FAF99A1-CCBF-435D-B599-4DAE1DAD5105}" @@ -25,4 +23,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection EndGlobal diff --git a/UnitTestProject/AuthenticatorTests.cs b/UnitTestProject/AuthenticatorTests.cs new file mode 100644 index 0000000..f32ce6f --- /dev/null +++ b/UnitTestProject/AuthenticatorTests.cs @@ -0,0 +1,52 @@ +using Newtonsoft.Json; +using NUnit.Framework; +using Refactoring; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UnitTestProject +{ + [TestFixture] + class AuthenticatorTests + { + private User testUser; + private Authenticator authenticator; + + [Test] + public void Test_Authenticate_ShouldSucceed() + { + User user = authenticator.Authenticate("Chris", ""); + Assert.AreEqual(testUser, user); + } + + [Test] + public void Test_Authenticate_InvalidPassword() + { + User user = authenticator.Authenticate("Chris", "asdf"); + Assert.Null(user); + } + + [Test] + public void Test_Authenticate_NullUserName() + { + User user = authenticator.Authenticate(null, ""); + Assert.Null(user); + } + + [SetUp] + public void Test_Initialize() + { + List users = new List(); + testUser = new User(); + testUser.Name = "Chris"; + testUser.Password = ""; + testUser.Balance = 100; + users.Add(testUser); + + authenticator = new Authenticator(users); + } + } +} diff --git a/UnitTestProject/UnitTestProject.csproj b/UnitTestProject/UnitTestProject.csproj index bcf3847..1a9e337 100644 --- a/UnitTestProject/UnitTestProject.csproj +++ b/UnitTestProject/UnitTestProject.csproj @@ -70,6 +70,7 @@ +