Example #1
0
        public void Work_ScriptCreationStateModel_ArgumentNullException_StateModel()
        {
            // Arrange
            var daMock     = Mock.Of <IDacAccess>();
            var fsaMock    = Mock.Of <IFileSystemAccess>();
            var loggerMock = Mock.Of <ILogger>();
            IWorkUnit <ScriptCreationStateModel> unit = new CreateDeploymentFilesUnit(daMock, fsaMock, loggerMock);

            // Act & Assert
            // ReSharper disable once AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(() => unit.Work(null, CancellationToken.None));
        }
Example #2
0
        public async Task Work_ScriptCreationStateModel_CreateScriptAndReport_Async(bool includePreDeployment, bool includePostDeployment)
        {
            // Arrange
            var daMock = new Mock <IDacAccess>();

            daMock.Setup(m => m.CreateDeployFilesAsync("previousDacpacPath", "newDacpacPath", "publishProfilePath", true, true))
            .ReturnsAsync(new CreateDeployFilesResult((includePreDeployment ? "pre " : "") + "script" + (includePostDeployment ? " post" : ""),
                                                      "report",
                                                      includePreDeployment ? "pre " : null,
                                                      includePostDeployment ? " post" : null,
                                                      new PublishProfile()));
            var fsaMock    = new Mock <IFileSystemAccess>();
            var loggerMock = new Mock <ILogger>();
            IWorkUnit <ScriptCreationStateModel> unit = new CreateDeploymentFilesUnit(daMock.Object, fsaMock.Object, loggerMock.Object);
            var project       = new SqlProject("a", "b", "c");
            var configuration = ConfigurationModel.GetDefault();

            configuration.CreateDocumentationWithScriptCreation = true;
            var previousVersion = new Version(1, 0);

            Task HandlerFunc(bool b) => Task.CompletedTask;

            var directories = new DirectoryPaths("projectDirectory", "latestArtifactsDirectory", "newArtifactsDirectory");
            var sourcePaths = new DeploySourcePaths("newDacpacPath", "publishProfilePath", "previousDacpacPath");
            var targetPaths = new DeployTargetPaths("deployScriptPath", "deployReportPath");
            var paths       = new PathCollection(directories, sourcePaths, targetPaths);
            var model       = new ScriptCreationStateModel(project, configuration, previousVersion, false, HandlerFunc)
            {
                Paths = paths
            };

            // Act
            await unit.Work(model, CancellationToken.None);

            // Assert
            Assert.AreEqual(StateModelState.TriedToCreateDeploymentFiles, model.CurrentState);
            Assert.IsNull(model.Result);
            fsaMock.Verify(m => m.WriteFileAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(2));
            fsaMock.Verify(m => m.WriteFileAsync("deployScriptPath", (includePreDeployment ? "pre " : "") + "script" + (includePostDeployment ? " post" : "")), Times.Once);
            fsaMock.Verify(m => m.WriteFileAsync("deployReportPath", "report"), Times.Once);
            loggerMock.Verify(m => m.LogDebugAsync("Previous DACPAC path: \"previousDacpacPath\""), Times.Once);
            loggerMock.Verify(m => m.LogDebugAsync("New DACPAC path: \"newDacpacPath\""), Times.Once);
            loggerMock.Verify(m => m.LogDebugAsync("Publish profile path: \"publishProfilePath\""), Times.Once);
            loggerMock.Verify(m => m.LogDebugAsync(It.Is <string>(s => s.StartsWith("Current working directory: \"") && s.EndsWith("\""))), Times.Once);
            loggerMock.Verify(m => m.LogErrorAsync(It.IsAny <string>()), Times.Never);
            loggerMock.Verify(m => m.LogErrorAsync(It.IsAny <Exception>(), It.IsAny <string>()), Times.Never);
        }
Example #3
0
        public async Task Work_ScriptCreationStateModel_PersistReportError_Async()
        {
            // Arrange
            var testException = new Exception("test exception");
            var daMock        = new Mock <IDacAccess>();

            daMock.Setup(m => m.CreateDeployFilesAsync("previousDacpacPath", "newDacpacPath", "publishProfilePath", true, true))
            .ReturnsAsync(new CreateDeployFilesResult("pre script post", "report", "pre ", " post", new PublishProfile()));
            var fsaMock = new Mock <IFileSystemAccess>();

            fsaMock.Setup(m => m.WriteFileAsync("deployReportPath", "report"))
            .ThrowsAsync(testException);
            var loggerMock = new Mock <ILogger>();
            IWorkUnit <ScriptCreationStateModel> unit = new CreateDeploymentFilesUnit(daMock.Object, fsaMock.Object, loggerMock.Object);
            var project       = new SqlProject("a", "b", "c");
            var configuration = ConfigurationModel.GetDefault();

            configuration.CreateDocumentationWithScriptCreation = true;
            var previousVersion = new Version(1, 0);

            Task HandlerFunc(bool b) => Task.CompletedTask;

            var directories = new DirectoryPaths("projectDirectory", "latestArtifactsDirectory", "newArtifactsDirectory");
            var sourcePaths = new DeploySourcePaths("newDacpacPath", "publishProfilePath", "previousDacpacPath");
            var targetPaths = new DeployTargetPaths("deployScriptPath", "deployReportPath");
            var paths       = new PathCollection(directories, sourcePaths, targetPaths);
            var model       = new ScriptCreationStateModel(project, configuration, previousVersion, false, HandlerFunc)
            {
                Paths = paths
            };

            // Act
            await unit.Work(model, CancellationToken.None);

            // Assert
            Assert.AreEqual(StateModelState.TriedToCreateDeploymentFiles, model.CurrentState);
            Assert.IsFalse(model.Result);
            fsaMock.Verify(m => m.WriteFileAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(2));
            loggerMock.Verify(m => m.LogErrorAsync("Script creation aborted."), Times.Once);
            loggerMock.Verify(m => m.LogErrorAsync(testException, "Failed to write deploy report"), Times.Once);
        }
