Skip to content

Remove SQL dependencies from common test project. #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions samples/WebApi/WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8"/>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.*"/>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.1"/>
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1"/>
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0"/>
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.*"/>
<PackageReference Include="Serilog.Sinks.Map" Version="2.0.0"/>
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="6.6.0"/>
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="10.0.0"/>
Expand Down
8 changes: 4 additions & 4 deletions samples/WebApp/WebApp.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
Expand All @@ -24,15 +24,15 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.33"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.3*"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.20"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.*"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.*"/>
</ItemGroup>

<!-- Serilog UI packages -->
Expand Down
17 changes: 8 additions & 9 deletions tests/Serilog.Ui.Common.Tests/DataSamples/PropertiesFaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
using Microsoft.Extensions.Logging;
using Serilog.Ui.Common.Tests.FakeObjectModels;

namespace Serilog.Ui.Common.Tests.DataSamples
namespace Serilog.Ui.Common.Tests.DataSamples;

public static class PropertiesFaker
{
internal static class PropertiesFaker
{
private static readonly Faker<Properties> Properties = new Faker<Properties>()
.RuleFor(p => p.EventId, f => new EventId(f.Random.Int(), f.Hacker.Noun()))
.RuleForType(typeof(string), p => p.System.Random.Words(3));
public static readonly Faker<Properties> Properties = new Faker<Properties>()
.RuleFor(p => p.EventId, f => new EventId(f.Random.Int(), f.Hacker.Noun()))
.RuleForType(typeof(string), p => p.System.Random.Words(3));

internal static readonly string SerializedProperties = JsonSerializer.Serialize(Properties.Generate());
}
}
internal static readonly string SerializedProperties = JsonSerializer.Serialize(Properties.Generate());
}
29 changes: 29 additions & 0 deletions tests/Serilog.Ui.Common.Tests/FakeObjectModels/EventIdConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;

namespace Serilog.Ui.Common.Tests.FakeObjectModels;

/// <summary>
/// A custom converter for the <see cref="EventId"/> class, since constructing an immutable struct type is not supported when [JsonConstructor] is not used.
/// See: <see href="https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/immutability"/>
/// </summary>
public class EventIdConverter : JsonConverter<EventId>
{
public override EventId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var obj = (JsonObject)JsonNode.Parse(ref reader)! ?? throw new JsonException();

var id = (obj[nameof(EventId.Id)] ?? throw new JsonException()).GetValue<int>();
var name = (obj[nameof(EventId.Name)] ?? throw new JsonException()).GetValue<string>();
return new EventId(id, name);
}

public override void Write(Utf8JsonWriter writer, EventId value, JsonSerializerOptions options)
{
// STJ handles writing fine, so we just delegate to the default implementation.
((JsonConverter<EventId>)JsonSerializerOptions.Default.GetConverter(typeof(EventId))).Write(writer, value, options);
}
}
46 changes: 10 additions & 36 deletions tests/Serilog.Ui.Common.Tests/FakeObjectModels/Properties.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;

namespace Serilog.Ui.Common.Tests.FakeObjectModels
{
internal class Properties
{
public string SourceContext { get; set; } = string.Empty;

[JsonConverter(typeof(EventIdConverter))]
public EventId EventId { get; set; }
namespace Serilog.Ui.Common.Tests.FakeObjectModels;

public string Protocol { get; set; } = string.Empty;

public string Host { get; set; } = string.Empty;
}
public class Properties
{
public string SourceContext { get; set; } = string.Empty;

/// <summary>
/// A custom converter for the <see cref="EventId"/> class, since constructing an immutable struct type is not supported when [JsonConstructor] is not used.
/// See: <see href="https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/immutability"/>
/// </summary>
internal class EventIdConverter : JsonConverter<EventId>
{
public override EventId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var obj = (JsonObject)JsonNode.Parse(ref reader)! ?? throw new JsonException();
[JsonConverter(typeof(EventIdConverter))]
public EventId EventId { get; set; }

var id = (obj[nameof(EventId.Id)] ?? throw new JsonException()).GetValue<int>();
var name = (obj[nameof(EventId.Name)] ?? throw new JsonException()).GetValue<string>();
return new EventId(id, name);
}
public string Protocol { get; set; } = string.Empty;

public override void Write(Utf8JsonWriter writer, EventId value, JsonSerializerOptions options)
{
// STJ handles writing fine, so we just delegate to the default implementation.
((JsonConverter<EventId>)JsonSerializerOptions.Default.GetConverter(typeof(EventId))).Write(writer, value, options);
}
}
}
public string Host { get; set; } = string.Empty;
}
7 changes: 1 addition & 6 deletions tests/Serilog.Ui.Common.Tests/Serilog.Ui.Common.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsTestProject>false</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="35.6.0"/>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.1"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="MySqlConnector" Version="2.3.7" />
<PackageReference Include="Npgsql" Version="8.0.3" />
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Serilog.Ui.Core\Serilog.Ui.Core.csproj" />
<ProjectReference Include="..\..\src\Serilog.Ui.MongoDbProvider\Serilog.Ui.MongoDbProvider.csproj" />
</ItemGroup>

</Project>
146 changes: 54 additions & 92 deletions tests/Serilog.Ui.Common.Tests/SqlUtil/DatabaseInstance.cs
Original file line number Diff line number Diff line change
@@ -1,112 +1,74 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using DotNet.Testcontainers.Containers;
using Microsoft.Data.SqlClient;
using MySqlConnector;
using Npgsql;
using Serilog.Ui.Common.Tests.DataSamples;
using Serilog.Ui.Common.Tests.TestSuites;
using Serilog.Ui.Core;

