Ejemplo n.º 1
0
        public void Include_Test()
        {
            using (var db = new IncludeDatabase())
            {
                var customer = new Customer { Name = "John Doe" };

                var product1 = new Product { Name = "TV", Price = 800 };
                var product2 = new Product { Name = "DVD", Price = 200 };

                // insert ref documents
                db.Customers.Insert(customer);
                db.Products.Insert(new Product[] { product1, product2 });

                var order = new Order
                {
                    Customer = customer,
                    CustomerNull = null,
                    Products = new List<Product>() { product1, product2 },
                    ProductArray = new Product[] { product1 },
                    ProductColl = new List<Product>() { product2 },
                    ProductEmpty = new List<Product>(),
                    ProductsNull = null
                };

                db.Orders.Insert(order);

                var query = db.Orders
                    .Include(x => x.Customer)
                    .Include(x => x.CustomerNull)
                    .Include(x => x.Products)
                    .Include(x => x.ProductArray)
                    .Include(x => x.ProductColl)
                    .Include(x => x.ProductsNull)
                    .FindAll()
                    .FirstOrDefault();

                Assert.AreEqual(customer.Name, query.Customer.Name);
                Assert.AreEqual(product1.Price, query.Products[0].Price);
                Assert.AreEqual(product2.Name, query.Products[1].Name);
                Assert.AreEqual(product1.Name, query.ProductArray[0].Name);
                Assert.AreEqual(product2.Price, query.ProductColl.ElementAt(0).Price);
                Assert.AreEqual(null, query.ProductsNull);
                Assert.AreEqual(0, query.ProductEmpty.Count);
            }
        }
Ejemplo n.º 2
0
        public void Include_Test()
        {
            var mapper = new BsonMapper();

            mapper.Entity<Order>()
               .DbRef(x => x.Products, "products")
               .DbRef(x => x.ProductArray, "products")
               .DbRef(x => x.ProductColl, "products")
               .DbRef(x => x.ProductEmpty, "products")
               .DbRef(x => x.ProductsNull, "products")
               .DbRef(x => x.Customer, "customers")
               .DbRef(x => x.CustomerNull, "customers");

            mapper.Entity<Customer>()
                .DbRef(x => x.MainAddress, "addresses");

            mapper.Entity<Product>()
                .DbRef(x => x.SupplierAddress, "addresses");

            using (var file = new TempFile())
            using (var db = new LiteDatabase(file.Filename, mapper))
            {
                var address = new Address { StreetName = "3600 S Las Vegas Blvd" };
                var customer = new Customer { Name = "John Doe", MainAddress = address };

                var product1 = new Product { Name = "TV", Price = 800, SupplierAddress = address };
                var product2 = new Product { Name = "DVD", Price = 200 };

                var customers = db.GetCollection<Customer>("customers");
                var addresses = db.GetCollection<Address>("addresses");
                var products = db.GetCollection<Product>("products");
                var orders = db.GetCollection<Order>("orders");

                // insert ref documents
                addresses.Insert(address);
                customers.Insert(customer);
                products.Insert(new Product[] { product1, product2 });

                var order = new Order
                {
                    Customer = customer,
                    CustomerNull = null,
                    Products = new List<Product>() { product1, product2 },
                    ProductArray = new Product[] { product1 },
                    ProductColl = new List<Product>() { product2 },
                    ProductEmpty = new List<Product>(),
                    ProductsNull = null
                };

                orders.Insert(order);

                var query = orders
                    .Include(x => x.Customer)
                    .Include(x => x.Customer.MainAddress)
                    .Include(x => x.CustomerNull)
                    .Include(x => x.Products)
                    .Include(x => x.ProductArray)
                    .Include(x => x.ProductColl)
                    .Include(x => x.ProductsNull)
                    .FindAll()
                    .FirstOrDefault();

                Assert.AreEqual(customer.Name, query.Customer.Name);
                Assert.AreEqual(customer.MainAddress.StreetName, query.Customer.MainAddress.StreetName);
                Assert.AreEqual(product1.Price, query.Products[0].Price);
                Assert.AreEqual(product2.Name, query.Products[1].Name);
                Assert.AreEqual(product1.Name, query.ProductArray[0].Name);
                Assert.AreEqual(product2.Price, query.ProductColl.ElementAt(0).Price);
                Assert.AreEqual(null, query.ProductsNull);
                Assert.AreEqual(0, query.ProductEmpty.Count);
            }
        }