Exemplo n.º 1
0
        public void ReverseRelation_Generic()
        {
            Order order = new Order()
            {
                Customer = new Customer() {Name = "A"},
                OrderItems = new List<OrderItem>()
                {
                    new OrderItem() {Description = "X"},
                    new OrderItem() {Description = "X"},
                    new OrderItem() {Description = "X"},
                    new OrderItem() {Description = "X"},
                    new OrderItem() {Description = "X"},
                }
            };

            var originalOrder = order;

            DB.Orders.Insert(order, true);

            order = DB.Orders.Read(originalOrder.OrderID);

            Vx.LoadRelations(order, o => o.OrderItems);

            order.OrderItems.Should().HaveCount(5).And.OnlyContain(item => item.Order == order);
        }
Exemplo n.º 2
0
        public WithStandardTestData()
        {
            DB.CreateAllTables();
            DB.PurgeAll();

            Random rnd = new Random();

            Customer[] customers = new Customer[NUM_CUSTOMERS];
            Product[] products = new Product[NUM_PRODUCTS];

            for (int productIndex = 0; productIndex < NUM_PRODUCTS; productIndex++)
            {
                Product product = new Product()
                {
                    ProductID = "" + (char)('A' + productIndex),
                    Description = "Product " + (productIndex + 1),
                    MinQty = 1,
                    Price = (decimal) (rnd.Next(100, 20000) / 100.0)
                };

                DB.Products.Insert(product);

                products[productIndex] = product;
            }

            for (int customerIndex = 0; customerIndex < NUM_CUSTOMERS; customerIndex++)
            {
                Customer customer = new Customer
                {
                    Name = "Customer " + (customerIndex + 1)
                };

                customer.Save();

                customers[customerIndex] = customer;

                for (int orderIndex = 0; orderIndex < customerIndex%10; orderIndex++)
                {
                    var order = new Order
                    {
                        CustomerID = customers[customerIndex].CustomerID,
                        Remark = new string((char) ('A' + rnd.Next(0, 26)), 1),
                        OrderDate = DateTime.Now
                    };

                    order.Save();

                    for (int itemIndex = 0; itemIndex < customerIndex%5; itemIndex++)
                    {
                        var item = new OrderItem
                        {
                            OrderID = order.OrderID,

                            Description = "Item " + (orderIndex + 1) + "/" + (itemIndex + 1),
                            Qty = (short) rnd.Next(1, 10),
                            Price = (double) products[itemIndex].Price,
                            ProductID = itemIndex == 0 ? null : products[itemIndex].ProductID
                        };

                        item.Save();
                    }
                }
            }

            FIRST_CUSTOMERID = customers[0].CustomerID;
        }
Exemplo n.º 3
0
        public void ManyToOne()
        {
            Customer customer = new Customer { Name = "x" };

            customer.Save();

            SalesPerson salesPerson = new SalesPerson {Name = "Test"};

            salesPerson.Save();

            var order = new Order
            {
                SalesPersonID = null,
                CustomerID = customer.CustomerID
            };

            DB.Orders.Insert(order);

            int id = order.OrderID;

            order = DB.Orders.Read(id, o => o.Customer);

            Assert.AreEqual(order.Customer.CustomerID, customer.CustomerID);

            order.SalesPersonID = salesPerson.ID;
            order.Save();

            order = DB.Orders.Read(id, (o) => o.SalesPerson);

            Assert.AreEqual(salesPerson.ID, order.SalesPerson.ID);

            order.SalesPersonID = null;
            order.SalesPerson = null;
            order.Save();

            order = DB.Orders.Read(id, o => o.SalesPerson);

            Assert.IsNull(order.SalesPerson);
            Assert.IsNull(order.SalesPersonID);
        }
Exemplo n.º 4
0
        public void RandomCreation()
        {
            Random rnd = new Random();

            Customer cust = new Customer();
            cust.Name = "A";
            cust.Save();

            double total = 0.0;

            for (int i = 0; i < 5; i++)
            {
                Order order = new Order
                {
                    Customer = cust
                };

                order.Save();

                for (int j = 0; j < 20; j++)
                {
                    int qty = rnd.Next(1, 10);
                    double price = rnd.NextDouble() * 500.0;

                    OrderItem item = new OrderItem() { Description = "test", Qty = (short)qty, Price = price, OrderID = order.OrderID };

                    item.Save();

                    total += qty * price;
                }

            }

            var orders = DB.Orders.ToArray();

            Assert.AreEqual(5, orders.Length);

            double total2 = DB.OrderItems.Sum(item => item.Qty*item.Price);

            Assert.AreEqual(total, total2, 0.000001);

            foreach (Order order in orders)
            {
                Vx.LoadRelations(order, o => o.Customer, o => o.OrderItems);

                Assert.AreEqual(cust.CustomerID, order.Customer.CustomerID);
                Assert.AreEqual(20, order.OrderItems.Count);
                Assert.AreEqual(cust.Name, order.Customer.Name);

                DB.OrderItems.Delete(order.OrderItems.First());
            }

            total2 = DB.OrderItems.Sum(item => item.Qty * item.Price);

            total.Should().BeGreaterThan(total2);

            Assert.AreEqual(95, DB.OrderItems.Count());
        }
