Esempio n. 1
0
        private void InsertSeedData()
        {
            var products = CreateProducts(_productCount, setPrimaryKeys: false);
            using (var context = new OrdersContext(_connectionString))
            {
                context.Products.AddRange(products);
                context.SaveChanges();
            }

            var customers = CreateCustomers(_customerCount, setPrimaryKeys: false);
            using (var context = new OrdersContext(_connectionString))
            {
                context.Customers.AddRange(customers);
                context.SaveChanges();
            }

            var orders = CreateOrders(customers, _ordersPerCustomer, setPrimaryKeys: false);
            using (var context = new OrdersContext(_connectionString))
            {
                context.Orders.AddRange(orders);
                context.SaveChanges();
            }

            var lines = CreateOrderLines(products, orders, _linesPerOrder, setPrimaryKeys: false);

            using (var context = new OrdersContext(_connectionString))
            {
                context.OrderLines.AddRange(lines);
                context.SaveChanges();
            }
        }
        public void ValueFromObject()
        {
            new TestDefinition
                {
                    TestName = "Query_Funcletization_ValueFromObject",
                    IterationCount = 50,
                    WarmupCount = 5,
                    Setup = EnsureDatabaseSetup,
                    Run = harness =>
                        {
                            using (var context = new OrdersContext(_connectionString))
                            {
                                using (harness.StartCollection())
                                {
                                    var valueHolder = new ValueHolder();
                                    for (var i = 0; i < _funcletizationIterationCount; i++)
                                    {
                                        var result = context.Products.Where(p => p.ProductId < valueHolder.SecondLevelProperty).ToList();

                                        Assert.Equal(10, result.Count);
                                    }
                                }
                            }
                        }
                }.RunTest();
        }
Esempio n. 3
0
        public void AddParents()
        {
            new TestDefinition
                {
                    TestName = "ChangeTracker_Fixup_AddParents",
                    IterationCount = 10,
                    WarmupCount = 5,
                    Run = harness =>
                        {
                            using (var context = new OrdersContext(_connectionString))
                            {
                                var customers = new List<Customer>();
                                for (var i = 0; i < 1000; i++)
                                {
                                    customers.Add(new Customer { CustomerId = i + 1 });
                                    context.Orders.Add(new Order { CustomerId = i + 1 });
                                }

                                foreach (var customer in customers)
                                {
                                    using (harness.StartCollection())
                                    {
                                        context.Customers.Add(customer);
                                    }

                                    Assert.Same(customer, customer.Orders.Single().Customer);
                                }
                            }
                        }
                }.RunTest();
        }
