diff --git a/Refactoring/InsufficientFundsException.cs b/Refactoring/InsufficientFundsException.cs index 3d56609..716e8ba 100644 --- a/Refactoring/InsufficientFundsException.cs +++ b/Refactoring/InsufficientFundsException.cs @@ -9,5 +9,6 @@ namespace Refactoring [Serializable] public class InsufficientFundsException : Exception { + public static object i { get; set; } } } 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/AuthenticatorTests.cs b/UnitTestProject/AuthenticatorTests.cs new file mode 100644 index 0000000..8ec4cda --- /dev/null +++ b/UnitTestProject/AuthenticatorTests.cs @@ -0,0 +1,74 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using Refactoring; +using NUnit.Framework; + +namespace UnitTestProject +{ + [TestFixture] + public class AuthenticatorTests + { + #region Variables + private const string FirstUserName = "John"; + private const string FirstUserPassword = ""; + + private const string EmptyUserName = ""; + private const string EmptyUserPassword = ""; + #endregion Variables + + #region TestSetUp + private User CreateEmptyTestUser() + { + User testUser = new User(); + testUser.Name = EmptyUserName; + testUser.Password = EmptyUserPassword; + + return testUser; + } + + private User CreateFirstTestUser() + { + User testUser = new User(); + testUser.Name = FirstUserName; + testUser.Password = FirstUserPassword; + + return testUser; + } + + #endregion TestSetUp + + #region TestMethod + [Test] + public void Test_NullUserName() + { + //Assign + var users = new List(); + users.Add(CreateEmptyTestUser()); + + //Act + var auth = new Authenticator(users); + + //Assert + Assert.IsNull(auth.Authenticate(users[0].Name, users[0].Password)); + + } + + [Test] + public void Test_ContainUserByUserName() + { + //Assign + var users = new List(); + users.Add(CreateFirstTestUser()); + + //Act + var auth = new Authenticator(users); + + //Assert + Assert.AreEqual(FirstUserName, users[0].Name); + + } + #endregion TestMethod + + } +} 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..f5a9f49 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -13,6 +13,29 @@ namespace UnitTestProject [TestFixture] class StoreTests { + #region Variables + private const string FirstTestPrdId = "1"; + private const string FirstTestPrdName = "Coke"; + private const double FirstTestPrdPrice = 1.01; + private const int FirstTestPrdQty = 10; + + private const string FirstUserName = "John"; + private const string FirstUserPassword = ""; + private const double FirstUserBalance = 99.99; + + + private const string SecondTestPrdID = "2"; + private const string SecondTestPrdName = "Ice Tea"; + private const double SecondTestPrdPrice = 1.50; + private const int SecondTestPrdQty = 5; + + User _firstUser = new User(); + Product _firstProduct = new Product(); + Product _secondProduct = new Product(); + #endregion Variables + + + #region TestSetUp private User createTestUser(string name, string password, double balance) { User testUser = new User(); @@ -34,23 +57,54 @@ private Product createTestProduct(string id, string name, double price, int quan return testProduct; } - [Test] - public void Test_PurchaseThrowsNoErrorForValidFunds() - { - //Arrange - const string TEST_PRODUCT_ID = "1"; + private Store setUpFirstUserFirstProductPurchase() + { + + var users = new List(); + _firstUser = createTestUser(FirstUserName, FirstUserPassword, FirstUserBalance); + users.Add(_firstUser); + + var products = new List(); + _firstProduct = createTestProduct(FirstTestPrdId, FirstTestPrdName, FirstTestPrdPrice, FirstTestPrdQty); + products.Add(_firstProduct); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); + return store; + + } + private Store setUpTwoProducts() + { var users = new List(); - users.Add(createTestUser("Test User", "", 99.99)); + _firstUser = createTestUser(FirstUserName, FirstUserPassword, FirstUserBalance); + users.Add(_firstUser); var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10)); + _firstProduct = createTestProduct(FirstTestPrdId, FirstTestPrdName, FirstTestPrdPrice, FirstTestPrdQty); + products.Add(_firstProduct); + _secondProduct = createTestProduct(SecondTestPrdID, SecondTestPrdName, SecondTestPrdPrice, SecondTestPrdQty); + products.Add(_secondProduct); + var dataManager = new DataManager(users, products); var store = new Store(users[0], dataManager); + return store; + } + + #endregion TestSetUp + + + #region Test + [Test] + public void Test_PurchaseThrowsNoErrorForValidFunds() + { + //Arrange + int purchaseValidFunds = 1; + var store = setUpFirstUserFirstProductPurchase(); //Act - store.Purchase(TEST_PRODUCT_ID, 10); + store.Purchase(FirstTestPrdId, purchaseValidFunds); //Assert Assert.Pass("No assertion really necessary here"); @@ -60,36 +114,112 @@ public void Test_PurchaseThrowsNoErrorForValidFunds() public void Test_PurchaseRemovesProductFromStore() { //Arrange + int removeQty = 4; + int equalCount = 6; + var store = setUpFirstUserFirstProductPurchase(); //Act - + store.Purchase(FirstTestPrdId, removeQty); + //Assert //(choose the appropriate statement(s)) - //Assert.AreEqual(1, products[0].Quantity); + Assert.AreEqual(equalCount, _firstProduct.Quantity); //Assert.AreSame(1, products[0].Quantity); //Assert.IsTrue(products[0].Quantity == 1); } [Test] + [ExpectedException(typeof(InsufficientFundsException))] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange - + int purchaseInsufficientQty = 100; + var store = setUpFirstUserFirstProductPurchase(); + //Act - - //Assert + store.Purchase(FirstTestPrdId, purchaseInsufficientQty); + + //Assert } [Test] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() { //Arrange + int purchaseInsufficientQty = 100; + var store = setUpFirstUserFirstProductPurchase(); + + try + { + //Act + store.Purchase(FirstTestPrdId, purchaseInsufficientQty); + + //Assert + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsTrue(e is InsufficientFundsException ); + } + } + + [Test] + public void Test_PurchaseThrowsExceptionWhenOutOfStock() + { + //Arrange + int purchaseInsufficientQty = 11; + var store = setUpFirstUserFirstProductPurchase(); + + try + { + //Act + store.Purchase(FirstTestPrdId, purchaseInsufficientQty); + + //Assert + Assert.Fail(); + } + catch (Exception e) + { + Assert.IsTrue(e is OutOfStockException); + } + } + + [Test] + public void Test_NullWhenGetProductList() + { + //Arrange + var store = setUpFirstUserFirstProductPurchase(); + + Assert.NotNull(store.GetProductList()); + + } + + [Test] + public void Test_ValidProductCount() + { + //Arrange + int equalCount = 2; + + var store = setUpTwoProducts(); + + Assert.AreEqual(equalCount, store.NumberOfProducts()); + + } + + [Test] + public void Test_ContainsProductByProductID() + { + //Arrange + var store = setUpTwoProducts(); //Act //Assert + Assert.IsTrue(store.ContainsProduct(_firstProduct.Id)); + } + #endregion 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 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 @@ +