Exemplo n.º 1
0
        public void InMemoryRepository_Save_UpdatesExistingItem()
        {
            _repository = new InMemoryRepository <StoreableEntity>();
            var itemToBeSaved = new StoreableEntity()
            {
                Id = 1, Name = "ItemToBeSaved"
            };

            _repository.Save(itemToBeSaved);

            _repository.Save(itemToBeSaved);

            _result = _repository.All();
            Assert.IsTrue(((IEnumerable <StoreableEntity>)_result).All(c => c == itemToBeSaved));
        }
Exemplo n.º 2
0
        public void Test_Find()
        {
            repository = new InMemoryRepository <Storeable>();
            var firstItem = new Storeable {
                Id = 1, Name = "First Item"
            };
            var secondItem = new Storeable {
                Id = 2, Name = "Second Item"
            };
            var thirdItem = new Storeable {
                Id = 3, Name = "Third Item"
            };

            repository.Save(firstItem);
            repository.Save(secondItem);
            repository.Save(thirdItem);

            var result = repository.FindById(2);

            Assert.AreEqual(secondItem, result);
        }
Exemplo n.º 3
0
        public void Save_1Entity()
        {
            var entity = new Book()
            {
                Id = 1
            };
            var booksList = new List <Book>();
            var inMemoryBookRepository = new InMemoryRepository <Book>(booksList);

            inMemoryBookRepository.Save(entity);
            Assert.NotEmpty(booksList);
        }
Exemplo n.º 4
0
        public void Test_Save()
        {
            repository = new InMemoryRepository <Storeable>();
            var newitem = new Storeable {
                Id = 1, Name = "New Item"
            };

            repository.Save(newitem);

            var result = repository.All();

            Assert.IsTrue(((IEnumerable <Storeable>)result).Contains(newitem));
        }
Exemplo n.º 5
0
        public void InMemoryRepository_FindById_ReturnsCorrectItem()
        {
            _repository = new InMemoryRepository <StoreableEntity>();

            var firstItem = new StoreableEntity()
            {
                Id = 1, Name = "firstItem"
            };

            _repository.Save(firstItem);

            var secondItem = new StoreableEntity()
            {
                Id = 2, Name = "SecondItem"
            };

            _repository.Save(secondItem);

            _result = _repository.FindById(1);

            Assert.AreEqual(firstItem, _result);
        }
Exemplo n.º 6
0
        public void Save_EntityIsExisting_ThrowsArgumentNullException()
        {
            var entity = new Book()
            {
                Id = 1
            };
            var booksList = new List <Book>()
            {
                entity
            };
            var inMemoryBookRepository = new InMemoryRepository <Book>(booksList);

            Assert.Throws <InvalidOperationException>(() => inMemoryBookRepository.Save(entity));
        }
Exemplo n.º 7
0
        public void InMemoryRepository_Save_AddsNewItem()
        {
            _repository = new InMemoryRepository <StoreableEntity>();

            var itemToBeSaved = new StoreableEntity()
            {
                Id = 1, Name = "ItemToBeSaved"
            };

            _repository.Save(itemToBeSaved);

            _result = _repository.All();
            Assert.IsTrue(((IEnumerable <StoreableEntity>)_result).Contains(itemToBeSaved));
        }
Exemplo n.º 8
0
        public void InMemoryRepository_Delete_RemovesExistingItem()
        {
            _repository = new InMemoryRepository <StoreableEntity>();
            var itemToBeSaved = new StoreableEntity()
            {
                Id = 1, Name = "ItemToBeSaved"
            };

            _repository.Save(itemToBeSaved);

            _repository.Delete(itemToBeSaved.Id);

            _result = _repository.All();
            Assert.IsFalse(((IEnumerable <StoreableEntity>)_result).Contains(itemToBeSaved));
        }
Exemplo n.º 9
0
        public void AddObject()
        {
            // Arrange
            InMemoryRepository <Object1> controller = new InMemoryRepository <Object1>();

            // Act
            Object1 test = GetObject1("Test1", 4, true);

            controller.Save(test);
            controller.Commmit();
            Dictionary <Guid, Object1> results = controller.dbRepository;

            // Assert
            CollectionAssert.Contains(results.Values, test);
        }
Exemplo n.º 10
0
        public void Update()
        {
            InMemoryRepository <Object1> controller = new InMemoryRepository <Object1>();
            Object1 test = GetObject1("Test1", 4, true);

            // Act
            controller.Save(test);
            controller.Commmit();
            test.Item2 = 5;
            controller.Update(test);
            controller.Commmit();
            Object1 result = controller.FindById(test.Id);

            // Assert
            Assert.AreEqual(result.Item2, test.Item2);
        }
Exemplo n.º 11
0
        public void Add_SingleItem_ContainsExactItem()
        {
            // Arrange - setup quick mock storable
            Mock <IStoreable> storableMock = new Mock <IStoreable>();

            storableMock.Setup(i => i.Id).Returns(validId);
            IStoreable item = storableMock.Object;

            // Act - save item to repository
            InMemoryRepository <IStoreable> repository = new InMemoryRepository <IStoreable>();

            repository.Save(item);

            // Assert - check that the collection contains the correct item
            ICollection <IStoreable> collection = repository.All() as ICollection <IStoreable>;

            Assert.IsTrue(collection.Contains(item));
        }
