public void Delete_Method()
    {
      Assert.Throws<ArgumentNullException>(() => new EFCodeFirstRepository<TestEntity>(new TestContext()).Delete(null));

      var entity = new TestEntity();

      using (var repository = new EFCodeFirstRepository<TestEntity>(new TestContext()))
      {
        Assert.Throws<InvalidOperationException>(() => repository.Delete(entity));
        Assert.False(repository.Persist(entity).Delete(entity).Commit().Any());
      }
    }
    public void Commit_Method()
    {
      var entity = new TestEntity();

      using (var repository = new EFCodeFirstRepository<TestEntity>(new TestContext()))
      {
        Assert.False(repository.Any());

        Assert.True(ReferenceEquals(repository.Persist(entity).Commit(), repository));
        Assert.True(ReferenceEquals(repository.Single(), entity));

        repository.Delete(entity);
        Assert.True(ReferenceEquals(repository.Single(), entity));

        repository.Commit();
        Assert.False(repository.Any());
      }
    }
    public void Commit_Method()
    {
      var entity = new TestEntity();

      using (var repository = new NHibernateRepository<TestEntity>(this.configuration))
      {
        Assert.False(repository.Any());

        Assert.True(ReferenceEquals(repository.Persist(entity).Commit(), repository));
        Assert.True(ReferenceEquals(repository.Single(), entity));

        repository.Delete(entity);
        Assert.True(ReferenceEquals(repository.Single(), entity));

        repository.Commit();
        Assert.False(repository.Any());
      }
    }
    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 Commit_Method()
    {
      var entity = new TestEntity();

      using (var repository = new LinqToSqlRepository<TestEntity>(this.Connection()))
      {
        Assert.False(repository.Any());

        Assert.True(ReferenceEquals(repository.Persist(entity).Commit(), repository));
        Assert.True(ReferenceEquals(repository.Single(), entity));

        repository.Delete(entity);
        Assert.True(ReferenceEquals(repository.Single(), entity));

        repository.Commit();
        Assert.False(repository.Any());
      }
    }
    public void Delete_Method()
    {
      Assert.Throws<ArgumentNullException>(() => ObjectExtensions.Delete<TestEntity>(null));
      Assert.Throws<InvalidOperationException>(() => ServiceLocator.Current.GetInstance<IRepository<TestEntity>>());

      ServiceLocator.SetLocatorProvider(() => this.serviceLocator);

      var entity = new TestEntity();
      using (var repository = ServiceLocator.Current.GetInstance<IRepository<TestEntity>>())
      {
        repository.Transaction(() => Assert.True(ReferenceEquals(entity.Delete(), entity)));
        repository.Transaction(() => entity.Persist());
        repository.Transaction(() => new TestEntity().Delete());
        Assert.True(ReferenceEquals(repository.Single(), entity));

        repository.Transaction(() => entity.Delete());
        Assert.False(repository.Any());
      }
    }
    public void Refresh_Method()
    {
      Assert.Throws<ArgumentNullException>(() => ObjectExtensions.Refresh<TestEntity>(null));
      Assert.Throws<InvalidOperationException>(() => ServiceLocator.Current.GetInstance<IRepository<TestEntity>>());

      ServiceLocator.SetLocatorProvider(() => this.serviceLocator);

      var entity = new TestEntity { Name = "first" };
      using (var repository = ServiceLocator.Current.GetInstance<IRepository<TestEntity>>())
      {
        Assert.True(ReferenceEquals(entity.Refresh(), entity));
        Assert.Equal(0, entity.Id);
        Assert.Equal("first", entity.Name);

        repository.Transaction(() => entity.Persist());
        var originalId = entity.Id;
        var originalName = entity.Name;

        entity.Name = "second";
        entity.Refresh();
        Assert.Equal(originalId, entity.Id);
        Assert.Equal(originalName, entity.Name);
      }
    }
    public void Persist_Method()
    {
      Assert.Throws<ArgumentNullException>(() => ObjectExtensions.Persist<TestEntity>(null));
      Assert.Throws<InvalidOperationException>(() => ServiceLocator.Current.GetInstance<IRepository<TestEntity>>());

      ServiceLocator.SetLocatorProvider(() => this.serviceLocator);

      var entity = new TestEntity { Name = "first" };
      using (var repository = ServiceLocator.Current.GetInstance<IRepository<TestEntity>>())
      {
        Assert.False(repository.Any());

        repository.Transaction(() => Assert.True(ReferenceEquals(entity.Persist(), entity)));
        entity = repository.Single();
        Assert.NotEqual(0, entity.Id);
        Assert.Equal("first", entity.Name);

        entity.Name = "second";
        repository.Transaction(() => entity.Persist());
        entity = repository.Single();
        Assert.NotEqual(0, entity.Id);
        Assert.Equal("second", entity.Name);
      }
    }
