222 lines
6.2 KiB
C#
222 lines
6.2 KiB
C#
6 years ago
|
using System;
|
||
6 years ago
|
using System.Data.Common;
|
||
6 years ago
|
using Microsoft.AspNet.Hosting;
|
||
|
using Microsoft.AspNet.Hosting.Internal;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
using Microsoft.Extensions.OptionsModel;
|
||
|
using Yavsc.Lib;
|
||
|
using Yavsc.Services;
|
||
6 years ago
|
using Yavsc;
|
||
6 years ago
|
using Yavsc.Models;
|
||
6 years ago
|
using Xunit;
|
||
6 years ago
|
using Npgsql;
|
||
4 years ago
|
using test.Settings;
|
||
|
using Microsoft.Data.Entity;
|
||
|
using Microsoft.Data.Entity.Metadata.Conventions;
|
||
6 years ago
|
|
||
6 years ago
|
namespace test
|
||
6 years ago
|
{
|
||
4 years ago
|
[Trait("regression", "II")]
|
||
4 years ago
|
public class ServerSideFixture : IDisposable
|
||
|
{
|
||
6 years ago
|
SiteSettings _siteSetup;
|
||
|
ILogger _logger;
|
||
|
IApplication _app;
|
||
4 years ago
|
readonly EMailer _mailer;
|
||
|
readonly ILoggerFactory _loggerFactory;
|
||
6 years ago
|
IEmailSender _mailSender;
|
||
|
|
||
4 years ago
|
public static string ApiKey => "53f4d5da-93a9-4584-82f9-b8fdf243b002";
|
||
|
|
||
|
public ApplicationDbContext DbContext { get; private set; }
|
||
6 years ago
|
public SiteSettings SiteSetup
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return _siteSetup;
|
||
|
}
|
||
|
|
||
|
set
|
||
|
{
|
||
|
_siteSetup = value;
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
/// <summary>
|
||
|
/// initialized by Init
|
||
|
/// </summary>
|
||
|
/// <value></value>
|
||
|
public static object TestingSetup { get; private set; }
|
||
|
|
||
6 years ago
|
public IEmailSender MailSender
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return _mailSender;
|
||
|
}
|
||
|
|
||
|
set
|
||
|
{
|
||
|
_mailSender = value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public IApplication App
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return _app;
|
||
|
}
|
||
|
|
||
|
set
|
||
|
{
|
||
|
_app = value;
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
|
||
|
|
||
6 years ago
|
internal void UpgradeDb()
|
||
|
{
|
||
|
Microsoft.Data.Entity.Commands.Program.Main(
|
||
4 years ago
|
new string[] { "database", "update" });
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
public ILogger Logger
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return _logger;
|
||
|
}
|
||
|
|
||
|
set
|
||
|
{
|
||
|
_logger = value;
|
||
|
}
|
||
|
}
|
||
4 years ago
|
bool dbCreated;
|
||
4 years ago
|
private readonly WebHostBuilder host;
|
||
|
private readonly IHostingEngine hostengnine;
|
||
4 years ago
|
|
||
6 years ago
|
|
||
4 years ago
|
void AssertNotNull(object obj, string msg)
|
||
|
{
|
||
|
if (obj == null)
|
||
|
throw new Exception(msg);
|
||
|
}
|
||
|
|
||
6 years ago
|
//
|
||
6 years ago
|
public ServerSideFixture()
|
||
|
{
|
||
4 years ago
|
host = new WebHostBuilder();
|
||
4 years ago
|
AssertNotNull(host, nameof(host));
|
||
6 years ago
|
|
||
4 years ago
|
hostengnine = host
|
||
6 years ago
|
.UseEnvironment("Development")
|
||
4 years ago
|
.UseServer("test")
|
||
6 years ago
|
.UseStartup<test.Startup>()
|
||
6 years ago
|
.Build();
|
||
4 years ago
|
|
||
|
AssertNotNull(hostengnine, nameof(hostengnine));
|
||
6 years ago
|
|
||
6 years ago
|
App = hostengnine.Start();
|
||
4 years ago
|
|
||
|
AssertNotNull(App, nameof(App));
|
||
4 years ago
|
|
||
|
// hostengnine.ApplicationServices
|
||
|
|
||
6 years ago
|
_mailer = App.Services.GetService(typeof(EMailer)) as EMailer;
|
||
4 years ago
|
AssertNotNull(_mailer, nameof(_mailer));
|
||
|
MailSender = App.Services.GetService(typeof(IEmailSender)) as IEmailSender;
|
||
|
AssertNotNull(MailSender, nameof(MailSender));
|
||
|
|
||
6 years ago
|
_loggerFactory = App.Services.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
|
||
4 years ago
|
AssertNotNull(_loggerFactory, nameof(_loggerFactory));
|
||
6 years ago
|
var siteSetup = App.Services.GetService(typeof(IOptions<SiteSettings>)) as IOptions<SiteSettings>;
|
||
4 years ago
|
AssertNotNull(siteSetup, nameof(siteSetup));
|
||
4 years ago
|
var testingSetup = App.Services.GetService(typeof(IOptions<Testing>)) as IOptions<Testing>;
|
||
|
DbContext = App.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
|
||
6 years ago
|
|
||
4 years ago
|
SiteSetup = siteSetup.Value;
|
||
|
TestingSetup = testingSetup.Value;
|
||
6 years ago
|
|
||
|
|
||
4 years ago
|
Logger = _loggerFactory.CreateLogger<ServerSideFixture>();
|
||
|
|
||
|
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"];
|
||
4 years ago
|
|
||
4 years ago
|
Logger.LogInformation("ServerSideFixture created.");
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
|
||
4 years ago
|
private readonly ModelBuilder modelBuilder;
|
||
4 years ago
|
|
||
6 years ago
|
public string TestingDatabase { get; private set; }
|
||
|
|
||
|
public void CheckDbExistence()
|
||
6 years ago
|
{
|
||
|
using (
|
||
4 years ago
|
NpgsqlConnection cx = new NpgsqlConnection(Startup.Testing.ConnectionStrings.DatabaseCtor))
|
||
6 years ago
|
{
|
||
|
cx.Open();
|
||
4 years ago
|
_logger.LogInformation($"check db for TestingDatabase:{TestingDatabase}");
|
||
6 years ago
|
var command = cx.CreateCommand();
|
||
6 years ago
|
command.CommandText = $"SELECT 1 FROM pg_database WHERE datname='{TestingDatabase}';";
|
||
4 years ago
|
dbCreated = (command.ExecuteScalar()!=null);
|
||
|
_logger.LogInformation($"DbCreated:{dbCreated}");
|
||
6 years ago
|
cx.Close();
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
public bool EnsureTestDb()
|
||
6 years ago
|
{
|
||
4 years ago
|
if (!DbCreated)
|
||
6 years ago
|
{
|
||
4 years ago
|
using (NpgsqlConnection cx = new NpgsqlConnection(Startup.Testing.ConnectionStrings.DatabaseCtor))
|
||
|
{
|
||
4 years ago
|
_logger.LogInformation($"create database for TestingDatabase : {TestingDatabase}");
|
||
4 years ago
|
|
||
|
cx.Open();
|
||
|
var command = cx.CreateCommand();
|
||
|
using (NpgsqlConnection ownercx = new NpgsqlConnection(Startup.Testing.ConnectionStrings.Default))
|
||
4 years ago
|
command.CommandText = $"create database \"{TestingDatabase}\" OWNER \"{ownercx.UserName}\";";
|
||
|
_logger.LogInformation(command.CommandText);
|
||
4 years ago
|
command.ExecuteNonQuery();
|
||
|
}
|
||
4 years ago
|
dbCreated = true;
|
||
|
|
||
6 years ago
|
}
|
||
4 years ago
|
return dbCreated;
|
||
6 years ago
|
}
|
||
4 years ago
|
|
||
6 years ago
|
public void DropTestDb()
|
||
|
{
|
||
4 years ago
|
if (dbCreated)
|
||
|
DbContext.Database.EnsureDeleted();
|
||
|
dbCreated = false;
|
||
|
}
|
||
|
|
||
6 years ago
|
public void Dispose()
|
||
|
{
|
||
6 years ago
|
Logger.LogInformation("Disposing");
|
||
6 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
public bool DbCreated { get {
|
||
|
CheckDbExistence();
|
||
|
return dbCreated; } }
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
|
||
|
|