Files
Umbraco-CMS/src/Umbraco.Tests.Integration/Testing/TestDatabaseFactory.cs

45 lines
1.8 KiB
C#
Raw Normal View History

using System;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using Umbraco.Core.Persistence;
namespace Umbraco.Tests.Integration.Testing
{
public class TestDatabaseFactory
{
public static ITestDatabase Create(string filesPath, ILoggerFactory loggerFactory, IUmbracoDatabaseFactory dbFactory)
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? CreateLocalDb(filesPath, loggerFactory, dbFactory)
: CreateSqlDeveloper(loggerFactory, dbFactory);
}
private static ITestDatabase CreateLocalDb(string filesPath, ILoggerFactory loggerFactory, IUmbracoDatabaseFactory dbFactory)
{
var localDb = new LocalDb();
if (!localDb.IsAvailable)
{
throw new InvalidOperationException("LocalDB is not available.");
}
return new LocalDbTestDatabase(loggerFactory, localDb, filesPath, dbFactory);
}
private static ITestDatabase CreateSqlDeveloper(ILoggerFactory loggerFactory, IUmbracoDatabaseFactory dbFactory)
{
// $ export SA_PASSWORD=Foobar123!
// $ export UmbracoIntegrationTestConnectionString="Server=localhost,1433;User Id=sa;Password=$SA_PASSWORD;"
// $ docker run -e 'ACCEPT_EULA=Y' -e "SA_PASSWORD=$SA_PASSWORD" -e 'MSSQL_PID=Developer' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest-ubuntu
var connectionString = Environment.GetEnvironmentVariable("UmbracoIntegrationTestConnectionString");
if (string.IsNullOrEmpty(connectionString))
{
throw new InvalidOperationException("ENV: UmbracoIntegrationTestConnectionString is not set");
}
return new SqlDeveloperTestDatabase(loggerFactory, dbFactory, connectionString);
}
}
}