2020-12-23 11:35:49 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
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;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
2020-11-27 19:15:49 +00:00
|
|
|
|
2021-02-11 08:30:27 +01:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Testing
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
|
|
|
|
public class TestDatabaseFactory
|
|
|
|
|
{
|
2020-12-19 11:49:37 +00:00
|
|
|
public static ITestDatabase Create(TestDatabaseSettings settings, string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
string connectionString = Environment.GetEnvironmentVariable("UmbracoIntegrationTestConnectionString");
|
2020-12-16 01:54:49 +00:00
|
|
|
|
|
|
|
|
return string.IsNullOrEmpty(connectionString)
|
2020-12-19 11:49:37 +00:00
|
|
|
? CreateLocalDb(settings, filesPath, loggerFactory, dbFactory)
|
|
|
|
|
: CreateSqlDeveloper(settings, loggerFactory, dbFactory, connectionString);
|
2020-11-27 19:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
private static ITestDatabase CreateLocalDb(TestDatabaseSettings settings, 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-19 11:49:37 +00:00
|
|
|
return new LocalDbTestDatabase(settings, loggerFactory, localDb, filesPath, dbFactory.Create());
|
2020-11-27 19:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
private static ITestDatabase CreateSqlDeveloper(TestDatabaseSettings settings, 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-19 11:49:37 +00:00
|
|
|
return new SqlDeveloperTestDatabase(settings, loggerFactory, dbFactory.Create(), connectionString);
|
2020-11-27 19:15:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|