Esempio n. 4
0
        public void AddChildren()
        {
            new TestDefinition
                {
                    TestName = "ChangeTracker_Fixup_AddChildren",
                    IterationCount = 10,
                    WarmupCount = 5,
                    Setup = EnsureDatabaseSetup,
                    Run = harness =>
                        {
                            using (var context = new OrdersContext(_connectionString))
                            {
                                var customers = context.Customers.ToList();
                                Assert.Equal(1000, customers.Count);

                                foreach (var customer in customers)
                                {
                                    var order = new Order { CustomerId = customer.CustomerId };

                                    using (harness.StartCollection())
                                    {
                                        context.Orders.Add(order);
                                    }

                                    Assert.Same(order, order.Customer.Orders.Single());
                                }
                            }
                        }
                }.RunTest();
        }
        public void AddCollection()
        {
            new TestDefinition
                {
                    TestName = "ChangeTracker_DbSetOperation_AddCollection",
                    IterationCount = 100,
                    WarmupCount = 5,
                    Run = harness =>
                        {
                            using (var context = new OrdersContext(_connectionString))
                            {
                                var customers = new Customer[1000];
                                for (var i = 0; i < customers.Length; i++)
                                {
                                    customers[i] = new Customer { Name = "Customer " + i };
                                }

                                using (harness.StartCollection())
                                {
                                    context.Customers.AddRange(customers);
                                }
                            }
                        }
                }.RunTest();
        }
        public void Attach()
        {
            new TestDefinition
                {
                    TestName = "ChangeTracker_DbSetOperation_Attach",
                    IterationCount = 100,
                    WarmupCount = 5,
                    Setup = EnsureDatabaseSetup,
                    Run = harness =>
                        {
                            using (var context = new OrdersContext(_connectionString))
                            {
                                var customers = GetAllCustomersFromDatabase();
                                Assert.Equal(1000, customers.Length);

                                using (harness.StartCollection())
                                {
                                    foreach (var customer in customers)
                                    {
                                        context.Customers.Attach(customer);
                                    }
                                }
                            }
                        }
                }.RunTest();
        }
        public void SameQueryInstance()
        {
            new TestDefinition
                {
                    TestName = "Query_Funcletization_SameQueryInstance",
                    IterationCount = 50,
                    WarmupCount = 5,
                    Setup = EnsureDatabaseSetup,
                    Run = harness =>
                        {
                            using (var context = new OrdersContext(_connectionString))
                            {
                                using (harness.StartCollection())
                                {
                                    var val = 11;
                                    var query = context.Products.Where(p => p.ProductId < val);

                                    for (var i = 0; i < _funcletizationIterationCount; i++)
                                    {
                                        var result = query.ToList();

                                        Assert.Equal(10, result.Count);
                                    }
                                }
                            }
                        }
                }.RunTest();
        }
        private static void Insert(TestHarness harness, bool disableBatching)
        {
            using (var context = new OrdersContext(_connectionString, disableBatching))
            {
                using (context.Database.AsRelational().Connection.BeginTransaction())
                {
                    for (var i = 0; i < 1000; i++)
                    {
                        context.Customers.Add(new Customer { Name = "New Customer " + i });
                    }

                    harness.StartCollection();
                    var records = context.SaveChanges();
                    harness.StopCollection();

                    Assert.Equal(1000, records);
                }
            }
        }
 public void OrderBy()
 {
     new TestDefinition
         {
             TestName = "Query_Simple_OrderBy",
             IterationCount = 100,
             WarmupCount = 5,
             Setup = EnsureDatabaseSetup,
             Run = harness =>
                 {
                     using (var context = new OrdersContext(_connectionString))
                     {
                         harness.StartCollection();
                         var result = context.Products.OrderBy(p => p.Retail).ToList();
                         harness.StopCollection();
                         Assert.Equal(1000, result.Count);
                     }
                 }
         }.RunTest();
 }
Esempio n. 10
0
        private bool IsDatabaseCorrect(OrdersContext context)
        {
            try
            {
                context.Customers.FirstOrDefault();
                context.Products.FirstOrDefault();
                context.Orders.FirstOrDefault();
                context.OrderLines.FirstOrDefault();
            }
            catch (SqlException)
            {
                // Assume an exception means the schema is out of date
                return false;
            }

            return _productCount == context.Products.Count()
                && _customerCount == context.Customers.Count()
                && _customerCount * _ordersPerCustomer == context.Orders.Count()
                && _customerCount * _ordersPerCustomer * _linesPerOrder == context.OrderLines.Count();
        }
Esempio n. 11
0
        public void EnsureCreated(
            string connectionString,
            int productCount,
            int customerCount,
            int ordersPerCustomer,
            int linesPerOrder)
        {
            using (var context = new OrdersContext(connectionString))
            {
                if (context.Database.EnsureCreated())
                {
                    InsertSeedData(connectionString, productCount, customerCount, ordersPerCustomer, linesPerOrder);
                }

                Assert.Equal(productCount, context.Products.Count());
                Assert.Equal(customerCount, context.Customers.Count());
                Assert.Equal(customerCount * ordersPerCustomer, context.Orders.Count());
                Assert.Equal(customerCount * ordersPerCustomer * linesPerOrder, context.OrderLines.Count());
            }
        }
