예제 #1
0
        protected override void OnTearDown()
        {
            IParentWithCollection dummyParent = CreateParent("dummyParent");

            dummyParent.NewChildren(CreateCollection());
            IChild dummyChild = dummyParent.AddChild("dummyChild");

            using (ISession s = OpenSession())
            {
                using (ITransaction tx = s.BeginTransaction())
                {
                    IList children = s.CreateCriteria(dummyChild.GetType()).List();
                    IList parents  = s.CreateCriteria(dummyParent.GetType()).List();
                    foreach (IParentWithCollection parent in parents)
                    {
                        parent.ClearChildren();
                        s.Delete(parent);
                    }
                    foreach (IChild child in children)
                    {
                        s.Delete(child);
                    }

                    tx.Commit();
                }
            }
            base.OnTearDown();
        }
예제 #2
0
        public void UpdateParentTwoChildrenToOne()
        {
            CollectionListeners   listeners = new CollectionListeners(Sfi);
            IParentWithCollection parent    = CreateParentWithOneChild("parent", "child");

            Assert.That(parent.Children.Count, Is.EqualTo(1));
            IChild oldChild = GetFirstChild(parent.Children);

            listeners.Clear();
            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

            parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id);
            parent.AddChild("new");
            tx.Commit();
            s.Close();
            listeners.Clear();
            s      = OpenSession();
            tx     = s.BeginTransaction();
            parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id);
            IEntity e = oldChild as IEntity;

            if (e != null)
            {
                oldChild = (IChild)s.Get(oldChild.GetType(), e.Id);
            }

            parent.RemoveChild(oldChild);
            tx.Commit();
            s.Close();
            int index = 0;

            if (((IPersistentCollection)parent.Children).WasInitialized)
            {
                CheckResult(listeners, listeners.InitializeCollection, parent, index++);
            }
            ChildWithBidirectionalManyToMany oldChildWithManyToMany = oldChild as ChildWithBidirectionalManyToMany;

            if (oldChildWithManyToMany != null)
            {
                if (((IPersistentCollection)oldChildWithManyToMany.Parents).WasInitialized)
                {
                    CheckResult(listeners, listeners.InitializeCollection, oldChildWithManyToMany, index++);
                }
            }

            CheckResult(listeners, listeners.PreCollectionUpdate, parent, index++);
            CheckResult(listeners, listeners.PostCollectionUpdate, parent, index++);
            if (oldChildWithManyToMany != null)
            {
                CheckResult(listeners, listeners.PreCollectionUpdate, oldChildWithManyToMany, index++);
                CheckResult(listeners, listeners.PostCollectionUpdate, oldChildWithManyToMany, index++);
            }
            CheckNumberOfResults(listeners, index);
        }
