public void GetAllEntitiesPopulatesCache()
        {
            // Arrange
            IEnumerable <SimpleEntity> entities = new List <SimpleEntity>
            {
                new SimpleEntity {
                    Id = 10
                },
                new SimpleEntity {
                    Id = 11
                }
            };

            // Create a repository with a mock dbset, so any queries will be done against the list rather than DB
            SimpleEntityRepository entityRepository = CreateSimpleEntityRepositoryWithMockDbSet(entities);

            entityRepository.ClearCache();

            // Get entities to populate the cache
            entityRepository.GetList();

            // Re-create the repository with an empty list in the DbSet
            entityRepository = CreateSimpleEntityRepositoryWithMockDbSet(new List <SimpleEntity>());

            // Act
            // Re-query the repository, we should still get results as the queries should now work against the cache not against the empty DbSet
            IList <SimpleEntity> results = entityRepository.GetList();

            // Assert
            Assert.IsNotNull(results);
            //Assert.AreEqual(2, results.Count);
        }
        public void UpdateMultipleSimpleEntities()
        {
            // Arrange
            const int expectedId1 = 10;
            const int expectedId2 = 10;

            FakeDbContext          dbContext        = new FakeDbContext();
            SimpleEntityRepository entityRepository = new SimpleEntityRepository(dbContext);

            SimpleEntity simpleEntity1 = new SimpleEntity {
                Id = expectedId1
            };
            SimpleEntity simpleEntity2 = new SimpleEntity {
                Id = expectedId2
            };

            // Act
            entityRepository.Update(simpleEntity1);
            entityRepository.Update(simpleEntity2);

            // Assert
            Assert.IsNotNull(entityRepository.DbContext);
            Assert.AreEqual(1, entityRepository.DbContext.ChangeTracker.Entries().Count());
            Assert.AreEqual(EntityState.Modified, entityRepository.DbContext.FindEntity(simpleEntity1.Id).State);
            Assert.AreEqual(EntityState.Modified, entityRepository.DbContext.FindEntity(simpleEntity2.Id).State);
        }
        public void InsertSimpleEntityFollowedByAnUpdate()
        {
            // Arrange
            const string updatedValue = "Updated Value";

            FakeDbContext          dbContext        = new FakeDbContext();
            SimpleEntityRepository entityRepository = new SimpleEntityRepository(dbContext);

            SimpleEntity simpleEntity = new SimpleEntity {
                Id = 1, SomeProperty = "Initial value"
            };

            // Act
            entityRepository.Insert(simpleEntity);

            simpleEntity.SomeProperty = updatedValue;
            entityRepository.Update(simpleEntity);

            // Assert
            Assert.IsNotNull(entityRepository.DbContext);
            Assert.AreEqual(1, entityRepository.DbContext.ChangeTracker.Entries().Count());

            // Ensure the entity in cache is still at a state of 'Added' but has the updated properties
            DbEntityEntry entry = entityRepository.DbContext.ChangeTracker.Entries().ToList()[0];

            Assert.AreEqual(EntityState.Added, entry.State);
            Assert.AreEqual(updatedValue, ((SimpleEntity)entry.Entity).SomeProperty);
        }
        /// <summary>
        /// Tests that when we delete an existing entity, the entity is tracked as 'Deleted' in the context
        /// </summary>
        public void DeleteSimpleEntity()
        {
            // Arrange
            const int expectedId = 10;

            FakeDbContext          dbContext        = new FakeDbContext();
            SimpleEntityRepository entityRepository = new SimpleEntityRepository(dbContext);

            // Act
            entityRepository.Delete(expectedId);

            // Assert
            Assert.IsNotNull(entityRepository.DbContext);
            Assert.AreEqual(1, entityRepository.DbContext.ChangeTracker.Entries().Count());
            Assert.AreEqual(EntityState.Deleted, entityRepository.DbContext.ChangeTracker.Entries().ToList()[0].State);
        }
        public void AddSimpleEntity()
        {
            // Arrange
            FakeDbContext          dbContext        = new FakeDbContext();
            SimpleEntityRepository entityRepository = new SimpleEntityRepository(dbContext);

            SimpleEntity simpleEntity = new SimpleEntity {
                SomeProperty = "Some property"
            };

            // Act
            entityRepository.Insert(simpleEntity);

            // Assert
            Assert.IsNotNull(entityRepository.DbContext);
            Assert.AreEqual(1, entityRepository.DbContext.ChangeTracker.Entries().Count());
            Assert.AreEqual(EntityState.Added, entityRepository.DbContext.ChangeTracker.Entries().ToList()[0].State);
        }
        public void GetNonExistingSingleEntity()
        {
            // Arrange
            // Create a repository with a mock dbset, so any queries will be done against the list rather than DB
            SimpleEntityRepository entityRepository = CreateSimpleEntityRepositoryWithMockDbSet(
                new List <SimpleEntity>
            {
                new SimpleEntity {
                    Id = 10
                }
            }
                );

            // Act
            SimpleEntity entity = entityRepository.GetSingle(se => se.Id == 9999);

            // Assert
            Assert.IsNull(entity);
        }
        /// <summary>
        /// Tests that when we query for an existing entity, it is returned correctly
        /// </summary>
        //[Test]
        public void GetExistingSingleEntity()
        {
            // Arrange
            const int expectedId = 10;

            // Create a repository with a mock dbset, so any queries will be done against the list rather than DB
            SimpleEntityRepository entityRepository = CreateSimpleEntityRepositoryWithMockDbSet(
                new List <SimpleEntity>
            {
                new SimpleEntity {
                    Id = expectedId
                }
            }
                );

            // Act
            SimpleEntity entity = entityRepository.GetSingle(se => se.Id == expectedId);

            // Assert
            Assert.IsNotNull(entity);
            Assert.AreEqual(expectedId, entity.Id);
        }
        public void InsertMultipleSimpleEntities()
        {
            // Arrange
            FakeDbContext          dbContext        = new FakeDbContext();
            SimpleEntityRepository entityRepository = new SimpleEntityRepository(dbContext);

            SimpleEntity simpleEntity1 = new SimpleEntity {
                SomeProperty = "Some property 1"
            };
            SimpleEntity simpleEntity2 = new SimpleEntity {
                SomeProperty = "Some property 2"
            };

            // Act
            entityRepository.Insert(simpleEntity1);
            entityRepository.Insert(simpleEntity2);

            // Assert
            Assert.IsNotNull(entityRepository.DbContext);
            Assert.AreEqual(2, entityRepository.DbContext.ChangeTracker.Entries().Count());
            entityRepository.DbContext.ChangeTracker.Entries().ToList().ForEach(entry => Assert.AreEqual(EntityState.Added, entry.State));
        }