public void OptimizedDeleteQuery() { using (var container = new RhetosTestContainer()) { var sqlExecuter = container.Resolve <ISqlExecuter>(); var repository = container.Resolve <Common.DomRepository>(); repository.TestEntity.UniqueEntity.Delete(repository.TestEntity.UniqueEntity.Load()); var newItem = new TestEntity.UniqueEntity { ID = Guid.NewGuid(), Name = "a", Data = "b" }; repository.TestEntity.UniqueEntity.Insert(newItem); // Temporarily removing a column to detect if the following code will try to read it. sqlExecuter.ExecuteSql("ALTER TABLE TestEntity.UniqueEntity DROP COLUMN Data"); IEnumerable <TestEntity.UniqueEntity> items = repository.TestEntity.UniqueEntity.Query(); // The following line should not try to read all columns. DomHelper.MaterializeItemsToDelete(ref items); Assert.AreEqual(1, items.Count()); Assert.AreEqual(newItem.ID, items.Single().ID); Assert.IsNull(items.Single().Name); Assert.IsNull(items.Single().Data); } }
public void DeleteUpdateInsert_ConflictUnique() { foreach (bool useDatabaseNullSemantics in new[] { false, true }) { using (var container = new RhetosTestContainer()) { container.OverrideConfiguration(("EntityFramework.UseDatabaseNullSemantics", useDatabaseNullSemantics)); container.Resolve <ISqlExecuter>().ExecuteSql("DELETE FROM TestEntity.UniqueEntity"); var r = container.Resolve <Common.DomRepository>().TestEntity.UniqueEntity; var context = container.Resolve <Common.ExecutionContext>(); var ia = new TestEntity.UniqueEntity { Name = "a", ID = Guid.NewGuid() }; var ib = new TestEntity.UniqueEntity { Name = "b", ID = Guid.NewGuid() }; var ic1 = new TestEntity.UniqueEntity { Name = "c", ID = Guid.NewGuid() }; r.Insert(ia, ib, ic1); Assert.AreEqual("a, b, c", TestUtility.DumpSorted(r.Load(), item => item.Name + item.Data)); // Deleting old 'c' and inserting new 'c'. Possible conflict on unique constraint for property Name. var ic2 = new TestEntity.UniqueEntity { Name = "c", ID = Guid.NewGuid() }; r.Save(new[] { ic2 }, null, new[] { ic1 }); Assert.AreEqual("a, b, c", TestUtility.DumpSorted(r.Load(), item => item.Name + item.Data)); Guid currentCID = r.Query().Where(item => item.Name == "c").Select(item => item.ID).Single(); Assert.AreEqual(ic2.ID, currentCID, "new inserted item 'c'"); Assert.AreNotEqual(ic1.ID, currentCID, "old deleted item 'c'"); // Deleting old 'c' and inserting new 'c' with same ID. Possible conflict on primary key. var ic3 = new TestEntity.UniqueEntity { Name = "c", Data = "x", ID = ic2.ID }; r.Save(new[] { ic3 }, null, new[] { ic2 }); Assert.AreEqual("a, b, cx", TestUtility.DumpSorted(r.Load(), item => item.Name + item.Data)); // Renaming old 'c' and inserting new 'c'. Possible conflict on unique constraint for property Name. ic3.Name = "oldc"; var ic4 = new TestEntity.UniqueEntity { Name = "c", ID = Guid.NewGuid() }; r.Save(new[] { ic4 }, new[] { ic3 }, null); Assert.AreEqual("a, b, c, oldcx", TestUtility.DumpSorted(r.Load(), item => item.Name + item.Data)); } } }
public void DeleteUpdateInsert_ConflictUnique() { using (var container = new RhetosTestContainer()) { container.Resolve<ISqlExecuter>().ExecuteSql(new[] { "DELETE FROM TestEntity.UniqueEntity" }); var r = container.Resolve<Common.DomRepository>().TestEntity.UniqueEntity; var nhSession = container.Resolve<Common.ExecutionContext>().NHibernateSession; var ia = new TestEntity.UniqueEntity { Name = "a", ID = Guid.NewGuid() }; var ib = new TestEntity.UniqueEntity { Name = "b", ID = Guid.NewGuid() }; var ic1 = new TestEntity.UniqueEntity { Name = "c", ID = Guid.NewGuid() }; r.Insert(new[] { ia, ib, ic1 }); Assert.AreEqual("a, b, c", TestUtility.DumpSorted(r.All(), item => item.Name + item.Data)); // Deleting old 'c' and inserting new 'c'. Possible conflict on unique constraint for property Name. var ic2 = new TestEntity.UniqueEntity { Name = "c", ID = Guid.NewGuid() }; r.Save(new[] { ic2 }, null, new[] { ic1 }); nhSession.Clear(); Assert.AreEqual("a, b, c", TestUtility.DumpSorted(r.All(), item => item.Name + item.Data)); Guid currentCID = r.Query().Where(item => item.Name == "c").Select(item => item.ID).Single(); Assert.AreEqual(ic2.ID, currentCID, "new inserted item 'c'"); Assert.AreNotEqual(ic1.ID, currentCID, "old deleted item 'c'"); // Deleting old 'c' and inserting new 'c' with same ID. Possible conflict on primary key. var ic3 = new TestEntity.UniqueEntity { Name = "c", Data = "x", ID = ic2.ID }; r.Save(new[] { ic3 }, null, new[] { ic2 }); nhSession.Clear(); Assert.AreEqual("a, b, cx", TestUtility.DumpSorted(r.All(), item => item.Name + item.Data)); // Renaming old 'c' and inserting new 'c'. Possible conflict on unique constraint for property Name. ic3.Name = "oldc"; var ic4 = new TestEntity.UniqueEntity { Name = "c", ID = Guid.NewGuid() }; r.Save(new[] { ic4 }, new[] { ic3 }, null); nhSession.Clear(); Assert.AreEqual("a, b, c, oldcx", TestUtility.DumpSorted(r.All(), item => item.Name + item.Data)); } }
public void OptimizedDeleteQuery() { using (var container = new RhetosTestContainer()) { var sqlExecuter = container.Resolve<ISqlExecuter>(); var repository = container.Resolve<Common.DomRepository>(); repository.TestEntity.UniqueEntity.Delete(repository.TestEntity.UniqueEntity.Load()); var newItem = new TestEntity.UniqueEntity { ID = Guid.NewGuid(), Name = "a", Data = "b" }; repository.TestEntity.UniqueEntity.Insert(newItem); // Temporarily removing a column to detect if the following code will try to read it. sqlExecuter.ExecuteSql("ALTER TABLE TestEntity.UniqueEntity DROP COLUMN Data"); IEnumerable<TestEntity.UniqueEntity> items = repository.TestEntity.UniqueEntity.Query(); // The following line should not try to read all columns. Common.Infrastructure.MaterializeItemsToDelete(ref items); Assert.AreEqual(1, items.Count()); Assert.AreEqual(newItem.ID, items.Single().ID); Assert.IsNull(items.Single().Name); Assert.IsNull(items.Single().Data); } }