Exemplo n.º 9
0
        public void Refresh_Method()
        {
            Assert.Throws <ArgumentNullException>(() => new NHibernateRepository <TestEntity>(this.configuration).Refresh(null));

            var entity = new TestEntity {
                Name = "first"
            };

            using (var repository = new NHibernateRepository <TestEntity>(this.configuration))
            {
                Assert.True(ReferenceEquals(repository.Refresh(entity), repository));
                Assert.Equal(0, entity.Id);
                Assert.Equal("first", entity.Name);

                repository.Transaction(() => repository.Persist(entity));
                var originalId   = entity.Id;
                var originalName = entity.Name;

                entity.Name = "second";
                repository.Refresh(entity);
                Assert.Equal(originalId, entity.Id);
                Assert.Equal(originalName, entity.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 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 Dispose_Method()
    {
      var entity = new TestEntity();

      using (var repository = new EFCodeFirstRepository<TestEntity>(new TestContext()))
      {
        repository.Persist(entity).Dispose();
        Assert.Throws<InvalidOperationException>(() => repository.Single());

        repository.Dispose();
      }
    }
    public void Refresh_Method()
    {
      Assert.Throws<ArgumentNullException>(() => new LinqToSqlRepository<TestEntity>(this.Connection()).Refresh(null));

      var entity = new TestEntity { Name = "first" };

      using (var repository = new LinqToSqlRepository<TestEntity>(this.Connection()))
      {
        Assert.Throws<ArgumentException>(() => repository.Refresh(entity));

        repository.Persist(entity).Commit();
        var originalId = entity.Id;
        var originalName = entity.Name;

        entity.Name = "second";
        Assert.True(ReferenceEquals(repository.Refresh(entity), repository));
        Assert.Equal(originalId, entity.Id);
        Assert.Equal(originalName, entity.Name);
      }
    }
    public void Transaction_Method()
    {
      var entity = new TestEntity();

      using (var repository = new NHibernateRepository<TestEntity>(this.configuration))
      {
        using (repository.Transaction())
        {
          repository.Persist(new TestEntity());
        }
        Assert.Equal(0, repository.Count());

        using (var transaction = repository.Transaction())
        {
          repository.Persist(new TestEntity());
          transaction.Rollback();
        }
        Assert.Equal(0, repository.Count());

        try
        {
          using (repository.Transaction())
          {
            repository.Persist(new TestEntity());
            throw new Exception();
          }
        }
        catch
        {
          Assert.True(true);
        }
        Assert.Equal(0, 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(1, repository.Count());

        using (var transaction = repository.Transaction())
        {
          repository.Delete(entity);
          transaction.Rollback();
        }
        Assert.Equal(1, repository.Count());

        try
        {
          using (repository.Transaction())
          {
            repository.Delete(entity);
            throw new Exception();
          }
        }
        catch
        {
          Assert.True(true);
        }
        Assert.Equal(1, repository.Count());

        using (var transaction = repository.Transaction())
        {
          repository.Delete(entity);
          transaction.Commit();
        }
        Assert.Equal(0, repository.Count());
      }
    }
    public void Persist_Method()
    {
      Assert.Throws<ArgumentNullException>(() => new NHibernateRepository<TestEntity>(this.configuration).Persist(null));

      var entity = new TestEntity { Name = "first" };

      using (var repository = new NHibernateRepository<TestEntity>(this.configuration))
      {
        Assert.False(repository.Any());

        repository.Transaction(() => Assert.True(ReferenceEquals(repository.Persist(entity), repository)));
        entity = repository.Single();
        Assert.NotEqual(0, entity.Id);
        Assert.Equal("first", entity.Name);

        entity.Name = "second";
        repository.Transaction(() => repository.Persist(entity));
        entity = repository.Single();
        Assert.NotEqual(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 Persist_Method()
    {
      Assert.Throws<ArgumentNullException>(() => new EFCodeFirstRepository<TestEntity>(new TestContext()).Persist(null));

      var entity = new TestEntity { Name = "first" };

      using (var repository = new EFCodeFirstRepository<TestEntity>(new TestContext()))
      {
        Assert.False(repository.Any());

        Assert.True(ReferenceEquals(repository.Persist(entity), repository));
        repository.Commit();
        entity = repository.Single();
        Assert.NotEqual(0, entity.Id);
        Assert.Equal("first", entity.Name);

        entity.Name = "second";
        repository.Commit();
        entity = repository.Single();
        Assert.NotEqual(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 GetEnumerator_Method()
    {
      var entity = new TestEntity();

      using (var repository = new EFCodeFirstRepository<TestEntity>(new TestContext()))
      {
        Assert.False(repository.GetEnumerator().MoveNext());

        repository.Persist(entity).Commit();
        var enumerator = repository.GetEnumerator();
        Assert.True(enumerator.MoveNext());
        Assert.False(enumerator.MoveNext());
      }
    }
    public void Dispose_Method()
    {
      var entity = new TestEntity();

      using (var repository = new NHibernateRepository<TestEntity>(this.configuration))
      {
        repository.Persist(entity).Dispose();
        Assert.Throws<ObjectDisposedException>(() => repository.Single());

        repository.Dispose();
      }
    }
    public void GetEnumerator_Method()
    {
      var entity = new TestEntity();

      using (var repository = new NHibernateRepository<TestEntity>(this.configuration))
      {
        Assert.False(repository.GetEnumerator().MoveNext());

        repository.Transaction(() => repository.Persist(entity));
        var enumerator = repository.GetEnumerator();
        Assert.True(enumerator.MoveNext());
        Assert.False(enumerator.MoveNext());
      }
    }
    public void Refresh_Method()
    {
      Assert.Throws<ArgumentNullException>(() => new EFCodeFirstRepository<TestEntity>(new TestContext()).Refresh(null));

      var entity = new TestEntity { Name = "first" };

      using (var repository = new EFCodeFirstRepository<TestEntity>(new TestContext()))
      {
        Assert.Throws<InvalidOperationException>(() => repository.Refresh(entity));

        repository.Persist(entity).Commit();
        var originalId = entity.Id;
        var originalName = entity.Name;

        entity.Name = "second";
        Assert.True(ReferenceEquals(repository.Refresh(entity), repository));
        Assert.Equal(originalId, entity.Id);
        Assert.Equal(originalName, entity.Name);
      }
    }
    public void Refresh_Method()
    {
      Assert.Throws<ArgumentNullException>(() => new NHibernateRepository<TestEntity>(this.configuration).Refresh(null));

      var entity = new TestEntity { Name = "first" };

      using (var repository = new NHibernateRepository<TestEntity>(this.configuration))
      {
        Assert.True(ReferenceEquals(repository.Refresh(entity), repository));
        Assert.Equal(0, entity.Id);
        Assert.Equal("first", entity.Name);

        repository.Transaction(() => repository.Persist(entity));
        var originalId = entity.Id;
        var originalName = entity.Name;

        entity.Name = "second";
        repository.Refresh(entity);
        Assert.Equal(originalId, entity.Id);
        Assert.Equal(originalName, entity.Name);
      }
    }
Exemplo n.º 22
0
        public void Transaction_Method()
        {
            var entity = new TestEntity();

            using (var repository = new NHibernateRepository <TestEntity>(this.configuration))
            {
                using (repository.Transaction())
                {
                    repository.Persist(new TestEntity());
                }
                Assert.Equal(0, repository.Count());

                using (var transaction = repository.Transaction())
                {
                    repository.Persist(new TestEntity());
                    transaction.Rollback();
                }
                Assert.Equal(0, repository.Count());

                try
                {
                    using (repository.Transaction())
                    {
                        repository.Persist(new TestEntity());
                        throw new Exception();
                    }
                }
                catch
                {
                    Assert.True(true);
                }
                Assert.Equal(0, 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(1, repository.Count());

                using (var transaction = repository.Transaction())
                {
                    repository.Delete(entity);
                    transaction.Rollback();
                }
                Assert.Equal(1, repository.Count());

                try
                {
                    using (repository.Transaction())
                    {
                        repository.Delete(entity);
                        throw new Exception();
                    }
                }
                catch
                {
                    Assert.True(true);
                }
                Assert.Equal(1, repository.Count());

                using (var transaction = repository.Transaction())
                {
                    repository.Delete(entity);
                    transaction.Commit();
                }
                Assert.Equal(0, repository.Count());
            }
        }
    public void Dispose_Method()
    {
      var entity = new TestEntity();

      using (var repository = new LinqToSqlRepository<TestEntity>(this.Connection()))
      {
        repository.Persist(entity).Dispose();
        Assert.Throws<ObjectDisposedException>(() => repository.Single());

        repository.Dispose();
      }
    }
    public void GetEnumerator_Method()
    {
      var entity = new TestEntity();

      using (var repository = new LinqToSqlRepository<TestEntity>(this.Connection()))
      {
        Assert.False(repository.GetEnumerator().MoveNext());

        repository.Persist(entity).Commit();
        var enumerator = repository.GetEnumerator();
        Assert.True(enumerator.MoveNext());
        Assert.False(enumerator.MoveNext());
      }
    }