Resolve issues with AdvancedMigrationTests
This commit is contained in:
@@ -327,16 +327,5 @@ namespace Umbraco.Core.Persistence
|
||||
//db?.Dispose();
|
||||
Volatile.Write(ref _initialized, false);
|
||||
}
|
||||
|
||||
// during tests, the thread static var can leak between tests
|
||||
// this method provides a way to force-reset the variable
|
||||
internal void ResetForTests()
|
||||
{
|
||||
// TODO: remove all this eventually
|
||||
//var db = _umbracoDatabaseAccessor.UmbracoDatabase;
|
||||
//_umbracoDatabaseAccessor.UmbracoDatabase = null;
|
||||
//db?.Dispose();
|
||||
//_databaseScopeAccessor.Scope = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,11 +132,12 @@ namespace Umbraco.Tests.Integration.TestServerTest
|
||||
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<TestUmbracoDatabaseFactoryProvider>();
|
||||
var typeLoader = services.AddTypeLoader(GetType().Assembly, TestHelper.GetWebHostEnvironment(), TestHelper.GetHostingEnvironment(),
|
||||
TestHelper.ConsoleLoggerFactory, AppCaches.NoCache, Configuration, TestHelper.Profiler);
|
||||
|
||||
var builder = new UmbracoBuilder(services, Configuration, typeLoader);
|
||||
|
||||
|
||||
builder
|
||||
.AddConfiguration()
|
||||
.AddTestCore(TestHelper) // This is the important one!
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace Umbraco.Tests.Integration.Testing
|
||||
private readonly IUmbracoDatabaseFactory _databaseFactory;
|
||||
private readonly IDictionary<int, TestDbMeta> _testDatabases;
|
||||
private UmbracoDatabase.CommandInfo[] _cachedDatabaseInitCommands;
|
||||
|
||||
|
||||
private BlockingCollection<TestDbMeta> _prepareQueue;
|
||||
private BlockingCollection<TestDbMeta> _readySchemaQueue;
|
||||
private BlockingCollection<TestDbMeta> _readyEmptyQueue;
|
||||
@@ -54,7 +54,6 @@ namespace Umbraco.Tests.Integration.Testing
|
||||
new TestDbMeta(2, false, masterConnectionString),
|
||||
|
||||
new TestDbMeta(3, true, masterConnectionString),
|
||||
new TestDbMeta(4, true, masterConnectionString),
|
||||
}.ToDictionary(x => x.Id);
|
||||
|
||||
Instance = this; // For GlobalSetupTeardown.cs
|
||||
|
||||
@@ -7,11 +7,11 @@ namespace Umbraco.Tests.Integration.Testing
|
||||
{
|
||||
public class TestDatabaseFactory
|
||||
{
|
||||
public static ITestDatabase Create(string filesPath, ILoggerFactory loggerFactory, IUmbracoDatabaseFactory dbFactory)
|
||||
public static ITestDatabase Create(string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
|
||||
{
|
||||
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
|
||||
? CreateLocalDb(filesPath, loggerFactory, dbFactory)
|
||||
: CreateSqlDeveloper(loggerFactory, dbFactory);
|
||||
? CreateLocalDb(filesPath, loggerFactory, dbFactory.Create())
|
||||
: CreateSqlDeveloper(loggerFactory, dbFactory.Create());
|
||||
}
|
||||
|
||||
private static ITestDatabase CreateLocalDb(string filesPath, ILoggerFactory loggerFactory, IUmbracoDatabaseFactory dbFactory)
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Umbraco.Core.Configuration.Models;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.Mappers;
|
||||
|
||||
namespace Umbraco.Tests.Integration.Testing
|
||||
{
|
||||
/// <summary>
|
||||
/// I want to be able to create a database for integration testsing without setting the connection string on the
|
||||
/// singleton database factory forever.
|
||||
/// </summary>
|
||||
public class TestUmbracoDatabaseFactoryProvider
|
||||
{
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
private readonly IOptions<GlobalSettings> _globalSettings;
|
||||
private readonly IOptions<ConnectionStrings> _connectionStrings;
|
||||
private readonly Lazy<IMapperCollection> _mappers;
|
||||
private readonly IDbProviderFactoryCreator _dbProviderFactoryCreator;
|
||||
|
||||
public TestUmbracoDatabaseFactoryProvider(
|
||||
ILoggerFactory loggerFactory,
|
||||
IOptions<GlobalSettings> globalSettings,
|
||||
IOptions<ConnectionStrings> connectionStrings,
|
||||
Lazy<IMapperCollection> mappers,
|
||||
IDbProviderFactoryCreator dbProviderFactoryCreator)
|
||||
{
|
||||
_loggerFactory = loggerFactory;
|
||||
_globalSettings = globalSettings;
|
||||
_connectionStrings = connectionStrings;
|
||||
_mappers = mappers;
|
||||
_dbProviderFactoryCreator = dbProviderFactoryCreator;
|
||||
}
|
||||
|
||||
public IUmbracoDatabaseFactory Create()
|
||||
{
|
||||
// ReSharper disable once ArrangeMethodOrOperatorBody
|
||||
return new UmbracoDatabaseFactory(
|
||||
_loggerFactory.CreateLogger<UmbracoDatabaseFactory>(),
|
||||
_loggerFactory,
|
||||
_globalSettings.Value,
|
||||
_connectionStrings.Value,
|
||||
_mappers,
|
||||
_dbProviderFactoryCreator);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,6 +165,7 @@ namespace Umbraco.Tests.Integration.Testing
|
||||
public virtual void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton(TestHelper.DbProviderFactoryCreator);
|
||||
services.AddTransient<TestUmbracoDatabaseFactoryProvider>();
|
||||
var webHostEnvironment = TestHelper.GetWebHostEnvironment();
|
||||
services.AddRequiredNetCoreServices(TestHelper, webHostEnvironment);
|
||||
|
||||
@@ -247,10 +248,11 @@ namespace Umbraco.Tests.Integration.Testing
|
||||
protected void UseTestLocalDb(IServiceProvider serviceProvider)
|
||||
{
|
||||
var state = serviceProvider.GetRequiredService<IRuntimeState>();
|
||||
var testDatabaseFactoryProvider = serviceProvider.GetRequiredService<TestUmbracoDatabaseFactoryProvider>();
|
||||
var databaseFactory = serviceProvider.GetRequiredService<IUmbracoDatabaseFactory>();
|
||||
|
||||
// This will create a db, install the schema and ensure the app is configured to run
|
||||
InstallTestLocalDb(databaseFactory, serviceProvider.GetRequiredService<ILoggerFactory>(), state, TestHelper.WorkingDirectory);
|
||||
InstallTestLocalDb(testDatabaseFactoryProvider, databaseFactory, serviceProvider.GetRequiredService<ILoggerFactory>(), state, TestHelper.WorkingDirectory);
|
||||
TestDBConnectionString = databaseFactory.ConnectionString;
|
||||
InMemoryConfiguration["ConnectionStrings:" + Constants.System.UmbracoConnectionName] = TestDBConnectionString;
|
||||
}
|
||||
@@ -267,7 +269,7 @@ namespace Umbraco.Tests.Integration.Testing
|
||||
/// <remarks>
|
||||
/// There must only be ONE instance shared between all tests in a session
|
||||
/// </remarks>
|
||||
private static ITestDatabase GetOrCreateDatabase(string filesPath, ILoggerFactory loggerFactory, IUmbracoDatabaseFactory dbFactory)
|
||||
private static ITestDatabase GetOrCreateDatabase(string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
|
||||
{
|
||||
lock (_dbLocker)
|
||||
{
|
||||
@@ -282,16 +284,12 @@ namespace Umbraco.Tests.Integration.Testing
|
||||
/// <summary>
|
||||
/// Creates a LocalDb instance to use for the test
|
||||
/// </summary>
|
||||
/// <param name="runtimeState"></param>
|
||||
/// <param name="workingDirectory"></param>
|
||||
/// <param name="connectionString"></param>
|
||||
/// <param name="databaseFactory"></param>
|
||||
/// <param name="loggerFactory"></param>
|
||||
/// <param name="globalSettings"></param>
|
||||
/// <returns></returns>
|
||||
private void InstallTestLocalDb(
|
||||
IUmbracoDatabaseFactory databaseFactory, ILoggerFactory loggerFactory,
|
||||
IRuntimeState runtimeState, string workingDirectory)
|
||||
TestUmbracoDatabaseFactoryProvider testUmbracoDatabaseFactoryProvider,
|
||||
IUmbracoDatabaseFactory databaseFactory,
|
||||
ILoggerFactory loggerFactory,
|
||||
IRuntimeState runtimeState,
|
||||
string workingDirectory)
|
||||
{
|
||||
var dbFilePath = Path.Combine(workingDirectory, "LocalDb");
|
||||
|
||||
@@ -307,7 +305,7 @@ namespace Umbraco.Tests.Integration.Testing
|
||||
if (!Directory.Exists(dbFilePath))
|
||||
Directory.CreateDirectory(dbFilePath);
|
||||
|
||||
var db = GetOrCreateDatabase(dbFilePath, loggerFactory, databaseFactory);
|
||||
var db = GetOrCreateDatabase(dbFilePath, loggerFactory, testUmbracoDatabaseFactoryProvider);
|
||||
|
||||
switch (testOptions.Database)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user