-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove SQL dependencies from common test project. (#141)
Remove SQL dependencies from common test project.
- Loading branch information
Showing
11 changed files
with
115 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
tests/Serilog.Ui.Common.Tests/FakeObjectModels/EventIdConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
tests/Serilog.Ui.Common.Tests/FakeObjectModels/Properties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
146
tests/Serilog.Ui.Common.Tests/SqlUtil/DatabaseInstance.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters