示例#1
0
        public void ThatBuildUndoScriptWorks()
        {
            string expectedResult   = string.Format("Undo Script Header -- CurrentVersion = 3;Current DateTime = {1};Current User = {0}\r\nScript Header -- Script Name = 2 2.sql;Script Id = 2;Script Description = 2.sql\r\nUndo text.\r\nScript Footer -- String Name = 2 2.sql;Script Id = 2\r\nUndo Script Footer -- Current DateTime = {1}\r\n", Environment.UserName, DateTime.Now.ToString("g"));
            string undoScriptHeader = "Undo Script Header -- CurrentVersion = $(CurrentVersion);Current DateTime = $(CurrentDateTime);Current User = $(CurrentUser)";
            string undoHeader       = "Script Header -- Script Name = $(ScriptName);Script Id = $(ScriptId);Script Description = $(ScriptDescription)";
            string undoFooter       = "Script Footer -- String Name = $(ScriptName);Script Id = $(ScriptId)";
            string undoScriptFooter = "Undo Script Footer -- Current DateTime = $(CurrentDateTime)";
            string undoToken        = "--//@Undo";

            ScriptService constructorTest = new ScriptService();

            var databaseServiceMock = new Mock <IDatabaseService>(MockBehavior.Strict);
            var fileServiceMock     = new Mock <IFileService>(MockBehavior.Strict);

            // Database service mock setup
            databaseServiceMock.Setup(d => d.GetScriptFromFile(DatabaseScriptEnum.UndoScriptHeader)).Returns(undoScriptHeader);
            databaseServiceMock.Setup(d => d.GetScriptFromFile(DatabaseScriptEnum.UndoHeader)).Returns(undoHeader);
            databaseServiceMock.Setup(d => d.GetScriptFromFile(DatabaseScriptEnum.UndoFooter)).Returns(undoFooter);
            databaseServiceMock.Setup(d => d.GetScriptFromFile(DatabaseScriptEnum.UndoScriptFooter)).Returns(undoScriptFooter);
            databaseServiceMock.Setup(d => d.GetScriptFromFile(DatabaseScriptEnum.UndoToken)).Returns(undoToken);

            // file Service mock setup
            fileServiceMock.Setup(f => f.GetFileContents(It.IsAny <string>(), It.IsAny <bool>())).Returns(
                (string s, bool b) =>
            {
                ScriptFile file = new ScriptFile();
                file.Parse(s);
                if (file.Id % 2 == 0)
                {
                    return(file.Description + undoToken + "Undo text.");
                }
                else
                {
                    return(file.Description);
                }
            });

            IDictionary <int, IScriptFile> changes = this.GetChanges();

            IScriptService scriptService = new ScriptService(databaseServiceMock.Object, fileServiceMock.Object, new TokenReplacer(), new ConfigurationService());

            string result = scriptService.BuildUndoScript(changes);

            Assert.That(result, Is.EqualTo(expectedResult));
        }
示例#2
0
        public void ThatBuildUndoScriptWorks()
        {
            DateTime dateTime = new DateTime(2014, 09, 17, 17, 42, 55);

            TimeProvider.Current = new MockTimeProvider(dateTime);
            MockEnvironmentProvider mockEnvironmentProvider = new MockEnvironmentProvider();

            mockEnvironmentProvider.SetUserName("userName");
            EnvironmentProvider.Current = mockEnvironmentProvider;

            string undoScriptHeader =
                "Undo Script Header -- CurrentVersion = $(CurrentVersion);Current DateTime = $(CurrentDateTime);Current User = $(CurrentUser)";
            string undoHeader =
                "Script Header -- Script Name = $(ScriptName);Script Id = $(ScriptId);Script Description = $(ScriptDescription)";
            string undoFooter       = "Script Footer -- String Name = $(ScriptName);Script Id = $(ScriptId)";
            string undoScriptFooter = "Undo Script Footer -- Current DateTime = $(CurrentDateTime)";
            string undoToken        = "--//@Undo";

            ScriptService constructorTest = new ScriptService();

            Mock <IDatabaseService> databaseServiceMock = new Mock <IDatabaseService>(MockBehavior.Strict);
            Mock <IFileService>     fileServiceMock     = new Mock <IFileService>(MockBehavior.Strict);

            // Database service mock setup
            databaseServiceMock.Setup(d => d.GetScriptFromFile(DatabaseScriptEnum.UndoScriptHeader))
            .Returns(undoScriptHeader);
            databaseServiceMock.Setup(d => d.GetScriptFromFile(DatabaseScriptEnum.UndoHeader)).Returns(undoHeader);
            databaseServiceMock.Setup(d => d.GetScriptFromFile(DatabaseScriptEnum.UndoFooter)).Returns(undoFooter);
            databaseServiceMock.Setup(d => d.GetScriptFromFile(DatabaseScriptEnum.UndoScriptFooter))
            .Returns(undoScriptFooter);
            databaseServiceMock.Setup(d => d.GetScriptFromFile(DatabaseScriptEnum.UndoToken)).Returns(undoToken);

            Mock <IFileService> fsMock2 = new Mock <IFileService>(MockBehavior.Strict);

            fsMock2.Setup(f => f.GetFileContents(It.IsAny <string>(), It.IsAny <bool>())).Returns(string.Empty);

            // file Service mock setup
            fileServiceMock.Setup(f => f.GetFileContents(It.IsAny <string>(), It.IsAny <bool>()))
            .Returns(
                (string s, bool b) =>
            {
                ScriptFile file = new ScriptFile();
                file.Parse(fsMock2.Object, s);
                if (file.Id % 2 == 0)
                {
                    return(file.Description + undoToken + "Undo text.");
                }
                return(file.Description);
            });

            IDictionary <decimal, IScriptFile> changes = this.GetChanges();
            Mock <IConfigurationService>       configurationService = new Mock <IConfigurationService>();

            configurationService.Setup(c => c.DatabaseService).Returns(databaseServiceMock.Object);

            IScriptService scriptService = new ScriptService(
                fileServiceMock.Object,
                new TokenReplacer(new ConfigurationService()),
                configurationService.Object);

            string result = scriptService.BuildUndoScript(changes);

            TimeProvider.ResetToDefault();
            EnvironmentProvider.ResetToDefault();
            Approvals.Verify(result);
        }