From 86bd90c13c2dbe648a750f74233d26b9747469c0 Mon Sep 17 00:00:00 2001 From: EricRecksiedler Date: Tue, 10 May 2016 10:04:58 -0500 Subject: [PATCH] Unit Test Training --- Refactoring/Authenticator.cs | 2 +- Refactoring/Store.cs | 2 +- UnitTestProject/AuthenticatorTests.cs | 121 +++++++++++++++++++++++++ UnitTestProject/IntegrationTests.cs | 2 +- UnitTestProject/StoreTests.cs | 67 ++++++++++---- UnitTestProject/UnitTestProject.csproj | 1 + 6 files changed, 176 insertions(+), 19 deletions(-) create mode 100644 UnitTestProject/AuthenticatorTests.cs diff --git a/Refactoring/Authenticator.cs b/Refactoring/Authenticator.cs index 30e40f6..2d5a21c 100644 --- a/Refactoring/Authenticator.cs +++ b/Refactoring/Authenticator.cs @@ -24,7 +24,7 @@ private User FindUserByCredentials(string username, string password) { if (IsValidUsername(username)) { - return users.FirstOrDefault(user => user.Name.Equals(username) && (password == null || user.Password.Equals(password))); + return users.FirstOrDefault(user => user.Name.Equals(username) && password != null && user.Password.Equals(password)); } else { 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..d570c0b --- /dev/null +++ b/UnitTestProject/AuthenticatorTests.cs @@ -0,0 +1,121 @@ +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 Authenticator authenticator; + + private User createTestUser(string name, string password, double balance) + { + User testUser = new User(); + testUser.Name = name; + testUser.Password = password; + testUser.Balance = balance; + + return testUser; + } + + private List CreateTestUserList() + { + var users = new List(); + users.Add(createTestUser("Test User", "TestPassWord", 5.01)); + users.Add(createTestUser("Test User2", "TestPassWord2", 0.00)); + return users; + } + + private Authenticator CreateAuthenticator(List users) + { + return new Authenticator(users); + } + + [Test] + public void Test_AuthenticateValidUser() + { + string userName = "Test User"; + string userPassword = "TestPassWord"; + + User user = authenticator.Authenticate(userName, userPassword); + Assert.IsNotNull(user); + Assert.AreEqual(userName, user.Name); + Assert.AreEqual(userPassword, user.Password); + } + + [Test] + public void Test_RejectInvalidUsername() + { + string userName = "Test User3"; + string userPassword = "TestPassWord"; + + User user = authenticator.Authenticate(userName, userPassword); + Assert.IsNull(user); + } + + [Test] + public void Test_RejectInvalidPassword() + { + string userName = "Test User2"; + string userPassword = "TestPassWord"; + + User user = authenticator.Authenticate(userName, userPassword); + Assert.IsNull(user); + } + + [Test] + public void Test_HandleNullInputs() + { + string userName = "Test User"; + string userPassword = null; + + User user = authenticator.Authenticate(userName, userPassword); + Assert.IsNull(user); + + userName = null; + userPassword = null; + + user = authenticator.Authenticate(userName, userPassword); + Assert.IsNull(user); + } + + // 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")); + + var users = CreateTestUserList(); + authenticator = CreateAuthenticator(users); + } + + + [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/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..7cb60d5 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(); @@ -23,6 +25,13 @@ private User createTestUser(string name, string password, double balance) return testUser; } + private List CreateTestUserList(double credit) + { + var users = new List(); + users.Add(createTestUser("Test User", "", credit)); + return users; + } + private Product createTestProduct(string id, string name, double price, int quantity) { Product testProduct = new Product(); @@ -34,20 +43,27 @@ private Product createTestProduct(string id, string name, double price, int quan return testProduct; } - [Test] - public void Test_PurchaseThrowsNoErrorForValidFunds() + private List CreateTestProductList(string productId, double price, int quantity) { - //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)); + products.Add(createTestProduct(productId, "Product", price, quantity)); + return products; + } + private static Store CreateStore(List users, List products) + { var dataManager = new DataManager(users, products); var store = new Store(users[0], dataManager); + return store; + } + + [Test] + public void Test_PurchaseThrowsNoErrorForValidFunds() + { + //Arrange + var users = CreateTestUserList(99.99); + var products = CreateTestProductList(TEST_PRODUCT_ID, 9.99, 10); + var store = CreateStore(users, products); //Act store.Purchase(TEST_PRODUCT_ID, 10); @@ -60,37 +76,56 @@ public void Test_PurchaseThrowsNoErrorForValidFunds() public void Test_PurchaseRemovesProductFromStore() { //Arrange + var users = CreateTestUserList(99.99); + var products = CreateTestProductList(TEST_PRODUCT_ID, 9.99, 10); + var store = CreateStore(users, products); //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.AreEqual(1, products[0].Quantity); } [Test] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange + var users = CreateTestUserList(1.00); + var products = CreateTestProductList(TEST_PRODUCT_ID, 1.01, 1); + var store = CreateStore(users, products); //Act + var ex = Assert.Throws(() =>store.Purchase(TEST_PRODUCT_ID, 1)); //Assert + Assert.AreEqual(1, products[0].Quantity); } [Test] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() { //Arrange + var users = CreateTestUserList(1.00); + var products = CreateTestProductList(TEST_PRODUCT_ID, 1.01, 1); + var store = CreateStore(users, products); - //Act - - //Assert + //Act & Assert + var ex = Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 1)); } + [Test] + public void Test_PurchaseThrowsExceptionWhenOutOfStock() + { + //Arrange + const string TEST_PRODUCT_ID = "1"; + var users = CreateTestUserList(5.00); + var products = CreateTestProductList(TEST_PRODUCT_ID, 1.01, 1); + var store = CreateStore(users, products); + //Act & Assert + var ex = Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 2)); + } // 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. 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 @@ +