Exemple #1
0
        public void Should_rollback_root_transaction_and_underlying_transaction()
        {
            var rootTx = SUT.BeginTransaction();

            ActDbContext.Add(new TestEntity());
            ActDbContext.SaveChanges();

            rootTx.Rollback();

            SUT.CurrentTransaction.Should().BeNull();
            IsTransactionUsable(rootTx.GetDbTransaction()).Should().BeFalse();
            AssertDbContext.TestEntities.Should().BeEmpty();
        }
Exemple #2
0
        public void Should_rollback_uncompleted_root_transaction()
        {
            var rootTx = SUT.BeginTransaction();

            ActDbContext.Add(new TestEntity());
            ActDbContext.SaveChanges();

            rootTx.Dispose();

            SUT.CurrentTransaction.Should().BeNull();
            IsTransactionUsable(rootTx.GetDbTransaction()).Should().BeFalse();
            AssertDbContext.TestEntities.Should().HaveCount(0);
        }
Exemple #3
0
        public void Should_do_nothing_when_disposing_multiple_times()
        {
            var rootTx = SUT.BeginTransaction();

            ActDbContext.Add(new TestEntity());
            ActDbContext.SaveChanges();

            rootTx.Dispose();
            rootTx.Dispose();

            SUT.CurrentTransaction.Should().BeNull();
            IsTransactionUsable(rootTx.GetDbTransaction()).Should().BeFalse();
            AssertDbContext.TestEntities.Should().HaveCount(0);
        }
Exemple #4
0
        public void Should_rollback_root_transaction_if_child_transaction_is_committed()
        {
            var rootTx  = SUT.BeginTransaction();
            var childTx = SUT.BeginTransaction();

            ActDbContext.Add(new TestEntity());
            ActDbContext.SaveChanges();

            childTx.Commit();
            rootTx.Rollback();

            SUT.CurrentTransaction.Should().BeNull();
            IsTransactionUsable(rootTx.GetDbTransaction()).Should().BeFalse();
            AssertDbContext.TestEntities.Should().HaveCount(0);
        }
Exemple #5
0
        public void Should_throw_when_trying_to_rollback_twice()
        {
            var rootTx = SUT.BeginTransaction();

            ActDbContext.Add(new TestEntity());
            ActDbContext.SaveChanges();

            rootTx.Rollback();

            rootTx.Invoking(tx => tx.Rollback())
            .Should().Throw <InvalidOperationException>().WithMessage("This transaction has completed; it is no longer usable.");

            SUT.CurrentTransaction.Should().BeNull();
            IsTransactionUsable(rootTx.GetDbTransaction()).Should().BeFalse();
            AssertDbContext.TestEntities.Should().HaveCount(0);
        }
Exemple #6
0
        public void Should_throw_when_rollback_child_transaction_if_another_child_transaction_is_not_completed()
        {
            // ReSharper disable once UnusedVariable
            var rootTx         = SUT.BeginTransaction();
            var childTx        = SUT.BeginTransaction();
            var anotherChildTx = SUT.BeginTransaction();

            ActDbContext.Add(new TestEntity());
            ActDbContext.SaveChanges();

            childTx.Invoking(tx => tx.Rollback())
            .Should().Throw <InvalidOperationException>().WithMessage("Transactions nested incorrectly. At least one of the child transactions is not completed.");

            SUT.CurrentTransaction.Should().Be(anotherChildTx);
            IsTransactionUsable(anotherChildTx.GetDbTransaction()).Should().BeTrue();
        }
Exemple #7
0
        public void Should_commit_children_and_root_transactions()
        {
            var rootTx         = SUT.BeginTransaction();
            var childTx        = SUT.BeginTransaction();
            var anotherChildTx = SUT.BeginTransaction();

            ActDbContext.Add(new TestEntity());
            ActDbContext.SaveChanges();

            anotherChildTx.Commit();
            childTx.Commit();
            rootTx.Commit();

            SUT.CurrentTransaction.Should().BeNull();
            IsTransactionUsable(rootTx.GetDbTransaction()).Should().BeFalse();
            AssertDbContext.TestEntities.Should().HaveCount(1);
        }