Beispiel #1
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());
        }
Beispiel #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;
        }
Beispiel #3
0
        public void OneToManyWithOptionalRelation()
        {
            Customer customer = new Customer { Name = "x" };

            customer.Save();

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

            salesPerson.Save();

            Order[] orders = new[]
            {
                new Order() { CustomerID = customer.CustomerID, OrderDate = DateTime.Today, SalesPersonID = null},
                new Order() { CustomerID = customer.CustomerID, OrderDate = DateTime.Today, SalesPersonID = salesPerson.ID}
            };

            foreach (var order in orders)
            {
                DB.Insert(order);
            }

            salesPerson = DB.SalesPeople.First();

            salesPerson.Orders.Count().Should().Be(1);
            salesPerson.Orders.First().OrderID.Should().Be(orders[1].OrderID);
        }
Beispiel #4
0
        public void ParallelTest1()
        {
            const int numThreads = 100;

            Task[] tasks = new Task[numThreads];

            List<string> failedList = new List<string>();
            Customer[] customers = new Customer[numThreads];
            List<Customer> createdCustomers = new List<Customer>();

            HashSet<int> ids = new HashSet<int>();

            for (int i = 0; i < numThreads; i++)
            {
                string name = "C" + (i + 1);

                tasks[i] = Task.Factory.StartNew(() =>
                {
                    Customer customer = new Customer { Name = name };

                    customer.Save();

                    if (customer.CustomerID == 0)
                        lock (failedList)
                            failedList.Add("CustomerID == 0");

                    lock (ids)
                    {
                        if (ids.Contains(customer.CustomerID))
                            failedList.Add("Dupicate CustomerID " + customer.CustomerID + " for " + customer.Name);

                        ids.Add(customer.CustomerID);
                    }

                    lock (createdCustomers)
                        createdCustomers.Add(customer);

                    var newCustomer = Vx.DataSet<Customer>().Read(customer.CustomerID);

                    if (customer.Name != newCustomer.Name)
                        lock (failedList)
                            failedList.Add(string.Format("Customer == ({0},{1})" + ", but should be ({2},{3})", newCustomer.CustomerID, newCustomer.Name, customer.CustomerID, customer.Name));

                });
            }

            foreach (var task in tasks)
            {
                task.Wait();
            }

            foreach (var fail in failedList)
            {
                Assert.Fail(fail);
            }

            createdCustomers.Count.Should().Be(numThreads);
        }
Beispiel #5
0
        public void DeleteSingleObject()
        {
            List<Customer> customers = new List<Customer>();

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

                customer.Save();

                customers.Add(customer);
            }

            DB.Customers.Delete(customers[5]);

            Assert.IsNull(DB.Customers.Read(customers[5].CustomerID));

            Assert.AreEqual(9,DB.Customers.Count());
        }
Beispiel #6
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);
        }
Beispiel #7
0
        public void CreateWithRelation_ManyToOne_ByRelationObject()
        {
            Customer customer = new Customer() { Name = "me" };

            customer.Save();

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

            Assert.IsTrue(order.Save());

            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);
        }
Beispiel #8
0
        public void DeleteMultipleObjects()
        {
            List<Customer> customers = new List<Customer>();

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

                customer.Save();

                customers.Add(customer);
            }

            DB.Customers.Delete(c => c.Name == "Customer 2" || c.Name == "Customer 4");

            Assert.IsNotNull(DB.Customers.Read(customers[0].CustomerID));
            Assert.IsNull(DB.Customers.Read(customers[1].CustomerID));
            Assert.IsNotNull(DB.Customers.Read(customers[2].CustomerID));
            Assert.IsNull(DB.Customers.Read(customers[3].CustomerID));

            Assert.AreEqual(8, DB.Customers.Count());
        }
Beispiel #9
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);
        }
Beispiel #10
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);
        }
Beispiel #11
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);
        }
Beispiel #12
0
        public void CreateAndUpdateSingleObject()
        {
            Customer customer = new Customer { Name = "A" };

            customer.Save();

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

            customer.Name = "B";
            customer.Save();

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

            Assert.AreEqual("B",customer.Name);
        }
Beispiel #13
0
        public void CreateAndReadSingleObject()
        {
            Customer customer = new Customer { Name = "A" };

            customer.Save();

            Assert.IsTrue(customer.CustomerID > 0);

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

            Assert.AreEqual("A",customer.Name);
        }