Exemple #1
0
        public void Should_complete_underlying_translations_after_root_is_completed()
        {
            using (var tx = SUT.BeginTransaction())
                using (var child = SUT.BeginTransaction())
                {
                    ActDbContext.TestEntities.Add(new TestEntity {
                        Id = new Guid("7631FF3B-E5B1-485A-ABD5-386C6B37E991")
                    });
                    ActDbContext.SaveChanges();

                    child.Commit();
                    ActDbContext.TestEntities.Add(new TestEntity {
                        Id = new Guid("7A4397B0-C9B6-46FF-B8C8-F39A9D6F5105")
                    });
                    ActDbContext.SaveChanges();

                    tx.Rollback();

                    // outside of the transaction
                    ActDbContext.TestEntities.Add(new TestEntity {
                        Id = new Guid("CB682609-1503-4FBE-8A29-66E2DF8F00E5")
                    });
                    ActDbContext.SaveChanges();
                }

            AssertDbContext.TestEntities.Should().HaveCount(1);
            AssertDbContext.TestEntities.First().Id.Should().Be(new Guid("CB682609-1503-4FBE-8A29-66E2DF8F00E5"));
        }
Exemple #2
0
        public void Should_create_new_root_transaction_and_dispose_correctly()
        {
            // ReSharper disable once UnusedVariable
            using (var tx = SUT.BeginTransaction())
            {
                ActDbContext.TestEntities.Add(new TestEntity());
                ActDbContext.SaveChanges();
            }

            AssertDbContext.TestEntities.Should().HaveCount(0);
        }
Exemple #3
0
        public void Should_create_new_child_transaction_and_dispose_correctly()
        {
            using (var tx = SUT.BeginTransaction())
                using (var childTx = SUT.BeginTransaction())
                {
                    ActDbContext.TestEntities.Add(new TestEntity());
                    ActDbContext.SaveChanges();
                }

            AssertDbContext.TestEntities.Should().HaveCount(0);
        }
Exemple #4
0
        public void Should_create_new_root_transaction_and_rollback_correctly()
        {
            using (var tx = SUT.BeginTransaction())
            {
                ActDbContext.TestEntities.Add(new TestEntity());
                ActDbContext.SaveChanges();

                tx.Rollback();
            }

            AssertDbContext.TestEntities.Should().HaveCount(0);
        }
Exemple #5
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 #6
0
        public void Should_dispose_child_transactions_if_root_transactions_is_disposed()
        {
            using (var tx = SUT.BeginTransaction())
            {
                var child = SUT.BeginTransaction();

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

            SUT.CurrentTransaction.Should().BeNull();
            AssertDbContext.TestEntities.Should().HaveCount(0);
        }
Exemple #7
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 #8
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 #9
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 #10
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 #11
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 #12
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);
        }
Exemple #13
0
        public void Should_rollback_if_a_sibling_transaction_rollbacks()
        {
            using (var tx = SUT.BeginTransaction())
            {
                using (var child = SUT.BeginTransaction())
                {
                    child.Rollback();
                }

                using (var child = SUT.BeginTransaction())
                {
                    child.Commit();
                }

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

                tx.Invoking(rootTx => rootTx.Commit())
                .Should().Throw <TransactionAbortedException>().WithMessage("The transaction has aborted.");
            }

            AssertDbContext.TestEntities.Should().HaveCount(0);
        }