Esempio n. 12
0
        public void EnsureCreated(
            string connectionString,
            int productCount,
            int customerCount,
            int ordersPerCustomer,
            int linesPerOrder)
        {
            using (var context = new OrdersContext(connectionString))
            {
                if (context.Database.EnsureCreated())
                {
                    InsertSeedData(connectionString, productCount, customerCount, ordersPerCustomer, linesPerOrder);
                }

                Assert.Equal(productCount, context.Products.Count());
                Assert.Equal(customerCount, context.Customers.Count());
                Assert.Equal(customerCount * ordersPerCustomer, context.Orders.Count());
                Assert.Equal(customerCount * ordersPerCustomer * linesPerOrder, context.OrderLines.Count());
            }
        }
Esempio n. 13
0
        private bool IsDatabaseCorrect(OrdersContext context)
        {
            try
            {
                context.Customers.FirstOrDefault();
                context.Products.FirstOrDefault();
                context.Orders.FirstOrDefault();
                context.OrderLines.FirstOrDefault();
            }
            catch (SqlException)
            {
                // Assume an exception means the schema is out of date
                return(false);
            }

            return((_productCount == context.Products.Count()) &&
                   (_customerCount == context.Customers.Count()) &&
                   (_customerCount * _ordersPerCustomer == context.Orders.Count()) &&
                   (_customerCount * _ordersPerCustomer * _linesPerOrder == context.OrderLines.Count()));
        }
Esempio n. 14
0
        private void EnsureDatabaseCreated()
        {
            using (var context = new OrdersContext(_connectionString))
            {
                var database = ((IInfrastructure<IServiceProvider>)context).GetService<IRelationalDatabaseCreator>();
                if (!database.Exists())
                {
                    context.Database.EnsureCreated();
                    InsertSeedData();
                }
                else if (!IsDatabaseCorrect(context))
                {
                    context.Database.EnsureDeleted();
                    context.Database.EnsureCreated();
                    InsertSeedData();
                }

                Assert.True(IsDatabaseCorrect(context));
            }
        }
Esempio n. 15
0
        private void EnsureDatabaseCreated()
        {
            using (var context = new OrdersContext(_connectionString))
            {
                var database = ((IInfrastructure <IServiceProvider>)context).GetService <IRelationalDatabaseCreator>();
                if (!database.Exists())
                {
                    context.Database.EnsureCreated();
                    InsertSeedData();
                }
                else if (!IsDatabaseCorrect(context))
                {
                    context.Database.EnsureDeleted();
                    context.Database.EnsureCreated();
                    InsertSeedData();
                }

                Assert.True(IsDatabaseCorrect(context));
            }
        }
Esempio n. 16
0
        public void InsertSeedData(
            string connectionString,
            int productCount,
            int customerCount,
            int ordersPerCustomer,
            int linesPerOrder)
        {
            List <Product> products = CreateProducts(productCount);

            using (var context = new OrdersContext(connectionString))
            {
                context.Products.Add(products.ToArray());
                context.SaveChanges();
            }

            List <Customer> customers = CreateCustomers(customerCount);

            using (var context = new OrdersContext(connectionString))
            {
                context.Customers.Add(customers.ToArray());
                context.SaveChanges();
            }

            List <Order> orders = CreateOrders(ordersPerCustomer, customers);

            using (var context = new OrdersContext(connectionString))
            {
                context.Orders.Add(orders.ToArray());
                context.SaveChanges();
            }

            List <OrderLine> lines = CreateOrderLines(linesPerOrder, products, orders);

            using (var context = new OrdersContext(connectionString))
            {
                context.OrderLines.Add(lines.ToArray());
                context.SaveChanges();
            }
        }
        public void InsertSeedData(
            string connectionString,
            int productCount,
            int customerCount,
            int ordersPerCustomer,
            int linesPerOrder)
        {
            var products = CreateProducts(productCount);

            using (var context = new OrdersContext(connectionString))
            {
                context.Products.AddRange(products);
                context.SaveChanges();
            }

            var customers = CreateCustomers(customerCount);

            using (var context = new OrdersContext(connectionString))
            {
                context.Customers.AddRange(customers);
                context.SaveChanges();
            }

            var orders = CreateOrders(ordersPerCustomer, customers);

            using (var context = new OrdersContext(connectionString))
            {
                context.Orders.AddRange(orders);
                context.SaveChanges();
            }

            var lines = CreateOrderLines(linesPerOrder, products, orders);

            using (var context = new OrdersContext(connectionString))
            {
                context.OrderLines.AddRange(lines);
                context.SaveChanges();
            }
        }
