Example #1
0
        public void Test_StackTrace_Shown_Depending_On_Debug_Flag(bool isDebug)
        {
            //arrange
            var errorTraceMsg = string.Empty;
            var traceService  = new Mock <ITraceService>();

            traceService.Setup(s => s.Error(It.IsAny <string>(), null))
            .Callback <string, object>((msg, o) => errorTraceMsg = msg);

            var exc = new Exception("Fake exception");
            var environmentService = new Mock <IEnvironmentService>();

            environmentService.Setup(s => s.GetCurrentDirectory()).Throws(exc);

            //act
            var option = new RunOption {
                Debug = isDebug
            };
            var sut = new CommandLineService(null, null, environmentService.Object, traceService.Object);

            var returnCode = sut.RunMigration(option);

            //assert
            if (isDebug)
            {
                errorTraceMsg.ShouldContain(exc.StackTrace);
            }
            else
            {
                errorTraceMsg.ShouldNotContain(exc.StackTrace);
            }
        }
Example #2
0
        public void Test_Run_Option_No_Explicit_Options()
        {
            //arrange
            var traceService       = new Mock <ITraceService>();
            var environmentService = new Mock <IEnvironmentService>();

            environmentService.Setup(s => s.GetCurrentDirectory()).Returns(@"c:\temp\yuniql");
            environmentService.Setup(s => s.GetEnvironmentVariable("YUNIQL_CONNECTION_STRING")).Returns("sqlserver-connection-string");

            var localVersionService = new Mock <ILocalVersionService>();

            localVersionService.Setup(s => s.GetLatestVersion(@"c:\temp\yuniql")).Returns("v1.00");

            var migrationService        = new Mock <IMigrationService>();
            var migrationServiceFactory = new Mock <CLI.IMigrationServiceFactory>();

            migrationServiceFactory.Setup(s => s.Create("sqlserver")).Returns(migrationService.Object);

            //act
            var option = new RunOption {
            };
            var sut    = new CommandLineService(migrationServiceFactory.Object, localVersionService.Object, environmentService.Object, traceService.Object);

            sut.RunMigration(option);

            //assert
            var toolName    = "yuniql-cli";
            var toolVersion = typeof(CommandLineService).Assembly.GetName().Version.ToString();

            migrationService.Verify(s => s.Initialize("sqlserver-connection-string", DEFAULT_CONSTANTS.COMMAND_TIMEOUT_SECS));
            migrationService.Verify(s => s.Run(@"c:\temp\yuniql", "v1.00", false, It.Is <List <KeyValuePair <string, string> > >(x => x.Count == 0), false, DEFAULT_CONSTANTS.BULK_SEPARATOR, null, null, DEFAULT_CONSTANTS.COMMAND_TIMEOUT_SECS, 0, toolName, toolVersion, null, null, false));
        }
Example #3
0
        public void Test_StackTrace_Shown_Depending_On_Debug_Flag(bool debugEnabled)
        {
            //arrange
            var errorTraceMsg = string.Empty;
            var traceService  = new Mock <ITraceService>();

            traceService.Setup(s => s.Error(It.IsAny <string>(), null))
            .Callback <string, object>((msg, o) => errorTraceMsg = msg);
            var environmentService   = new Mock <IEnvironmentService>();
            var configurationService = new Mock <IConfigurationService>();

            configurationService.Setup(s => s.GetValueOrDefault(null, ENVIRONMENT_VARIABLE.YUNIQL_PLATFORM, SUPPORTED_DATABASES.SQLSERVER)).Returns(SUPPORTED_DATABASES.SQLSERVER);
            configurationService.Setup(s => s.GetConfiguration()).Returns(Configuration.Instance);

            var migrationService        = new Mock <IMigrationService>();
            var migrationServiceFactory = new Mock <CLI.IMigrationServiceFactory>();

            var fakeException = new Exception("Fake exception");

            migrationServiceFactory.Setup(s => s.Create("sqlserver")).Throws(fakeException);

            var dataService        = new Mock <IDataService>();
            var dataServiceFactory = new Mock <CLI.IDataServiceFactory>();

            dataServiceFactory.Setup(s => s.Create("sqlserver")).Returns(dataService.Object);

            var manifest        = new Mock <ManifestData>();
            var manifestFactory = new Mock <IManifestFactory>();

            manifestFactory.Setup(s => s.Create("sqlserver")).Returns(manifest.Object);

            var workspaceService = new Mock <IWorkspaceService>();

            //act
            var option = new RunOption {
                IsDebug = debugEnabled
            };
            var sut        = new CommandLineService(migrationServiceFactory.Object, dataServiceFactory.Object, manifestFactory.Object, workspaceService.Object, environmentService.Object, traceService.Object, configurationService.Object);
            var returnCode = sut.RunRunOption(option);

            //assert
            if (debugEnabled)
            {
                errorTraceMsg.ShouldContain(fakeException.StackTrace);
            }
            else
            {
                errorTraceMsg.ShouldNotContain(fakeException.StackTrace);
            }
        }
