From e333ad2b237ee1b14540d873b3620856588dba09 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 20 Feb 2013 14:38:28 -0100 Subject: [PATCH] Tests.Routing - fix tests --- src/SQLCE4Umbraco/SqlCEHelper.cs | 89 +++++++++++++++---- src/Umbraco.Tests/Models/ContentXmlTest.cs | 4 +- src/Umbraco.Tests/Models/MediaXmlTest.cs | 8 +- .../LookupByNiceUrlWithDomainsTests.cs | 6 +- .../NiceUrlsProviderWithDomainsTests.cs | 8 ++ .../Routing/UmbracoModuleTests.cs | 27 ------ .../Routing/UrlsWithNestedDomains.cs | 8 ++ src/Umbraco.Tests/TestHelpers/TestHelper.cs | 11 +++ 8 files changed, 112 insertions(+), 49 deletions(-) diff --git a/src/SQLCE4Umbraco/SqlCEHelper.cs b/src/SQLCE4Umbraco/SqlCEHelper.cs index c0a4c875b1..a35f84de82 100644 --- a/src/SQLCE4Umbraco/SqlCEHelper.cs +++ b/src/SQLCE4Umbraco/SqlCEHelper.cs @@ -50,35 +50,90 @@ namespace SqlCE4Umbraco /// internal void ClearDatabase() { + // drop constraints before tables to avoid exceptions + // looping on try/catching exceptions was not really nice + + // http://stackoverflow.com/questions/536350/drop-all-the-tables-stored-procedures-triggers-constriants-and-all-the-depend + var localConnection = new SqlCeConnection(ConnectionString); if (System.IO.File.Exists(ReplaceDataDirectory(localConnection.Database))) { - var tables = new List(); - using (var reader = ExecuteReader("select table_name from information_schema.tables where TABLE_TYPE <> 'VIEW'")) + List tables; + + // drop foreign keys + // SQL may need "where constraint_catalog=DB_NAME() and ..." + tables = new List(); + using (var reader = ExecuteReader("select table_name from information_schema.table_constraints where constraint_type = 'FOREIGN KEY' order by table_name")) { - while (reader.Read()) + while (reader.Read()) tables.Add(reader.GetString("table_name").Trim()); + } + + foreach (var table in tables) + { + var constraints = new List(); + using (var reader = ExecuteReader("select constraint_name from information_schema.table_constraints where constraint_type = 'FOREIGN KEY' and table_name = '" + table + "' order by constraint_name")) { - tables.Add(reader.GetString("TABLE_NAME")); + while (reader.Read()) constraints.Add(reader.GetString("constraint_name").Trim()); + } + foreach (var constraint in constraints) + { + // SQL may need "[dbo].[table]" + ExecuteNonQuery("alter table [" + table + "] drop constraint [" + constraint + "]"); } } - while(tables.Any()) + // drop primary keys + // SQL may need "where constraint_catalog=DB_NAME() and ..." + tables = new List(); + using (var reader = ExecuteReader("select table_name from information_schema.table_constraints where constraint_type = 'PRIMARY KEY' order by table_name")) { - for (var i = 0; i < tables.Count; i++) - { - var dropTable = "DROP TABLE " + tables[i]; + while (reader.Read()) tables.Add(reader.GetString("table_name").Trim()); + } - try - { - ExecuteNonQuery(dropTable); - tables.Remove(tables[i]); - } - catch (SqlHelperException ex) - { - //this will occur because there is no cascade option, so we just wanna try the next one - } + foreach (var table in tables) + { + var constraints = new List(); + using (var reader = ExecuteReader("select constraint_name from information_schema.table_constraints where constraint_type = 'PRIMARY KEY' and table_name = '" + table + "' order by constraint_name")) + { + while (reader.Read()) constraints.Add(reader.GetString("constraint_name").Trim()); + } + foreach (var constraint in constraints) + { + // SQL may need "[dbo].[table]" + ExecuteNonQuery("alter table [" + table + "] drop constraint [" + constraint + "]"); } } + + // drop tables + tables = new List(); + using (var reader = ExecuteReader("select table_name from information_schema.tables where table_type <> 'VIEW' order by table_name")) + { + while (reader.Read()) tables.Add(reader.GetString("table_name").Trim()); + } + + foreach (var table in tables) + { + ExecuteNonQuery("drop table [" + table + "]"); + } + } + } + + /// + /// Drops all foreign keys on a table. + /// + /// The name of the table. + /// To be used in unit tests. + internal void DropForeignKeys(string table) + { + var constraints = new List(); + using (var reader = ExecuteReader("select constraint_name from information_schema.table_constraints where constraint_type = 'FOREIGN KEY' and table_name = '" + table + "' order by constraint_name")) + { + while (reader.Read()) constraints.Add(reader.GetString("constraint_name").Trim()); + } + foreach (var constraint in constraints) + { + // SQL may need "[dbo].[table]" + ExecuteNonQuery("alter table [" + table + "] drop constraint [" + constraint + "]"); } } diff --git a/src/Umbraco.Tests/Models/ContentXmlTest.cs b/src/Umbraco.Tests/Models/ContentXmlTest.cs index 38f75bf9c6..4f820dd4e2 100644 --- a/src/Umbraco.Tests/Models/ContentXmlTest.cs +++ b/src/Umbraco.Tests/Models/ContentXmlTest.cs @@ -35,10 +35,10 @@ namespace Umbraco.Tests.Models [TearDown] public override void TearDown() { + base.TearDown(); + //reset the app context DataTypesResolver.Reset(); - - base.TearDown(); } [Test] public void Can_Generate_Xml_Representation_Of_Content() diff --git a/src/Umbraco.Tests/Models/MediaXmlTest.cs b/src/Umbraco.Tests/Models/MediaXmlTest.cs index ff7756b804..773a7ad09a 100644 --- a/src/Umbraco.Tests/Models/MediaXmlTest.cs +++ b/src/Umbraco.Tests/Models/MediaXmlTest.cs @@ -2,6 +2,7 @@ using System.Xml.Linq; using NUnit.Framework; using Umbraco.Core; +using Umbraco.Core.ObjectResolution; using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; @@ -16,6 +17,8 @@ namespace Umbraco.Tests.Models [SetUp] public override void Initialize() { + Resolution.IsFrozen = false; + //this ensures its reset PluginManager.Current = new PluginManager(); @@ -26,6 +29,7 @@ namespace Umbraco.Tests.Models typeof(tinyMCE3dataType).Assembly }; + DataTypesResolver.Reset(); DataTypesResolver.Current = new DataTypesResolver( () => PluginManager.Current.ResolveDataTypes()); @@ -35,10 +39,10 @@ namespace Umbraco.Tests.Models [TearDown] public override void TearDown() { + base.TearDown(); + //reset the app context DataTypesResolver.Reset(); - - base.TearDown(); } [Test] public void Can_Generate_Xml_Representation_Of_Media() diff --git a/src/Umbraco.Tests/Routing/LookupByNiceUrlWithDomainsTests.cs b/src/Umbraco.Tests/Routing/LookupByNiceUrlWithDomainsTests.cs index 0fe5b1f679..1b75ee81d5 100644 --- a/src/Umbraco.Tests/Routing/LookupByNiceUrlWithDomainsTests.cs +++ b/src/Umbraco.Tests/Routing/LookupByNiceUrlWithDomainsTests.cs @@ -17,7 +17,11 @@ namespace Umbraco.Tests.Routing public override void Initialize() { base.Initialize(); - InitializeLanguagesAndDomains(); + + // ensure we can create them although the content is not in the database + TestHelper.DropForeignKeys("umbracoDomains"); + + InitializeLanguagesAndDomains(); } void InitializeLanguagesAndDomains() diff --git a/src/Umbraco.Tests/Routing/NiceUrlsProviderWithDomainsTests.cs b/src/Umbraco.Tests/Routing/NiceUrlsProviderWithDomainsTests.cs index 03e9d31ee7..28752762a7 100644 --- a/src/Umbraco.Tests/Routing/NiceUrlsProviderWithDomainsTests.cs +++ b/src/Umbraco.Tests/Routing/NiceUrlsProviderWithDomainsTests.cs @@ -14,6 +14,14 @@ namespace Umbraco.Tests.Routing [TestFixture] public class NiceUrlsProviderWithDomainsTests : BaseRoutingTest { + public override void Initialize() + { + base.Initialize(); + + // ensure we can create them although the content is not in the database + TestHelper.DropForeignKeys("umbracoDomains"); + } + internal override IRoutesCache GetRoutesCache() { return new DefaultRoutesCache(false); diff --git a/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs b/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs index 34aec56aed..5e7e47fc71 100644 --- a/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs +++ b/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs @@ -16,33 +16,6 @@ using umbraco.cms.businesslogic.template; namespace Umbraco.Tests.Routing { - [TestFixture, RequiresSTA] - public class PublishedContentRequestBuilderTests : BaseRoutingTest - { - - //[Test] - //public void Alt_Template() - //{ - // var template = Template.MakeNew("test", new User(0)); - // var altTemplate = Template.MakeNew("alt", new User(0)); - // var umbracoContext = GetUmbracoContext("/home?altTemplate=" + altTemplate.Alias, template.Id); - // // create the new document request since we're rendering a document on the front-end - // var docreq = PublishedContentRequest.CreateForFrontEndRequest(umbracoContext); - - // //create the searcher - // var searcher = new PublishedContentRequestBuilder(docreq); - // //find domain - // searcher.LookupDomain(); - // // redirect if it has been flagged - // Assert.IsFalse(docreq.IsRedirect); - - // //find the document, found will be true if the doc request has found BOTH a node and a template - // var found = searcher.LookupDocument(); - - //} - - } - [TestFixture, RequiresSTA] public class UmbracoModuleTests : BaseRoutingTest { diff --git a/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs b/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs index 858ef9c85b..515263eef6 100644 --- a/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs +++ b/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs @@ -64,6 +64,14 @@ namespace Umbraco.Tests.Routing //Assert.AreEqual("http://domain1.com/1001-1/1001-1-1", routingContext.NiceUrlProvider.GetNiceUrl(100111, true)); // bad } + public override void Initialize() + { + base.Initialize(); + + // ensure we can create them although the content is not in the database + TestHelper.DropForeignKeys("umbracoDomains"); + } + internal override IRoutesCache GetRoutesCache() { return new DefaultRoutesCache(false); diff --git a/src/Umbraco.Tests/TestHelpers/TestHelper.cs b/src/Umbraco.Tests/TestHelpers/TestHelper.cs index 819ccdb68b..6ab161a3b0 100644 --- a/src/Umbraco.Tests/TestHelpers/TestHelper.cs +++ b/src/Umbraco.Tests/TestHelpers/TestHelper.cs @@ -31,6 +31,17 @@ namespace Umbraco.Tests.TestHelpers dataHelper.ClearDatabase(); } + public static void DropForeignKeys(string table) + { + var databaseSettings = ConfigurationManager.ConnectionStrings[Core.Configuration.GlobalSettings.UmbracoConnectionName]; + var dataHelper = DataLayerHelper.CreateSqlHelper(databaseSettings.ConnectionString, false) as SqlCEHelper; + + if (dataHelper == null) + throw new InvalidOperationException("The sql helper for unit tests must be of type SqlCEHelper, check the ensure the connection string used for this test is set to use SQLCE"); + + dataHelper.DropForeignKeys(table); + } + /// /// Initializes a new database ///