Exemplo n.º 1
0
        public void FailedValidationAutoValidateTest()
        {
            var customerBus = new busCustomer()
            {
                // Validates on Save automatically
                AutoValidate = true
            };


            var custExisting = customerBus.Load(1);

            var cust = new Customer()
            {
                // create dupe values which should fail validation
                FirstName = custExisting.FirstName,
                LastName  = custExisting.LastName,
                Company   = custExisting.Company
            };

            cust = customerBus.NewEntity(cust);

            Assert.IsFalse(customerBus.Save());
            Assert.IsFalse(string.IsNullOrEmpty(customerBus.ErrorMessage));
            Console.WriteLine("Validation Failed (test passed): " + customerBus.ErrorMessage);
        }
Exemplo n.º 2
0
        public void AttachNewTest()
        {
            var cust = new Customer()
            {
                Company   = "East Wind Technologies",
                FirstName = "Jimmy",
                LastName  = "Ropain",
            };

            using (var custBus = new busCustomer())
            {
                custBus.Attach(cust, true);
                Assert.IsTrue(custBus.Save(), custBus.ErrorMessage);
            }

            int custId = cust.Id;

            // load new bus/context to force load from disk
            // otherwise load loads from cached context
            using (var custBus = new busCustomer())
            {
                Customer cust2 = custBus.Load(custId);
                Assert.IsNotNull(cust2, custBus.ErrorMessage);

                Assert.AreEqual(cust.Company, cust2.Company);

                Assert.IsTrue(custBus.Delete(custId), custBus.ErrorMessage);
            }

            Console.WriteLine(custId);
        }
Exemplo n.º 3
0
        public void AttachWithChildExistingTest()
        {
            var orderBo = new busOrder();
            var custBo  = new busCustomer(orderBo); // share context

            // same as this to share context
            //custBo.Context = orderBo.Context;

            var order = orderBo.NewEntity();

            order.OrderId = StringUtils.NewStringId();

            // this is easiest and most efficient - if you have a PK
            //order.CustomerPk = 1;

            // manually load customer instance from context
            var cust = custBo.Load(1);

            cust.Updated = DateTime.Now; // make a change

            // assign the customer
            order.Customer = cust;

            // add a new line item
            order.LineItems.Add(new LineItem()
            {
                Description = "Cool new Item",
                Price       = 40M,
                Sku         = "COOLNEW",
                Quantity    = 1,
                Total       = 40m
            });

            Assert.IsTrue(orderBo.Save(), orderBo.ErrorMessage);
        }
Exemplo n.º 4
0
        public void NewEntity1Test()
        {
            int      custId;
            Customer cust = null;

            using (var custBus = new busCustomer())
            {
                cust = custBus.NewEntity();

                cust.FirstName = "Oscar";
                cust.LastName  = "Grouch";
                cust.Company   = "Groucho Inc.";
                cust.Entered   = DateTime.Now;

                Assert.IsTrue(custBus.Save(), custBus.ErrorMessage);

                // cust.Id is updated after save operation
                custId = cust.Id;
            }

            // New bus object to ensure new context is used
            // and data is loaded from disk - normally not required.
            using (var custBus = new busCustomer())
            {
                // check to ensure record was created
                var cust2 = custBus.Load(custId);
                Assert.AreEqual(cust2.Company, cust.Company);

                // Remove the new record
                Assert.IsTrue(custBus.Delete(custId), custBus.ErrorMessage);
            }
        }
Exemplo n.º 5
0
        public void AttachExistingTest()
        {
            string newCompany = "West Wind " + DateTime.Now;
            int    custId     = 1;

            var cust = new Customer()
            {
                Id        = custId,
                Company   = newCompany,
                FirstName = "Ricky",
                LastName  = "Strahl",
                Address   = "33 Kaiea Place"
            };

            using (var custBo = new busCustomer())
            {
                custBo.Attach(cust);
                Assert.IsTrue(custBo.Save(), custBo.ErrorMessage);
            }

            // load new bus/context to force load from disk
            // otherwise load loads from cached context
            using (var custBus = new busCustomer())
            {
                Customer cust2 = custBus.Load(custId);
                Assert.IsNotNull(cust2, custBus.ErrorMessage);

                Assert.AreEqual(cust2.Company, newCompany);
            }
        }
Exemplo n.º 6
0
 public void LoadEntityTest()
 {
     using (var custBus = new busCustomer())
     {
         var cust = custBus.Load(1);
         Assert.IsNotNull(cust, custBus.ErrorMessage);
         Console.WriteLine(cust.Company);
         Assert.IsTrue(cust.Company.StartsWith("West Wind"));
     }
 }
        public void PassContextToConstructor()
        {
            var customerBus = new busCustomer();

            customerBus.CreateContext("WebStoreContext");

            var cust = customerBus.Load(1);

            Assert.IsNotNull(cust, customerBus.ErrorMessage);
        }
