public void ShouldDeleteACategoryFromDifferentContext() { var categories = new List <Category> { new Category { CategoryName = "Foo" }, }; _repo.AddRange(categories); Assert.Equal(1, _repo.Table.Count()); var category = _repo.Table.First(); using (var context = new StoreContextFactory().CreateDbContext(null)) { using (CategoryRepo repo = new CategoryRepo(context)) { var catToDelete = new Category { Id = category.Id, TimeStamp = category.TimeStamp }; var count = repo.Delete(catToDelete, false); Assert.Equal(0, count); count = repo.Context.SaveChanges(); Assert.Equal(1, count); Assert.Equal(0, repo.Table.Count()); } } }
public void ShouldUpdateARangeOfCategoryEntities() { var categories = new List <Category> { new Category { CategoryName = "Foo" }, new Category { CategoryName = "Bar" }, new Category { CategoryName = "FooBar" } }; _repo.AddRange(categories); categories[0].CategoryName = "Foo1"; categories[1].CategoryName = "Foo2"; categories[2].CategoryName = "Foo3"; _repo.UpdateRange(categories, false); var count = _repo.SaveChanges(); Assert.Equal(3, count); using (var context = new StoreContextFactory().CreateDbContext(null)) { using (var repo = new CategoryRepo(context)) { var cat = repo.Find(categories[0].Id); Assert.Equal("Foo1", cat.CategoryName); } } }
public OrderTests() { var storeContextFactory = new StoreContextFactory(); SampleDataInitializer.InitializeData(storeContextFactory.CreateDbContext(new string[0])); _db = storeContextFactory.CreateDbContext(new string[0]); }
public OrderTests() { _db = new StoreContextFactory().CreateDbContext(new string[0]); _db.CustomerId = 1; //Have to load database with different context, OR call reload on each entity SampleDataInitializer.InitializeData(new StoreContextFactory().CreateDbContext(new string[0])); }
public ShoppingCartRepoTests() { var context = new StoreContextFactory().CreateDbContext(null); _repo = new ShoppingCartRepo(context,new ProductRepo(context),new CustomerRepo(context); SampleDataInitializer.InitializeData(_repo.Context); }
public void Test1() { StoreContext _context = new StoreContextFactory().CreateDbContext(null); var cat = new Category { CategoryName = "CatName" }; _context.Categories.Add(cat); _context.SaveChanges(); var cats = new List <Category> { new Category { CategoryName = "Cat1Name" }, new Category { CategoryName = "Cat2Name" }, new Category { CategoryName = "Cat3Name" }, }; _context.Categories.AddRange(cats); _context.SaveChanges(); }
public void ShouldGetOrderTotalAfterAddingAnOrderDetail() { var order = _db.Orders.FirstOrDefault(); var orderDetail = new OrderDetail() { OrderId = order.Id, ProductId = 2, Quantity = 5, UnitCost = 100M }; _db.OrderDetails.Add(orderDetail); _db.SaveChanges(); //Need to use a new DbContext to get the updated value var storeContext = new StoreContextFactory().CreateDbContext(new string[0]); storeContext.CustomerId = 1; order = storeContext.Orders.First(); //order = _db.Orders.FirstOrDefault(); Assert.Equal(4914.90M, order.OrderTotal); }
private static void InitializeDb(IWebHost host) { try { var storeContextFactory = new StoreContextFactory(); var context = storeContextFactory.CreateDbContext(null); context.Database.Migrate(); SeedData.Initialize(context); } catch (Exception) { throw new Exception("Cannot initialize database."); } }
public void ShouldDeleteACategoryWithTimestampData() { var category = new Category { CategoryName = "Foo" }; _db.Categories.Add(category); _db.SaveChanges(); var context = new StoreContextFactory().CreateDbContext(null); var catToDelete = new Category { Id = category.Id, TimeStamp = category.TimeStamp }; context.Entry(catToDelete).State = EntityState.Deleted; var affected = context.SaveChanges(); Assert.Equal(1, affected); }
public void ShouldNotDeleteACategoryWithoutTimestampData() { var category = new Category { CategoryName = "Foo" }; _db.Categories.Add(category); _db.SaveChanges(); var context = new StoreContextFactory().CreateDbContext(null); var catToDelete = new Category { Id = category.Id }; context.Categories.Remove(catToDelete); var ex = Assert.Throws <DbUpdateConcurrencyException>(() => context.SaveChanges()); Assert.Equal(1, ex.Entries.Count); Assert.Equal(category.Id, ((Category)ex.Entries[0].Entity).Id); }
public void ShouldUpdateACategory() { var category = new Category { CategoryName = "Foo" }; _db.Categories.Add(category); _db.SaveChanges(); category.CategoryName = "Bar"; _db.Categories.Update(category); Assert.Equal(EntityState.Modified, _db.Entry(category).State); _db.SaveChanges(); Assert.Equal(EntityState.Unchanged, _db.Entry(category).State); StoreContext context; using (context = new StoreContextFactory().CreateDbContext(null)) { Assert.Equal("Bar", context.Categories.First().CategoryName); } }
public void ShouldUpdateACategoryEntity() { var category = new Category { CategoryName = "Foo" }; _repo.AddRange(new List <Category> { category, }); category.CategoryName = "Bar"; _repo.Update(category, false); var count = _repo.SaveChanges(); Assert.Equal(1, count); using (var context = new StoreContextFactory().CreateDbContext(null)) { using (var repo = new CategoryRepo(context)) { var cat = repo.Find(category.Id); Assert.Equal(cat.CategoryName, category.CategoryName); } } }
public CategoryTests() { _db = new StoreContextFactory().CreateDbContext(new string[0]); CleanDatabase(); }