From cfaa353dff491e1046ee65e4f55faa2066c81224 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Wed, 28 Oct 2020 14:54:16 +0100 Subject: [PATCH] Fixed isses with NewEmpty* integration tests Signed-off-by: Bjarke Berg --- .../Testing/UmbracoIntegrationTest.cs | 34 ++++++++-- .../Scoping/ScopeFileSystemsTests.cs | 6 +- .../Scoping/ScopeTests.cs | 62 +++++++++---------- .../Services/ThreadSafetyServiceTest.cs | 2 - src/Umbraco.Tests/Umbraco.Tests.csproj | 1 - 5 files changed, 63 insertions(+), 42 deletions(-) rename src/{Umbraco.Tests => Umbraco.Tests.Integration/Umbraco.Infrastructure}/Scoping/ScopeTests.cs (93%) diff --git a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs index f29ca61d65..3430644795 100644 --- a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs +++ b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs @@ -92,6 +92,7 @@ namespace Umbraco.Tests.Integration.Testing [SetUp] public virtual void Setup() { + InMemoryConfiguration[Constants.Configuration.ConfigGlobal + ":" + nameof(GlobalSettings.InstallEmptyDatabase)] = "true"; var hostBuilder = CreateHostBuilder(); var host = hostBuilder.Start(); @@ -143,8 +144,10 @@ namespace Umbraco.Tests.Integration.Testing .ConfigureAppConfiguration((context, configBuilder) => { context.HostingEnvironment = TestHelper.GetWebHostEnvironment(); - Configuration = context.Configuration; + configBuilder.Sources.Clear(); configBuilder.AddInMemoryCollection(InMemoryConfiguration); + + Configuration = configBuilder.Build(); }) .ConfigureServices((hostContext, services) => { @@ -392,12 +395,21 @@ namespace Umbraco.Tests.Integration.Testing break; case UmbracoTestOptions.Database.NewEmptyPerTest: - var newEmptyDbId = db.AttachEmpty(); // Add teardown callback OnTestTearDown(() => db.Detach(newEmptyDbId)); + // We must re-configure our current factory since attaching a new LocalDb from the pool changes connection strings + if (!databaseFactory.Configured) + { + databaseFactory.Configure(db.ConnectionString, Constants.DatabaseProviders.SqlServer); + } + + // re-run the runtime level check + runtimeState.DetermineRuntimeLevel(); + + Assert.AreEqual(RuntimeLevel.Install, runtimeState.Level); break; case UmbracoTestOptions.Database.NewSchemaPerFixture: @@ -424,11 +436,23 @@ namespace Umbraco.Tests.Integration.Testing break; case UmbracoTestOptions.Database.NewEmptyPerFixture: + // Only attach schema once per fixture + // Doing it more than once will block the process since the old db hasn't been detached + // and it would be the same as NewSchemaPerTest even if it didn't block + if (FirstTestInFixture) + { + // New DB + Schema + var newEmptyFixtureDbId = db.AttachEmpty(); - throw new NotImplementedException(); + // Add teardown callback + OnFixtureTearDown(() => db.Detach(newEmptyFixtureDbId)); + } - //// Add teardown callback - //integrationTest.OnFixtureTearDown(() => db.Detach()); + // We must re-configure our current factory since attaching a new LocalDb from the pool changes connection strings + if (!databaseFactory.Configured) + { + databaseFactory.Configure(db.ConnectionString, Constants.DatabaseProviders.SqlServer); + } break; default: diff --git a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/ScopeFileSystemsTests.cs b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/ScopeFileSystemsTests.cs index 38a3674d21..1c398f681a 100644 --- a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/ScopeFileSystemsTests.cs +++ b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/ScopeFileSystemsTests.cs @@ -14,7 +14,7 @@ using Umbraco.Tests.Testing; using Umbraco.Tests.Integration.Testing; using FileSystems = Umbraco.Core.IO.FileSystems; -namespace Umbraco.Tests.Scoping +namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Scoping { [TestFixture] [UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerTest)] @@ -44,7 +44,7 @@ namespace Umbraco.Tests.Scoping TestHelper.DeleteDirectory(ioHelper.MapPath("FileSysTests")); TestHelper.DeleteDirectory(ioHelper.MapPath(Constants.SystemDirectories.TempData.EnsureEndsWith('/') + "ShadowFs")); } - + [Test] public void test_MediaFileSystem_does_not_write_to_physical_file_system_when_scoped_if_scope_does_not_complete() { @@ -61,7 +61,7 @@ namespace Umbraco.Tests.Scoping mediaFileSystem.AddFile("f1.txt", ms); Assert.IsTrue(mediaFileSystem.FileExists("f1.txt")); Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt")); - + Assert.IsTrue(mediaFileSystem.FileExists("f1.txt")); Assert.IsFalse(physMediaFileSystem.FileExists("f1.txt")); } diff --git a/src/Umbraco.Tests/Scoping/ScopeTests.cs b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/ScopeTests.cs similarity index 93% rename from src/Umbraco.Tests/Scoping/ScopeTests.cs rename to src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/ScopeTests.cs index eb4a01d06b..85331bb2af 100644 --- a/src/Umbraco.Tests/Scoping/ScopeTests.cs +++ b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/ScopeTests.cs @@ -3,20 +3,20 @@ using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Persistence; using Umbraco.Core.Scoping; -using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.Testing; -namespace Umbraco.Tests.Scoping +namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Scoping { [TestFixture] - [UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerTest)] - public class ScopeTests : TestWithDatabaseBase + [UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerFixture)] + public class ScopeTests : UmbracoIntegrationTest { - // setup - public override void SetUp() - { - base.SetUp(); + private new ScopeProvider ScopeProvider => (ScopeProvider) base.ScopeProvider; + [SetUp] + public void SetUp() + { Assert.IsNull(ScopeProvider.AmbientScope); // gone } @@ -25,7 +25,7 @@ namespace Umbraco.Tests.Scoping { var scopeProvider = ScopeProvider; - Assert.IsNull(scopeProvider.AmbientScope); + Assert.IsNull(ScopeProvider.AmbientScope); using (var scope = scopeProvider.CreateScope()) { Assert.IsInstanceOf(scope); @@ -241,32 +241,32 @@ namespace Umbraco.Tests.Scoping using (var scope = scopeProvider.CreateScope()) { - scope.Database.Execute("CREATE TABLE tmp (id INT, name NVARCHAR(64))"); + scope.Database.Execute("CREATE TABLE tmp3 (id INT, name NVARCHAR(64))"); scope.Complete(); } using (var scope = scopeProvider.CreateScope()) { - scope.Database.Execute("INSERT INTO tmp (id, name) VALUES (1, 'a')"); - var n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=1"); + scope.Database.Execute("INSERT INTO tmp3 (id, name) VALUES (1, 'a')"); + var n = scope.Database.ExecuteScalar("SELECT name FROM tmp3 WHERE id=1"); Assert.AreEqual("a", n); } using (var scope = scopeProvider.CreateScope()) { - var n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=1"); + var n = scope.Database.ExecuteScalar("SELECT name FROM tmp3 WHERE id=1"); Assert.IsNull(n); } using (var scope = scopeProvider.CreateScope()) { - scope.Database.Execute("INSERT INTO tmp (id, name) VALUES (1, 'a')"); + scope.Database.Execute("INSERT INTO tmp3 (id, name) VALUES (1, 'a')"); scope.Complete(); } using (var scope = scopeProvider.CreateScope()) { - var n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=1"); + var n = scope.Database.ExecuteScalar("SELECT name FROM tmp3 WHERE id=1"); Assert.AreEqual("a", n); } } @@ -278,24 +278,24 @@ namespace Umbraco.Tests.Scoping using (var scope = scopeProvider.CreateScope()) { - scope.Database.Execute("CREATE TABLE tmp (id INT, name NVARCHAR(64))"); + scope.Database.Execute($"CREATE TABLE tmp1 (id INT, name NVARCHAR(64))"); scope.Complete(); } using (var scope = scopeProvider.CreateScope()) { - scope.Database.Execute("INSERT INTO tmp (id, name) VALUES (1, 'a')"); - var n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=1"); + scope.Database.Execute("INSERT INTO tmp1 (id, name) VALUES (1, 'a')"); + var n = scope.Database.ExecuteScalar("SELECT name FROM tmp1 WHERE id=1"); Assert.AreEqual("a", n); using (var nested = scopeProvider.CreateScope()) { - nested.Database.Execute("INSERT INTO tmp (id, name) VALUES (2, 'b')"); - var nn = nested.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=2"); + nested.Database.Execute("INSERT INTO tmp1 (id, name) VALUES (2, 'b')"); + var nn = nested.Database.ExecuteScalar("SELECT name FROM tmp1 WHERE id=2"); Assert.AreEqual("b", nn); } - n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=2"); + n = scope.Database.ExecuteScalar("SELECT name FROM tmp1 WHERE id=2"); Assert.AreEqual("b", n); scope.Complete(); @@ -303,9 +303,9 @@ namespace Umbraco.Tests.Scoping using (var scope = scopeProvider.CreateScope()) { - var n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=1"); + var n = scope.Database.ExecuteScalar("SELECT name FROM tmp1 WHERE id=1"); Assert.IsNull(n); - n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=2"); + n = scope.Database.ExecuteScalar("SELECT name FROM tmp1 WHERE id=2"); Assert.IsNull(n); } } @@ -317,33 +317,33 @@ namespace Umbraco.Tests.Scoping using (var scope = scopeProvider.CreateScope()) { - scope.Database.Execute("CREATE TABLE tmp (id INT, name NVARCHAR(64))"); + scope.Database.Execute("CREATE TABLE tmp2 (id INT, name NVARCHAR(64))"); scope.Complete(); } using (var scope = scopeProvider.CreateScope()) { - scope.Database.Execute("INSERT INTO tmp (id, name) VALUES (1, 'a')"); - var n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=1"); + scope.Database.Execute("INSERT INTO tmp2 (id, name) VALUES (1, 'a')"); + var n = scope.Database.ExecuteScalar("SELECT name FROM tmp2 WHERE id=1"); Assert.AreEqual("a", n); using (var nested = scopeProvider.CreateScope()) { - nested.Database.Execute("INSERT INTO tmp (id, name) VALUES (2, 'b')"); - var nn = nested.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=2"); + nested.Database.Execute("INSERT INTO tmp2 (id, name) VALUES (2, 'b')"); + var nn = nested.Database.ExecuteScalar("SELECT name FROM tmp2 WHERE id=2"); Assert.AreEqual("b", nn); nested.Complete(); } - n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=2"); + n = scope.Database.ExecuteScalar("SELECT name FROM tmp2 WHERE id=2"); Assert.AreEqual("b", n); } using (var scope = scopeProvider.CreateScope()) { - var n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=1"); + var n = scope.Database.ExecuteScalar("SELECT name FROM tmp2 WHERE id=1"); Assert.IsNull(n); - n = scope.Database.ExecuteScalar("SELECT name FROM tmp WHERE id=2"); + n = scope.Database.ExecuteScalar("SELECT name FROM tmp2 WHERE id=2"); Assert.IsNull(n); } } diff --git a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ThreadSafetyServiceTest.cs b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ThreadSafetyServiceTest.cs index c36968adeb..1d3b799679 100644 --- a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ThreadSafetyServiceTest.cs +++ b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ThreadSafetyServiceTest.cs @@ -44,8 +44,6 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Services CreateTestData(); } - protected override string TestDBConnectionString => base.TestDBConnectionString + "default lock timeout=60000;"; - private const int MaxThreadCount = 20; diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 526e1aa252..7c2214a0ed 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -144,7 +144,6 @@ -