public async Task Work_ScriptCreationStateModel_VerificationSuccessful_Async() { // Arrange var project = new SqlProject("a", "b", "c"); var configuration = ConfigurationModel.GetDefault(); var previousVersion = new Version(1, 0); Task HandleWorkInProgressChanged(bool arg) => 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, true, HandleWorkInProgressChanged) { Paths = paths }; var fsaMock = new Mock <IFileSystemAccess>(); fsaMock.Setup(m => m.CheckIfFileExists(paths.DeploySources.PublishProfilePath)).Returns(true); var loggerMock = new Mock <ILogger>(); IWorkUnit <ScriptCreationStateModel> unit = new VerifyPathsUnit(fsaMock.Object, loggerMock.Object); // Act await unit.Work(model, CancellationToken.None); // Assert Assert.AreEqual(StateModelState.PathsVerified, model.CurrentState); Assert.IsNull(model.Result); loggerMock.Verify(m => m.LogErrorAsync(It.IsAny <string>()), Times.Never); }
public async Task Work_ScriptCreationStateModel_CompleteRun_NoFilesDeleted_ConfigurationDisabled_Async() { // Arrange var fsaMock = new Mock <IFileSystemAccess>(); var vsaMock = new Mock <IVisualStudioAccess>(); var loggerMock = new Mock <ILogger>(); IWorkUnit <ScriptCreationStateModel> unit = new DeleteRefactorLogUnit(fsaMock.Object, vsaMock.Object, loggerMock.Object); var project = new SqlProject("a", "b", "c"); var configuration = ConfigurationModel.GetDefault(); configuration.DeleteRefactorlogAfterVersionedScriptGeneration = false; 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, true, HandlerFunc) { Paths = paths }; // Act await unit.Work(model, CancellationToken.None); // Assert Assert.AreEqual(StateModelState.DeletedRefactorLog, model.CurrentState); Assert.IsNull(model.Result); fsaMock.Verify(m => m.TryToCleanDirectory(It.IsAny <string>(), It.IsAny <string>()), Times.Never); vsaMock.Verify(m => m.RemoveItemFromProjectRoot(project, It.IsAny <string>()), Times.Never); loggerMock.Verify(m => m.LogInfoAsync(It.IsAny <string>()), Times.Never); loggerMock.Verify(m => m.LogTraceAsync(It.IsAny <string>()), Times.Never); }
public async Task Work_ScriptCreationStateModel_FailedToWriteModifiedScript_Async() { // Arrange var exception = new IOException("foo"); const string baseScript = "foo bar"; const string expectedResultScript = "foo bar ab"; var smHeaderMock = new Mock <IScriptModifier>(); var smFooterMock = new Mock <IScriptModifier>(); var mpsMock = new Mock <IScriptModifierProviderService>(); var fsaMock = new Mock <IFileSystemAccess>(); fsaMock.Setup(m => m.ReadFileAsync("deployScriptPath")) .ReturnsAsync(baseScript); fsaMock.Setup(m => m.WriteFileAsync("deployScriptPath", It.IsAny <string>())) .Throws(exception); var loggerMock = new Mock <ILogger>(); IWorkUnit <ScriptCreationStateModel> unit = new ModifyDeploymentScriptUnit(mpsMock.Object, fsaMock.Object, loggerMock.Object); var project = new SqlProject("a", "b", "c"); var configuration = ConfigurationModel.GetDefault(); var previousVersion = new Version(1, 0); const bool createLatest = false; 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, createLatest, HandlerFunc) { Paths = paths }; mpsMock.Setup(m => m.GetScriptModifiers(configuration)).Returns(new Dictionary <ScriptModifier, IScriptModifier> { { ScriptModifier.AddCustomFooter, smFooterMock.Object }, { ScriptModifier.AddCustomHeader, smHeaderMock.Object } }); smHeaderMock.Setup(m => m.ModifyAsync(It.IsNotNull <ScriptModificationModel>())) .Callback((ScriptModificationModel modificationModel) => modificationModel.CurrentScript += " a") .Returns(Task.CompletedTask); smFooterMock.Setup(m => m.ModifyAsync(It.IsNotNull <ScriptModificationModel>())) .Callback((ScriptModificationModel modificationModel) => modificationModel.CurrentScript += "b") .Returns(Task.CompletedTask); // Act await unit.Work(model, CancellationToken.None); // Assert Assert.AreEqual(StateModelState.ModifiedDeploymentScript, model.CurrentState); Assert.IsFalse(model.Result); mpsMock.Verify(m => m.GetScriptModifiers(It.IsAny <ConfigurationModel>()), Times.Once); fsaMock.Verify(m => m.ReadFileAsync(paths.DeployTargets.DeployScriptPath), Times.Once); fsaMock.Verify(m => m.WriteFileAsync(paths.DeployTargets.DeployScriptPath, expectedResultScript), Times.Once); loggerMock.Verify(m => m.LogInfoAsync(It.IsAny <string>()), Times.Exactly(2)); smHeaderMock.Verify(m => m.ModifyAsync(It.IsNotNull <ScriptModificationModel>()), Times.Once); smFooterMock.Verify(m => m.ModifyAsync(It.IsNotNull <ScriptModificationModel>()), Times.Once); loggerMock.Verify(m => m.LogErrorAsync(exception, "Failed to write the modified script"), Times.Once); }
public void Constructor_NoInvalidOperationException_NeitherScriptPathNorDeployPathSet_WhenPreviousDacpacPathIsNotSet() { // Arrange var directories = new DirectoryPaths("projectDirectory", "latestArtifactsDirectory", "newArtifactsDirectory"); var sourcePaths = new DeploySourcePaths("newDacpacPath", "publishProfilePath", null); var targetPaths = new DeployTargetPaths(null, null); // Act & Assert // ReSharper disable once ObjectCreationAsStatement Assert.DoesNotThrow(() => new PathCollection(directories, sourcePaths, targetPaths)); }
private async Task <PathCollection> DeterminePathsAsync([NotNull] SqlProject project, [NotNull] ConfigurationModel configuration, [CanBeNull] Version previousVersion, bool createLatest) { var projectPath = project.FullName; var projectDirectory = Path.GetDirectoryName(projectPath); if (projectDirectory == null) { await _logger.LogErrorAsync($"Cannot get project directory for {project.FullName}"); return(null); } // Versions const string latestKeyword = "latest"; var previousVersionString = previousVersion == null ? null : _versionService.FormatVersion(previousVersion, configuration); var newVersionString = createLatest ? latestKeyword : _versionService.FormatVersion(project.ProjectProperties.DacVersion, configuration); // Directories var artifactsPath = Path.Combine(projectDirectory, configuration.ArtifactsPath); DirectoryPaths directories; { var latestDirectory = Path.Combine(artifactsPath, latestKeyword); var newVersionDirectory = Path.Combine(artifactsPath, newVersionString); directories = new DirectoryPaths(projectDirectory, latestDirectory, newVersionDirectory); } // Source paths var newVersionPath = Path.Combine(directories.NewArtifactsDirectory, $"{project.ProjectProperties.SqlTargetName}.dacpac"); var profilePath = DeterminePublishProfilePath(configuration, projectDirectory); var previousVersionDirectory = previousVersion == null ? null : Path.Combine(artifactsPath, previousVersionString); var previousVersionPath = previousVersion == null ? null : Path.Combine(previousVersionDirectory, $"{project.ProjectProperties.SqlTargetName}.dacpac"); var sources = new DeploySourcePaths(newVersionPath, profilePath, previousVersionPath); // Target paths var deployScriptPath = previousVersion == null ? null : Path.Combine(directories.NewArtifactsDirectory, $"{project.ProjectProperties.SqlTargetName}_{previousVersionString}_{newVersionString}.sql"); var deployReportPath = previousVersion != null && // Can only create report when comparing against a previous version configuration.CreateDocumentationWithScriptCreation ? Path.Combine(directories.NewArtifactsDirectory, $"{project.ProjectProperties.SqlTargetName}_{previousVersionString}_{newVersionString}.xml") : null; var targets = new DeployTargetPaths(deployScriptPath, deployReportPath); return(new PathCollection(directories, sources, targets)); }
public void Constructor_ArgumentNullException_DeployTargets() { // Arrange var directories = new DirectoryPaths("projectDirectory", "latestArtifactsDirectory", "newArtifactsDirectory"); var sourcePaths = new DeploySourcePaths("newDacpacPath", "publishProfilePath", "previousDacpacPath"); // Act & Assert // ReSharper disable once ObjectCreationAsStatement // ReSharper disable AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => new PathCollection(directories, sourcePaths, null)); // ReSharper restore AssignNullToNotNullAttribute }
public async Task ModifyAsync_ErrorsGettingDacpacConstraints_Async() { // Arrange var daMock = new Mock <IDacAccess>(); var loggerMock = new Mock <ILogger>(); daMock.Setup(m => m.GetDefaultConstraintsAsync("previousDacpacPath")) .ReturnsAsync(() => (new DefaultConstraint[0], new [] { "oldError1", "oldError2" })); daMock.Setup(m => m.GetDefaultConstraintsAsync("newDacpacPath")) .ReturnsAsync(() => (new DefaultConstraint[0], new[] { "newError1", "newError2" })); var project = new SqlProject("a", "b", "c"); var config = new ConfigurationModel { ArtifactsPath = "foobar", ReplaceUnnamedDefaultConstraintDrops = true, CommentOutUnnamedDefaultConstraintDrops = false, PublishProfilePath = "Test.publish.xml", VersionPattern = "1.2.3.4", CreateDocumentationWithScriptCreation = true, CustomHeader = "awesome header", CustomFooter = "lame footer", BuildBeforeScriptCreation = true, TrackDacpacVersion = false }; 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); IScriptModifier modifier = new ReplaceUnnamedDefaultConstraintDropsModifier(daMock.Object, loggerMock.Object); var model = new ScriptModificationModel(MultipleDropDefaultConstraintStatements, project, config, paths, new Version(1, 0, 0), false); // Act await modifier.ModifyAsync(model); // Assert Assert.AreEqual(MultipleDropDefaultConstraintStatements, model.CurrentScript); loggerMock.Verify(m => m.LogErrorAsync("Failed to load the default constraints of the previous DACPAC:"), Times.Once); loggerMock.Verify(m => m.LogErrorAsync("oldError1"), Times.Once); loggerMock.Verify(m => m.LogErrorAsync("oldError2"), Times.Once); loggerMock.Verify(m => m.LogErrorAsync("Failed to load the default constraints of the current DACPAC:"), Times.Once); loggerMock.Verify(m => m.LogErrorAsync("newError1"), Times.Once); loggerMock.Verify(m => m.LogErrorAsync("newError2"), Times.Once); }
public void Constructor_CorrectSettingOfProperties() { // Arrange const string newDacpacPath = "newDacpacPath"; const string publishProfilePath = "publishProfilePath"; const string previousDacpacPath = "previousDacpacPath"; // Act var dsp = new DeploySourcePaths(newDacpacPath, publishProfilePath, previousDacpacPath); // Assert Assert.AreEqual(newDacpacPath, dsp.NewDacpacPath); Assert.AreEqual(publishProfilePath, dsp.PublishProfilePath); Assert.AreEqual(previousDacpacPath, dsp.PreviousDacpacPath); }
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); }
public void Constructor_ArgumentNullException_PreviousVersion() { // Arrange var initialScript = "script"; var project = new SqlProject("a", "b", "c"); var configuration = new ConfigurationModel(); 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); // Act & Assert // ReSharper disable once ObjectCreationAsStatement // ReSharper disable AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => new ScriptModificationModel(initialScript, project, configuration, paths, null, false)); // ReSharper restore AssignNullToNotNullAttribute }
public async Task ModifyAsync_CorrectReplacement_OneRegexTimeout_Async() { // Arrange var schemaName = new string('a', 1_000_000) + '.'; var input = string.Format(MultipleDropDefaultConstraintStatementsWithPlaceholder, schemaName); var expectedOutput = string.Format(MultipleDropDefaultConstraintStatementsReplacedPartiallyWithPlaceholder, schemaName); var daMock = new Mock <IDacAccess>(); var loggerMock = new Mock <ILogger>(); daMock.Setup(m => m.GetDefaultConstraintsAsync("previousDacpacPath")) .ReturnsAsync(() => (new[] { new DefaultConstraint("dbo", "Author", "LastName", null), new DefaultConstraint("dbo", "Book", "Title", null), new DefaultConstraint("dbo", "Book", "RegisteredDate", "DF_RegisteredDate_Today") }, null)); daMock.Setup(m => m.GetDefaultConstraintsAsync("newDacpacPath")) .ReturnsAsync(() => (new DefaultConstraint[0], null)); var project = new SqlProject("a", "b", "c"); var config = new ConfigurationModel { ArtifactsPath = "foobar", ReplaceUnnamedDefaultConstraintDrops = true, CommentOutUnnamedDefaultConstraintDrops = false, PublishProfilePath = "Test.publish.xml", VersionPattern = "1.2.3.4", CreateDocumentationWithScriptCreation = true, CustomHeader = "awesome header", CustomFooter = "lame footer", BuildBeforeScriptCreation = true, TrackDacpacVersion = false }; 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); IScriptModifier modifier = new ReplaceUnnamedDefaultConstraintDropsModifier(daMock.Object, loggerMock.Object); var model = new ScriptModificationModel(input, project, config, paths, new Version(1, 0, 0), false); // Act await modifier.ModifyAsync(model); // Assert Assert.AreEqual(expectedOutput, model.CurrentScript); loggerMock.Verify(m => m.LogWarningAsync($"{nameof(ReplaceUnnamedDefaultConstraintDropsModifier)}: Regular expression matching timed out 1 time(s)."), Times.Once); }
public void CurrentScript_Set_ArgumentNullException_WhenSetTotNull() { // Arrange var initialScript = "script"; var project = new SqlProject("a", "b", "c"); var configuration = new ConfigurationModel(); 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 previousVersion = new Version(1, 2, 0); var model = new ScriptModificationModel(initialScript, project, configuration, paths, previousVersion, true); // Act & Assert // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => model.CurrentScript = null); }
public void Constructor_CorrectSettingOfProperties(bool setDeployScriptPath, bool setDeployReportPath) { // Arrange var directories = new DirectoryPaths("projectDirectory", "latestArtifactsDirectory", "newArtifactsDirectory"); var sourcePaths = new DeploySourcePaths("newDacpacPath", "publishProfilePath", "previousDacpacPath"); var targetPaths = new DeployTargetPaths(setDeployScriptPath ? "deployScriptPath" : null, setDeployReportPath ? "deployReportPath" : null); // Act var pc = new PathCollection(directories, sourcePaths, targetPaths); // Assert Assert.AreSame(directories, pc.Directories); Assert.AreSame(sourcePaths, pc.DeploySources); Assert.AreSame(targetPaths, pc.DeployTargets); }
public async Task Modify_CorrectModification_Async() { // Arrange IScriptModifier modifier = new CommentOutUnnamedDefaultConstraintDropsModifier(); var project = new SqlProject("", "", ""); var configuration = new ConfigurationModel(); 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 ScriptModificationModel(MultipleDropDefaultConstraintStatements, project, configuration, paths, new Version(1, 0, 0), false); // Act await modifier.ModifyAsync(model); // Assert Assert.AreEqual(MultipleDropDefaultConstraintStatementsCommented, model.CurrentScript); }
public void Modify_ArgumentException_ProjectPropertiesSqlTargetNameNull() { // Arrange IScriptModifier modifier = new TrackDacpacVersionModifier(); var project = new SqlProject("", "", ""); var configuration = new ConfigurationModel(); 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 ScriptModificationModel(MultiLineInputWithFinalGo, project, configuration, paths, new Version(1, 0, 0), false); // Act var e = Assert.Throws <ArgumentException>(() => modifier.ModifyAsync(model)); // Assert Assert.IsNotNull(e); Assert.IsTrue(e.Message.Contains(nameof(SqlProjectProperties.SqlTargetName))); }
public async Task ModifyAsync_CorrectReplacement_Async() { // Arrange var daMock = new Mock <IDacAccess>(); var loggerMock = new Mock <ILogger>(); daMock.Setup(m => m.GetDefaultConstraintsAsync("previousDacpacPath")) .ReturnsAsync(() => (new[] { new DefaultConstraint("dbo", "Author", "LastName", null), new DefaultConstraint("dbo", "Book", "Title", null), new DefaultConstraint("dbo", "Book", "RegisteredDate", "DF_RegisteredDate_Today") }, null)); daMock.Setup(m => m.GetDefaultConstraintsAsync("newDacpacPath")) .ReturnsAsync(() => (new DefaultConstraint[0], null)); var project = new SqlProject("a", "b", "c"); var config = new ConfigurationModel { ArtifactsPath = "foobar", ReplaceUnnamedDefaultConstraintDrops = true, CommentOutUnnamedDefaultConstraintDrops = false, PublishProfilePath = "Test.publish.xml", VersionPattern = "1.2.3.4", CreateDocumentationWithScriptCreation = true, CustomHeader = "awesome header", CustomFooter = "lame footer", BuildBeforeScriptCreation = true, TrackDacpacVersion = false }; 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); IScriptModifier modifier = new ReplaceUnnamedDefaultConstraintDropsModifier(daMock.Object, loggerMock.Object); var model = new ScriptModificationModel(MultipleDropDefaultConstraintStatements, project, config, paths, new Version(1, 0, 0), false); // Act await modifier.ModifyAsync(model); // Assert Assert.AreEqual(MultipleDropDefaultConstraintStatementsReplaced, model.CurrentScript); }
public void Paths_Get_Set() { // Arrange var project = new SqlProject("a", "b", "c"); var configuration = ConfigurationModel.GetDefault(); var previousVersion = new Version(1, 0); // ReSharper disable once ConvertToLocalFunction Func <bool, Task> changeHandler = b => Task.CompletedTask; var model = new ScriptCreationStateModel(project, configuration, previousVersion, true, changeHandler); 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); // Act model.Paths = paths; // Assert Assert.AreSame(paths, model.Paths); }
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); }
public async Task Modify_CorrectModification_MajorMinorVersion_Async(string input) { // Arrange IScriptModifier modifier = new TrackDacpacVersionModifier(); var project = new SqlProject("", "", ""); project.ProjectProperties.SqlTargetName = "Database.TestProject"; project.ProjectProperties.DacVersion = new Version(500, 30); var configuration = new ConfigurationModel(); 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 ScriptModificationModel(input, project, configuration, paths, new Version(1, 0, 0), false); // Act await modifier.ModifyAsync(model); // Assert Assert.AreEqual(FinalMultilineStatementMajorMinorVersion, model.CurrentScript); }
public async Task Work_ScriptCreationStateModel_NoError_Async() { // Arrange var project = new SqlProject("a", "b", "c"); var configuration = ConfigurationModel.GetDefault(); configuration.SharedDacpacRepositoryPath = "C:\\Temp\\Test\\"; var previousVersion = new Version(1, 2, 3); Task HandleWorkInProgressChanged(bool arg) => 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, true, HandleWorkInProgressChanged) { Paths = paths }; var fsaMock = new Mock <IFileSystemAccess>(); fsaMock.Setup(m => m.EnsureDirectoryExists("C:\\Temp\\Test\\")) .Returns(null as string); fsaMock.Setup(m => m.CopyFile("newDacpacPath", "C:\\Temp\\Test\\newDacpacPath")) .Returns(null as string); var loggerMock = new Mock <ILogger>(); IWorkUnit <ScriptCreationStateModel> unit = new CopyDacpacToSharedDacpacRepositoryUnit(fsaMock.Object, loggerMock.Object); // Act await unit.Work(model, CancellationToken.None); // Assert Assert.AreEqual(StateModelState.TriedToCopyDacpacToSharedDacpacRepository, model.CurrentState); Assert.IsNull(model.Result); fsaMock.Verify(m => m.EnsureDirectoryExists("C:\\Temp\\Test\\"), Times.Once); fsaMock.Verify(m => m.CopyFile("newDacpacPath", "C:\\Temp\\Test\\newDacpacPath"), Times.Once); loggerMock.Verify(m => m.LogInfoAsync("Copying DACPAC to shared DACPAC repository ..."), 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); }
public async Task Modify_NoLeadingNewLineWhenNullOrWhiteSpace_Async(string customHeader) { // Arrange IScriptModifier s = new AddCustomHeaderModifier(); const string input = "foobar"; var project = new SqlProject("a", "b", "c"); var configuration = new ConfigurationModel { CustomHeader = customHeader }; 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 ScriptModificationModel(input, project, configuration, paths, new Version(1, 0, 0), false); // Act await s.ModifyAsync(model); // Assert Assert.IsNotNull(model.CurrentScript); Assert.AreEqual("foobar", model.CurrentScript); }
public async Task ModifyAsync_CorrectModification_Async(string input) { // Arrange IScriptModifier modifier = new RemoveSqlCmdStatementsModifier(); var project = new SqlProject("a", "b", "c"); project.ProjectProperties.DacVersion = new Version(1, 3, 0); var configuration = new ConfigurationModel { CustomFooter = "footer" }; 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 ScriptModificationModel(input, project, configuration, paths, new Version(1, 2, 0), false); // Act await modifier.ModifyAsync(model); // Assert Assert.AreEqual(MultiLineInputWithoutSqlcmdStatements, model.CurrentScript); }
public void Constructor_CorrectInitialization() { // Arrange var initialScript = "script"; var project = new SqlProject("a", "b", "c"); var configuration = new ConfigurationModel(); 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 previousVersion = new Version(1, 2, 0); // Act var model = new ScriptModificationModel(initialScript, project, configuration, paths, previousVersion, true); // Assert Assert.AreEqual(initialScript, model.CurrentScript); Assert.AreSame(project, model.Project); Assert.AreSame(configuration, model.Configuration); Assert.AreSame(paths, model.Paths); Assert.AreSame(previousVersion, model.PreviousVersion); Assert.IsTrue(model.CreateLatest); }
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); } }