예제 #3
0
        public void UpdateParentOneChildDiffCollectionSameChild()
        {
            CollectionListeners   listeners = new CollectionListeners(Sfi);
            IParentWithCollection parent    = CreateParentWithOneChild("parent", "child");
            IChild child = GetFirstChild(parent.Children);

            listeners.Clear();
            Assert.That(parent.Children.Count, Is.EqualTo(1));
            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

            parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id);
            IEntity e = child as IEntity;

            if (e != null)
            {
                child = (IChild)s.Get(child.GetType(), e.Id);
            }
            ICollection <IChild> oldCollection = parent.Children;

            parent.NewChildren(CreateCollection());
            parent.AddChild(child);
            tx.Commit();
            s.Close();
            int index = 0;

            if (((IPersistentCollection)oldCollection).WasInitialized)
            {
                CheckResult(listeners, listeners.InitializeCollection, parent, oldCollection, index++);
            }
            ChildWithBidirectionalManyToMany childWithManyToMany = child as ChildWithBidirectionalManyToMany;

            if (childWithManyToMany != null)
            {
                if (((IPersistentCollection)childWithManyToMany.Parents).WasInitialized)
                {
                    CheckResult(listeners, listeners.InitializeCollection, childWithManyToMany, index++);
                }
            }

            CheckResult(listeners, listeners.PreCollectionRemove, parent, oldCollection, index++);
            CheckResult(listeners, listeners.PostCollectionRemove, parent, oldCollection, index++);
            if (childWithManyToMany != null)
            {
                // hmmm, the same parent was removed and re-added to the child's collection;
                // should this be considered an update?
                CheckResult(listeners, listeners.PreCollectionUpdate, childWithManyToMany, index++);
                CheckResult(listeners, listeners.PostCollectionUpdate, childWithManyToMany, index++);
            }
            CheckResult(listeners, listeners.PreCollectionRecreate, parent, index++);
            CheckResult(listeners, listeners.PostCollectionRecreate, parent, index++);
            CheckNumberOfResults(listeners, index);
        }
        public async Task UpdateParentOneChildDiffCollectionDiffChildAsync()
        {
            CollectionListeners   listeners = new CollectionListeners(Sfi);
            IParentWithCollection parent    = await(CreateParentWithOneChildAsync("parent", "child"));
            IChild oldChild = GetFirstChild(parent.Children);

            listeners.Clear();
            Assert.That(parent.Children.Count, Is.EqualTo(1));
            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

            parent = (IParentWithCollection)await(s.GetAsync(parent.GetType(), parent.Id));
            IEntity e = oldChild as IEntity;
            ChildWithBidirectionalManyToMany oldChildWithManyToMany = null;

            if (e != null)
            {
                oldChildWithManyToMany = await(s.GetAsync(oldChild.GetType(), e.Id)) as ChildWithBidirectionalManyToMany;
            }
            ICollection <IChild> oldCollection = parent.Children;

            parent.NewChildren(CreateCollection());
            IChild newChild = parent.AddChild("new1");

            await(tx.CommitAsync());
            s.Close();
            int index = 0;

            if (((IPersistentCollection)oldCollection).WasInitialized)
            {
                CheckResult(listeners, listeners.InitializeCollection, parent, oldCollection, index++);
            }
            if (oldChildWithManyToMany != null)
            {
                if (((IPersistentCollection)oldChildWithManyToMany.Parents).WasInitialized)
                {
                    CheckResult(listeners, listeners.InitializeCollection, oldChildWithManyToMany, index++);
                }
            }
            CheckResult(listeners, listeners.PreCollectionRemove, parent, oldCollection, index++);
            CheckResult(listeners, listeners.PostCollectionRemove, parent, oldCollection, index++);
            if (oldChildWithManyToMany != null)
            {
                CheckResult(listeners, listeners.PreCollectionUpdate, oldChildWithManyToMany, index++);
                CheckResult(listeners, listeners.PostCollectionUpdate, oldChildWithManyToMany, index++);
                CheckResult(listeners, listeners.PreCollectionRecreate, (ChildWithBidirectionalManyToMany)newChild, index++);
                CheckResult(listeners, listeners.PostCollectionRecreate, (ChildWithBidirectionalManyToMany)newChild, index++);
            }
            CheckResult(listeners, listeners.PreCollectionRecreate, parent, index++);
            CheckResult(listeners, listeners.PostCollectionRecreate, parent, index++);
            CheckNumberOfResults(listeners, index);
        }
예제 #5
0
        public void MoveAllChildrenToDifferentParent()
        {
            CollectionListeners   listeners   = new CollectionListeners(Sfi);
            IParentWithCollection parent      = CreateParentWithOneChild("parent", "child");
            IParentWithCollection otherParent = CreateParentWithOneChild("otherParent", "otherChild");
            IChild child = GetFirstChild(parent.Children);

            listeners.Clear();
            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

            parent      = (IParentWithCollection)s.Get(parent.GetType(), parent.Id);
            otherParent = (IParentWithCollection)s.Get(otherParent.GetType(), otherParent.Id);
            IEntity e = child as IEntity;

            if (e != null)
            {
                child = (IChild)s.Get(child.GetType(), e.Id);
            }
            otherParent.AddAllChildren(parent.Children);
            parent.ClearChildren();
            tx.Commit();
            s.Close();
            int index = 0;

            if (((IPersistentCollection)parent.Children).WasInitialized)
            {
                CheckResult(listeners, listeners.InitializeCollection, parent, index++);
            }
            if (((IPersistentCollection)otherParent.Children).WasInitialized)
            {
                CheckResult(listeners, listeners.InitializeCollection, otherParent, index++);
            }
            ChildWithBidirectionalManyToMany childWithManyToMany = child as ChildWithBidirectionalManyToMany;

            if (childWithManyToMany != null)
            {
                CheckResult(listeners, listeners.InitializeCollection, childWithManyToMany, index++);
            }
            CheckResult(listeners, listeners.PreCollectionUpdate, parent, index++);
            CheckResult(listeners, listeners.PostCollectionUpdate, parent, index++);
            CheckResult(listeners, listeners.PreCollectionUpdate, otherParent, index++);
            CheckResult(listeners, listeners.PostCollectionUpdate, otherParent, index++);
            if (childWithManyToMany != null)
            {
                CheckResult(listeners, listeners.PreCollectionUpdate, childWithManyToMany, index++);
                CheckResult(listeners, listeners.PostCollectionUpdate, childWithManyToMany, index++);
            }
            CheckNumberOfResults(listeners, index);
        }
