Click here to Skip to main content
15,896,348 members
Articles / Programming Languages / XML

DBKeeperNet - Keeps Your DB Schema Up-to-date

Rate me:
Please Sign up or sign in to vote.
4.88/5 (14 votes)
26 Aug 2014BSD4 min read 50.6K   575   89  
An article describing a simple .NET library which simply keeps your database schema up-to-date.
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;

namespace DbKeeperNet.Engine.Tests
{
    [TestFixture]
    public class UpdateContextTests
    {
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RegisterPreconditionNull()
        {
            IUpdateContext context = new UpdateContext();
            context.RegisterPrecondition(null);
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void RegisterPreconditionNullName()
        {
            MockRepository repository = new MockRepository();
            IPrecondition mockPrec = repository.DynamicMock<IPrecondition>();

            using (repository.Record())
            {
                SetupResult.For(mockPrec.Name).Return(null);
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterPrecondition(mockPrec);
            }
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void RegisterPreconditionEmptyName()
        {
            MockRepository repository = new MockRepository();
            IPrecondition mockPrec = repository.DynamicMock<IPrecondition>();

            using (repository.Record())
            {
                SetupResult.For(mockPrec.Name).Return("");
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterPrecondition(mockPrec);
            }
        }

        [Test]
        public void RegisterPrecondition()
        {
            MockRepository repository = new MockRepository();
            IPrecondition mockPrec = repository.DynamicMock<IPrecondition>();

            using (repository.Record())
            {
                SetupResult.For(mockPrec.Name).Return("testingPrecondition");
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterPrecondition(mockPrec);
            }
        }

        [Test]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void CheckNonExistingPrecondition()
        {
            MockRepository repository = new MockRepository();
            IPrecondition mockPrec = repository.DynamicMock<IPrecondition>();

            using (repository.Record())
            {
                SetupResult.For(mockPrec.Name).Return("testingPrecondition");
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterPrecondition(mockPrec);
                context.CheckPrecondition("xxx", null);
            }
        }

        [Test]
        public void CheckPrecondition()
        {
            MockRepository repository = new MockRepository();
            IPrecondition mockPrec = repository.DynamicMock<IPrecondition>();
            IUpdateContext context = new UpdateContext();

            using (repository.Record())
            {
                SetupResult.For(mockPrec.Name).Return("testingPrecondition");

                Expect.Call(mockPrec.CheckPrecondition(context, null)).Return(true);
                Expect.Call(mockPrec.CheckPrecondition(context, null)).Return(false);
            }

            using (repository.Playback())
            {
                context.RegisterPrecondition(mockPrec);
                Assert.That(context.CheckPrecondition("testingPrecondition", null), Is.EqualTo(true));
                Assert.That(context.CheckPrecondition("testingPrecondition", null), Is.EqualTo(false));
            }

            repository.VerifyAll();
        }

        [Test]
        public void RegisterDatabaseService()
        {
            MockRepository repository = new MockRepository();
            IDatabaseService mockService = repository.DynamicMock<IDatabaseService>();

            using (repository.Record())
            {
                SetupResult.For(mockService.Name).Return("testingService");
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterDatabaseService(mockService);
            }
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void RegisterDatabaseServiceNullName()
        {
            MockRepository repository = new MockRepository();
            IDatabaseService mockService = repository.DynamicMock<IDatabaseService>();

            using (repository.Record())
            {
                SetupResult.For(mockService.Name).Return(null);
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterDatabaseService(mockService);
            }
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void RegisterDatabaseServiceEmptyName()
        {
            MockRepository repository = new MockRepository();
            IDatabaseService mockService = repository.DynamicMock<IDatabaseService>();

            using (repository.Record())
            {
                SetupResult.For(mockService.Name).Return("");
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterDatabaseService(mockService);
            }
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RegisterDatabaseServiceNull()
        {
            IUpdateContext context = new UpdateContext();
            context.RegisterDatabaseService(null);
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void RegisterLoggingServiceNullName()
        {
            MockRepository repository = new MockRepository();
            ILoggingService mockService = repository.DynamicMock<ILoggingService>();

            using (repository.Record())
            {
                SetupResult.For(mockService.Name).Return(null);
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterLoggingService(mockService);
            }
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void RegisterLoggingServiceEmptyName()
        {
            MockRepository repository = new MockRepository();
            ILoggingService mockService = repository.DynamicMock<ILoggingService>();

            using (repository.Record())
            {
                SetupResult.For(mockService.Name).Return("");
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterLoggingService(mockService);
            }
        }

        [Test]
        public void RegisterLoggingService()
        {
            MockRepository repository = new MockRepository();
            ILoggingService mockService = repository.DynamicMock<ILoggingService>();

            using (repository.Record())
            {
                SetupResult.For(mockService.Name).Return("testingLogger");
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterLoggingService(mockService);
            }
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void InitializeDatabaseServiceUsingDatabaseServiceShouldThrowExceptionForNullArgument()
        {
            using(IUpdateContext context = new UpdateContext())
            {
                context.InitializeDatabaseService((IDatabaseService)null, false);
            }
        }

        [Test]
        public void InitializeDatabaseServiceUsingDatabaseServiceShouldWork()
        {
            MockRepository mockRepository = new MockRepository();
            IDatabaseService databaseService = mockRepository.DynamicMock<IDatabaseService>();

            using (IUpdateContext context = CreateAContext())
            {
                context.InitializeDatabaseService(databaseService, false);
            }
        }

        [Test]
        public void RegisterScriptExecutionService()
        {
            MockRepository repository = new MockRepository();
            IScriptProviderService mockService = repository.DynamicMock<IScriptProviderService>();

            using (repository.Record())
            {
                SetupResult.For(mockService.Name).Return("testingScriptExecition");
            }

            using (repository.Playback())
            {
                IUpdateContext context = new UpdateContext();
                context.RegisterScriptProviderService(mockService);
            }

            repository.VerifyAll();
        }

        [Test]
        public void InitializeServiceWithADatabaseServiceAndDisposeServiceTrueShouldDisposeServiceDuringContextDispose()
        {
            var repository = new MockRepository();
            var databaseService = repository.DynamicMock<IDatabaseService>();

            using(repository.Record())
            {
                Expect.Call(databaseService.Dispose);
            }

            using (repository.Playback())
            {
                var context = CreateAContext();

                context.InitializeDatabaseService(databaseService, true);

                context.Dispose();
            }
        }

        [Test]
        public void InitializeServiceWithADatabaseServiceAndDisposeServiceFalseShouldNotDisposeServiceDuringContextDispose()
        {
            var repository = new MockRepository();
            var databaseService = repository.DynamicMock<IDatabaseService>();

            using (repository.Record())
            {
                DoNotExpect.Call(databaseService.Dispose);
            }

            using (repository.Playback())
            {
                var context = CreateAContext();

                context.InitializeDatabaseService(databaseService, false);

                context.Dispose();
            }
        }

        private static UpdateContext CreateAContext()
        {
            var context = new UpdateContext();

            context.LoadExtensions();
            context.InitializeLoggingService(@"dummy");
            return context;
        }
    }
}

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 BSD License


Written By
Team Leader NCR
Czech Republic Czech Republic
I'm software developer since 1996. I started with assembler on Intel 8051 CPUs, during years I was interested in C, C++, Sybase PowerBuilder, PHP, Sybase Anywhere Database, MSSQL server and multiplatform development.

Currently I'm developing in C++ and C# (this is my favorit and I spent some time with MCPD achievement). I'm also interested in design patterns.

Comments and Discussions