public void CanLazilyLoadSeveralProducts() { //arrange IDocumentStore store = _kernel.Get<IDocumentStore>(); Category cat = new Category { Id = "categories-1", Description = "unit-test-category" }; using (IDocumentSession session = store.OpenSession()) { session.Store(cat); session.SaveChanges(); } using (IDocumentSession session = store.OpenSession()) { for (int i = 1; i <= 10; i++) { Product prod = new Product { Id = "products-" + i, CategoryId = cat.Id, Price = 1234, Description = "unit-test-product" }; session.Store(prod); session.SaveChanges(); } } using(IDocumentSession session = store.OpenSession()) { var cat1 = session.Advanced.Lazily.Load<Category>("categories-1"); var prod1 = session.Advanced.Lazily.Load<Product>("products-1"); var prod2 = session.Advanced.Lazily.Load<Product>("products-2"); var prod3 = session.Advanced.Lazily.Load<Product>("products-3"); var prod4 = session.Advanced.Lazily.Load<Product>("products-4"); var prod5 = session.Advanced.Lazily.Load<Product>("products-5"); var prod6 = session.Advanced.Lazily.Load<Product>("products-6"); var prod7 = session.Advanced.Lazily.Load<Product>("products-7"); var prod8 = session.Advanced.Lazily.Load<Product>("products-8"); var prod9 = session.Advanced.Lazily.Load<Product>("products-9"); var prod10 = session.Advanced.Lazily.Load<Product>("products-10"); session.Advanced.Eagerly.ExecuteAllPendingLazyOperations(); Assert.That(cat1.Value.Description, Is.EqualTo("unit-test-category")); Assert.That(prod1.Value.Description, Is.EqualTo("unit-test-product")); Assert.That(prod2.Value.Description, Is.EqualTo("unit-test-product")); } //clean up using (IDocumentSession session = store.OpenSession()) { Category deleteCategory = session.Load<Category>("categories-1"); session.Delete(deleteCategory); session.SaveChanges(); for (int i = 1; i <= 10; i++) { Product deleteProd = session.Load<Product>("products-" + i); session.Delete(deleteProd); session.SaveChanges(); } } }
// // GET: /FillData/ public ActionResult Index() { //delete everything List<Category> existingCategories = _categoryRepository.Query(c => c.Id != null).ToList(); List<Product> existingProducts = _productRepository.Query(c => c.Id != null).ToList(); existingCategories.ForEach(c => _categoryRepository.Delete(c)); existingProducts.ForEach(p => _productRepository.Delete(p)); //add new categories and products List<Category> newCategories = new List<Category>(); for (int i = 0; i < 5; i++) { Category category = new Category { Description = "Description for " + i }; newCategories.Add(category); } newCategories.ForEach(c => _categoryRepository.Save(c)); Random rnd = new Random(DateTime.Now.Millisecond); List<Product> newProducts = new List<Product>(); for (int i = 0; i < 20; i++) { Product product = new Product { Description = "Product description for " + i, Price = rnd.Next(1, 10000), CategoryId = newCategories[rnd.Next(0, newCategories.Count - 1)].Id }; newProducts.Add(product); } newProducts.ForEach(p => _productRepository.Save(p)); return View(); }
public void CanLoadProductAndCategoryInOnlyOneHttpRequest() { //arrange IDocumentStore store = _kernel.Get<IDocumentStore>(); Category cat = new Category {Description = "unit-test-category"}; using(IDocumentSession session = store.OpenSession()) { session.Store(cat); session.SaveChanges(); } Product prod = new Product {CategoryId = cat.Id, Price = 1234, Description = "unit-test-product"}; using(IDocumentSession session = store.OpenSession()) { session.Store(prod); session.SaveChanges(); } string pId = prod.Id; //act RavenQueryStatistics stats; Product actualProduct; Category actualCategory; using(IDocumentSession session = store.OpenSession()) { actualProduct = session.Query<Product>() .Customize(c => c.Include<Product>(p => p.CategoryId).WaitForNonStaleResultsAsOfNow()) .Statistics(out stats) .FirstOrDefault(p => p.Id == pId); actualCategory = session.Load<Category>(actualProduct.CategoryId); } //assert Assert.That(cat.Id, Is.Not.Empty); Assert.That(prod.Id, Is.Not.Empty); Assert.That(stats, Is.Not.Null); Assert.That(stats.TotalResults, Is.EqualTo(1)); Assert.That(actualProduct, Is.Not.Null); Assert.That(actualProduct, Is.Not.Null); //cleanup using(IDocumentSession session = store.OpenSession()) { Category deleteCategory = session.Load<Category>(actualCategory.Id); session.Delete(deleteCategory); Product deleteProduct = session.Load<Product>(actualProduct.Id); session.Delete(deleteProduct); } }
public void CanSummarizeCategoryPrices() { //arrange IDocumentStore store = _kernel.Get<IDocumentStore>(); using (IDocumentSession session = store.OpenSession()) { Category cat1 = new Category {Id = "categories-1", Description = "cat1"}; session.Store(cat1); Category cat2 = new Category {Id = "categories-2", Description = "cat2"}; session.Store(cat2); Product prod1 = new Product {Id = "products-1", CategoryId = cat1.Id, Price = 2, Description = "prod1"}; session.Store(prod1); Product prod2 = new Product {Id = "products-2", CategoryId = cat1.Id, Price = 3, Description = "prod2"}; session.Store(prod2); Product prod3 = new Product {Id = "products-3", CategoryId = cat2.Id, Price = 4, Description = "prod3"}; session.Store(prod3); session.SaveChanges(); } using (IDocumentSession session = store.OpenSession()) { var categoryTotals = session.Query<ProductTotalPricePerCategoryIndex.ReduceResult, ProductTotalPricePerCategoryIndex>() .Customize(c => c.WaitForNonStaleResultsAsOfNow()) .Where(r => r.CategoryId == "categories-1").ToList(); } }