|
|
@ -10,11 +10,15 @@ using Yavsc;
|
|
|
|
using Yavsc.Models;
|
|
|
|
using Yavsc.Models;
|
|
|
|
using Xunit;
|
|
|
|
using Xunit;
|
|
|
|
using Npgsql;
|
|
|
|
using Npgsql;
|
|
|
|
|
|
|
|
using test.Settings;
|
|
|
|
|
|
|
|
using Microsoft.Data.Entity;
|
|
|
|
|
|
|
|
using Microsoft.Data.Entity.Metadata.Conventions;
|
|
|
|
|
|
|
|
|
|
|
|
namespace test
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[Trait("regres", "no")]
|
|
|
|
[Trait("regres", "no")]
|
|
|
|
public class ServerSideFixture : IDisposable {
|
|
|
|
public class ServerSideFixture : IDisposable
|
|
|
|
|
|
|
|
{
|
|
|
|
SiteSettings _siteSetup;
|
|
|
|
SiteSettings _siteSetup;
|
|
|
|
ILogger _logger;
|
|
|
|
ILogger _logger;
|
|
|
|
IApplication _app;
|
|
|
|
IApplication _app;
|
|
|
@ -22,9 +26,9 @@ namespace test
|
|
|
|
ILoggerFactory _loggerFactory;
|
|
|
|
ILoggerFactory _loggerFactory;
|
|
|
|
IEmailSender _mailSender;
|
|
|
|
IEmailSender _mailSender;
|
|
|
|
|
|
|
|
|
|
|
|
public static string ApiKey => "53f4d5da-93a9-4584-82f9-b8fdf243b002" ;
|
|
|
|
public static string ApiKey => "53f4d5da-93a9-4584-82f9-b8fdf243b002";
|
|
|
|
|
|
|
|
|
|
|
|
public ApplicationDbContext DbContext { get; private set; }
|
|
|
|
public ApplicationDbContext DbContext { get; private set; }
|
|
|
|
public SiteSettings SiteSetup
|
|
|
|
public SiteSettings SiteSetup
|
|
|
|
{
|
|
|
|
{
|
|
|
|
get
|
|
|
|
get
|
|
|
@ -38,6 +42,12 @@ namespace test
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// initialized by Init
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <value></value>
|
|
|
|
|
|
|
|
public static object TestingSetup { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public IEmailSender MailSender
|
|
|
|
public IEmailSender MailSender
|
|
|
|
{
|
|
|
|
{
|
|
|
|
get
|
|
|
|
get
|
|
|
@ -64,10 +74,12 @@ namespace test
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal void UpgradeDb()
|
|
|
|
internal void UpgradeDb()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Microsoft.Data.Entity.Commands.Program.Main(
|
|
|
|
Microsoft.Data.Entity.Commands.Program.Main(
|
|
|
|
new string [] { "database", "update" });
|
|
|
|
new string[] { "database", "update" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ILogger Logger
|
|
|
|
public ILogger Logger
|
|
|
@ -82,95 +94,107 @@ namespace test
|
|
|
|
_logger = value;
|
|
|
|
_logger = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public bool DbCreated { get; private set; }
|
|
|
|
bool dbCreated;
|
|
|
|
|
|
|
|
private WebHostBuilder host;
|
|
|
|
|
|
|
|
private IHostingEngine hostengnine;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
public ServerSideFixture()
|
|
|
|
public ServerSideFixture()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
InitTestHost();
|
|
|
|
host = new WebHostBuilder();
|
|
|
|
Logger.LogInformation("ServerSideFixture created.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
hostengnine = host
|
|
|
|
void InitTestHost()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var host = new WebHostBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var hostengnine = host
|
|
|
|
|
|
|
|
.UseEnvironment("Development")
|
|
|
|
.UseEnvironment("Development")
|
|
|
|
.UseServer("test")
|
|
|
|
.UseServer("test")
|
|
|
|
.UseStartup<test.Startup>()
|
|
|
|
.UseStartup<test.Startup>()
|
|
|
|
.Build();
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
|
|
App = hostengnine.Start();
|
|
|
|
App = hostengnine.Start();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// hostengnine.ApplicationServices
|
|
|
|
|
|
|
|
|
|
|
|
_mailer = App.Services.GetService(typeof(EMailer)) as EMailer;
|
|
|
|
_mailer = App.Services.GetService(typeof(EMailer)) as EMailer;
|
|
|
|
_loggerFactory = App.Services.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
|
|
|
|
_loggerFactory = App.Services.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
|
|
|
|
|
|
|
|
|
|
|
|
Logger = _loggerFactory.CreateLogger<ServerSideFixture> ();
|
|
|
|
|
|
|
|
var siteSetup = App.Services.GetService(typeof(IOptions<SiteSettings>)) as IOptions<SiteSettings>;
|
|
|
|
var siteSetup = App.Services.GetService(typeof(IOptions<SiteSettings>)) as IOptions<SiteSettings>;
|
|
|
|
SiteSetup = siteSetup.Value;
|
|
|
|
var testingSetup = App.Services.GetService(typeof(IOptions<Testing>)) as IOptions<Testing>;
|
|
|
|
MailSender = App.Services.GetService(typeof(IEmailSender)) as IEmailSender;
|
|
|
|
MailSender = App.Services.GetService(typeof(IEmailSender)) as IEmailSender;
|
|
|
|
|
|
|
|
DbContext = App.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SiteSetup = siteSetup.Value;
|
|
|
|
|
|
|
|
TestingSetup = testingSetup.Value;
|
|
|
|
|
|
|
|
|
|
|
|
var builder = new DbConnectionStringBuilder();
|
|
|
|
|
|
|
|
builder.ConnectionString = Startup.DbSettings.Testing;
|
|
|
|
|
|
|
|
Logger.LogInformation("testing database:" +builder["Database"]);
|
|
|
|
|
|
|
|
TestingDatabase = (string) builder["Database"];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CheckDbExistence();
|
|
|
|
Logger = _loggerFactory.CreateLogger<ServerSideFixture>();
|
|
|
|
if (!DbCreated)
|
|
|
|
|
|
|
|
CreateTestDb();
|
|
|
|
|
|
|
|
DbContext = App.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var builder = new DbConnectionStringBuilder
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ConnectionString = Startup.Testing.ConnectionStrings.Default
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
ConventionSet conventions = new ConventionSet();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modelBuilder = new ModelBuilder(conventions);
|
|
|
|
|
|
|
|
ApplicationDbContext context = new ApplicationDbContext();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TestingDatabase = (string)builder["Database"];
|
|
|
|
|
|
|
|
Logger.LogInformation("ServerSideFixture created.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void InitTestHost()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EnsureTestDb();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ModelBuilder modelBuilder;
|
|
|
|
|
|
|
|
|
|
|
|
public string TestingDatabase { get; private set; }
|
|
|
|
public string TestingDatabase { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public void CheckDbExistence()
|
|
|
|
public void CheckDbExistence()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
using (
|
|
|
|
using (
|
|
|
|
NpgsqlConnection cx = new NpgsqlConnection(Startup.DbSettings.DatabaseCtor))
|
|
|
|
NpgsqlConnection cx = new NpgsqlConnection(Startup.Testing.ConnectionStrings.DatabaseCtor))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cx.Open();
|
|
|
|
cx.Open();
|
|
|
|
var command = cx.CreateCommand();
|
|
|
|
var command = cx.CreateCommand();
|
|
|
|
command.CommandText = $"SELECT 1 FROM pg_database WHERE datname='{TestingDatabase}';";
|
|
|
|
command.CommandText = $"SELECT 1 FROM pg_database WHERE datname='{TestingDatabase}';";
|
|
|
|
DbCreated = (command.ExecuteScalar()!=null);
|
|
|
|
dbCreated = (command.ExecuteScalar()!=null);
|
|
|
|
|
|
|
|
_logger.LogInformation($"DbCreated:{dbCreated}");
|
|
|
|
_logger.LogInformation($"DbCreated:{DbCreated}");
|
|
|
|
|
|
|
|
cx.Close();
|
|
|
|
cx.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void CreateTestDb()
|
|
|
|
|
|
|
|
|
|
|
|
public bool EnsureTestDb()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!DbCreated)
|
|
|
|
CheckDbExistence();
|
|
|
|
using (
|
|
|
|
if (!dbCreated)
|
|
|
|
NpgsqlConnection cx = new NpgsqlConnection(Startup.DbSettings.DatabaseCtor))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cx.Open();
|
|
|
|
using (NpgsqlConnection cx = new NpgsqlConnection(Startup.Testing.ConnectionStrings.DatabaseCtor))
|
|
|
|
var command = cx.CreateCommand();
|
|
|
|
{
|
|
|
|
command.CommandText = $"create database \"{TestingDatabase}\";";
|
|
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
cx.Open();
|
|
|
|
|
|
|
|
var command = cx.CreateCommand();
|
|
|
|
_logger.LogInformation($"database created.");
|
|
|
|
using (NpgsqlConnection ownercx = new NpgsqlConnection(Startup.Testing.ConnectionStrings.Default))
|
|
|
|
cx.Close();
|
|
|
|
command.CommandText = $"create database '{TestingDatabase}' OWNER = '{ownercx.UserName}';";
|
|
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dbCreated = DbContext.Database.EnsureCreated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DbCreated=true;
|
|
|
|
return dbCreated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DropTestDb()
|
|
|
|
public void DropTestDb()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (DbCreated)
|
|
|
|
if (dbCreated)
|
|
|
|
using (
|
|
|
|
DbContext.Database.EnsureDeleted();
|
|
|
|
NpgsqlConnection cx = new NpgsqlConnection(Startup.DbSettings.DatabaseCtor))
|
|
|
|
dbCreated = false;
|
|
|
|
{
|
|
|
|
|
|
|
|
cx.Open();
|
|
|
|
|
|
|
|
var command = cx.CreateCommand();
|
|
|
|
|
|
|
|
command.CommandText = $"drop database \"{TestingDatabase}\"";
|
|
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
|
|
_logger.LogInformation($"database dropped");
|
|
|
|
|
|
|
|
cx.Close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
DbCreated=false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
public void Dispose()
|
|
|
|