Ejemplo n.º 1
0
        public void count()
        {
            var criteria = _playlistTrackRepo.Session.CreateCriteria <PlaylistTrack>();

            criteria.Add(Restrictions.Eq("PlaylistId", 8));

            var count = _playlistTrackRepo.Count(criteria);

            Assert.AreEqual(3290, count);
        }
    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 DeleteAll_Method()
    {
      using (var repository = new NHibernateRepository<TestEntity>(this.configuration))
      {
        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());
      }
    }