예제 #6
0
        public virtual void UpdateParentOneToTwoSameChildren()
        {
            CollectionListeners   listeners = new CollectionListeners(Sfi);
            IParentWithCollection parent    = CreateParentWithOneChild("parent", "child");
            IChild child = GetFirstChild(parent.Children);

            Assert.That(parent.Children.Count, Is.EqualTo(1));
            listeners.Clear();
            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id);
                    IEntity e = child as IEntity;
                    if (e != null)
                    {
                        child = (IChild)s.Get(child.GetType(), e.Id);
                    }
                    parent.AddChild(child);
                    tx.Commit();
                }
            int index = 0;

            if (((IPersistentCollection)parent.Children).WasInitialized)
            {
                CheckResult(listeners, listeners.InitializeCollection, parent, index++);
            }
            ChildWithBidirectionalManyToMany childWithManyToMany = child as ChildWithBidirectionalManyToMany;

            if (childWithManyToMany != null)
            {
                if (((IPersistentCollection)childWithManyToMany.Parents).WasInitialized)
                {
                    CheckResult(listeners, listeners.InitializeCollection, childWithManyToMany, index++);
                }
            }

            if (!(parent.Children is PersistentGenericSet <IChild>))
            {
                CheckResult(listeners, listeners.PreCollectionUpdate, parent, index++);
                CheckResult(listeners, listeners.PostCollectionUpdate, parent, index++);
            }
            if (childWithManyToMany != null && !(childWithManyToMany.Parents is PersistentGenericSet <ParentWithBidirectionalManyToMany>))
            {
                CheckResult(listeners, listeners.PreCollectionUpdate, childWithManyToMany, index++);
                CheckResult(listeners, listeners.PostCollectionUpdate, childWithManyToMany, index++);
            }

            CheckNumberOfResults(listeners, index);
        }
        public async Task UpdateParentOneChildToNoneByClearAsync()
        {
            CollectionListeners   listeners = new CollectionListeners(Sfi);
            IParentWithCollection parent    = await(CreateParentWithOneChildAsync("parent", "child"));

            Assert.That(parent.Children.Count, Is.EqualTo(1));
            IChild child = GetFirstChild(parent.Children);

            listeners.Clear();
            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

            parent = (IParentWithCollection)await(s.GetAsync(parent.GetType(), parent.Id));
            IEntity e = child as IEntity;

            if (e != null)
            {
                child = (IChild)await(s.GetAsync(child.GetType(), e.Id));
            }
            parent.ClearChildren();
            await(tx.CommitAsync());
            s.Close();
            int index = 0;

            if (((IPersistentCollection)parent.Children).WasInitialized)
            {
                CheckResult(listeners, listeners.InitializeCollection, parent, index++);
            }
            ChildWithBidirectionalManyToMany childWithManyToMany = child as ChildWithBidirectionalManyToMany;

            if (childWithManyToMany != null)
            {
                if (((IPersistentCollection)childWithManyToMany.Parents).WasInitialized)
                {
                    CheckResult(listeners, listeners.InitializeCollection, childWithManyToMany, index++);
                }
            }
            CheckResult(listeners, listeners.PreCollectionUpdate, parent, index++);
            CheckResult(listeners, listeners.PostCollectionUpdate, parent, index++);
            if (childWithManyToMany != null)
            {
                CheckResult(listeners, listeners.PreCollectionUpdate, childWithManyToMany, index++);
                CheckResult(listeners, listeners.PostCollectionUpdate, childWithManyToMany, index++);
            }
            CheckNumberOfResults(listeners, index);
        }
예제 #8
0
        public void DeleteParentAndChild()
        {
            CollectionListeners   listeners = new CollectionListeners(Sfi);
            IParentWithCollection parent    = CreateParentWithOneChild("parent", "child");
            IChild child = GetFirstChild(parent.Children);

            listeners.Clear();
            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

            parent = (IParentWithCollection)s.Get(parent.GetType(), parent.Id);
            IEntity e = child as IEntity;

            if (e != null)
            {
                child = (IChild)s.Get(child.GetType(), e.Id);
            }
            parent.RemoveChild(child);
            if (e != null)
            {
                s.Delete(child);
            }
            s.Delete(parent);
            tx.Commit();
            s.Close();
            int index = 0;

            CheckResult(listeners, listeners.InitializeCollection, parent, index++);
            ChildWithBidirectionalManyToMany childWithManyToMany = child as ChildWithBidirectionalManyToMany;

            if (childWithManyToMany != null)
            {
                CheckResult(listeners, listeners.InitializeCollection, childWithManyToMany, index++);
            }
            CheckResult(listeners, listeners.PreCollectionRemove, parent, index++);
            CheckResult(listeners, listeners.PostCollectionRemove, parent, index++);
            if (childWithManyToMany != null)
            {
                CheckResult(listeners, listeners.PreCollectionRemove, childWithManyToMany, index++);
                CheckResult(listeners, listeners.PostCollectionRemove, childWithManyToMany, index++);
            }
            CheckNumberOfResults(listeners, index);
        }