Esempio n. 18
0
        public void InsertSeedData(
            string connectionString,
            int productCount,
            int customerCount,
            int ordersPerCustomer,
            int linesPerOrder)
        {
            var products = CreateProducts(productCount);
            using (var context = new OrdersContext(connectionString))
            {
                context.Products.AddRange(products);
                context.SaveChanges();
            }

            var customers = CreateCustomers(customerCount);
            using (var context = new OrdersContext(connectionString))
            {
                context.Customers.AddRange(customers);
                context.SaveChanges();
            }

            var orders = CreateOrders(ordersPerCustomer, customers);
            using (var context = new OrdersContext(connectionString))
            {
                context.Orders.AddRange(orders);
                context.SaveChanges();
            }

            var lines = CreateOrderLines(linesPerOrder, products, orders);

            using (var context = new OrdersContext(connectionString))
            {
                context.OrderLines.AddRange(lines);
                context.SaveChanges();
            }
        }
Esempio n. 19
0
 public void Include()
 {
     new TestDefinition
         {
             TestName = "Query_Simple_Include",
             // TODO Increase iteration count once perf issues addressed
             IterationCount = 2,
             WarmupCount = 1,
             Setup = EnsureDatabaseSetup,
             Run = harness =>
                 {
                     using (var context = new OrdersContext(_connectionString))
                     {
                         harness.StartCollection();
                         var result = context.Customers.Include(c => c.Orders).ToList();
                         harness.StopCollection();
                         Assert.Equal(1000, result.Count);
                         Assert.Equal(2000, result.SelectMany(c => c.Orders).Count());
                     }
                 }
         }.RunTest();
 }
Esempio n. 20
0
 protected virtual void OnDatabaseCreated(OrdersContext context)
 {
 }
Esempio n. 21
0
        public void AttachParents()
        {
            new TestDefinition
                {
                    TestName = "ChangeTracker_Fixup_AttachParents",
                    IterationCount = 10,
                    WarmupCount = 5,
                    Setup = EnsureDatabaseSetup,
                    Run = harness =>
                        {
                            List<Customer> customers;
                            using (var context = new OrdersContext(_connectionString))
                            {
                                customers = context.Customers.ToList();
                            }

                            using (var context = new OrdersContext(_connectionString))
                            {
                                var orders = context.Orders.ToList();
                                Assert.Equal(1000, orders.Count);
                                Assert.Equal(1000, customers.Count);

                                foreach (var customer in customers)
                                {
                                    using (harness.StartCollection())
                                    {
                                        context.Customers.Attach(customer);
                                    }

                                    Assert.Same(customer, customer.Orders.Single().Customer);
                                }
                            }
                        }
                }.RunTest();
        }
