testing db update

vnext
Paul Schneider 6 years ago
parent cb592c6b5d
commit 27480c0290
8 changed files with 161 additions and 20 deletions

@ -21,6 +21,9 @@ $(BINTARGET): project.lock.json ../Yavsc/bin/$(CONFIGURATION)/dnx451/Yavsc.dll .
breaking: breaking:
dnx test -trait regres=yes dnx test -trait regres=yes
testdev: $(BINTARGET)
ASPNET_ENV=Development dnx test -maxthreads 1 -trait dev=wip
test: $(BINTARGET) test: $(BINTARGET)
ASPNET_ENV=Development dnx test -maxthreads 1 -trait regres=no ASPNET_ENV=Development dnx test -maxthreads 1 -trait regres=no

@ -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.Services;
using Yavsc; using Yavsc;
using Xunit; using Xunit;
using Npgsql;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Storage.Internal;
namespace test namespace test
{ {
@ -59,6 +62,12 @@ namespace test
} }
} }
internal void UpgradeDb()
{
Microsoft.Data.Entity.Commands.Program.Main(
new string [] { "database", "update" });
}
public ILogger Logger public ILogger Logger
{ {
get get
@ -72,20 +81,19 @@ namespace test
} }
} }
public bool DbCreated { get; private set; }
// //
public ServerSideFixture() public ServerSideFixture()
{ {
InitTestHost(); InitTestHost();
Logger = _loggerFactory.CreateLogger<ServerSideFixture> ();
Logger.LogInformation("ServerSideFixture created."); Logger.LogInformation("ServerSideFixture created.");
} }
[Fact] [Fact]
void InitTestHost() void InitTestHost()
{ {
var host = new WebHostBuilder(); var host = new WebHostBuilder();
var hostengnine = host var hostengnine = host
@ -97,11 +105,60 @@ namespace test
App = hostengnine.Start(); App = hostengnine.Start();
_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; SiteSetup = siteSetup.Value;
MailSender = App.Services.GetService(typeof(IEmailSender)) as IEmailSender; 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() public void Dispose()
{ {
Logger.LogInformation("Disposing"); Logger.LogInformation("Disposing");

@ -40,12 +40,12 @@ namespace test
public void GitClone() 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(); var firstProject = dbc.Projects.Include(p=>p.Repository).FirstOrDefault();
Assert.NotNull (firstProject); Assert.NotNull (firstProject);
var clone = new GitClone(_serverFixture._siteSetup.GitRepository); var clone = new GitClone(_serverFixture.SiteSetup.GitRepository);
clone.Launch(firstProject); clone.Launch(firstProject);
} }

@ -0,0 +1,23 @@
namespace test.Settings
{
public abstract class DbConnectionSettings
{
public string Database { get; set; }
public string Server { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string ConnectionString => $"Database={Database};Server={Server};Port={Port};Username={Username};Password={Password};";
public string Password { get; set; }
}
public class DevConnectionSettings : DbConnectionSettings
{
}
public class TestConnectionSettings : DbConnectionSettings
{
}
}

@ -13,22 +13,19 @@ using Yavsc.Services;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Extensions.WebEncoders; using Microsoft.Extensions.WebEncoders;
using Yavsc.Lib; using Yavsc.Lib;
using test.Settings;
namespace test namespace test
{ {
public class Startup public class Startup
{ {
public static string ConnectionString
{
get ; set;
}
public static SiteSettings SiteSetup { get; private set; }
public static SmtpSettings SmtpSettup { get; private set; }
public static IConfiguration Configuration { get; set; } public static IConfiguration Configuration { get; set; }
public static string HostingFullName { get; private set; } public static string HostingFullName { get; private set; }
public static DbConnectionSettings DevDbSettings { get; private set; }
public static DbConnectionSettings TestDbSettings { get; private set; }
ILogger logger; ILogger logger;
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{ {
@ -44,8 +41,6 @@ namespace test
.AddJsonFile("appsettings.json") .AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
Configuration = builder.Build(); Configuration = builder.Build();
ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"];
AppDomain.CurrentDomain.SetData("YAVSC_CONNECTION", ConnectionString);
} }
public void ConfigureServices (IServiceCollection services) public void ConfigureServices (IServiceCollection services)
@ -55,6 +50,12 @@ namespace test
services.Configure<SiteSettings>(siteSettingsconf); services.Configure<SiteSettings>(siteSettingsconf);
var smtpSettingsconf = Configuration.GetSection("Smtp"); var smtpSettingsconf = Configuration.GetSection("Smtp");
services.Configure<SmtpSettings>(smtpSettingsconf); services.Configure<SmtpSettings>(smtpSettingsconf);
var devCx = Configuration.GetSection("Data:DevConnection");
services.Configure<DevConnectionSettings>(devCx);
var testCx = Configuration.GetSection("Data:TestConnection");
services.Configure<TestConnectionSettings>(testCx);
services.AddInstance(typeof(ILoggerFactory), new LoggerFactory()); services.AddInstance(typeof(ILoggerFactory), new LoggerFactory());
services.AddTransient(typeof(IEmailSender), typeof(MailSender)); services.AddTransient(typeof(IEmailSender), typeof(MailSender));
services.AddEntityFramework().AddNpgsql().AddDbContext<ApplicationDbContext>(); services.AddEntityFramework().AddNpgsql().AddDbContext<ApplicationDbContext>();
@ -69,21 +70,29 @@ namespace test
services.AddEntityFramework() services.AddEntityFramework()
.AddNpgsql() .AddNpgsql()
.AddDbContext<ApplicationDbContext>( .AddDbContext<ApplicationDbContext>(
db => db.UseNpgsql(ConnectionString) db => db.UseNpgsql(Startup.TestDbSettings.ConnectionString)
); );
services.AddTransient<Microsoft.Extensions.WebEncoders.UrlEncoder, UrlEncoder>(); services.AddTransient<Microsoft.Extensions.WebEncoders.UrlEncoder, UrlEncoder>();
} }
public void Configure (IApplicationBuilder app, IHostingEnvironment env, public void Configure (IApplicationBuilder app, IHostingEnvironment env,
IOptions<SiteSettings> siteSettings, ILoggerFactory loggerFactory) IOptions<SiteSettings> siteSettings,
IOptions<TestConnectionSettings> testCxOptions,
IOptions<DevConnectionSettings> devCxOptions,
ILoggerFactory loggerFactory)
{ {
loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug(); loggerFactory.AddDebug();
logger = loggerFactory.CreateLogger<Startup>(); logger = loggerFactory.CreateLogger<Startup>();
logger.LogInformation(env.EnvironmentName); logger.LogInformation(env.EnvironmentName);
TestDbSettings = testCxOptions.Value;
DevDbSettings = devCxOptions.Value;
logger.LogInformation($"test db : {TestDbSettings.ConnectionString}");
AppDomain.CurrentDomain.SetData("YAVSC_CONNECTION", TestDbSettings.ConnectionString);
var authConf = Configuration.GetSection("Authentication").GetSection("Yavsc"); var authConf = Configuration.GetSection("Authentication").GetSection("Yavsc");
var clientId = authConf.GetSection("ClientId").Value; var clientId = authConf.GetSection("ClientId").Value;
var clientSecret = authConf.GetSection("ClientSecret").Value; var clientSecret = authConf.GetSection("ClientSecret").Value;

@ -39,8 +39,19 @@
} }
}, },
"Data": { "Data": {
"DefaultConnection": { "DevConnection": {
"ConnectionString": "Server=[NpgsqlHostName];Port=[5432?];Database=[DevDnName];Username=[devUserName];Password=[DevPassword];" "Database":"postgres",
} "Server": "[NpgsqlHostName]",
"Port": 5432,
"Username": "[devUserName]",
"Password": "[DevPassword]"
},
"TestConnection": {
"Database":"[TestDbName]",
"Server": "[TestNpgsqlHostName]",
"Port": 5432,
"Username": "[TestUserName]",
"Password": "[TestPassword]"
}
} }
} }

@ -35,6 +35,9 @@
"defaultNamespace": "test" "defaultNamespace": "test"
}, },
"dependencies": { "dependencies": {
"EntityFramework.Commands": "7.0.0-rc1-final",
"EntityFramework7.Npgsql": "3.1.0-rc1-3",
"EntityFramework7.Npgsql.Design": "3.1.0-rc1-5",
"Newtonsoft.Json": "9.0.1", "Newtonsoft.Json": "9.0.1",
"xunit": "2.1.0", "xunit": "2.1.0",
"xunit.analyzers": "0.9.0", "xunit.analyzers": "0.9.0",

Loading…