Click here to Skip to main content
15,891,925 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 3 - Southwind Load

Rate me:
Please Sign up or sign in to vote.
4.62/5 (7 votes)
21 Nov 2012CPOL23 min read 24.5K   319   10  
In this part we'll write the loading application to move data from Northwind to Southwind.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Signum.Engine;
using Signum.Entities.Basics;
using Signum.Engine.DynamicQuery;
using Signum.Utilities.Reflection;
using Signum.Utilities;
using System.Reflection;
using Signum.Engine.Linq;
using Signum.Entities;
using Signum.Entities.DynamicQuery;
using System.Data;

namespace Signum.Test
{
    [TestClass]
    public class TransactionTest
    {
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            Starter.StartAndLoad();
        }

        [TestInitialize]
        public void Initialize()
        {
            Connection.CurrentLog = new DebugTextWriter();
        }

        void SetName(string newName)
        {
            Database.Query<ArtistDN>().Where(a => a.Id == 1).UnsafeUpdate(a => new ArtistDN { Name = newName });
        }

        string GetName()
        {
            return Database.Query<ArtistDN>().Where(a => a.Id == 1).Select(a => a.Name).Single();
        }

        [TestMethod]
        public void NoTransaction()
        {
            SetName("Mickey");

            Assert.AreEqual("Mickey", GetName());

            SetName("Mouse");

            Assert.AreEqual("Mouse", GetName());

            Starter.Dirty();
        }

        [TestMethod]
        public void Commit()
        {
            using (Transaction tr = new Transaction())
            {
                SetName("Mouse");

                Assert.AreEqual("Mouse", GetName());
                tr.Commit();
            }

            Assert.AreEqual("Mouse", GetName());

            Starter.Dirty();
        }

        [TestMethod]
        public void Rollback()
        {
            SetName("Mickey");

            Assert.AreEqual("Mickey", GetName());

            using (Transaction tr = new Transaction())
            {
                SetName("Mouse");

                Assert.AreEqual("Mouse", GetName());
                //tr.Commit();
            }

            Assert.AreEqual("Mickey", GetName());
               
            Starter.Dirty();
        }

        [TestMethod]
        public void CommitNested()
        {
            SetName("Mickey");

            Assert.AreEqual("Mickey", GetName());

            using (Transaction tr = new Transaction())
            {
                using (Transaction tr2 = new Transaction())
                {
                    SetName("Mouse");

                    Assert.AreEqual("Mouse", GetName());
                    tr2.Commit();
                }
                tr.Commit();
            }

            Assert.AreEqual("Mouse", GetName());

            Starter.Dirty();
        }

        [TestMethod]
        public void RollbackNested()
        {
            SetName("Mickey");

            Assert.AreEqual("Mickey", GetName());

            using (Transaction tr = new Transaction())
            {
                SetName("Minie");

                Assert.AreEqual("Minie", GetName());

                using (Transaction tr2 = new Transaction())
                {
                    SetName("Mouse");

                    Assert.AreEqual("Mouse", GetName());
                    //tr.Commit();
                }
                Assert2.Throws<InvalidOperationException>(() => tr.Commit());
            }

            Assert.AreEqual("Mickey", GetName());

            Starter.Dirty();
        }

        [TestMethod]
        public void IndependantBlocking()
        {
            Administrator.SetSnapshotIsolation(false);
            
            using (new CommandTimeoutScope(3))
            {
                SetName("Mickey");

                Assert.AreEqual("Mickey", GetName());

                using (Transaction tr = new Transaction())
                {
                    SetName("Minie");

                    Assert.AreEqual("Minie", GetName());

                    using (Transaction tr2 = new Transaction(true))
                    {
                        Assert2.Throws<TimeoutException>(() => GetName()); //Timeout reading 

                        return;
                    }
                }
            }
        }


        [TestMethod]
        public void IndependantSnapshot()
        {
            try
            {
                Administrator.SetSnapshotIsolation(true);
                using (new CommandTimeoutScope(3))
                {
                    SetName("Mickey");

                    Assert.AreEqual("Mickey", GetName());

                    using (Transaction tr = new Transaction(true, IsolationLevel.Snapshot))
                    {
                        SetName("Minie");

                        Assert.AreEqual("Minie", GetName());

                        using (Transaction tr2 = new Transaction(true, IsolationLevel.Snapshot))
                        {
                            Assert.AreEqual("Mickey", GetName()); //Independant transaction

                            Assert2.Throws<TimeoutException>(() => SetName("Mouse")); //Timeout writing 

                            return;
                        }
                    }
                }
            }
            finally
            {
                Administrator.SetSnapshotIsolation(false);
            }
        }

