testing
Some checks failed
Dotnet build and test / log-the-inputs (push) Failing after 2s
Dotnet build and test / build (push) Failing after 1s

This commit is contained in:
Paul Schneider
2025-07-14 18:58:04 +01:00
parent 0a270c9218
commit 62e863e343
20 changed files with 233 additions and 266 deletions

View File

@ -40,7 +40,6 @@ namespace yavscTests
[Fact]
public void GitClone()
{
Assert.True(_serverFixture.EnsureTestDb());
Assert.NotNull (_serverFixture.DbContext.Project);
var firstProject = _serverFixture.DbContext.Project.Include(p=>p.Repository).FirstOrDefault();
Assert.NotNull (firstProject);
@ -54,8 +53,6 @@ namespace yavscTests
string gitRepo=null;
private IConfigurationRoot configurationRoot;
[Fact]
public void HaveConfigurationRoot()
@ -66,55 +63,12 @@ namespace yavscTests
configurationRoot = builder.Build();
}
internal static IConfigurationRoot CreateConfiguration(string prjDir)
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile(Path.Combine(prjDir, "appsettings.json"), false);
builder.AddJsonFile(Path.Combine(prjDir, "appsettings.Development.json"), true);
return builder.Build();
}
static IConfigurationRoot ConfigurationRoot;
internal static void ConfigureServices
(ServiceCollection serviceCollection,
string prjDir,
out IConfigurationRoot configuration,
out IServiceProvider provider)
{
ConfigurationRoot = configuration = CreateConfiguration(prjDir);
serviceCollection.AddOptions();
var siteSettingsconf = configuration.GetSection("Site");
serviceCollection.Configure<SiteSettings>(siteSettingsconf);
var smtpSettingsconf = configuration.GetSection("Smtp");
serviceCollection.Configure<SmtpSettings>(smtpSettingsconf);
var locOptions = configuration.GetSection("Localization");
serviceCollection.Configure<LocalizationOptions>(locOptions);
serviceCollection.AddSingleton(typeof(ILoggerFactory), typeof(LoggerFactory));
serviceCollection.AddTransient(typeof(IEmailSender<ApplicationUser>), typeof(MailSender));
serviceCollection.AddLogging();
serviceCollection.AddMvcCore();
serviceCollection.AddLocalization(options =>
{
options.ResourcesPath = "Resources";
});
serviceCollection.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(o => ConfigurationRoot.GetConnectionString("DefaultConnection")));
provider = serviceCollection.BuildServiceProvider();
}
public void Dispose()
{
if (gitRepo!=null)
{
Directory.Delete(Path.Combine(gitRepo,"yavsc"), true);
}
_serverFixture.DropTestDb();
}
}
}

View File