Exemplo n.º 5
0
        public void CreateWithRelation_ManyToOne_ByRelationObject_New()
        {
            Customer customer = new Customer() { Name = "me" };

            var order = new Order
            {
                Remark = "test",
                Customer = customer
            };

            Assert.IsTrue(order.Save(true));

            Order order2 = DB.Orders.Read(order.OrderID, o => o.Customer);

            Vx.LoadRelations(() => order.Customer);

            Assert.AreEqual(order2.Customer.Name, order.Customer.Name);
            Assert.AreEqual(order2.Customer.CustomerID, order.Customer.CustomerID);
            Assert.AreEqual(order2.Customer.CustomerID, order.CustomerID);
        }
Exemplo n.º 6
0
        public void CreateWithRelation_ManyToOne_ByID()
        {
            Customer customer = new Customer { Name = "A" };

            customer.Save();

            var order = new Order
            {
                Remark = "test",
                CustomerID = customer.CustomerID
            };

            Assert.IsTrue(order.Save());

            Order order2 = DB.Orders.Read(order.OrderID, o => o.Customer);

            Vx.LoadRelations(() => order.Customer);

            order2.Customer.Name.Should().Be(order.Customer.Name);
            order2.Customer.CustomerID.Should().Be(order.Customer.CustomerID);
            order2.Customer.CustomerID.Should().Be(order.CustomerID);
        }
Exemplo n.º 7
0
        public void CreateOrderWithNewItems()
        {
            Order order = new Order
            {
                Customer = new Customer
                {
                    Name = "test"
                },
                OrderItems = new List<OrderItem>
                {
                    new OrderItem {Description = "test", Qty = 5, Price = 200.0},
                    new OrderItem {Description = "test", Qty = 3, Price = 45.0}
                }
            };

            Assert.IsTrue(order.Save(true));

            order = DB.Orders.Read(order.OrderID, o => o.OrderItems);

            //double totalPrice = Convert.ToDouble(order.OrderItems.GetScalar("Qty * Price", CSAggregate.Sum));

            Assert.AreEqual(2, order.OrderItems.Count, "Order items not added");
            //Assert.AreEqual(1135.0, totalPrice, "Incorrect total amount");

            order.OrderItems.Add(new OrderItem { Description = "test", Qty = 2, Price = 1000.0 });

            Assert.IsTrue(order.Save(true));

            order = DB.Orders.Read(order.OrderID, o => o.OrderItems);

            //totalPrice = Convert.ToDouble(order.OrderItems.GetScalar("Qty * Price", CSAggregate.Sum));

            Assert.AreEqual(3, order.OrderItems.Count, "Order item not added");
            //Assert.AreEqual(3135.0, totalPrice, "Total price incorrect");

            /*
            order.OrderItems.DeleteAll();

            order = Order.Read(order.OrderID);

            Assert.AreEqual(0, order.OrderItems.Count, "Order items not deleted");

            Assert.IsTrue(order.Delete());
                */
        }
Exemplo n.º 8
0
        public void CreateOrderWithNewCustomer()
        {
            Customer customer = new Customer() {Name = "me"};

            customer.Save();

            var order = new Order
            {
                Remark = "test",
                CustomerID = customer.CustomerID
            };

            Assert.IsTrue(order.Save());

            Vx.LoadRelations(() => order.Customer);

            Order order2 = DB.Orders.Read(order.OrderID , o => o.Customer);

            Assert.AreEqual(order2.Customer.Name, order.Customer.Name);
            Assert.AreEqual(order2.Customer.CustomerID, order.Customer.CustomerID);
            Assert.AreEqual(order2.Customer.CustomerID, order.CustomerID);

            Vx.LoadRelations(() => order2.Customer.Orders);

            Assert.AreEqual(order2.Customer.Orders.First().CustomerID, order.CustomerID);
        }
Exemplo n.º 9
0
        public void CreateOrderWithExistingCustomer()
        {
            Customer cust = new Customer { Name = "A" };

            cust.Save();

            cust = DB.Customers.Read(cust.CustomerID);

            Order order = new Order();

            order.CustomerID = cust.CustomerID;

            Assert.IsTrue(order.Save());

            order = DB.Orders.Read(order.OrderID);

            Vx.LoadRelations(() => order.Customer);
            Vx.LoadRelations(() => order.Customer.Orders);

            Assert.AreEqual(order.Customer.Name, cust.Name);
            Assert.AreEqual(order.Customer.CustomerID, cust.CustomerID);
            Assert.AreEqual(order.CustomerID, cust.CustomerID);

            Assert.AreEqual((order.Customer.Orders.First()).CustomerID, cust.CustomerID);

            order.Customer.Name = "B";
            order.Customer.Save();

            order = DB.Orders.Read(order.OrderID);

            Vx.LoadRelations(() => order.Customer);

            Assert.AreEqual(order.CustomerID, cust.CustomerID);

            Assert.AreEqual("B", order.Customer.Name);
        }