namespace Serilog.Ui.Common.Tests.SqlUtil
{
public abstract class DatabaseInstance : IIntegrationRunner
{
private bool _disposedValue;

protected virtual string Name { get; } = nameof(IContainer);

/// <summary>
/// Gets or sets the TestContainers container.
/// </summary>
protected virtual IContainer? Container { get; set; }

/// <summary>
/// Gets or sets the IDataProvider.
/// </summary>
protected IDataProvider? Provider { get; set; }

protected LogModelPropsCollector? Collector { get; set; }

public IDataProvider GetDataProvider() => Guard.Against.Null(Provider);

public LogModelPropsCollector GetPropsCollector() => Guard.Against.Null(Collector);
namespace Serilog.Ui.Common.Tests.SqlUtil;

public async Task InitializeAsync()
{
await Container!.StartAsync();
await GetDbContextInstanceAsync();
}
public abstract class DatabaseInstance : IIntegrationRunner
{
protected abstract string Name { get; }

/// <summary>
/// Register an operation to check for db readiness.
/// You can access the container connection string from this point onwards.
/// Runs before <see cref="InitializeAdditionalAsync"/>.
/// </summary>
/// <returns><see cref="Task"/></returns>
protected abstract Task CheckDbReadinessAsync();
/// <summary>
/// Gets or sets the TestContainers container.
/// </summary>
protected virtual IContainer Container { get; set; } = null!;

/// <summary>
/// Register operations to set up the database in a functional status.
/// Runs after <see cref="CheckDbReadinessAsync"/>.
/// </summary>
/// <returns><see cref="Task"/></returns>
protected abstract Task InitializeAdditionalAsync();
/// <summary>
/// Gets or sets the IDataProvider.
/// </summary>
protected IDataProvider? Provider { get; set; }

private async Task GetDbContextInstanceAsync(CancellationToken token = default)
{
int retry = default;
protected LogModelPropsCollector? Collector { get; set; }

do
{
try
{
await CheckDbReadinessAsync();
break;
}
catch (Exception ex) when (ex is SqlException or MySqlException or NpgsqlException)
{
retry += 1;
await Task.Delay(1000 * retry, token);
}
} while (retry < 10);
public IDataProvider GetDataProvider() => Guard.Against.Null(Provider);

await InitializeAdditionalAsync();
}
public LogModelPropsCollector GetPropsCollector() => Guard.Against.Null(Collector);

/// <inheritdoc/>
public async Task DisposeAsync()
{
if (Container != null)
{
await Container.DisposeAsync()
.ConfigureAwait(false);
}
}
public async Task InitializeAsync()
{
await Container.StartAsync();
await CheckDbReadinessAsync();
await InitializeAdditionalAsync();
}

/// <summary>
/// Dispose any additional managed dependencies.
/// </summary>
/// <param name="disposing">If it's disposing.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposedValue) return;
if (disposing)
{
// additional dispose items
}
/// <summary>
/// Register an operation to check for db readiness.
/// You can access the container connection string from this point onwards.
/// Runs before <see cref="InitializeAdditionalAsync"/>.
/// </summary>
/// <returns><see cref="Task"/></returns>
protected abstract Task CheckDbReadinessAsync();

/// <summary>
/// Register operations to set up the database in a functional status.
/// Runs after <see cref="CheckDbReadinessAsync"/>.
/// </summary>
/// <returns><see cref="Task"/></returns>
protected abstract Task InitializeAdditionalAsync();

/// <inheritdoc/>
public async Task DisposeAsync()
{
await Container.DisposeAsync().ConfigureAwait(false);
}

_disposedValue = true;
}
/// <summary>
/// Dispose any additional managed dependencies.
/// </summary>
/// <param name="disposing">If it's disposing.</param>
protected virtual void Dispose(bool disposing)
{
}

/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ protected MsSqlServerTestProvider()
Container = new MsSqlBuilder().Build();
}

private RelationalDbOptions DbOptions { get; set; } = new RelationalDbOptions("dbo").WithTable("Logs");
private RelationalDbOptions DbOptions { get; } = new RelationalDbOptions("dbo").WithTable("Logs");

protected sealed override IContainer? Container { get; set; }
protected override sealed IContainer Container { get; set; }

protected override string Name => nameof(MsSqlContainer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected MariaDbTestProvider()

private RelationalDbOptions DbOptions { get; set; } = new RelationalDbOptions("dbo").WithTable("Logs");

protected sealed override IContainer? Container { get; set; }
protected override sealed IContainer Container { get; set; }

protected virtual Dictionary<string, string>? PropertiesToColumnsMapping => new MariaDBSinkOptions().PropertiesToColumnsMapping;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected PostgresTestProvider()
.WithTable("logs")
.WithSinkType(PostgreSqlSinkType.SerilogSinksPostgreSQLAlternative);

protected sealed override IContainer? Container { get; set; }
protected override sealed IContainer Container { get; set; }

protected virtual Dictionary<string, ColumnWriterBase>? ColumnOptions => null;

Expand Down
6 changes: 3 additions & 3 deletions tests/Serilog.Ui.Web.Tests/Serilog.Ui.Web.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.33"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.*"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.20"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.*"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.8"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.*"/>
</ItemGroup>

<ItemGroup>
Expand Down
Loading