yavsc/test/yavscTests/Mandatory/ServerSideFixture.cs

240 lines
6.8 KiB
C#
Raw Normal View History

using System;
2019-01-05 13:28:17 +00:00
using System.Data.Common;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Hosting.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Yavsc.Lib;
using Yavsc.Services;
2018-07-20 02:27:53 +02:00
using Yavsc;
2019-01-05 13:28:17 +00:00
using Yavsc.Models;
2018-08-01 06:12:42 +02:00
using Xunit;
2018-09-04 16:40:02 +02:00
using Npgsql;
2021-01-02 00:41:54 +00:00
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata.Conventions;
2021-06-06 20:32:39 +01:00
using yavscTests.Settings;
2021-06-06 20:32:39 +01:00
namespace yavscTests
{
2021-04-05 00:11:54 +01:00
[Trait("regression", "II")]
2021-01-02 00:41:54 +00:00
public class ServerSideFixture : IDisposable
{
2018-09-03 13:56:35 +02:00
SiteSettings _siteSetup;
ILogger _logger;
IApplication _app;
2021-03-10 01:41:31 +00:00
readonly EMailer _mailer;
readonly ILoggerFactory _loggerFactory;
2018-09-03 13:56:35 +02:00
IEmailSender _mailSender;
2021-06-06 20:32:39 +01:00
public string ApiKey { get; private set; }
2021-01-02 00:41:54 +00:00
public ApplicationDbContext DbContext { get; private set; }
2018-09-03 13:56:35 +02:00
public SiteSettings SiteSetup
{
get
{
return _siteSetup;
}
set
{
_siteSetup = value;
}
}
2021-01-02 00:41:54 +00:00
/// <summary>
/// initialized by Init
/// </summary>
/// <value></value>
2021-06-06 20:32:39 +01:00
public TestingSetup TestingSetup { get; private set; }
2021-01-02 00:41:54 +00:00
2018-09-03 13:56:35 +02:00
public IEmailSender MailSender
{
get
{
return _mailSender;
}
set
{
_mailSender = value;
}
}
public IApplication App
{
get
{
return _app;
}
set
{
_app = value;
}
}
2021-01-02 00:41:54 +00:00
2021-06-06 20:32:39 +01:00
internal int UpgradeDb()
2018-09-04 16:40:02 +02:00
{
2021-06-06 20:32:39 +01:00
return Microsoft.Data.Entity.Commands.Program.Main(
2021-01-02 00:41:54 +00:00
new string[] { "database", "update" });
2018-09-04 16:40:02 +02:00
}
2018-09-03 13:56:35 +02:00
public ILogger Logger
{
get
{
return _logger;
}
set
{
_logger = value;
}
}
2021-01-02 00:41:54 +00:00
bool dbCreated;
2021-06-06 20:32:39 +01:00
public WebHostBuilder Host { get; private set; }
2021-03-10 01:41:31 +00:00
private readonly IHostingEngine hostengnine;
2021-01-02 00:41:54 +00:00
2018-09-03 13:56:35 +02:00
2021-04-05 00:11:54 +01:00
void AssertNotNull(object obj, string msg)
{
if (obj == null)
throw new Exception(msg);
}
2018-08-03 02:59:37 +02:00
//
public ServerSideFixture()
{
2021-06-06 20:32:39 +01:00
Host = new WebHostBuilder();
AssertNotNull(Host, nameof(Host));
2018-07-20 02:27:53 +02:00
2021-06-06 20:32:39 +01:00
hostengnine = Host
.UseEnvironment("Testing")
.UseServer("yavscTests")
.UseStartup<Startup>()
.Build();
2021-04-05 00:11:54 +01:00
AssertNotNull(hostengnine, nameof(hostengnine));
2018-09-03 13:56:35 +02:00
App = hostengnine.Start();
2021-04-05 00:11:54 +01:00
AssertNotNull(App, nameof(App));
2021-01-02 00:41:54 +00:00
// hostengnine.ApplicationServices
2018-09-03 13:56:35 +02:00
_mailer = App.Services.GetService(typeof(EMailer)) as EMailer;
2021-04-05 00:11:54 +01:00
AssertNotNull(_mailer, nameof(_mailer));
MailSender = App.Services.GetService(typeof(IEmailSender)) as IEmailSender;
AssertNotNull(MailSender, nameof(MailSender));
2018-09-03 13:56:35 +02:00
_loggerFactory = App.Services.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
2021-04-05 00:11:54 +01:00
AssertNotNull(_loggerFactory, nameof(_loggerFactory));
2021-06-06 20:32:39 +01:00
2018-09-03 13:56:35 +02:00
var siteSetup = App.Services.GetService(typeof(IOptions<SiteSettings>)) as IOptions<SiteSettings>;
2021-04-05 00:11:54 +01:00
AssertNotNull(siteSetup, nameof(siteSetup));
2021-06-06 20:32:39 +01:00
var testingSetup = App.Services.GetService(typeof(IOptions<TestingSetup>)) as IOptions<TestingSetup>;
AssertNotNull(testingSetup, nameof(testingSetup));
2021-01-02 00:41:54 +00:00
DbContext = App.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
2019-01-05 13:28:17 +00:00
2021-01-02 00:41:54 +00:00
SiteSetup = siteSetup.Value;
2021-06-06 20:32:39 +01:00
AssertNotNull(SiteSetup, nameof(SiteSetup));
2019-01-05 13:28:17 +00:00
2021-06-06 20:32:39 +01:00
TestingSetup = testingSetup.Value;
AssertNotNull(TestingSetup, nameof(TestingSetup));
2019-01-05 13:28:17 +00:00
2021-01-02 00:41:54 +00:00
Logger = _loggerFactory.CreateLogger<ServerSideFixture>();
2021-06-06 20:32:39 +01:00
AssertNotNull(Logger, nameof(Logger));
2021-01-02 00:41:54 +00:00
var builder = new DbConnectionStringBuilder
{
2021-06-06 20:32:39 +01:00
ConnectionString = Startup.TestingSetup.ConnectionStrings.Default
2021-01-02 00:41:54 +00:00
};
ConventionSet conventions = new ConventionSet();
modelBuilder = new ModelBuilder(conventions);
ApplicationDbContext context = new ApplicationDbContext();
TestingDatabase = (string)builder["Database"];
2021-06-06 20:32:39 +01:00
AssertNotNull(TestingDatabase, nameof(TestingDatabase));
2021-01-02 03:12:43 +00:00
2021-01-02 00:41:54 +00:00
Logger.LogInformation("ServerSideFixture created.");
}
2021-01-02 00:41:54 +00:00
2021-03-10 01:41:31 +00:00
private readonly ModelBuilder modelBuilder;
2021-01-02 00:41:54 +00:00
2019-01-05 13:28:17 +00:00
public string TestingDatabase { get; private set; }
public void CheckDbExistence()
2018-09-04 16:40:02 +02:00
{
using (
2021-06-06 20:32:39 +01:00
NpgsqlConnection cx = new NpgsqlConnection(Startup.TestingSetup.ConnectionStrings.Default))
2018-09-04 16:40:02 +02:00
{
cx.Open();
2021-01-02 03:20:49 +00:00
_logger.LogInformation($"check db for TestingDatabase:{TestingDatabase}");
2018-09-04 16:40:02 +02:00
var command = cx.CreateCommand();
2019-01-05 13:28:17 +00:00
command.CommandText = $"SELECT 1 FROM pg_database WHERE datname='{TestingDatabase}';";
2021-01-02 00:41:54 +00:00
dbCreated = (command.ExecuteScalar()!=null);
_logger.LogInformation($"DbCreated:{dbCreated}");
2018-09-04 16:40:02 +02:00
cx.Close();
}
}
2021-01-02 00:41:54 +00:00
public bool EnsureTestDb()
2018-09-04 16:40:02 +02:00
{
2021-01-02 03:20:49 +00:00
if (!DbCreated)
2018-09-04 16:40:02 +02:00
{
2021-06-06 20:32:39 +01:00
using (NpgsqlConnection cx =
new NpgsqlConnection(Startup.TestingSetup.ConnectionStrings.DatabaseCtor))
2021-01-02 00:41:54 +00:00
{
2021-01-02 03:20:49 +00:00
_logger.LogInformation($"create database for TestingDatabase : {TestingDatabase}");
2021-01-02 00:41:54 +00:00
cx.Open();
var command = cx.CreateCommand();
2021-06-06 20:32:39 +01:00
using (NpgsqlConnection ownercx = new NpgsqlConnection(Startup.TestingSetup.ConnectionStrings.Default))
2021-01-02 03:20:49 +00:00
command.CommandText = $"create database \"{TestingDatabase}\" OWNER \"{ownercx.UserName}\";";
2021-06-06 20:32:39 +01:00
2021-01-02 03:20:49 +00:00
_logger.LogInformation(command.CommandText);
2021-01-02 00:41:54 +00:00
command.ExecuteNonQuery();
2021-06-06 20:32:39 +01:00
cx.Close();
2021-01-02 00:41:54 +00:00
}
2021-01-02 03:20:49 +00:00
dbCreated = true;
2018-09-04 16:40:02 +02:00
}
2021-01-02 00:41:54 +00:00
return dbCreated;
2018-09-04 16:40:02 +02:00
}
2021-01-02 00:41:54 +00:00
2018-09-04 16:40:02 +02:00
public void DropTestDb()
{
2021-01-02 00:41:54 +00:00
if (dbCreated)
DbContext.Database.EnsureDeleted();
dbCreated = false;
}
public void Dispose()
{
2018-09-03 13:56:35 +02:00
Logger.LogInformation("Disposing");
}
2021-01-02 03:12:43 +00:00
2021-01-02 03:20:49 +00:00
public bool DbCreated { get {
2021-06-06 20:32:39 +01:00
try {
2021-01-02 03:20:49 +00:00
CheckDbExistence();
2021-06-06 20:32:39 +01:00
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
_logger.LogError(ex.StackTrace);
}
2021-01-02 03:20:49 +00:00
return dbCreated; } }
}
2018-07-20 02:27:53 +02:00
}