public void ModifyAsync_ArgumentNullException_Model()
        {
            // Arrange
            var             daMock     = Mock.Of <IDacAccess>();
            var             loggerMock = Mock.Of <ILogger>();
            IScriptModifier modifier   = new ReplaceUnnamedDefaultConstraintDropsModifier(daMock, loggerMock);

            // Act & Assert
            // ReSharper disable AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(() => modifier.ModifyAsync(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 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 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);
        }