Exemplo n.º 1
0
        public void save_update_delete()
        {
            var playlist = new Playlist {
                Name = "New Playlist"
            };

            _playlistRepo.Save(playlist);
            Assert.IsTrue(playlist.PlaylistId > 0);

            var playlistId = playlist.PlaylistId;

            playlist = _playlistRepo.Get(playlistId);
            Assert.IsNotNull(playlist);
            Assert.AreEqual(playlistId, playlist.PlaylistId);

            playlist.Name = "Updated Playlist";
            _playlistRepo.Update(playlist);

            playlist = _playlistRepo.Get(playlistId);
            Assert.IsNotNull(playlist);
            Assert.AreEqual("Updated Playlist", playlist.Name);

            _playlistRepo.Delete(playlist);
            playlist = _playlistRepo.Get(playlistId);
            Assert.IsNull(playlist);
        }
        public void Delete(IndividualEntity entity)
        {
            // Guard against invalid arguments.
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            if (entity.Id <= 0)
            {
                throw new InvalidOperationException("Entity is invalid, the Id must be greater than 0.");
            }

            var entityToDelete = this.Retrieve(entity.Id);

            if (entityToDelete == null)
            {
                throw new InvalidOperationException("Entity is invalid, entity not found.");
            }

            try
            {
                repository.Delete(entityToDelete);
            }
            catch (Exception exception)
            {
                // Throw an exception if the Id was not returned.
                throw new InvalidOperationException("Failed to delete.", exception);
            }
        }
    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 IntegrationTest()
        {
            IList<Banana> bananas = new List<Banana>();
            bananas.Add(new Banana() { Color = "Brown" });
            bananas.Add(new Banana() { Color = "Yellow" });

            IRepository<Banana> bRepo = new NHibernateRepository<Banana>();
            foreach (Banana b in bananas)
                bRepo.Save(b);

            Assert.IsTrue(bRepo.GetAll().Count == bananas.Count);

            IList<Monkey> monkeys = new List<Monkey>();
            monkeys.Add(new Monkey() { Name = "George", FlingsPoo = true, Bananas = bananas });
            monkeys.Add(new Monkey() { Name = "Magilla", FlingsPoo = false });

            IRepository<Monkey> mRepo = new NHibernateRepository<Monkey>();
            foreach (Monkey m in monkeys)
                mRepo.Save(m);

            Assert.IsTrue(mRepo.GetAll().Count == monkeys.Count);

            Monkey tempMonkey;
            tempMonkey = mRepo.Get(monkeys[0].Id);
            Assert.IsNotNull(tempMonkey);
            tempMonkey = mRepo.Get(-1);
            Assert.IsNull(tempMonkey);

            IMonkeyRepository imRepo = new MonkeyRepository();
            Assert.IsTrue(imRepo.GetPooFlingingMonkeys().Count == 1);

            foreach (Banana b in bananas)
                bRepo.Delete(b);

            Assert.IsTrue(bRepo.GetAll().Count == 0);

            foreach (Monkey m in monkeys)
                mRepo.Delete(m);

            Assert.IsTrue(mRepo.GetAll().Count == 0);
        }
    public void Delete_Method()
    {
      Assert.Throws<ArgumentNullException>(() => new NHibernateRepository<TestEntity>(this.configuration).Delete(null));

      var entity = new TestEntity();

      using (var repository = new NHibernateRepository<TestEntity>(this.configuration))
      {
        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 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());
      }
    }
Exemplo n.º 7
0
 public void Remove(Guid id)
 {
     //TODO: Need a delete by Id method
     _repository.Delete(_repository.Get <DeferredMessage>(id));
 }
            public void ReturnsFalseOnFailure()
            {
                using (var session = GetInMemoryDatabaseSession())
                {
                    // Add a test entity.
                    using (var transaction = session.BeginTransaction())
                    {
                        session.Save(new TestEntity());
                        transaction.Commit();
                    }

                    // Mock session factory that will provide the in-memory session.
                    var mockSessionFactory = new Mock<ISessionFactory>();
                    mockSessionFactory.Setup(mock => mock.OpenSession()).Returns(session);

                    // Setup target
                    var target = new NHibernateRepository<TestEntity>(mockSessionFactory.Object);

                    // EXECUTE
                    var resultSingle = target.Delete((TestEntity)null);
                    var resultListing = target.Delete(new List<TestEntity> { (TestEntity)null });

                    //// VERIFY

                    var query = session.Query<TestEntity>();

                    Assert.False(resultSingle);
                    Assert.False(resultListing);
                    Assert.AreEqual(1, query.Count());
                }
            }
            public void DeletesSingleElementFromTheRepository()
            {
                //// SETUP

                var testEntity = new TestEntity { Name = "One" };

                using (var session = GetInMemoryDatabaseSession())
                {
                    // Add the test entity.
                    using (var transaction = session.BeginTransaction())
                    {
                        session.Save(testEntity);
                        transaction.Commit();
                    }

                    // Mock session factory that will provide the in-memory session.
                    var mockSessionFactory = new Mock<ISessionFactory>();
                    mockSessionFactory.Setup(mock => mock.OpenSession()).Returns(session);

                    // Setup target
                    var target = new NHibernateRepository<TestEntity>(mockSessionFactory.Object);

                    // EXECUTE
                    var result = target.Delete(testEntity);

                    // VERIFY
                    var query = session.Query<TestEntity>();
                    Assert.True(result);
                    Assert.False(query.Contains(testEntity));
                    Assert.AreEqual(0, query.Count());
                }
            }