Example #4
0
        public void Test_Run_Option_With_Tokens()
        {
            //arrange
            var traceService       = new Mock <ITraceService>();
            var environmentService = new Mock <IEnvironmentService>();

            environmentService.Setup(s => s.GetCurrentDirectory()).Returns(@"c:\temp\yuniql");
            environmentService.Setup(s => s.GetEnvironmentVariable("YUNIQL_CONNECTION_STRING")).Returns("sqlserver-connection-string");

            var workspaceService = new Mock <IWorkspaceService>();

            workspaceService.Setup(s => s.GetLatestVersion(@"c:\temp\yuniql")).Returns("v1.00");

            var configurationService = new Mock <IConfigurationService>();

            configurationService.Setup(s => s.GetValueOrDefault(null, ENVIRONMENT_VARIABLE.YUNIQL_PLATFORM, SUPPORTED_DATABASES.SQLSERVER)).Returns(SUPPORTED_DATABASES.SQLSERVER);

            var migrationService        = new Mock <IMigrationService>();
            var migrationServiceFactory = new Mock <CLI.IMigrationServiceFactory>();

            migrationServiceFactory.Setup(s => s.Create("sqlserver")).Returns(migrationService.Object);

            var dataService        = new Mock <IDataService>();
            var dataServiceFactory = new Mock <CLI.IDataServiceFactory>();

            dataServiceFactory.Setup(s => s.Create("sqlserver")).Returns(dataService.Object);

            var manifest        = new Mock <ManifestData>();
            var manifestFactory = new Mock <IManifestFactory>();

            manifestFactory.Setup(s => s.Create("sqlserver")).Returns(manifest.Object);

            //act
            var option = new RunOption {
                Tokens = new List <string> {
                    "Token1=TokenValue1", "Token2=TokenValue2", "Token3=TokenValue3"
                }
            };
            var sut = new CommandLineService(migrationServiceFactory.Object, dataServiceFactory.Object, manifestFactory.Object, workspaceService.Object, environmentService.Object, traceService.Object, configurationService.Object);

            sut.RunRunOption(option);

            //assert
            migrationService.Verify(s => s.Run());
        }
        public void Test_Run_NoTransaction_Option(bool noTransactionOption)
        {
            //arrange
            var transaction = new Mock <IDbTransaction>();
            var connection  = new Mock <IDbConnection>();

            connection.Setup(c => c.BeginTransaction())
            .Returns(transaction.Object);

            var dataService = new Mock <IDataService>();

            dataService.Setup(s => s.CreateConnection())
            .Returns(connection.Object);
            dataService.Setup(s => s.GetConnectionInfo())
            .Returns(() => new ConnectionInfo {
                Database = "test", DataSource = "test"
            });
            var configurationDataService = new Mock <IConfigurationDataService>();

            configurationDataService.Setup(s => s.IsDatabaseConfigured(It.IsAny <string>(), It.IsAny <string>(), null))
            .Returns(true);
            configurationDataService.Setup(s => s.GetCurrentVersion(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>()))
            .Returns(string.Empty);
            configurationDataService.Setup(s => s.GetAllVersions(It.IsAny <string>(), It.IsAny <string>(), null))
            .Returns(new List <DbVersion>());

            //act
            var option = new RunOption {
                NoTransaction = noTransactionOption
            };
            var sut = new MigrationService(new Mock <ILocalVersionService>().Object, dataService.Object, new Mock <IBulkImportService>().Object,
                                           configurationDataService.Object, new Mock <ITokenReplacementService>().Object,
                                           new Mock <IDirectoryService>().Object, new Mock <IFileService>().Object, new Mock <ITraceService>().Object);

            sut.Run(string.Empty, metaSchemaName: "any", metaTableName: "any", noTransaction: noTransactionOption);

            // assert
            connection.Verify(c => c.BeginTransaction(), noTransactionOption == true ? Times.Never() : Times.AtLeastOnce());
            transaction.Verify(t => t.Commit(), noTransactionOption == true ? Times.Never() : Times.AtLeastOnce());
            transaction.Verify(t => t.Rollback(), noTransactionOption == true ? Times.Never() : Times.AtMostOnce());
        }
Example #6
0
        /// <summary>
        /// Executes the scene now
        /// </summary>
        /// <remarks>This allows you to track the completion of the scene. If you don't want to handle the asynchronous execution, you should use <see cref="Start"/>.</remarks>
        /// <param name="ct">Cancellation token to abort the execution</param>
        /// <param name="options">Configures the behavior if this scene <see cref="IsRunning"/>.</param>
        /// <returns>An asynchronous operation that will complete when this scene is over.</returns>
        public async Task Run(CancellationToken ct, RunOption options = RunOption.IgnoreIfRunning)
        {
            if (IsRunning && options == RunOption.IgnoreIfRunning)
            {
                return;
            }

            try
            {
                // Set _isRunning prior to abort pending, so there is no flicker of 'IsRunning'
                // Note: we use a int as at this point we already have a running instance which will be aborted below
                Interlocked.Increment(ref _isRunning);

                using (var ctx = new AsyncContext(ct, Scheduler))
                {
                    // This will abort any other pending
                    _pending.Disposable = Disposable.Create(ctx.Cancel);

                    await _host.SetIsRunning(ctx.Token, this, true);

                    await Run();

                    await ctx.WaitForCompletion();
                }
            }
            finally
            {
                // We cannot abort the completion publication
                ct = CancellationToken.None;

                if (Interlocked.Decrement(ref _isRunning) == 0)
                {
                    await _host.SetIsRunning(ct, this, false);
                }
            }
        }