Esempio n. 1
0
        public void RemoveRecursiveChildFromRootThenSavingTest()
        {
            //Arrange
            int removedId               = 42;
            int childOfRemovedId        = 43;
            int childOfChildOfRemovedId = 44;

            Quest savedRoot = new FakeQuest()
            {
                Id = TopRootId
            };

            List <Quest> rootChildren = new List <Quest>()
            {
                new FakeQuest
                {
                    Id = removedId
                }
            };

            Quest toRemove        = rootChildren[0];
            Quest childOfToRemove = new FakeQuest
            {
                Id = childOfRemovedId,
            };
            Quest childOfChildToRemove = new FakeQuest
            {
                Id = childOfChildOfRemovedId
            };

            childOfToRemove.ParentId      = removedId;
            childOfChildToRemove.ParentId = childOfRemovedId;

            IQuestFactory factory = MockRepository.GenerateStrictMock <IQuestFactory>();

            IQuestDataLayer dataLayer = MockRepository.GenerateStrictMock <IQuestDataLayer>();

            dataLayer.Expect(dl => dl.Get(Arg <int> .Is.Equal(TopRootId))).
            Repeat.Once().
            Return(savedRoot);
            dataLayer.Expect(dl => dl.Open()).
            Repeat.Once();
            dataLayer.Expect(dl => dl.GetAll(Arg <int> .Is.Equal(TopRootId))).
            Repeat.Once().
            Return(rootChildren);
            dataLayer.Expect(dl => dl.IsClosed()).
            Repeat.Once().
            Return(false);
            dataLayer.Expect(dl => dl.Dispose()).
            Repeat.Once();

            #region  Mocks logic that loads all child hierarchy before deleting any quest

            dataLayer.Expect(dl => dl.Open()).
            Repeat.Times(1);
            dataLayer.Expect(dl => dl.IsClosed()).
            Repeat.Times(1).
            Return(false);
            dataLayer.Expect(dl => dl.GetAll(Arg <int> .Is.Equal(removedId))).
            Repeat.Once().
            Return(new List <Quest>()
            {
                childOfToRemove
            });
            dataLayer.Expect(dl => dl.Dispose()).
            Repeat.Times(1);

            dataLayer.Expect(dl => dl.Open()).
            Repeat.Times(1);
            dataLayer.Expect(dl => dl.IsClosed()).
            Repeat.Times(1).
            Return(false);
            dataLayer.Expect(dl => dl.GetAll(Arg <int> .Is.Equal(childOfRemovedId))).
            Repeat.Once().
            Return(new List <Quest>()
            {
                childOfChildToRemove
            });
            dataLayer.Expect(dl => dl.Dispose()).
            Repeat.Times(1);

            dataLayer.Expect(dl => dl.Open()).
            Repeat.Times(1);
            dataLayer.Expect(dl => dl.IsClosed()).
            Repeat.Times(1).
            Return(false);
            dataLayer.Expect(dl => dl.GetAll(Arg <int> .Is.Equal(childOfChildOfRemovedId))).
            Repeat.Once().
            Return(null);
            dataLayer.Expect(dl => dl.Dispose()).
            Repeat.Times(1);

            #endregion

            dataLayer.Expect(dl => dl.Open()).
            Repeat.Once();
            dataLayer.Expect(dl => dl.Delete(Arg <int> .Is.Equal(removedId))).
            Repeat.Once();
            dataLayer.Expect(dl => dl.Delete(Arg <int> .Is.Equal(childOfRemovedId))).
            Repeat.Once();
            dataLayer.Expect(dl => dl.Delete(Arg <int> .Is.Equal(childOfChildOfRemovedId))).
            Repeat.Once();
            dataLayer.Expect(dl => dl.Dispose()).
            Repeat.Once();

            QuestTreeInMemory tree = new QuestTreeInMemory(dataLayer, factory);

            //Act
            tree.Initialize();
            tree.RemoveChild(tree.Root, toRemove);
            tree.Save();
            Quest root = tree.Root;

            //Assert
            Quest removedInRootChildren = root.Children.Find(q => q == toRemove);
            Assert.IsNull(removedInRootChildren);
            Assert.AreEqual(TopRootId, toRemove.ParentId);
            Assert.AreEqual(null, toRemove.Parent);

            dataLayer.VerifyAllExpectations();
            factory.VerifyAllExpectations();
        }
