2020-12-12 11:33:57 +00:00
|
|
|
using System;
|
2020-12-16 01:54:49 +00:00
|
|
|
using System.IO;
|
2020-11-27 19:15:49 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Integration.Testing
|
|
|
|
|
{
|
|
|
|
|
public class TestDatabaseFactory
|
|
|
|
|
{
|
2020-12-11 18:20:07 +00:00
|
|
|
public static ITestDatabase Create(string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
2020-12-16 01:54:49 +00:00
|
|
|
var connectionString = Environment.GetEnvironmentVariable("UmbracoIntegrationTestConnectionString");
|
|
|
|
|
|
|
|
|
|
return string.IsNullOrEmpty(connectionString)
|
|
|
|
|
? CreateLocalDb(filesPath, loggerFactory, dbFactory)
|
|
|
|
|
: CreateSqlDeveloper(loggerFactory, dbFactory, connectionString);
|
2020-11-27 19:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-16 01:54:49 +00:00
|
|
|
private static ITestDatabase CreateLocalDb(string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
2020-12-16 01:54:49 +00:00
|
|
|
if (!Directory.Exists(filesPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(filesPath);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 19:15:49 +00:00
|
|
|
var localDb = new LocalDb();
|
|
|
|
|
|
|
|
|
|
if (!localDb.IsAvailable)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("LocalDB is not available.");
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 01:54:49 +00:00
|
|
|
return new LocalDbTestDatabase(loggerFactory, localDb, filesPath, dbFactory.Create());
|
2020-11-27 19:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-16 01:54:49 +00:00
|
|
|
private static ITestDatabase CreateSqlDeveloper(ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory, string connectionString)
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
2020-12-16 01:54:49 +00:00
|
|
|
// NOTE: Example setup for Linux box.
|
2020-11-27 19:15:49 +00:00
|
|
|
// $ 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
|
|
|
|
|
if (string.IsNullOrEmpty(connectionString))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("ENV: UmbracoIntegrationTestConnectionString is not set");
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 01:54:49 +00:00
|
|
|
return new SqlDeveloperTestDatabase(loggerFactory, dbFactory.Create(), connectionString);
|
2020-11-27 19:15:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|