From fb8f74df726a2dda7d44809d50c45dd057a2eb2b Mon Sep 17 00:00:00 2001 From: etanvir Date: Fri, 29 Apr 2016 15:14:11 -0500 Subject: [PATCH 1/3] etanvir-Exercise#1 --- Refactoring/Store.cs | 2 +- UnitTestProject/StoreTests.cs | 14 +++++++++++++- 2 files changed, 14 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..87eceee 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -60,14 +60,26 @@ 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.AreSame(1, products[0].Quantity); - //Assert.IsTrue(products[0].Quantity == 1); + Assert.IsTrue(products[0].Quantity == 1); } [Test] From e42d662f60ddbe06eb3675e31199fae757634ca7 Mon Sep 17 00:00:00 2001 From: etanvir Date: Fri, 29 Apr 2016 15:25:32 -0500 Subject: [PATCH 2/3] etanvir-Exercise2 --- UnitTestProject/StoreTests.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 87eceee..82ffb54 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -86,20 +86,44 @@ 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); //Act + store.Purchase(TEST_PRODUCT_ID, 1); //Assert + Assert.GreaterOrEqual(users[0].Balance, products[0].Price); } [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)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); //Act + store.Purchase(TEST_PRODUCT_ID, 1); //Assert + Assert.LessOrEqual(products[0].Price, users[0].Balance); } From 92a4e2184af11f804b234f0179fcd2223c857120 Mon Sep 17 00:00:00 2001 From: etanvir Date: Fri, 29 Apr 2016 15:40:40 -0500 Subject: [PATCH 3/3] etanvir-Exercise3 --- UnitTestProject/IntegrationTests.cs | 2 +- UnitTestProject/StoreTests.cs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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 82ffb54..91ae8af 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -126,6 +126,28 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() Assert.LessOrEqual(products[0].Price, users[0].Balance); } + [Test] + public void Test_PurchaseThrowsExceptionWhenProductOutOfStock() + { + //Arrange + const string TEST_PRODUCT_ID = "1"; + const int qt = 2; + + var users = new List(); + users.Add(createTestUser("Test User", "", 100.00)); + + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.00, 1)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); + + //Act + store.Purchase(TEST_PRODUCT_ID, qt); + + //Assert + Assert.GreaterOrEqual(products[0].Quantity,qt); + } // 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