Esempio n. 2
0
        public void RemoveChildFromRootAndSaveTest()
        {
            //Arrange
            int removedId = 42;

            Quest savedRoot = new FakeQuest()
            {
                Id = TopRootId
            };

            List <Quest> rootChildren = new List <Quest>()
            {
                new FakeQuest()
                {
                    Id = removedId
                }
            };

            Quest toRemove = rootChildren[0];

            IQuestFactory factory = MockRepository.GenerateStrictMock <IQuestFactory>();

            IQuestDataLayer dataLayer = MockRepository.GenerateStrictMock <IQuestDataLayer>();

            dataLayer.Expect(dl => dl.Get(Arg <int> .Is.Equal(TopRootId))).
            Repeat.Once().
            Return(savedRoot);
            dataLayer.Expect(dl => dl.Open()).
            Repeat.Once();
            dataLayer.Expect(dl => dl.GetAll(Arg <int> .Is.Equal(TopRootId))).
            Repeat.Once().
            Return(rootChildren);
            dataLayer.Expect(dl => dl.IsClosed()).
            Repeat.Once().
            Return(false);
            dataLayer.Expect(dl => dl.Dispose()).
            Repeat.Once();

            dataLayer.Expect(dl => dl.Open()).
            Repeat.Times(1);
            dataLayer.Expect(dl => dl.IsClosed()).
            Repeat.Times(1).
            Return(false);
            dataLayer.Expect(dl => dl.GetAll(Arg <int> .Is.Equal(removedId))).
            Repeat.Once().
            Return(null);
            dataLayer.Expect(dl => dl.Dispose()).
            Repeat.Times(1);

            dataLayer.Expect(dl => dl.Open()).
            Repeat.Once();
            dataLayer.Expect(dl => dl.Delete(Arg <int> .Is.Equal(removedId))).
            Repeat.Once();
            dataLayer.Expect(dl => dl.Dispose()).
            Repeat.Once();

            QuestTreeInMemory tree = new QuestTreeInMemory(dataLayer, factory);

            //Act
            tree.Initialize();
            tree.RemoveChild(tree.Root, toRemove);
            tree.Save();
            Quest root = tree.Root;

            //Assert
            Quest removedInRootChildren = root.Children.Find(q => q == toRemove);

            Assert.IsNull(removedInRootChildren);
            Assert.AreEqual(TopRootId, toRemove.ParentId);
            Assert.AreEqual(null, toRemove.Parent);

            Quest removedInTree = tree.Get(q => q == toRemove);

            Assert.IsNull(removedInTree);

            dataLayer.VerifyAllExpectations();
            factory.VerifyAllExpectations();
        }
Esempio n. 3
0
        public void AddRemoveChildThenSaveTest()
        {
            //Arrange
            int addedId = 42;

            Quest savedRoot = new FakeQuest()
            {
                Id = TopRootId
            };

            List <Quest> rootChildren = new List <Quest>()
            {
                new FakeQuest()
                {
                    Id = 1
                }
            };

            Quest toAdd = new FakeQuest()
            {
                Id = addedId
            };

            IQuestFactory factory = MockRepository.GenerateStrictMock <IQuestFactory>();

            IQuestDataLayer dataLayer = MockRepository.GenerateStrictMock <IQuestDataLayer>();

            dataLayer.Expect(dl => dl.Open()).
            Repeat.Once();
            dataLayer.Expect(dl => dl.Get(Arg <int> .Is.Equal(TopRootId))).
            Repeat.Once().
            Return(savedRoot);
            dataLayer.Expect(dl => dl.IsClosed()).
            Repeat.Once().
            Return(false);
            dataLayer.Expect(dl => dl.GetAll(Arg <int> .Is.Equal(TopRootId))).
            Repeat.Once().
            Return(rootChildren);
            dataLayer.Expect(dl => dl.Dispose()).
            Repeat.Once();

            dataLayer.Expect(dl => dl.Open()).
            Repeat.Times(1);
            dataLayer.Expect(dl => dl.IsClosed()).
            Repeat.Times(1).
            Return(false);
            dataLayer.Expect(dl => dl.GetAll(Arg <int> .Is.Equal(addedId))).
            Repeat.Once().
            Return(null);
            dataLayer.Expect(dl => dl.Dispose()).
            Repeat.Times(1);

            dataLayer.Expect(dl => dl.InsertAll(Arg <HashSet <Quest> > .Is.Anything)).
            IgnoreArguments().
            Repeat.Never();
            dataLayer.Expect(dl => dl.Delete(Arg <int> .Is.Anything)).
            IgnoreArguments().
            Repeat.Never();

            QuestTreeInMemory tree = new QuestTreeInMemory(dataLayer, factory);

            //Act
            tree.Initialize();
            tree.AddChild(tree.Root, toAdd);
            tree.RemoveChild(tree.Root, toAdd);
            tree.Save();

            Quest root = tree.Root;

            //Assert
            Quest added = root.Children.Find(q => q.Id == addedId);

            Assert.IsNull(added);
            Assert.AreEqual(TopRootId, toAdd.ParentId);
            Assert.AreEqual(null, toAdd.Parent);

            Quest addedFromTree = tree.Get(q => q.Id == addedId);

            Assert.IsNull(addedFromTree);

            dataLayer.VerifyAllExpectations();
            factory.VerifyAllExpectations();
        }