Exemplo n.º 12
0
        public void GetAll()
        {
            // Arrange
            InMemoryRepository <Object1> controller = new InMemoryRepository <Object1>();
            List <Object1> objectList = GetListObjects();

            // Act
            foreach (Object1 item in objectList)
            {
                controller.Save(item);
            }
            controller.Commmit();
            IEnumerable <Object1> results    = controller.All();
            IEnumerable <Object1> resultTest = controller.dbRepository.Values;

            // Assert
            CollectionAssert.AreEqual(results, resultTest);
        }
Exemplo n.º 13
0
        public void Add_SingleItem_RetunsCorrectCount()
        {
            // Arrange - setup quick mock storable
            Mock <IStoreable> storableMock = new Mock <IStoreable>();

            storableMock.Setup(i => i.Id).Returns(validId);
            IStoreable item = storableMock.Object;

            // Act - save item to repository
            InMemoryRepository <IStoreable> repository = new InMemoryRepository <IStoreable>();

            repository.Save(item);

            // Assert - check that the collection contains exactly one item
            ICollection <IStoreable> collection = repository.All() as ICollection <IStoreable>;

            Assert.AreEqual(1, collection.Count);
        }
Exemplo n.º 14
0
        public void FindById()
        {
            // Arrange
            InMemoryRepository <Object1> controller = new InMemoryRepository <Object1>();
            List <Object1> objectList = GetListObjects();

            // Act
            foreach (Object1 item in objectList)
            {
                controller.Save(item);
            }
            Object1 test = objectList[1];

            controller.Commmit();
            Object1 results = controller.FindById(test.Id);

            // Assert
            Assert.AreEqual(results, test);
        }
Exemplo n.º 15
0
        public void FindById_InvalidId_ReturnsNull()
        {
            // Arrange - setup quick mock storable
            Mock <IStoreable> storableMock = new Mock <IStoreable>();

            storableMock.Setup(i => i.Id).Returns(validId);
            IStoreable item = storableMock.Object;

            // Save the item to the repository
            InMemoryRepository <IStoreable> repository = new InMemoryRepository <IStoreable>();

            repository.Save(item);

            // Act - find item by Id used
            IStoreable storeable = repository.FindById(invalidId);

            // Assert - check that the storeable variable has a value
            Assert.IsNull(storeable);
        }
Exemplo n.º 16
0
        public void Delete()
        {
            // Arrange
            InMemoryRepository <Object1> controller = new InMemoryRepository <Object1>();
            Object1 test = GetObject1("Test1", 4, true);

            // Act
            controller.Save(test);
            IEnumerable <Object1> resultTest = controller.dbRepository.Values;

            controller.Commmit();
            CollectionAssert.Contains(resultTest, test);
            controller.Delete(test.Id);
            controller.Commmit();

            resultTest = controller.dbRepository.Values;

            // Assert
            CollectionAssert.DoesNotContain(resultTest, test);
        }
Exemplo n.º 17
0
        public void Delete_InvalidId_RemovesNoItem()
        {
            // Arrange - setup quick mock storable
            Mock <IStoreable> storableMock = new Mock <IStoreable>();

            storableMock.Setup(i => i.Id).Returns(validId);
            IStoreable item = storableMock.Object;

            // Save the item to the repository
            InMemoryRepository <IStoreable> repository = new InMemoryRepository <IStoreable>();

            repository.Save(item);

            // Act - try to delete an invalid item from the repository
            repository.Delete(invalidId);

            // Assert - check that the collection does not contain the item
            ICollection <IStoreable> collection = repository.All() as ICollection <IStoreable>;

            Assert.IsTrue(collection.Contains(item));
        }
Exemplo n.º 18
0
        public void Rollback()
        {
            InMemoryRepository <Object1> controller = new InMemoryRepository <Object1>();
            List <Object1> objectList = GetListObjects();


            // Act
            foreach (Object1 item in objectList)
            {
                controller.Save(item);
            }
            controller.Delete(objectList[2].Id);
            objectList[2].Item2 = 2;
            controller.Update(objectList[2]);
            controller.RollBack();
            controller.Commmit();
            IEnumerable <Object1> check = controller.All();

            // Act

            // Assert
            CollectionAssert.IsEmpty(check);
        }
Exemplo n.º 19
0
        public void CompletedTest()
        {
            // Arrange
            InMemoryRepository <Object1> controller = new InMemoryRepository <Object1>();
            List <Object1>        objectList        = GetListObjects();
            IEnumerable <Object1> repoList          = controller.dbRepository.Values;

            // Save Act
            foreach (Object1 item in objectList)
            {
                controller.Save(item);
            }

            // Delete Act
            controller.Delete(objectList[2].Id);
            objectList[0].Item2 = 25;
            controller.Update(objectList[0]);
            controller.Commmit();

            //Embty Assert
            CollectionAssert.Contains(repoList, objectList[0]);
            CollectionAssert.Contains(repoList, objectList[1]);
            CollectionAssert.DoesNotContain(repoList, objectList[2]);
        }
Exemplo n.º 20
0
        public void InMemoryRepository_Save_ThrowsArgumentNullException_WhenEmptyItemIsPassed()
        {
            _repository = new InMemoryRepository <StoreableEntity>();

            Assert.Throws <ArgumentNullException>(() => _repository.Save(null));
        }