/// <summary> /// Persistance test helper /// </summary> /// <typeparam name="T">Entity type</typeparam> /// <param name="entity">Entity</param> /// <param name="disposeContext">A value indicating whether to dispose context</param> protected T SaveAndLoadEntity <T>(T entity, bool disposeContext = true) where T : BaseEntity { context.Set <T>().Add(entity); context.SaveChanges(); object id = entity.Id; if (disposeContext) { context.Dispose(); context = new NopObjectContext(GetTestDbName()); } var fromDb = context.Set <T>().Find(id); return(fromDb); }
public static void Build() { var ctx = new NopObjectContext("Data Source=JASON-THINK;Initial Catalog=Brigita;Integrated Security=True;Persist Security Info=False"); var products = ctx.Set<Product>(); var cats = ctx.Set<Category>().ToArray(); var pic = ctx.Set<Picture>().OrderByDescending(p => p.ID).First(); foreach(var cat in cats) { for(int i = 0; i < 100; i++) { var p = new Product() { Name = "Product" + i, CreatedOnUtc = DateTime.UtcNow, UpdatedOnUtc = DateTime.UtcNow, ShortDescription = "Short desc blah blah blah blah blah.", FullDescription = "Full desc blah blah blah blah blah blah blah blah blah blah blah blah blah..." //... }; p.ProductCategories.Add(new ProductCategory() { Category = cat }); p.ProductPictures.Add(new ProductPicture() { Picture = pic }); products.Add(p); } } try { ctx.SaveChanges(); } catch(Exception ex) { //... } }
public void Can_get_customer_content_by_type() { var customer = SaveAndLoadEntity <Customer>(GetTestCustomer(), false); var product = SaveAndLoadEntity <Product>(GetTestProduct(), false); var productReview = new ProductReview { Customer = customer, Product = product, Title = "Test", ReviewText = "A review", IpAddress = "192.168.1.1", IsApproved = true, CreatedOnUtc = new DateTime(2010, 01, 01), UpdatedOnUtc = new DateTime(2010, 01, 02), }; var productReviewHelpfulness = new ProductReviewHelpfulness { Customer = customer, ProductReview = productReview, WasHelpful = true, IpAddress = "192.168.1.1", IsApproved = true, CreatedOnUtc = new DateTime(2010, 01, 03), UpdatedOnUtc = new DateTime(2010, 01, 04) }; var blogComment = new BlogComment { Customer = customer, IpAddress = "192.168.1.1", IsApproved = true, CreatedOnUtc = new DateTime(2010, 01, 03), UpdatedOnUtc = new DateTime(2010, 01, 04), BlogPost = new BlogPost() { Title = "Title 1", Body = "Body 1", AllowComments = true, CreatedOnUtc = new DateTime(2010, 01, 01), Language = new Language() { Name = "English", LanguageCulture = "en-Us", } } }; context.Set <CustomerContent>().Add(productReview); context.Set <CustomerContent>().Add(productReviewHelpfulness); context.Set <CustomerContent>().Add(blogComment); context.SaveChanges(); context.Dispose(); context = new NopObjectContext(GetTestDbName()); var query = context.Set <CustomerContent>(); query.ToList().Count.ShouldEqual(3); var dbReviews = query.OfType <ProductReview>().ToList(); dbReviews.Count().ShouldEqual(1); dbReviews.First().ReviewText.ShouldEqual("A review"); var dbHelpfulnessRecords = query.OfType <ProductReviewHelpfulness>().ToList(); dbHelpfulnessRecords.Count().ShouldEqual(1); dbHelpfulnessRecords.First().WasHelpful.ShouldEqual(true); var dbBlogCommentRecords = query.OfType <BlogComment>().ToList(); dbBlogCommentRecords.Count().ShouldEqual(1); }
public void Can_get_customer_content_by_type() { var customer = SaveAndLoadEntity<Customer>(GetTestCustomer(), false); var product = SaveAndLoadEntity<Product>(GetTestProduct(), false); var productReview = new ProductReview { Customer = customer, Product = product, Title = "Test", ReviewText = "A review", IpAddress = "192.168.1.1", IsApproved = true, CreatedOnUtc = new DateTime(2010, 01, 01), UpdatedOnUtc = new DateTime(2010, 01, 02), }; var productReviewHelpfulness = new ProductReviewHelpfulness { Customer = customer, ProductReview = productReview, WasHelpful = true, IpAddress = "192.168.1.1", IsApproved = true, CreatedOnUtc = new DateTime(2010, 01, 03), UpdatedOnUtc = new DateTime(2010, 01, 04) }; var blogComment = new BlogComment { Customer = customer, IpAddress = "192.168.1.1", IsApproved = true, CreatedOnUtc = new DateTime(2010, 01, 03), UpdatedOnUtc = new DateTime(2010, 01, 04), BlogPost = new BlogPost() { Title = "Title 1", Body = "Body 1", AllowComments = true, CreatedOnUtc = new DateTime(2010, 01, 01), Language = new Language() { Name = "English", LanguageCulture = "en-Us", } } }; context.Set<CustomerContent>().Add(productReview); context.Set<CustomerContent>().Add(productReviewHelpfulness); context.Set<CustomerContent>().Add(blogComment); context.SaveChanges(); context.Dispose(); context = new NopObjectContext(GetTestDbName()); var query = context.Set<CustomerContent>(); query.ToList().Count.ShouldEqual(3); var dbReviews = query.OfType<ProductReview>().ToList(); dbReviews.Count().ShouldEqual(1); dbReviews.First().ReviewText.ShouldEqual("A review"); var dbHelpfulnessRecords = query.OfType<ProductReviewHelpfulness>().ToList(); dbHelpfulnessRecords.Count().ShouldEqual(1); dbHelpfulnessRecords.First().WasHelpful.ShouldEqual(true); var dbBlogCommentRecords = query.OfType<BlogComment>().ToList(); dbBlogCommentRecords.Count().ShouldEqual(1); }