public void Common_CanDelete(IRepository<Product> db) { db.Save(new Product { ProductName = "Optimus", Category = "Autobots", MinimumPrice = 7 }); var px = new Product { ProductName = "Bumble Bee", Category = "Autobots", MinimumPrice = 8 }; db.Save(px); db.Save(new Product { ProductName = "Megatron", Category = "Decepticon", MinimumPrice = 9 }); db.Delete(px.ProductId, px.RowVersion); Assert.AreEqual(7 + 9, db.All.Sum(x => x.MinimumPrice)); Assert.AreEqual(null, db.Get(px.ProductId)); Assert.AreEqual(2, db.All.Count()); }
public void Common_CanDetectDeleteConflictingDelete(IRepository<Product> db) { // Arrange var px = new Product { ProductName = "Bumble Bee", Category = "Autobots", MinimumPrice = 8 }; db.Save(px); // Act Product firstOpener = new Product { ProductId = px.ProductId, ProductName = px.ProductName, Category = px.Category, RowVersion = px.RowVersion }; Product secondOpener = new Product { ProductId = px.ProductId, ProductName = px.ProductName, Category = px.Category, RowVersion = px.RowVersion }; db.Delete(firstOpener.ProductId, firstOpener.RowVersion); // Assert // Expected to fail db.Save(secondOpener); }
public void Saving_is_ok() { // Arrange /*EmptyDatabase(); IRepository<Product> db = new NhRepository<Product>(NhModelsMapper.GetSession(connectionString));*/ IRepository<Product> db = new Repository<Product>(); var px = new Product { ProductName = "Optimus", Category = "Autobots", MinimumPrice = 7 }; px.PriceList = new List<ProductPrice>() { new ProductPrice { Product = px, Price = 777, EffectiveDate = DateTime.Today }, new ProductPrice { Product = px, Price = 888, EffectiveDate = DateTime.Today }, new ProductPrice { Product = px, Price = 999, EffectiveDate = DateTime.Today }, new ProductPrice { Product = px, Price = 222, EffectiveDate = DateTime.Today }, }; // Act db.Save(px); /*var query = (from x in db.All where x.ProductId == px.ProductId select x).EagerLoadMany(x => x.PriceList); var xxx = query.Single(); */ var xxx = db.GetEager(px.ProductId); Assert.AreEqual(px.ProductName, xxx.ProductName); Assert.AreEqual(px.ProductId, xxx.ProductId); Assert.AreEqual(4, xxx.PriceList.Count()); }
public void Common_HasRowVersion(IRepository<Product> db) { string expected = "Jollibee"; // Arrange var px = new Product { ProductName = "Bumble Bee", Category = "Autobots", MinimumPrice = 8 }; // Act db.Save(px); byte[] originalVersion = px.RowVersion; // Noted corner case, when there's no changes in any of the properties, NHibernate still persist the class, // while EF don't; hence when the following line is commented, the rowversion doesn't change on EF. px.ProductName = expected; db.Save(px); byte[] newVersion = px.RowVersion; // Assert Assert.AreEqual(expected, px.ProductName); Assert.AreNotEqual(null, px.RowVersion); CollectionAssert.AreNotEqual(originalVersion, newVersion); }
public void Common_Can_Save_Then_Update(IRepository<Product> dbProd, IRepository<ProductPrice> dbPrice) { // Arrange string expected = "Bumble bee"; var bee = new Product { ProductName = expected, Category = "Autobots", MinimumPrice = 8 }; dbProd.Save(new Product { ProductName = "Optimus", Category = "Autobots", MinimumPrice = 7 }); dbProd.Save(bee); dbProd.Save(new Product { ProductName = "Megatron", Category = "Decepticon", MinimumPrice = 9 }); Assert.AreEqual(7 + 8 + 9, dbProd.All.Sum(x => x.MinimumPrice)); Assert.AreEqual(3, dbProd.All.Count()); Assert.AreNotEqual(0, bee.ProductId); // Act var beex = dbProd.Get(bee.ProductId); beex.ProductName = beex.ProductName + "yo"; dbProd.Save(beex); var beey = dbProd.Get(beex.ProductId); // Assert Assert.AreEqual(beex.ProductId, beey.ProductId); Assert.AreEqual(expected + "yo", beey.ProductName); /////////////////////// // Arrange // this has error, to fix later: // var pp = new ProductPrice { Product = dbProd.LoadStub(beex.ProductId), Price = 7, EffectiveDate = DateTime.Today }; var pp = new ProductPrice { Product = bee, Price = 7, EffectiveDate = DateTime.Today }; // Act dbPrice.Save(pp); var ppx = dbPrice.Get(pp.ProductPriceId); ppx.Price = ppx.Price + 1; dbPrice.Save(ppx); var ppy = dbPrice.Get(ppx.ProductPriceId); // Assert Assert.AreEqual(ppx.ProductPriceId, ppy.ProductPriceId); Assert.AreEqual(8, ppy.Price); }
public void Common_CanUpdate(IRepository<Product> db) { // Arrange var px = new Product { ProductName = "Bumble Bee", Category = "Autobots", MinimumPrice = 8 }; px.PriceList = new List<ProductPrice>() { new ProductPrice { Product = px, Price = 234, EffectiveDate = DateTime.Today }, new ProductPrice { Product = px, Price = 300, EffectiveDate = DateTime.Today.AddDays(100) } }; db.SaveGraph(px); // int n = px.PriceList[0].ProductPriceId; // simulate web(i.e. stateless) int productPriceId = db.Get(px.ProductId).PriceList[0].ProductPriceId; var fromWeb = new Product { ProductId = px.ProductId, ProductName = "hahaha", // px.ProductName + "---" + Guid.NewGuid().ToString(), Category = px.Category, MinimumPrice = px.MinimumPrice, RowVersion = db.Get(px.ProductId).RowVersion, PriceList = new List<ProductPrice>() }; fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, ProductPriceId = productPriceId, Price = 767676, EffectiveDate = DateTime.Today }); fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, Price = 2148, EffectiveDate = DateTime.Today }); fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, Price = 2048, EffectiveDate = DateTime.Today }); fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, Price = 222, EffectiveDate = DateTime.Today }); /*fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, Price = 888, EffectiveDate = DateTime.Today }); fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, Price = 222, EffectiveDate = DateTime.Today });*/ // Act string expecting = "Bumble Bee Battle Mode hickhickhick"; fromWeb.ProductName = expecting; db.SaveGraph(fromWeb); // Assert Assert.AreEqual(expecting, db.Get(fromWeb.ProductId).ProductName); // Assert.AreNotEqual(px.ProductName, db.Get(fromWeb.ProductId).ProductName); }
public void Common_CanHaveIncrementingKey(IRepository<Product> db) { // decimal ny = db.All.Where(x => x.ProductId == 111).Max(x => x.MinimumPrice); // decimal nx = db.All.Where(x => x.ProductId == 111).Select(x => new { TheMinimumPrice = (decimal?)x.MinimumPrice }).Max(x => x.TheMinimumPrice) ?? 0; // decimal nx = db.All.Where(x => x.ProductId == 111).Select(x => (decimal?)x.MinimumPrice).Max() ?? 0; // Arrange var optimusPrime = new Product { ProductName = "Optimus", Category = "Autobots", MinimumPrice = 7 }; var bumbleBee = new Product { ProductName = "Bumble Bee", Category = "Autobots", MinimumPrice = 8 }; var megatron = new Product { ProductName = "Megatron", Category = "Decepticon", MinimumPrice = 9 }; // Act db.Save(optimusPrime); db.Save(bumbleBee); db.Save(megatron); int n = optimusPrime.ProductId; // Assert Assert.AreEqual(n, optimusPrime.ProductId); Assert.AreEqual(n + 1, bumbleBee.ProductId); Assert.AreEqual(n + 2, megatron.ProductId); }
void Common_CanSaveHeaderDetail_ThenHeaderOnly(IRepository<Product> db) { // NHibernate.ISession xxx = NhModelsMapper.GetSession(connectionString); // Arrange var px = new Product { ProductName = "Optimus", Category = "Autobots", MinimumPrice = 7 }; px.PriceList = new List<ProductPrice>() { new ProductPrice { Product = px, Price = 777, EffectiveDate = DateTime.Today }, new ProductPrice { Product = px, Price = 888, EffectiveDate = DateTime.Today }, new ProductPrice { Product = px, Price = 999, EffectiveDate = DateTime.Today }, new ProductPrice { Product = px, Price = 222, EffectiveDate = DateTime.Today }, }; // Act db.SaveGraph(px); // xxx.Merge(px); byte[] rv = px.RowVersion; Assert.AreEqual(4, px.PriceList.Count()); px = db.GetEager(px.ProductId); Assert.AreEqual("Optimus", px.ProductName); px.ProductName = px.ProductName + "?"; px.PriceList[2].Price = 333; Assert.AreEqual(4, px.PriceList.Count); db.SaveGraph(px); Assert.AreNotEqual(0, px.ProductId); CollectionAssert.AreNotEqual(px.RowVersion, rv); // Assert Assert.AreEqual(px.ProductName, px.ProductName); Assert.AreNotEqual(0, px.ProductId); Assert.AreEqual(4, px.PriceList.Count); // Arrange Assert.AreNotEqual(0, px.ProductId); /* triggers concurrency exception byte[] x = px.RowVersion; x[0] += 1; px.RowVersion = x; */ var py = new Product { ProductId = px.ProductId, ProductName = "Optimus", Category = "Autobotszx", MinimumPrice = 7, RowVersion = px.RowVersion // empty list is incompatible with cascade=all-delete-orphan }; int pxid = px.ProductId; db.Save(py); Product pz = db.Get(px.ProductId); // throw new Exception(py.ProductId + " " + px.ProductId + " " + pxid); Assert.AreEqual(px.ProductId, py.ProductId); Assert.IsNotNull(pz.PriceList); Assert.AreEqual(4, pz.PriceList.Count); }
void Common_CanSaveHeaderDetail(IRepository<Product> db) { // Arrange var px = new Product { ProductName = "Optimus", Category = "Autobots", MinimumPrice = 7 }; px.PriceList = new List<ProductPrice>() { new ProductPrice { Product = px, Price = 777, EffectiveDate = DateTime.Today }, new ProductPrice { Product = px, Price = 888, EffectiveDate = DateTime.Today }, new ProductPrice { Product = px, Price = 999, EffectiveDate = DateTime.Today }, new ProductPrice { Product = px, Price = 222, EffectiveDate = DateTime.Today }, }; // Act db.SaveGraph(px); Assert.AreEqual(4, px.PriceList.Count()); px = db.GetEager(px.ProductId); Assert.AreEqual("Optimus", px.ProductName); px.ProductName = px.ProductName + "!"; px.PriceList[2].Price = 333; Assert.AreEqual(4, px.PriceList.Count); db.SaveGraph(px); // Assert Assert.AreEqual(px.ProductName, px.ProductName); Assert.AreEqual(px.ProductId, px.ProductId); Assert.AreEqual(4, px.PriceList.Count); }