using System; using System.Collections.Generic; using System.Linq; using System.Data.Linq; using System.Text; using NUnit.Framework; using Pariveda.DataAccess35.Business; using System.IO; using System.Collections; using System.Transactions; namespace Pariveda.DataAccess35.Test { [TestFixture] public class CustomerServiceTest { #region Test Initialization TransactionScope _transactionScope; /// /// Starts the transaction for all database activity /// Be sure the Distributed Transaction Coordinator Service is on or this won't work /// [SetUp] public void StartTransaction() { //Set the DataDirectory variable so that //the app.config can use the correct path to the database string dataDirectory = Path. GetFullPath(@"..\..\..\Pariveda.DataAccess35.Web\App_Data\"); AppDomain.CurrentDomain.SetData("DataDirectory", dataDirectory); _transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew); } /// /// Rollbacks the transaction for all database activity /// so the database remains unchanged /// [TearDown] public void RollbackTransaction() { _transactionScope.Dispose(); } #endregion Customer InsertCustomer() { CustomerService service = new CustomerService(); Customer customer = new Customer { CompanyName = "Accenture", EmailAddress = "info@accenture.com", FirstName = "Bob", LastName = "Jones", MiddleName = "Alan", Phone = "214-555-1212", SalesPerson = "Brian Orrell" }; customer.CustomerAddresses.Add(new CustomerAddress { AddressLine1 = "514 Newell Ave.", AddressLine2 = "Hollywood Heights", City = "Dallas", StateProvince = "TX", PostalCode = "75223" }); customer = service.Update(customer); return customer; } [Test] public void GetList_NonNullResult() { CustomerService service = new CustomerService(); IList customers = service.GetLookupList(); Assert.IsNotNull(customers); Assert.IsTrue(customers.Count > 0); } [Test] public void Get_NewlyInsertedCustomer_SameResult() { Customer customer = InsertCustomer(); CustomerService service = new CustomerService(); Customer newlyAddedCustomer = service.Get(customer.CustomerID, true); customer.ShouldEqual(newlyAddedCustomer); } [Test] public void Delete_NewlyInsertedCustomer_CustomerDeleted() { Customer customer = InsertCustomer(); CustomerService service = new CustomerService(); service.Delete(customer.CustomerID); Customer deletedCustomer = service.Get(customer.CustomerID, true); Assert.IsNull(deletedCustomer); } [Test] public void Delete_MultipleCustomers_AllAreDeleted() { List customerIds = new List(); for (int index = 0; index < 5; index++) { customerIds.Add(InsertCustomer().CustomerID); } CustomerService service = new CustomerService(); service.Delete(customerIds); foreach (int customerId in customerIds) { Customer customer = service.Get(customerId, true); Assert.IsNull(customer); } } [Test] public void Insert_SingleCustomer_DatabaseContainsNewCustomer() { Customer customer = InsertCustomer(); CustomerService service = new CustomerService(); Customer newCustomer = service.Get(customer.CustomerID, true); customer.ShouldEqual(newCustomer); } [Test] public void Update_UpdatedCompanyName_ModifiedDateIsLaterAndCompanyNameIsChanged() { Customer insertedCustomer = InsertCustomer(); CustomerService service = new CustomerService(); List customers = service.SearchByCompanyName(insertedCustomer.CompanyName); Assert.IsNotNull(customers); Assert.IsTrue(customers.Count > 0); Customer foundCustomer = customers[0]; foundCustomer.CompanyName += "X"; DateTime beforeUpdate = foundCustomer.ModifiedDate; foundCustomer = service.Update(foundCustomer); Assert.Greater(foundCustomer.ModifiedDate, beforeUpdate); Customer updatedCustomer = service.Get(foundCustomer.CustomerID, true); updatedCustomer.ShouldEqual(foundCustomer); } } static class CustomerTestExtensions { public static void ShouldEqual(this Customer customer1, Customer customerComparison) { Assert.AreEqual(customer1.CustomerID, customerComparison.CustomerID); Assert.AreEqual(customer1.CompanyName, customerComparison.CompanyName); Assert.AreEqual(customer1.FirstName, customerComparison.FirstName); Assert.AreEqual(customer1.LastName, customerComparison.LastName); Assert.AreEqual(customer1.MiddleName, customerComparison.MiddleName); Assert.AreEqual(customer1.EmailAddress, customerComparison.EmailAddress); Assert.AreEqual(customer1.SalesPerson, customerComparison.SalesPerson); customer1.CustomerAddresses.ShouldEqual(customerComparison.CustomerAddresses); } public static void ShouldEqual(this IEnumerable addresses, IEnumerable addressesComparison) { Assert.AreEqual(addresses.Count(), addressesComparison.Count()); foreach (CustomerAddress address in addresses) { CustomerAddress comparisonAddress = addressesComparison.SingleOrDefault(a => a.AddressID == address.AddressID); Assert.IsNotNull(comparisonAddress); address.ShouldEqual(comparisonAddress); } } public static void ShouldEqual(this CustomerAddress address, CustomerAddress addressComparison) { Assert.AreEqual(address.AddressLine1, addressComparison.AddressLine1); Assert.AreEqual(address.AddressLine2, addressComparison.AddressLine2); Assert.AreEqual(address.City, addressComparison.City); Assert.AreEqual(address.StateProvince, addressComparison.StateProvince); Assert.AreEqual(address.PostalCode, addressComparison.PostalCode); } } }