From dbf38fbeb83fcbdbd0a2839fe576c24b1144b7d7 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Sat, 5 Jan 2013 23:36:38 +0300 Subject: [PATCH] Changed BaseWebTest to use same logic as BaseDatabaseFactoryTests since we need the ServiceContext now. Fixed a bunch of failing tests... still have some more to fix though. --- src/Umbraco.Tests/TestHelpers/BaseWebTest.cs | 120 ++++++++++++++---- .../UmbracoExamine/IndexInitializer.cs | 40 ++---- src/UmbracoExamine/UmbracoExamine.csproj | 1 + 3 files changed, 106 insertions(+), 55 deletions(-) diff --git a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs index 4f1e3c158e..5e4c9700f0 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs @@ -1,4 +1,6 @@ using System; +using System.Configuration; +using System.Data.SqlServerCe; using System.IO; using System.Web.Routing; using System.Xml; @@ -9,6 +11,10 @@ using Umbraco.Core.Configuration; using Umbraco.Core.IO; using Umbraco.Core.ObjectResolution; using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.SqlSyntax; +using Umbraco.Core.Persistence.UnitOfWork; +using Umbraco.Core.Publishing; +using Umbraco.Core.Services; using Umbraco.Tests.Stubs; using Umbraco.Web; using Umbraco.Web.Routing; @@ -24,45 +30,107 @@ namespace Umbraco.Tests.TestHelpers [SetUp] public virtual void Initialize() { - TestHelper.SetupLog4NetForTests(); - TestHelper.InitializeContentDirectories(); + TestHelper.SetupLog4NetForTests(); + TestHelper.InitializeContentDirectories(); - AppDomain.CurrentDomain.SetData("DataDirectory", TestHelper.CurrentAssemblyDirectory); + string path = TestHelper.CurrentAssemblyDirectory; + AppDomain.CurrentDomain.SetData("DataDirectory", path); - if (RequiresDbSetup) - TestHelper.InitializeDatabase(); + UmbracoSettings.UseLegacyXmlSchema = false; - Resolution.Freeze(); + RepositoryResolver.Current = new RepositoryResolver( + new RepositoryFactory()); - //NOTE: We are not constructing with the service context here because it is not required for these tests (currently) - // if we do, this means that we have to initialized the RepositoryResolver too. - ApplicationContext.Current = new ApplicationContext - { - IsReady = true, - //assign the db context - DatabaseContext = new DatabaseContext(new DefaultDatabaseFactory()) - }; + //Ensure that any database connections from a previous test is disposed. This is really just double safety as its also done in the TearDown. + if (ApplicationContext != null && DatabaseContext != null) + DatabaseContext.Database.Dispose(); + SqlCeContextGuardian.CloseBackgroundConnection(); + + //Delete database file before continueing + string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf"); + if (File.Exists(filePath)) + { + File.Delete(filePath); + } + + //Get the connectionstring settings from config + var settings = ConfigurationManager.ConnectionStrings[Core.Configuration.GlobalSettings.UmbracoConnectionName]; + ConfigurationManager.AppSettings.Set(Core.Configuration.GlobalSettings.UmbracoConnectionName, @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\UmbracoPetaPocoTests.sdf"); + + //Create the Sql CE database + var engine = new SqlCeEngine(settings.ConnectionString); + if (RequiresDbSetup) + { + engine.CreateDatabase(); + } + + Resolution.Freeze(); + ApplicationContext.Current = new ApplicationContext( + //assign the db context + new DatabaseContext(new DefaultDatabaseFactory()), + //assign the service context + new ServiceContext(new PetaPocoUnitOfWorkProvider(), new FileUnitOfWorkProvider(), new PublishingStrategy())) { IsReady = true }; + + //Configure the Database and Sql Syntax based on connection string set in config + DatabaseContext.Initialize(); + if (RequiresDbSetup) + { + //Create the umbraco database and its base data + DatabaseContext.Database.CreateDatabaseSchema(); + } + + + //if (RequiresDbSetup) + // TestHelper.InitializeDatabase(); } [TearDown] public virtual void TearDown() { - TestHelper.CleanContentDirectories(); + DatabaseContext.Database.Dispose(); + //reset the app context + ApplicationContext.ApplicationCache.ClearAllCache(); - //reset the app context - DatabaseContext.Database.Dispose(); - ApplicationContext.ApplicationCache.ClearAllCache(); - ApplicationContext.Current = null; - Resolution.IsFrozen = false; + SyntaxConfig.SqlSyntaxProvider = null; - if (RequiresDbSetup) - { - TestHelper.ClearDatabase(); - SqlCeContextGuardian.CloseBackgroundConnection(); - } + if (RequiresDbSetup) + { + //legacy API database connection close - because a unit test using PetaPoco db-layer can trigger the usage of SqlHelper we need to ensure that a possible connection is closed. + SqlCeContextGuardian.CloseBackgroundConnection(); + } + - AppDomain.CurrentDomain.SetData("DataDirectory", null); + ApplicationContext.Current = null; + Resolution.IsFrozen = false; + RepositoryResolver.Reset(); + + TestHelper.CleanContentDirectories(); + + string path = TestHelper.CurrentAssemblyDirectory; + AppDomain.CurrentDomain.SetData("DataDirectory", null); + + string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf"); + if (File.Exists(filePath)) + { + File.Delete(filePath); + } + + //TestHelper.CleanContentDirectories(); + + ////reset the app context + //DatabaseContext.Database.Dispose(); + //ApplicationContext.ApplicationCache.ClearAllCache(); + //ApplicationContext.Current = null; + //Resolution.IsFrozen = false; + //RepositoryResolver.Reset(); + //if (RequiresDbSetup) + //{ + // TestHelper.ClearDatabase(); + // SqlCeContextGuardian.CloseBackgroundConnection(); + //} + + //AppDomain.CurrentDomain.SetData("DataDirectory", null); Cache.ClearAllCache(); diff --git a/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs b/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs index f70eeb235f..111e432b46 100644 --- a/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs +++ b/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Examine; using Examine.LuceneEngine.Providers; using Lucene.Net.Analysis.Standard; @@ -18,36 +19,17 @@ namespace Umbraco.Tests.UmbracoExamine var i = new UmbracoContentIndexer(new IndexCriteria( new[] { - new TestIndexField { Name = "id", EnableSorting = true, Type = "Number" }, - new TestIndexField { Name = "nodeName", EnableSorting = true }, - new TestIndexField { Name = "updateDate", EnableSorting = true, Type = "DateTime" }, - new TestIndexField { Name = "writerName" }, - new TestIndexField { Name = "path" }, - new TestIndexField { Name = "nodeTypeAlias" }, - new TestIndexField { Name = "parentID" } + new TestIndexField {Name = "id", EnableSorting = true, Type = "Number"}, + new TestIndexField {Name = "nodeName", EnableSorting = true}, + new TestIndexField {Name = "updateDate", EnableSorting = true, Type = "DateTime"}, + new TestIndexField {Name = "writerName"}, + new TestIndexField {Name = "path"}, + new TestIndexField {Name = "nodeTypeAlias"}, + new TestIndexField {Name = "parentID"} }, - new[] - { - new TestIndexField { Name = "headerText" }, - new TestIndexField { Name = "bodyText" }, - new TestIndexField { Name = "metaDescription" }, - new TestIndexField { Name = "metaKeywords" }, - new TestIndexField { Name = "bodyTextColOne" }, - new TestIndexField { Name = "bodyTextColTwo" }, - new TestIndexField { Name = "xmlStorageTest" } - }, - new[] - { - "CWS_Home", - "CWS_Textpage", - "CWS_TextpageTwoCol", - "CWS_NewsEventsList", - "CWS_NewsItem", - "CWS_Gallery", - "CWS_EventItem", - "Image", - }, - new string[] { }, + Enumerable.Empty(), + Enumerable.Empty(), + new string[] {}, -1), luceneDir, //custom lucene directory new TestDataService(), diff --git a/src/UmbracoExamine/UmbracoExamine.csproj b/src/UmbracoExamine/UmbracoExamine.csproj index 9a2a57e4f6..8aab83f62e 100644 --- a/src/UmbracoExamine/UmbracoExamine.csproj +++ b/src/UmbracoExamine/UmbracoExamine.csproj @@ -118,6 +118,7 @@ +