Esempio n. 22
0
        public void QueryParents()
        {
            new TestDefinition
                {
                    TestName = "ChangeTracker_Fixup_QueryParents",
                    IterationCount = 10,
                    WarmupCount = 5,
                    Setup = EnsureDatabaseSetup,
                    Run = harness =>
                        {
                            using (var context = new OrdersContext(_connectionString))
                            {
                                context.Orders.ToList();

                                harness.StartCollection();
                                var customers = context.Customers.ToList();
                                harness.StopCollection();

                                Assert.Equal(1000, context.ChangeTracker.Entries<Customer>().Count());
                                Assert.Equal(1000, context.ChangeTracker.Entries<Order>().Count());
                                Assert.All(customers, c => Assert.Equal(1, c.Orders.Count));
                            }
                        }
                }.RunTest();
        }
 private static Customer[] GetAllCustomersFromDatabase()
 {
     using (var context = new OrdersContext(_connectionString))
     {
         return context.Customers.ToArray();
     }
 }
        public void RemoveCollection()
        {
            new TestDefinition
                {
                    TestName = "ChangeTracker_DbSetOperation_RemoveCollection",
                    IterationCount = 100,
                    WarmupCount = 5,
                    Setup = EnsureDatabaseSetup,
                    Run = harness =>
                        {
                            using (var context = new OrdersContext(_connectionString))
                            {
                                var customers = context.Customers.ToArray();
                                Assert.Equal(1000, customers.Length);

                                using (harness.StartCollection())
                                {
                                    context.Customers.RemoveRange(customers);
                                }
                            }
                        }
                }.RunTest();
        }
Esempio n. 25
0
        public void ProjectionAcrossNavigation()
        {
            new TestDefinition
                {
                    TestName = "Query_Simple_ProjectionAcrossNavigation",
                    IterationCount = 100,
                    WarmupCount = 5,
                    Setup = EnsureDatabaseSetup,
                    Run = harness =>
                        {
                            using (var context = new OrdersContext(_connectionString))
                            {
                                harness.StartCollection();
                                // TODO Use navigation for projection when supported (#325)
                                var result = context.Orders.Join(
                                    context.Customers,
                                    o => o.CustomerId,
                                    c => c.CustomerId,
                                    (o, c) => new { CustomerName = c.Name, OrderDate = o.Date })
                                    .ToList();

                                harness.StopCollection();
                                Assert.Equal(2000, result.Count);
                            }
                        }
                }.RunTest();
        }
        private static void Update(TestHarness harness, bool disableBatching)
        {
            using (var context = new OrdersContext(_connectionString, disableBatching))
            {
                using (context.Database.AsRelational().Connection.BeginTransaction())
                {
                    foreach (var customer in context.Customers)
                    {
                        customer.Name += " Modified";
                    }

                    harness.StartCollection();
                    var records = context.SaveChanges();
                    harness.StopCollection();

                    Assert.Equal(1000, records);
                }
            }
        }
Esempio n. 27
0
        public void GroupBy()
        {
            new TestDefinition
                {
                    TestName = "Query_Simple_GroupBy",
                    IterationCount = 100,
                    WarmupCount = 5,
                    Setup = EnsureDatabaseSetup,
                    Run = harness =>
                        {
                            using (var context = new OrdersContext(_connectionString))
                            {
                                harness.StartCollection();
                                var result = context.Products
                                    .GroupBy(p => p.Retail)
                                    .Select(g => new
                                        {
                                            Retail = g.Key,
                                            Products = g
                                        })
                                    .ToList();

                                harness.StopCollection();
                                Assert.Equal(10, result.Count);
                                Assert.All(result, g => Assert.Equal(100, g.Products.Count()));
                            }
                        }
                }.RunTest();
        }
        private static void Mixed(TestHarness harness, bool disableBatching)
        {
            using (var context = new OrdersContext(_connectionString, disableBatching))
            {
                using (context.Database.AsRelational().Connection.BeginTransaction())
                {
                    var customers = context.Customers.ToArray();

                    for (var i = 0; i < 333; i++)
                    {
                        context.Customers.Add(new Customer { Name = "New Customer " + i });
                    }

                    for (var i = 0; i < 1000; i += 3)
                    {
                        context.Customers.Remove(customers[i]);
                    }

                    for (var i = 1; i < 1000; i += 3)
                    {
                        customers[i].Name += " Modified";
                    }

                    harness.StartCollection();
                    var records = context.SaveChanges();
                    harness.StopCollection();

                    Assert.Equal(1000, records);
                }
            }
        }