From 7f7accb055bc61a4123f3c24e29550a67cfced80 Mon Sep 17 00:00:00 2001 From: KrisLamontagne Date: Thu, 28 Apr 2016 12:22:57 -0500 Subject: [PATCH 1/2] KL Unit Test --- Refactoring/Store.cs | 2 +- UnitTestProject/AuthenticatorTests.cs | 112 +++++++++++++++++++++++++ UnitTestProject/IntegrationTests.cs | 2 +- UnitTestProject/StoreTests.cs | 73 +++++++++++++++- UnitTestProject/UnitTestProject.csproj | 1 + 5 files changed, 184 insertions(+), 6 deletions(-) create mode 100644 UnitTestProject/AuthenticatorTests.cs 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..f9f7fb3 --- /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 User createTestUser(string name, string password, double balance) + { + User testUser = new User(); + testUser.Name = name; + testUser.Password = password; + testUser.Balance = balance; + + return testUser; + } + + [Test] + public void Test_FindValidUser() + { + User loggedInUser = null; + string userName = "Test User1"; + string userPassword = "123456"; + + var users = new List(); + users.Add(createTestUser(userName, userPassword, 99.99)); + + var authenticator = new Authenticator(users); + + loggedInUser = authenticator.Authenticate(userName, userPassword); + + Assert.AreEqual(userName, loggedInUser.Name); + } + + [Test] + public void Test_FindInValidUser() + { + User loggedInUser = null; + string userName = "Test User1"; + string userPassword = "123456"; + + var users = new List(); + users.Add(createTestUser(userName, userPassword, 99.99)); + + var authenticator = new Authenticator(users); + + loggedInUser = authenticator.Authenticate(userName + "1", userPassword); + + Assert.AreEqual(null, loggedInUser); + } + + [Test] + public void Test_FindValidUserWithInvalidPassword() + { + User loggedInUser = null; + string userName = "Test User1"; + string userPassword = "123456"; + + var users = new List(); + users.Add(createTestUser(userName, userPassword, 99.99)); + + var authenticator = new Authenticator(users); + + loggedInUser = authenticator.Authenticate(userName, userPassword + "123"); + + Assert.AreEqual(null, loggedInUser); + } + + [Test] + public void Test_ValidateNullPassword() + { + User loggedInUser = null; + string userName = "Test User1"; + string userPassword = "123456"; + + var users = new List(); + users.Add(createTestUser(userName, userPassword, 99.99)); + + var authenticator = new Authenticator(users); + + loggedInUser = authenticator.Authenticate(userName, null); + + Assert.AreEqual(null, loggedInUser.Name); + } + + [Test] + public void Test_ValidateNullUserName() + { + User loggedInUser = null; + string userName = "Test User1"; + string userPassword = "123456"; + + var users = new List(); + users.Add(createTestUser(userName, userPassword, 99.99)); + + var authenticator = new Authenticator(users); + + loggedInUser = authenticator.Authenticate(null, userPassword); + + Assert.AreEqual(null, loggedInUser); + } + + } +} 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..92f8351 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -60,12 +60,24 @@ 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); } @@ -74,20 +86,73 @@ public void Test_PurchaseRemovesProductFromStore() public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange + const string TEST_PRODUCT_ID = "1"; + InsufficientFundsException INSUFICIANT_FUNDS = 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.IsTrue(e is InsufficientFundsException); + } + } [Test] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() { //Arrange + const string TEST_PRODUCT_ID = "1"; + InsufficientFundsException INSUFICIANT_FUNDS = 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); //Act - //Assert + var ex = Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 1)); + Assert.IsTrue(ex is InsufficientFundsException); + } + + [Test] + public void Test_PurchaseThrowsExceptionWhenProductOutofStock() + { + //Arrange + const string TEST_PRODUCT_ID = "1"; + InsufficientFundsException INSUFICIANT_FUNDS = new InsufficientFundsException(); + + var users = new List(); + users.Add(createTestUser("Test User", "", 10.00)); + + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 0)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); + + //Act + + var ex = Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 1)); + Assert.IsTrue(ex is OutOfStockException); } 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 @@ + From 778842105833fd3dab1671d6ecb00f3edac423b3 Mon Sep 17 00:00:00 2001 From: KrisLamontagne Date: Thu, 28 Apr 2016 12:29:10 -0500 Subject: [PATCH 2/2] Attempt 2 --- UnitTestProject/AuthenticatorTests.cs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/UnitTestProject/AuthenticatorTests.cs b/UnitTestProject/AuthenticatorTests.cs index f9f7fb3..8d1ac43 100644 --- a/UnitTestProject/AuthenticatorTests.cs +++ b/UnitTestProject/AuthenticatorTests.cs @@ -74,22 +74,6 @@ public void Test_FindValidUserWithInvalidPassword() Assert.AreEqual(null, loggedInUser); } - [Test] - public void Test_ValidateNullPassword() - { - User loggedInUser = null; - string userName = "Test User1"; - string userPassword = "123456"; - - var users = new List(); - users.Add(createTestUser(userName, userPassword, 99.99)); - - var authenticator = new Authenticator(users); - - loggedInUser = authenticator.Authenticate(userName, null); - - Assert.AreEqual(null, loggedInUser.Name); - } [Test] public void Test_ValidateNullUserName()