@ -5,6 +5,7 @@ using Xunit.Abstractions;
namespace yavscTests.Mandatory
{
[Collection("Database")]
[Trait("regression", "II")]
[Trait("dev", "wip")]
@ -16,29 +17,14 @@ namespace yavscTests.Mandatory
{
this.output = output;
_serverFixture = serverFixture;
try {
if (_serverFixture.DbCreated)
_serverFixture.DropTestDb();
}
catch (Exception)
{
output.WriteLine("db not dropped");
}
output.WriteLine($"Startup.Testing.ConnectionStrings.Default is {_serverFixture.TestingSetup.ConnectionStrings.Default}");
output.WriteLine($"Testing connection string is {_serverFixture?.TestingSetup?.ConnectionStrings.DefaultConnection}");
}
/// <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()
{
Assert.True(_serverFixture.EnsureTestDb());
Assert.True(_serverFixture.UpgradeDb()==0);
}
public void Dispose()
{

View File

@ -2,7 +2,6 @@ namespace yavscTests.Settings
{
public class DbConnectionSettings
{
public string DatabaseCtor { get; set; }
public string Default { get; set; }
public string? DefaultConnection { get; set; }
}
}

View File

@ -1,5 +1,6 @@
using System.Net;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
@ -13,6 +14,8 @@ using Yavsc;
using Yavsc.Models;
using Yavsc.Services;
using yavscTests.Settings;
using Yavsc.Extensions;
using Microsoft.EntityFrameworkCore;
namespace isnd.tests
{
@ -20,12 +23,14 @@ namespace isnd.tests
[CollectionDefinition("Web server collection")]
public class WebServerFixture : IDisposable
{
public IWebHost Host { get; private set;}
public List<string> Addresses { get; private set; } = new List<string>();
public Microsoft.Extensions.Logging.ILogger Logger { get; internal set; }
private SiteSettings siteSettings;
public IConfigurationRoot Configuration { get; private set; }
private WebApplication app;
public string TestingUserName { get; private set; }
public string ProtectedTestingApiKey { get; internal set; }
@ -38,34 +43,41 @@ namespace isnd.tests
public WebServerFixture()
{
SetupHost();
SetupHost().Wait();
}
public void Dispose()
{
Host.StopAsync().Wait();
Host.Dispose();
if (app!=null)
app.StopAsync().Wait();
}
public void SetupHost()
public async Task SetupHost()
{
var builder = WebHost.CreateDefaultBuilder(new string[0]);
var builder = WebApplication.CreateBuilder();
Configuration = builder.Configuration
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables()
.Build();
// .UseContentRoot("../../../../../src/isnd")
builder.UseStartup(typeof(Startup))
.ConfigureAppConfiguration((builderContext, config) =>
this.app = builder.ConfigureWebAppServices();
using (var migrationScope = app.Services.CreateScope())
{
config.AddJsonFile("appsettings.json", true);
config.AddJsonFile("appsettings.Development.json", false);
});
var db = migrationScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await db.Database.MigrateAsync();
}
await app.ConfigurePipeline();
app.UseSession();
await app.StartAsync();
Host = builder.Build();
var logFactory = Host.Services.GetRequiredService<ILoggerFactory>();
var logFactory = app.Services.GetRequiredService<ILoggerFactory>();
Logger = logFactory.CreateLogger<WebServerFixture>();
Host.Start(); //Starts listening on the configured addresses.
var server = Host.Services.GetRequiredService<IServer>();
var server = app.Services.GetRequiredService<IServer>();
var addressFeatures = server.Features.Get<IServerAddressesFeature>();
@ -73,12 +85,13 @@ namespace isnd.tests
{
Addresses.Add(address);
}
SiteSettings = Host.Services.GetRequiredService<IOptions<SiteSettings>>().Value;
SiteSettings = app.Services.GetRequiredService<IOptions<SiteSettings>>().Value;
using IServiceScope scope = Host.Services.CreateScope();
using IServiceScope scope = app.Services.CreateScope();
ApplicationDbContext dbContext =
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
//dbContext.Database.EnsureCreated();
dbContext.Database.Migrate();
TestingUserName = "Tester";
TestingUser = dbContext.Users.FirstOrDefault(u => u.UserName == TestingUserName);
@ -93,7 +106,7 @@ namespace isnd.tests
{
if (TestingUser == null)
{
using IServiceScope scope = Host.Services.CreateScope();
using IServiceScope scope = app.Services.CreateScope();
var userManager =
scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
@ -114,19 +127,6 @@ namespace isnd.tests
}
}
internal void DropTestDb()
{
throw new NotImplementedException();
}
internal bool EnsureTestDb()
{
throw new NotImplementedException();
}
internal int UpgradeDb()
{
throw new NotImplementedException();
}
}
}

View File

@ -41,9 +41,8 @@
}
},
"ConnectionStrings": {
"Default": "Server=lame-NpgsqlHostName;Port=5432;Database=lame-DataBase;Username=lame-Username;Password=lame-dbPassword;",
"DatabaseCtor": "Server=lame-NpgsqlHostName;Port=5432;Database=lame-ctor-DataBase;Username=lame-ctor-Username;Password=lame-ctordbPassword;"
},
"DefaultConnection": "Server=lame-NpgsqlHostName;Port=5432;Database=lame-DataBase;Username=lame-Username;Password=lame-dbPassword;",
},
"DataProtection": {
"Keys": {
"Dir": "DataProtection-Keys"

View File

@ -1,26 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.3.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.7" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.7" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.7" />
<PackageReference Include="Microsoft.Playwright" Version="1.53.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" />
<PackageReference Include="Microsoft.Extensions.Options" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
<PackageReference Include="Microsoft.Playwright" />
<PackageReference Include="Newtonsoft.Json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Yavsc\Yavsc.csproj" />
<ProjectReference Include="..\..\src\Yavsc.Abstract\Yavsc.Abstract.csproj" />
<ProjectReference Include="..\..\src\Yavsc.Server\Yavsc.Server.csproj" />
</ItemGroup>
</Project>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>