From 27480c0290ce1232e1883bb1d7b4cfcdf3e008a4 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Tue, 4 Sep 2018 16:40:02 +0200 Subject: [PATCH] testing db update --- test/Makefile | 3 ++ test/Mandatory/Database.cs | 35 +++++++++++++++ test/Mandatory/ServerSideFixture.cs | 63 +++++++++++++++++++++++++-- test/Mandatory/YavscMandatory.cs | 4 +- test/Settings/DbConnectionSettings.cs | 23 ++++++++++ test/Startup.cs | 33 +++++++++----- test/appsettings.json | 17 ++++++-- test/project.json | 3 ++ 8 files changed, 161 insertions(+), 20 deletions(-) create mode 100644 test/Mandatory/Database.cs create mode 100644 test/Settings/DbConnectionSettings.cs diff --git a/test/Makefile b/test/Makefile index d322e43a..ddc77835 100644 --- a/test/Makefile +++ b/test/Makefile @@ -21,6 +21,9 @@ $(BINTARGET): project.lock.json ../Yavsc/bin/$(CONFIGURATION)/dnx451/Yavsc.dll . breaking: dnx test -trait regres=yes +testdev: $(BINTARGET) + ASPNET_ENV=Development dnx test -maxthreads 1 -trait dev=wip + test: $(BINTARGET) ASPNET_ENV=Development dnx test -maxthreads 1 -trait regres=no diff --git a/test/Mandatory/Database.cs b/test/Mandatory/Database.cs new file mode 100644 index 00000000..05787024 --- /dev/null +++ b/test/Mandatory/Database.cs @@ -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 _serverFixture; + ITestOutputHelper output; + public Database(ServerSideFixture serverFixture, ITestOutputHelper output) + { + this.output = output; + _serverFixture = serverFixture; + + output.WriteLine($"Startup.TestDbSettings.Database was {Startup.TestDbSettings.Database}"); + } + + /// + /// Assuming we're using an account that may create databases, + /// Install all our migrations in a fresh new database. + /// + [Fact] + public void InstallFromScratchUsingPoweredNpgsqlUser() + { + if (_serverFixture.DbCreated) + _serverFixture.DropTestDb(); + + _serverFixture.CreateTestDb(); + _serverFixture.UpgradeDb(); + } + } +} \ No newline at end of file diff --git a/test/Mandatory/ServerSideFixture.cs b/test/Mandatory/ServerSideFixture.cs index 1cc4e3bb..edf5d7ac 100644 --- a/test/Mandatory/ServerSideFixture.cs +++ b/test/Mandatory/ServerSideFixture.cs @@ -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 (); 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 (); var siteSetup = App.Services.GetService(typeof(IOptions)) as IOptions; 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"); diff --git a/test/Mandatory/YavscMandatory.cs b/test/Mandatory/YavscMandatory.cs index 73297826..3d495b5e 100755 --- a/test/Mandatory/YavscMandatory.cs +++ b/test/Mandatory/YavscMandatory.cs @@ -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); } diff --git a/test/Settings/DbConnectionSettings.cs b/test/Settings/DbConnectionSettings.cs new file mode 100644 index 00000000..ff09ff4d --- /dev/null +++ b/test/Settings/DbConnectionSettings.cs @@ -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 + { + + } +} \ No newline at end of file diff --git a/test/Startup.cs b/test/Startup.cs index ebecbd3e..b99f4f68 100644 --- a/test/Startup.cs +++ b/test/Startup.cs @@ -13,22 +13,19 @@ using Yavsc.Services; using Microsoft.Data.Entity; using Microsoft.Extensions.WebEncoders; using Yavsc.Lib; +using test.Settings; namespace test { 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 string HostingFullName { get; private set; } - + public static DbConnectionSettings DevDbSettings { get; private set; } + public static DbConnectionSettings TestDbSettings { get; private set; } + ILogger logger; public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) { @@ -44,8 +41,6 @@ namespace test .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); Configuration = builder.Build(); - ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"]; - AppDomain.CurrentDomain.SetData("YAVSC_CONNECTION", ConnectionString); } public void ConfigureServices (IServiceCollection services) @@ -55,6 +50,12 @@ namespace test services.Configure(siteSettingsconf); var smtpSettingsconf = Configuration.GetSection("Smtp"); services.Configure(smtpSettingsconf); + var devCx = Configuration.GetSection("Data:DevConnection"); + services.Configure(devCx); + var testCx = Configuration.GetSection("Data:TestConnection"); + services.Configure(testCx); + + services.AddInstance(typeof(ILoggerFactory), new LoggerFactory()); services.AddTransient(typeof(IEmailSender), typeof(MailSender)); services.AddEntityFramework().AddNpgsql().AddDbContext(); @@ -69,21 +70,29 @@ namespace test services.AddEntityFramework() .AddNpgsql() .AddDbContext( - db => db.UseNpgsql(ConnectionString) + db => db.UseNpgsql(Startup.TestDbSettings.ConnectionString) ); services.AddTransient(); - } public void Configure (IApplicationBuilder app, IHostingEnvironment env, - IOptions siteSettings, ILoggerFactory loggerFactory) + IOptions siteSettings, + IOptions testCxOptions, + IOptions devCxOptions, + ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); logger = loggerFactory.CreateLogger(); 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 clientId = authConf.GetSection("ClientId").Value; var clientSecret = authConf.GetSection("ClientSecret").Value; diff --git a/test/appsettings.json b/test/appsettings.json index 6fde2dbb..09633a5d 100644 --- a/test/appsettings.json +++ b/test/appsettings.json @@ -39,8 +39,19 @@ } }, "Data": { - "DefaultConnection": { - "ConnectionString": "Server=[NpgsqlHostName];Port=[5432?];Database=[DevDnName];Username=[devUserName];Password=[DevPassword];" - } + "DevConnection": { + "Database":"postgres", + "Server": "[NpgsqlHostName]", + "Port": 5432, + "Username": "[devUserName]", + "Password": "[DevPassword]" + }, + "TestConnection": { + "Database":"[TestDbName]", + "Server": "[TestNpgsqlHostName]", + "Port": 5432, + "Username": "[TestUserName]", + "Password": "[TestPassword]" + } } } diff --git a/test/project.json b/test/project.json index a11f6e9e..9edb8503 100644 --- a/test/project.json +++ b/test/project.json @@ -35,6 +35,9 @@ "defaultNamespace": "test" }, "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", "xunit": "2.1.0", "xunit.analyzers": "0.9.0",