testing db update
This commit is contained in:
parent
8758f45c1f
commit
0be787c6fa
8 changed files with 161 additions and 20 deletions
35
test/Mandatory/Database.cs
Normal file
35
test/Mandatory/Database.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace test.Mandatory
|
||||
{
|
||||
[Collection("EMaillingTeststCollection")]
|
||||
[Trait("regres", "no")]
|
||||
[Trait("dev", "wip")]
|
||||
public class Database: IClassFixture<ServerSideFixture>
|
||||
{
|
||||
ServerSideFixture _serverFixture;
|
||||
ITestOutputHelper output;
|
||||
public Database(ServerSideFixture serverFixture, ITestOutputHelper output)
|
||||
{
|
||||
this.output = output;
|
||||
_serverFixture = serverFixture;
|
||||
|
||||
output.WriteLine($"Startup.TestDbSettings.Database was {Startup.TestDbSettings.Database}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Assuming we're using an account that may create databases,
|
||||
/// Install all our migrations in a fresh new database.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void InstallFromScratchUsingPoweredNpgsqlUser()
|
||||
{
|
||||
if (_serverFixture.DbCreated)
|
||||
_serverFixture.DropTestDb();
|
||||
|
||||
_serverFixture.CreateTestDb();
|
||||
_serverFixture.UpgradeDb();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,9 @@ using Yavsc.Lib;
|
|||
using Yavsc.Services;
|
||||
using Yavsc;
|
||||
using Xunit;
|
||||
using Npgsql;
|
||||
using Microsoft.Data.Entity.Migrations;
|
||||
using Microsoft.Data.Entity.Storage.Internal;
|
||||
|
||||
namespace test
|
||||
{
|
||||
|
|
@ -59,6 +62,12 @@ namespace test
|
|||
}
|
||||
}
|
||||
|
||||
internal void UpgradeDb()
|
||||
{
|
||||
Microsoft.Data.Entity.Commands.Program.Main(
|
||||
new string [] { "database", "update" });
|
||||
}
|
||||
|
||||
public ILogger Logger
|
||||
{
|
||||
get
|
||||
|
|
@ -72,20 +81,19 @@ namespace test
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public bool DbCreated { get; private set; }
|
||||
|
||||
//
|
||||
public ServerSideFixture()
|
||||
{
|
||||
InitTestHost();
|
||||
Logger = _loggerFactory.CreateLogger<ServerSideFixture> ();
|
||||
Logger.LogInformation("ServerSideFixture created.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
void InitTestHost()
|
||||
{
|
||||
|
||||
var host = new WebHostBuilder();
|
||||
|
||||
var hostengnine = host
|
||||
|
|
@ -97,11 +105,60 @@ namespace test
|
|||
App = hostengnine.Start();
|
||||
_mailer = App.Services.GetService(typeof(EMailer)) as EMailer;
|
||||
_loggerFactory = App.Services.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
|
||||
|
||||
Logger = _loggerFactory.CreateLogger<ServerSideFixture> ();
|
||||
var siteSetup = App.Services.GetService(typeof(IOptions<SiteSettings>)) as IOptions<SiteSettings>;
|
||||
SiteSetup = siteSetup.Value;
|
||||
MailSender = App.Services.GetService(typeof(IEmailSender)) as IEmailSender;
|
||||
CheckDbExistence();
|
||||
if (!DbCreated)
|
||||
CreateTestDb();
|
||||
}
|
||||
|
||||
public void CreateTestDb()
|
||||
{
|
||||
if (!DbCreated)
|
||||
using (
|
||||
NpgsqlConnection cx = new NpgsqlConnection(Startup.DevDbSettings.ConnectionString))
|
||||
{
|
||||
cx.Open();
|
||||
var command = cx.CreateCommand();
|
||||
command.CommandText = $"create database \"{Startup.TestDbSettings.Database}\";";
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
_logger.LogInformation($"database created.");
|
||||
cx.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void CheckDbExistence()
|
||||
{
|
||||
using (
|
||||
NpgsqlConnection cx = new NpgsqlConnection(Startup.DevDbSettings.ConnectionString))
|
||||
{
|
||||
cx.Open();
|
||||
var command = cx.CreateCommand();
|
||||
command.CommandText = $"SELECT 1 FROM pg_database WHERE datname='{Startup.TestDbSettings.Database}';";
|
||||
DbCreated = (command.ExecuteScalar()!=null);
|
||||
|
||||
_logger.LogInformation($"DbCreated:{DbCreated}");
|
||||
cx.Close();
|
||||
}
|
||||
}
|
||||
public void DropTestDb()
|
||||
{
|
||||
if (DbCreated)
|
||||
using (
|
||||
NpgsqlConnection cx = new NpgsqlConnection(Startup.DevDbSettings.ConnectionString))
|
||||
{
|
||||
cx.Open();
|
||||
var command = cx.CreateCommand();
|
||||
command.CommandText = $"drop database \"{Startup.TestDbSettings.Database}\";";
|
||||
command.ExecuteNonQuery();
|
||||
_logger.LogInformation($"database dropped");
|
||||
cx.Close();
|
||||
}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Logger.LogInformation("Disposing");
|
||||
|
|
|
|||
|
|
@ -40,12 +40,12 @@ namespace test
|
|||
public void GitClone()
|
||||
{
|
||||
|
||||
var dbc = _serverFixture._app.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
|
||||
var dbc = _serverFixture.App.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
|
||||
|
||||
var firstProject = dbc.Projects.Include(p=>p.Repository).FirstOrDefault();
|
||||
Assert.NotNull (firstProject);
|
||||
|
||||
var clone = new GitClone(_serverFixture._siteSetup.GitRepository);
|
||||
var clone = new GitClone(_serverFixture.SiteSetup.GitRepository);
|
||||
clone.Launch(firstProject);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue