public void Persist_Method() { Assert.Throws <ArgumentNullException>(() => new MemoryRepository <TestEntity>().Persist(null)); var entity = new TestEntity { Name = "first" }; using (var repository = new MemoryRepository <TestEntity>()) { Assert.False(repository.Any()); repository.Transaction(() => Assert.True(ReferenceEquals(repository.Persist(entity), repository))); entity = repository.Single(); Assert.Equal(0, entity.Id); Assert.Equal("first", entity.Name); entity.Name = "second"; repository.Transaction(() => repository.Persist(entity)); entity = repository.Single(); Assert.Equal(0, entity.Id); Assert.Equal("second", entity.Name); Assert.Equal(1, repository.Count(x => x.Name == "second")); Assert.Equal("second", repository.Single(x => x.Name == "second").Name); } }
public void Dispose_Method() { var entity = new TestEntity(); using (var repository = new MemoryRepository <TestEntity>()) { repository.Persist(entity).Dispose(); Assert.True(ReferenceEquals(repository.Single(), entity)); repository.Delete(entity).Dispose(); Assert.False(repository.Any()); } }
public void DeleteAll_Method() { using (var repository = new MemoryRepository <TestEntity>()) { repository.Transaction(() => Assert.True(ReferenceEquals(repository.DeleteAll(), repository))); Assert.False(repository.Any()); repository.Transaction(() => repository.Persist(new TestEntity()).Persist(new TestEntity())); Assert.Equal(2, repository.Count()); repository.Transaction(() => repository.DeleteAll()); Assert.False(repository.Any()); } }
public void GetEnumerator_Method() { var entity = new TestEntity(); using (var repository = new MemoryRepository <TestEntity>()) { Assert.False(repository.GetEnumerator().MoveNext()); repository.Transaction(() => repository.Persist(entity)); var enumerator = repository.GetEnumerator(); Assert.True(enumerator.MoveNext()); Assert.False(enumerator.MoveNext()); } }
public void Delete_Method() { Assert.Throws <ArgumentNullException>(() => new MemoryRepository <TestEntity>().Delete(null)); var entity = new TestEntity(); using (var repository = new MemoryRepository <TestEntity>()) { repository.Transaction(() => Assert.True(ReferenceEquals(repository.Delete(entity), repository))); repository.Transaction(() => repository.Persist(entity)); repository.Transaction(() => repository.Delete(new TestEntity())); Assert.True(ReferenceEquals(repository.Single(), entity)); repository.Transaction(() => repository.Delete(entity)); Assert.False(repository.Any()); } }
public void Refresh_Method() { Assert.Throws <ArgumentNullException>(() => new MemoryRepository <TestEntity>().Refresh(null)); var entity = new TestEntity { Name = "first" }; using (var repository = new MemoryRepository <TestEntity>()) { Assert.True(ReferenceEquals(repository.Refresh(entity), repository)); Assert.Equal(0, entity.Id); Assert.Equal("first", entity.Name); repository.Transaction(() => repository.Persist(entity)); entity.Name = "second"; repository.Refresh(entity); Assert.Equal(0, entity.Id); Assert.Equal("second", entity.Name); } }
public void Transaction_Method() { var entity = new TestEntity(); using (var repository = new MemoryRepository <TestEntity>()) { using (repository.Transaction()) { repository.Persist(entity); } Assert.Equal(1, repository.Count()); using (var transaction = repository.Transaction()) { repository.Persist(entity); transaction.Rollback(); } Assert.Equal(1, repository.Count()); try { using (repository.Transaction()) { repository.Persist(entity); throw new Exception(); } } catch { Assert.True(true); } Assert.Equal(1, repository.Count()); using (var transaction = repository.Transaction()) { repository.Persist(entity); transaction.Commit(); } Assert.Equal(1, repository.Count()); using (repository.Transaction()) { repository.Delete(entity); } Assert.Equal(0, repository.Count()); using (var transaction = repository.Transaction()) { repository.Delete(entity); transaction.Rollback(); } Assert.Equal(0, repository.Count()); try { using (repository.Transaction()) { repository.Delete(entity); throw new Exception(); } } catch { Assert.True(true); } Assert.Equal(0, repository.Count()); using (var transaction = repository.Transaction()) { repository.Delete(entity); transaction.Commit(); } Assert.Equal(0, repository.Count()); } }