Exemplo n.º 8
0
        public void TestContextExecuteNonQuery()
        {
            var custBo = new busCustomer();

            int count = custBo.Context.Db.ExecuteNonQuery("update customers set updated = getDate() where Id=@0", 3);

            Console.WriteLine(count);
            Console.WriteLine(custBo.Context.Db.ErrorMessage);

            Assert.IsTrue(count > -1);
        }
Exemplo n.º 9
0
 public void LoadEntityTest()
 {
     using (var custBus = new busCustomer())
     {
         Customer cust = null;
         try
         {
             cust = custBus.Load(1);
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
         }
         Assert.IsNotNull(cust, custBus.ErrorMessage);
         Console.WriteLine(cust.Company);
         Assert.IsTrue(cust.Company.StartsWith("West Wind"));
     }
 }
        public void ExceptionHandling()
        {
            var customerBus = new busCustomer()
            {
                // Validates on Save automatically
                ErrorHandlingMode = ErrorHandlingModes.ThrowExecptions
            };

            try
            {
                var custList = customerBus.ExecuteList <Customer>("select * from customerss where company like '%Wind%'");
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error correctly thrown: " + customerBus.ErrorMessage);
                return;
            }

            Assert.IsTrue(false, "Exception should have fired and code should not get here.");
        }
        public void ExceptionHandling()
        {
            var customerBus = new busCustomer()
            {
                ThrowExceptions = true
            };

            try
            {
                // this should throw SqlException
                var custList = customerBus.ExecuteList <Customer>("select * from customerss where company like '%Wind%'");
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error correctly thrown: " + customerBus.ErrorMessage);
                return;
            }

            Assert.IsTrue(false, "Exception should have fired and code should not get here.");
        }
Exemplo n.º 12
0
        public void AttachExistingTest()
        {
            int      custId = 0;
            Customer cust;
            DateTime time = DateTime.Now;


            using (var custBo = new busCustomer())
            {
                // grab existing customer id we can update
                custId = custBo.Context.Customers
                         .Where(c => c.LastName == "Strahl")
                         .Select(c => c.Id)
                         .First();

                cust = new Customer()
                {
                    Id        = custId,
                    FirstName = "Ricky",
                    LastName  = "Strahl",
                    Company   = "West Wind",
                    Entered   = time,
                    Address   = "31 Kaiea Place"
                };

                custBo.Attach(cust);
                Assert.IsTrue(custBo.Save(), custBo.ErrorMessage);
            }

            using (var custBo2 = new busCustomer())
            {
                // load new bus/context to force load from disk
                // otherwise load loads from cached context
                Customer cust2 = custBo2.Load(custId);
                Assert.IsNotNull(cust2, custBo2.ErrorMessage);

                Assert.IsTrue(cust2.Entered.ToString() == cust.Entered.ToString());
            }
        }
Exemplo n.º 13
0
        public void UpdateEntityTest()
        {
            string newCompanyName = "West Wind " + DateTime.Now;

            // load and update first
            using (var custBus = new busCustomer())
            {
                var cust = custBus.Load(1);
                Assert.IsNotNull(cust);

                cust.Company = newCompanyName;
                Assert.IsTrue(custBus.Save(), custBus.ErrorMessage);
            }

            // reload from disk via new context and check for validity
            using (var custBus = new busCustomer())
            {
                // reload entity
                var cust = custBus.Load(1);
                Assert.IsNotNull(cust);
                Assert.AreEqual(newCompanyName, cust.Company);
            }
        }
Exemplo n.º 14
0
        public void NewEntityWithTransactionTest()
        {
            int custId;

            using (var custBus = new busCustomer())
            {
                var cust = new Customer()
                {
                    FirstName = "John",
                    LastName  = "Farrow 2",
                    Company   = "Faraway2 Travel",
                    LastOrder = DateTime.Now,
                    Address   = "111 adsasdasdasd"
                };

                // Attach cust to Context and fire
                // NewEntity hooks
                custBus.NewEntity(cust);

                Assert.IsTrue(custBus.Save(useTransactionScope: true), custBus.ErrorMessage);

                // cust.Id is updated after save operation
                custId = cust.Id;

                // Use a new bus object/context to force
                // reload from disk - existing context loads from memory
                // which doesn't test properly
                using (var custBus2 = new busCustomer())
                {
                    // load and compare
                    var cust2 = custBus2.Load(custId);
                    Assert.AreEqual(cust2.Company, cust.Company);
                    // Remove the new record
                    Assert.IsTrue(custBus2.Delete(custId), custBus2.ErrorMessage);
                }
            }
        }