        [TestMethod]
        public void NamedCommit()
        {
            using (Transaction tr = new Transaction())
            {
                SetName("Mickey");

                Assert.AreEqual("Mickey", GetName());

                using (Transaction tr2 = new Transaction("hola"))
                {
                    SetName("Minie");

                    Assert.AreEqual("Minie", GetName());

                    tr2.Commit(); 
                }

                tr.Commit();
            }

            Assert.AreEqual("Minie", GetName());
        }

        [TestMethod]
        public void NamedRollback()
        {
            using (Transaction tr = new Transaction())
            {
                SetName("Mickey");

                Assert.AreEqual("Mickey", GetName());

                using (Transaction tr2 = new Transaction("hola"))
                {
                    SetName("Minie");

                    Assert.AreEqual("Minie", GetName());

                    //tr2.Commit();
                }

                tr.Commit();
            }

            Assert.AreEqual("Mickey", GetName());
        }

        [TestMethod]
        public void NestedNamedCommit()
        {
            using (Transaction tr = new Transaction())
            {
                SetName("Mickey");

                Assert.AreEqual("Mickey", GetName());

                using (Transaction tr2 = new Transaction("hola"))
                {
                    SetName("Minie");

                    Assert.AreEqual("Minie", GetName());

                    using (Transaction tr3 = new Transaction())
                    {
                        SetName("Mouse");

                        Assert.AreEqual("Mouse", GetName());

                        tr3.Commit();
                    }

                    tr2.Commit();
                }

                tr.Commit();
            }

            Assert.AreEqual("Mouse", GetName());
        }

        [TestMethod]
        public void NestedNamedRollback()
        {
            using (Transaction tr = new Transaction())
            {
                SetName("Mickey");

                Assert.AreEqual("Mickey", GetName());

                using (Transaction tr2 = new Transaction("hola"))
                {
                    SetName("Minie");

                    Assert.AreEqual("Minie", GetName());

                    using (Transaction tr3 = new Transaction())
                    {
                        SetName("Mouse");

                        Assert.AreEqual("Mouse", GetName());

                        //tr3.Commit();
                    }

                    Assert2.Throws<InvalidOperationException>(() =>
                    {
                        using (Transaction tr3 = new Transaction())
                        {
                            SetName("Mouse");

                            Assert.AreEqual("Mouse", GetName());

                            tr3.Commit();
                        }

                        tr2.Commit();
                    });
                }

                tr.Commit();
            }

            Assert.AreEqual("Mickey", GetName());
        }

        [TestMethod]
        public void NamedNamedRollbackRollback()
        {
            using (Transaction tr = new Transaction())
            {
                SetName("Mickey");

                Assert.AreEqual("Mickey", GetName());

                using (Transaction tr2 = new Transaction("hola"))
                {
                    SetName("Minie");

                    Assert.AreEqual("Minie", GetName());

                    using (Transaction tr3 = new Transaction("dola"))
                    {
                        SetName("Mouse");

                        Assert.AreEqual("Mouse", GetName());

                        //tr3.Commit();
                    }

                    Assert.AreEqual("Minie", GetName());

                    //tr2.Commit();
                }

                tr.Commit();
            }

            Assert.AreEqual("Mickey", GetName());
        }

        [TestMethod]
        public void NamedNamedRollbackCommit()
        {
            using (Transaction tr = new Transaction())
            {
                SetName("Mickey");

                Assert.AreEqual("Mickey", GetName());

                using (Transaction tr2 = new Transaction("hola"))
                {
                    SetName("Minie");

                    Assert.AreEqual("Minie", GetName());

                    using (Transaction tr3 = new Transaction("dola"))
                    {
                        SetName("Mouse");

                        Assert.AreEqual("Mouse", GetName());

                        //tr3.Commit();
                    }

                    Assert.AreEqual("Minie", GetName());

                    tr2.Commit();
                }

                tr.Commit();
            }

            Assert.AreEqual("Minie", GetName());
        }

        [TestMethod]
        public void NamedNamedCommitCommit()
        {
            using (Transaction tr = new Transaction())
            {
                SetName("Mickey");

                Assert.AreEqual("Mickey", GetName());

                using (Transaction tr2 = new Transaction("hola"))
                {
                    SetName("Minie");

                    Assert.AreEqual("Minie", GetName());

                    using (Transaction tr3 = new Transaction("dola"))
                    {
                        SetName("Mouse");

                        Assert.AreEqual("Mouse", GetName());

                        tr3.Commit();
                    }

                    Assert.AreEqual("Mouse", GetName());

                    tr2.Commit();
                }

                tr.Commit();
            }

            Assert.AreEqual("Mouse", GetName());
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.

www.signumframework.com

I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

Comments and Discussions