Improves configuration and telemetry handling in tests
Adds null checks for telemetry and logging configurations Prevents registration of modules with empty configuration Updates test configuration file copy behavior Enhances robustness of test infrastructure by conditionally registering telemetry and logging modules only when valid configuration is present
This commit is contained in:
parent
5275a75502
commit
3aeae6315b
3 changed files with 96 additions and 91 deletions
|
|
@ -1,87 +1,92 @@
|
|||
using System.Diagnostics;
|
||||
using Autofac;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SWP.Core.Configurations;
|
||||
using SWP.Core.Configurations.JsonConfigProvider;
|
||||
using SWP.Core.Database.ModuleRegistry;
|
||||
using SWP.Core.ModuleRegistry;
|
||||
using SWP.Core.SeqLogging;
|
||||
|
||||
namespace SWP.Core.X.TDD;
|
||||
|
||||
/// <summary>
|
||||
/// Act as base class for tests. Avoids duplication of test setup code
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public abstract class TestFixture
|
||||
{
|
||||
private readonly string _configurationFilePath;
|
||||
|
||||
protected TestFixture() : this(null)
|
||||
{
|
||||
}
|
||||
|
||||
public TestFixture(string configurationFilePath)
|
||||
{
|
||||
if (configurationFilePath is not null)
|
||||
_configurationFilePath = configurationFilePath?.TrimEnd('/') + "/";
|
||||
|
||||
CreateContainerBuilder();
|
||||
Container = ContainerBuilder.Build();
|
||||
}
|
||||
|
||||
protected IContainer Container { get; private set; }
|
||||
protected ContainerBuilder ContainerBuilder { get; private set; }
|
||||
|
||||
public virtual IConfigurationRoot Configuration()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile($"{_configurationFilePath}appconfiguration.dev.json")
|
||||
.Build();
|
||||
|
||||
return configuration;
|
||||
}
|
||||
|
||||
protected virtual void CreateContainerBuilder()
|
||||
{
|
||||
var configuration = Configuration();
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
builder.RegisterGeneric(typeof(Logger<>))
|
||||
.As(typeof(ILogger<>))
|
||||
.SingleInstance();
|
||||
|
||||
|
||||
builder.RegisterModule(new DbPostgreSqlModule
|
||||
{
|
||||
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
});
|
||||
|
||||
builder.RegisterModule(new TelemetryModule
|
||||
{
|
||||
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
||||
});
|
||||
builder.RegisterModule(new SeqLoggingModule
|
||||
{
|
||||
SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<SeqConfiguration>()
|
||||
});
|
||||
|
||||
using System.Diagnostics;
|
||||
using Autofac;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SWP.Core.Configurations;
|
||||
using SWP.Core.Configurations.JsonConfigProvider;
|
||||
using SWP.Core.Database.ModuleRegistry;
|
||||
using SWP.Core.ModuleRegistry;
|
||||
using SWP.Core.SeqLogging;
|
||||
|
||||
namespace SWP.Core.X.TDD;
|
||||
|
||||
/// <summary>
|
||||
/// Act as base class for tests. Avoids duplication of test setup code
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public abstract class TestFixture
|
||||
{
|
||||
private readonly string _configurationFilePath;
|
||||
|
||||
protected TestFixture() : this(null)
|
||||
{
|
||||
}
|
||||
|
||||
public TestFixture(string configurationFilePath)
|
||||
{
|
||||
if (configurationFilePath is not null)
|
||||
_configurationFilePath = configurationFilePath?.TrimEnd('/') + "/";
|
||||
|
||||
CreateContainerBuilder();
|
||||
Container = ContainerBuilder.Build();
|
||||
}
|
||||
|
||||
protected IContainer Container { get; private set; }
|
||||
protected ContainerBuilder ContainerBuilder { get; private set; }
|
||||
|
||||
public virtual IConfigurationRoot Configuration()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile($"{_configurationFilePath}appconfiguration.dev.json")
|
||||
.Build();
|
||||
|
||||
return configuration;
|
||||
}
|
||||
|
||||
protected virtual void CreateContainerBuilder()
|
||||
{
|
||||
var configuration = Configuration();
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
builder.RegisterGeneric(typeof(Logger<>))
|
||||
.As(typeof(ILogger<>))
|
||||
.SingleInstance();
|
||||
|
||||
|
||||
builder.RegisterModule(new DbPostgreSqlModule
|
||||
{
|
||||
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
});
|
||||
|
||||
if (!string.IsNullOrEmpty(configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>().ConnectionString))
|
||||
builder.RegisterModule(new TelemetryModule
|
||||
{
|
||||
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
||||
});
|
||||
|
||||
if (!string.IsNullOrEmpty(configuration.GetSection("SeqConfiguration").ToObject<SeqConfiguration>().IngestionEndpoint))
|
||||
builder.RegisterModule(new SeqLoggingModule
|
||||
{
|
||||
SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<SeqConfiguration>()
|
||||
});
|
||||
|
||||
builder.RegisterModule<SecurityModule>();
|
||||
|
||||
ContainerBuilder = builder;
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
Trace.Flush();
|
||||
var telemetryClient = Container.Resolve<TelemetryClient>();
|
||||
telemetryClient.Flush();
|
||||
|
||||
if (Container is null) return;
|
||||
|
||||
Container.Dispose();
|
||||
Container = null;
|
||||
}
|
||||
}
|
||||
ContainerBuilder = builder;
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
Trace.Flush();
|
||||
if (Container.IsRegistered<TelemetryClient>())
|
||||
{
|
||||
var telemetryClient = Container.Resolve<TelemetryClient>();
|
||||
telemetryClient.Flush();
|
||||
}
|
||||
if (Container is null) return;
|
||||
|
||||
Container.Dispose();
|
||||
Container = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue