ScriptHelperTests.cs :  » Bloggers » SubText » UnitTests » Subtext » Scripting » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » UnitTests » Subtext » Scripting » ScriptHelperTests.cs
#region Disclaimer/Info

///////////////////////////////////////////////////////////////////////////////////////////////////
// Subtext WebLog
// 
// Subtext is an open source weblog system that is a fork of the .TEXT
// weblog system.
//
// For updated news and information please visit http://subtextproject.com/
// Subtext is hosted at Google Code at http://code.google.com/p/subtext/
// The development mailing list is at subtext@googlegroups.com 
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.IO;
using MbUnit.Framework;
using Subtext.Framework.Infrastructure.Installation;
using Subtext.Scripting;
using Subtext.Scripting.Exceptions;

namespace UnitTests.Subtext.Scripting{
    /// <summary>
    /// Summary description for ScriptHelperTests.
    /// </summary>
    [TestFixture]
    public class ScriptHelperTests
    {
        [Test]
        public void CanParseGoWithDashDashCommentAfter()
        {
            const string script = @"SELECT * FROM foo;
 GO --  Hello Phil
CREATE PROCEDURE dbo.Test AS SELECT * FROM foo";
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(2, scripts.Count);
        }

        [Test]
        public void CanParseNestedComments()
        {
            const string script = @"/*
select 1
/* nested comment */
go
delete from users
-- */";
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(1, scripts.Count, "This contains a comment and no scripts.");
        }

        [Test]
        public void SlashStarCommentAfterGoThrowsException()
        {
            const string script = @"PRINT 'blah'
GO /* blah */";

            UnitTestHelper.AssertThrows<SqlParseException>(() => Script.ParseScripts(script));
        }

        [Test]
        public void CanParseSuccessiveGoStatements()
        {
            const string script = @"GO
GO";
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(0, scripts.Count, "Expected no scripts since they would be empty.");
        }

        [Test]
        public void SemiColonDoesNotSplitScript()
        {
            const string script = "CREATE PROC Blah AS SELECT FOO; SELECT Bar;";
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(1, scripts.Count, "Expected no scripts since they would be empty.");
        }

        [Test]
        public void CanParseQuotedCorrectly()
        {
            const string script = @"INSERT INTO #Indexes
  EXEC sp_helpindex 'dbo.subtext_URLs'";

            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(script, scripts[0].ScriptText, "Script text should not be modified");
        }

        [Test]
        public void CanParseSimpleScript()
        {
            string script = "Test" + Environment.NewLine + "go";
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(1, scripts.Count);
            Assert.AreEqual("Test", scripts[0].ScriptText);
        }

        [Test]
        public void CanParseCommentBeforeGoStatement()
        {
            const string script = @"SELECT FOO
/*TEST*/ GO
BAR";
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(2, scripts.Count);
        }

        [Test]
        public void CanParseCommentWithQuoteChar()
        {
            const string script = @"/* Add the Url column to the subtext_Log table if it doesn't exist */
  ADD [Url] VARCHAR(255) NULL
GO
    AND    COLUMN_NAME = 'BlogGroup') IS NULL";
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(2, scripts.Count);
        }

        [Test]
        public void CanParseDashDashCommentWithQuoteChar()
        {
            const string script = @"-- Add the Url column to the subtext_Log table if it doesn't exist
SELECT * FROM BLAH
GO
PRINT 'FOO'";
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(2, scripts.Count);
        }

        [Test]
        public void CanParseLineEndingInDashDashComment()
        {
            const string script = @"SELECT * FROM BLAH -- Comment
GO
FOOBAR
GO";
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(2, scripts.Count);
        }

        [Test]
        public void CanParseSimpleScriptEndingInNewLine()
        {
            string script = "Test" + Environment.NewLine + "GO" + Environment.NewLine;
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(1, scripts.Count);
            Assert.AreEqual("Test", scripts[0].ScriptText);
        }

        [Test]
        public void MultiLineQuoteShouldNotIgnoreDoubleQuote()
        {
            string script = "PRINT '" + Environment.NewLine
                            + "''" + Environment.NewLine
                            + "GO" + Environment.NewLine
                            + "/*" + Environment.NewLine
                            + "GO"
                            + "'";

            ScriptCollection scripts = Script.ParseScripts(script);
            
            Assert.AreEqual(1, scripts.Count);
            UnitTestHelper.AssertStringsEqualCharacterByCharacter(script, scripts[0].ScriptText);
        }

        [Test]
        public void MultiLineQuoteShouldNotBeSplitByGoKeyword()
        {
            string script = "PRINT '" + Environment.NewLine
                            + "GO" + Environment.NewLine
                            + "SELECT * FROM BLAH" + Environment.NewLine
                            + "GO" + Environment.NewLine
                            + "'";

            ScriptCollection scripts = Script.ParseScripts(script);

            UnitTestHelper.AssertStringsEqualCharacterByCharacter(script, scripts[0].ScriptText);
            Assert.AreEqual(1, scripts.Count, "expected only one script");
        }

        /// <summary>
        /// Makes sure that ParseScript parses correctly.
        /// </summary>
        [Test]
        [RollBack]
        public void ParseScriptParsesCorrectly()
        {
            const string script = @"SET QUOTED_IDENTIFIER OFF 
-- Comment
Go
    
SET ANSI_NULLS ON 


GO

GO

SET ANSI_NULLS ON 


CREATE TABLE [<username,varchar,dbo>].[blog_Gost] (
  [HostUserName] [nvarchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
  [Password] [nvarchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
  [Salt] [nvarchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
  [DateCreated] [datetime] NOT NULL
) ON [PRIMARY]
gO

";
            ScriptCollection scripts = Script.ParseScripts(script);
            Assert.AreEqual(3, scripts.Count, "This should parse to three scripts.");
            for(int i = 0; i < scripts.Count; i++)
            {
                Script sqlScript = scripts[i];
                Assert.IsFalse(sqlScript.ScriptText.StartsWith("GO"), "Script '" + i + "' failed had a GO statement");
            }

            string expectedThirdScriptBeginning = "SET ANSI_NULLS ON "
                                                  + Environment.NewLine
                                                  + Environment.NewLine
                                                  + Environment.NewLine +
                                                  "CREATE TABLE [<username,varchar,dbo>].[blog_Gost]";

            Assert.AreEqual(expectedThirdScriptBeginning,
                            scripts[2].OriginalScriptText.Substring(0, expectedThirdScriptBeginning.Length),
                            "Script not parsed correctly");

            scripts.TemplateParameters.SetValue("username", "haacked");

            expectedThirdScriptBeginning = "SET ANSI_NULLS ON "
                                           + Environment.NewLine
                                           + Environment.NewLine
                                           + Environment.NewLine + "CREATE TABLE [haacked].[blog_Gost]";

            Assert.AreEqual(expectedThirdScriptBeginning,
                            scripts[2].ScriptText.Substring(0, expectedThirdScriptBeginning.Length),
                            "Script not parsed correctly");
        }

        /// <summary>
        /// Unpacks the installation script and makes sure it returns a script.
        /// </summary>
        [Test]
        public void UnpackScriptReturnsScript()
        {
            Stream stream = ScriptHelper.UnpackEmbeddedScript("Installation.01.00.00.sql");
            Assert.IsNotNull(stream);
        }

        /// <summary>
        /// Unpacks the installation script and makes sure it returns a script.
        /// </summary>
        [Test]
        public void UnpackScriptAsStringReturnsScript()
        {
            string script = ScriptHelper.UnpackEmbeddedScriptAsString("Installation.01.00.00.sql");
            StringAssert.IsNonEmpty(script);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.