Example #4
0
        public async Task Work_ScriptCreationStateModel_PublishProfileValidation_Async(bool removeSqlCmdStatements,
                                                                                       bool createNewDatabase,
                                                                                       bool backupDatabaseBeforeChanges,
                                                                                       bool scriptDatabaseOptions,
                                                                                       bool scriptDeployStateChecks,
                                                                                       bool?expectedResult)
        {
            // Arrange
            var daMock = new Mock <IDacAccess>();

            daMock.Setup(m => m.CreateDeployFilesAsync("previousDacpacPath", "newDacpacPath", "publishProfilePath", true, true))
            .ReturnsAsync(new CreateDeployFilesResult("pre script post", "report", "pre ", " post", new PublishProfile
            {
                CreateNewDatabase           = createNewDatabase,
                BackupDatabaseBeforeChanges = backupDatabaseBeforeChanges,
                ScriptDatabaseOptions       = scriptDatabaseOptions,
                ScriptDeployStateChecks     = scriptDeployStateChecks
            }));
            var fsaMock    = new Mock <IFileSystemAccess>();
            var loggerMock = new Mock <ILogger>();
            IWorkUnit <ScriptCreationStateModel> unit = new CreateDeploymentFilesUnit(daMock.Object, fsaMock.Object, loggerMock.Object);
            var project       = new SqlProject("a", "b", "c");
            var configuration = ConfigurationModel.GetDefault();

            configuration.CreateDocumentationWithScriptCreation = true;
            configuration.RemoveSqlCmdStatements = removeSqlCmdStatements;
            var previousVersion = new Version(1, 0);

            Task HandlerFunc(bool b) => Task.CompletedTask;

            var directories = new DirectoryPaths("projectDirectory", "latestArtifactsDirectory", "newArtifactsDirectory");
            var sourcePaths = new DeploySourcePaths("newDacpacPath", "publishProfilePath", "previousDacpacPath");
            var targetPaths = new DeployTargetPaths("deployScriptPath", "deployReportPath");
            var paths       = new PathCollection(directories, sourcePaths, targetPaths);
            var model       = new ScriptCreationStateModel(project, configuration, previousVersion, false, HandlerFunc)
            {
                Paths = paths
            };

            // Act
            await unit.Work(model, CancellationToken.None);

            // Assert
            Assert.AreEqual(StateModelState.TriedToCreateDeploymentFiles, model.CurrentState);
            Assert.AreEqual(expectedResult, model.Result);
            if (removeSqlCmdStatements && createNewDatabase)
            {
                loggerMock.Verify(m => m.LogErrorAsync($"{nameof(PublishProfile.CreateNewDatabase)} cannot bet set to true, when {nameof(ConfigurationModel.RemoveSqlCmdStatements)} is also true."), Times.Once);
            }
            if (removeSqlCmdStatements && backupDatabaseBeforeChanges)
            {
                loggerMock.Verify(m => m.LogErrorAsync($"{nameof(PublishProfile.BackupDatabaseBeforeChanges)} cannot bet set to true, when {nameof(ConfigurationModel.RemoveSqlCmdStatements)} is also true."), Times.Once);
            }
            if (removeSqlCmdStatements && scriptDatabaseOptions)
            {
                loggerMock.Verify(m => m.LogErrorAsync($"{nameof(PublishProfile.ScriptDatabaseOptions)} cannot bet set to true, when {nameof(ConfigurationModel.RemoveSqlCmdStatements)} is also true."), Times.Once);
            }
            if (removeSqlCmdStatements && scriptDeployStateChecks)
            {
                loggerMock.Verify(m => m.LogErrorAsync($"{nameof(PublishProfile.ScriptDeployStateChecks)} cannot bet set to true, when {nameof(ConfigurationModel.RemoveSqlCmdStatements)} is also true."), Times.Once);
            }

            if (expectedResult == null)
            {
                fsaMock.Verify(m => m.WriteFileAsync("deployScriptPath", "pre script post"), Times.Once);
                fsaMock.Verify(m => m.WriteFileAsync("deployReportPath", "report"), Times.Once);
            }
            else
            {
                fsaMock.Verify(m => m.WriteFileAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
            }
        }