Skip to content

Commit

Permalink
Merge pull request #47 from followynne/perf/testcontainers-impl
Browse files Browse the repository at this point in the history
tests(data-providers): integration tests
  • Loading branch information
mo-esmp authored Mar 12, 2023
2 parents f087218 + 4c9948a commit a41fe14
Show file tree
Hide file tree
Showing 72 changed files with 2,487 additions and 513 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ To run the tests, use Test Explorer in Visual Studio or run from the root folder
dotnet test
```

Please notice: to run the tests, you'll need a running Docker instance on your sistem.
Integration tests (identified by traits) on MS Sql, MySql, Postgres, use Docker to spin the integration database.

## JS UI assets

Tests are located inside src/Serilog.Ui.Web/assets/__tests__
Expand Down
8 changes: 4 additions & 4 deletions Serilog.Ui.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.MongoDbProvider.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.Common.Tests", "tests\Serilog.Ui.Common.Tests\Serilog.Ui.Common.Tests.csproj", "{96689D04-8B2F-4C06-AE1D-3483B1508D59}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Ui.PostgreSqlProvider.Tests", "tests\Serilog.Ui.PostgreSqlProvider.Tests\Serilog.Ui.PostgreSqlProvider.Tests.csproj", "{D1688095-7E1F-42B2-BC3F-8CB29975CAD5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.PostgreSqlProvider.Tests", "tests\Serilog.Ui.PostgreSqlProvider.Tests\Serilog.Ui.PostgreSqlProvider.Tests.csproj", "{D1688095-7E1F-42B2-BC3F-8CB29975CAD5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.Web.Tests", "tests\Serilog.Ui.Web.Tests\Serilog.Ui.Web.Tests.csproj", "{9AD9BF6A-0CB0-467C-BB84-BE1C701C0113}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Ui.MySqlProvider.Tests", "tests\Serilog.Ui.MySqlProvider.Tests\Serilog.Ui.MySqlProvider.Tests.csproj", "{6986AD9C-3B9E-4DAF-A45F-643D964CEBD3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.MySqlProvider.Tests", "tests\Serilog.Ui.MySqlProvider.Tests\Serilog.Ui.MySqlProvider.Tests.csproj", "{6986AD9C-3B9E-4DAF-A45F-643D964CEBD3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Ui.MsSqlServerProvider.Tests", "tests\Serilog.Ui.MsSqlServerProvider.Tests\Serilog.Ui.MsSqlServerProvider.Tests.csproj", "{1F6675BC-32FC-47DE-96AB-422632AB2730}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.MsSqlServerProvider.Tests", "tests\Serilog.Ui.MsSqlServerProvider.Tests\Serilog.Ui.MsSqlServerProvider.Tests.csproj", "{1F6675BC-32FC-47DE-96AB-422632AB2730}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Ui.ElastichSearchProvider.Tests", "tests\Serilog.Ui.ElastichSearchProvider.Tests\Serilog.Ui.ElastichSearchProvider.Tests.csproj", "{1AB759E4-61CD-4195-9CA9-E70B63AF28B9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.ElasticSearchProvider.Tests", "tests\Serilog.Ui.ElastichSearchProvider.Tests\Serilog.Ui.ElasticSearchProvider.Tests.csproj", "{1AB759E4-61CD-4195-9CA9-E70B63AF28B9}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{83E91BE7-19B3-4AE0-992C-9DFF30FC409E}"
ProjectSection(SolutionItems) = preProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
using Elasticsearch.Net;
using Microsoft.Extensions.DependencyInjection;
using Nest;
using Serilog.Ui.Core;
using System;

namespace Serilog.Ui.ElasticSearchProvider
{
/// <summary>
/// ElasticSearch data provider specific extension methods for <see cref="SerilogUiOptionsBuilder"/>.
/// </summary>
public static class SerilogUiOptionBuilderExtensions
{
/// <summary>
/// Configures the SerilogUi to connect to a MongoDB database.
/// </summary>
/// <param name="optionsBuilder"> The options builder. </param>
/// <param name="endpoint"> The url of ElasticSearch server. </param>
/// <param name="indexName"> Name of the log index. </param>
/// <exception cref="ArgumentNullException"> throw if endpoint is null </exception>
/// <exception cref="ArgumentNullException"> throw is indexName is null </exception>
public static void UseElasticSearchDb(this SerilogUiOptionsBuilder optionsBuilder, Uri endpoint, string indexName)
{
if (endpoint == null)
throw new ArgumentNullException(nameof(endpoint));

if (string.IsNullOrEmpty(indexName))
throw new ArgumentNullException(nameof(indexName));

var options = new ElasticSearchDbOptions
{
IndexName = indexName
};

var builder = ((ISerilogUiOptionsBuilder)optionsBuilder);

builder.Services.AddSingleton(options);

var pool = new SingleNodeConnectionPool(endpoint);
var connectionSettings = new ConnectionSettings(pool, sourceSerializer: (builtin, values) => new VanillaSerializer());

builder.Services.AddSingleton<IElasticClient>(o => new ElasticClient(connectionSettings));
builder.Services.AddScoped<IDataProvider, ElasticSearchDbDataProvider>();
}
}
using Elasticsearch.Net;
using Microsoft.Extensions.DependencyInjection;
using Nest;
using Serilog.Ui.Core;
using System;

namespace Serilog.Ui.ElasticSearchProvider
{
/// <summary>
/// ElasticSearch data provider specific extension methods for <see cref="SerilogUiOptionsBuilder"/>.
/// </summary>
public static class SerilogUiOptionBuilderExtensions
{
/// <summary>
/// Configures the SerilogUi to connect to a MongoDB database.
/// </summary>
/// <param name="optionsBuilder"> The options builder. </param>
/// <param name="endpoint"> The url of ElasticSearch server. </param>
/// <param name="indexName"> Name of the log index. </param>
/// <exception cref="ArgumentNullException"> throw if endpoint is null </exception>
/// <exception cref="ArgumentNullException"> throw is indexName is null </exception>
public static void UseElasticSearchDb(this SerilogUiOptionsBuilder optionsBuilder, Uri endpoint, string indexName)
{
if (endpoint == null)
throw new ArgumentNullException(nameof(endpoint));

if (string.IsNullOrWhiteSpace(indexName))
throw new ArgumentNullException(nameof(indexName));

var options = new ElasticSearchDbOptions
{
IndexName = indexName
};

var builder = ((ISerilogUiOptionsBuilder)optionsBuilder);

builder.Services.AddSingleton(options);

var pool = new SingleNodeConnectionPool(endpoint);
var connectionSettings = new ConnectionSettings(pool, sourceSerializer: (builtin, values) => new VanillaSerializer());

builder.Services.AddSingleton<IElasticClient>(o => new ElasticClient(connectionSettings));
builder.Services.AddScoped<IDataProvider, ElasticSearchDbDataProvider>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.3" />
<PackageReference Include="NEST" Version="7.11.1" />
<PackageReference Include="NEST.JsonNetSerializer" Version="7.11.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ public static void UseMongoDb(
string collectionName
)
{
if (string.IsNullOrEmpty(connectionString))
if (string.IsNullOrWhiteSpace(connectionString))
throw new ArgumentNullException(nameof(connectionString));

if (string.IsNullOrEmpty(collectionName))
if (string.IsNullOrWhiteSpace(collectionName))
throw new ArgumentNullException(nameof(collectionName));

var databaseName = MongoUrl.Create(connectionString).DatabaseName;

if (string.IsNullOrWhiteSpace(databaseName)) throw new ArgumentException(nameof(MongoUrl.DatabaseName));

var mongoProvider = new MongoDbOptions
{
ConnectionString = connectionString,
DatabaseName = MongoUrl.Create(connectionString).DatabaseName,
DatabaseName = databaseName,
CollectionName = collectionName
};

Expand All @@ -60,13 +64,13 @@ public static void UseMongoDb(
string collectionName
)
{
if (string.IsNullOrEmpty(connectionString))
if (string.IsNullOrWhiteSpace(connectionString))
throw new ArgumentNullException(nameof(connectionString));

if (string.IsNullOrEmpty(databaseName))
if (string.IsNullOrWhiteSpace(databaseName))
throw new ArgumentNullException(nameof(databaseName));

if (string.IsNullOrEmpty(collectionName))
if (string.IsNullOrWhiteSpace(collectionName))
throw new ArgumentNullException(nameof(collectionName));

var mongoProvider = new MongoDbOptions
Expand All @@ -77,7 +81,7 @@ string collectionName
};

((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddSingleton(mongoProvider);
((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddSingleton<IMongoClient>(o => new MongoClient(connectionString));
((ISerilogUiOptionsBuilder)optionsBuilder).Services.TryAddSingleton<IMongoClient>(o => new MongoClient(connectionString));
((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddScoped<IDataProvider, MongoDbDataProvider>();
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/Serilog.Ui.MongoDbProvider/MongoDbDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public MongoDbDataProvider(IMongoClient client, MongoDbOptions options)
if (options is null) throw new ArgumentNullException(nameof(options));

_collection = client.GetDatabase(options.DatabaseName).GetCollection<MongoDbLogModel>(options.CollectionName);
var s = _collection.CollectionNamespace;
}

public async Task<(IEnumerable<LogModel>, int)> FetchDataAsync(
Expand Down Expand Up @@ -49,6 +48,12 @@ private async Task<IEnumerable<LogModel>> GetLogsAsync(
var builder = Builders<MongoDbLogModel>.Filter.Empty;
GenerateWhereClause(ref builder, level, searchCriteria, startDate, endDate);

if (!string.IsNullOrWhiteSpace(searchCriteria))
{
await _collection.Indexes.CreateOneAsync(
new CreateIndexModel<MongoDbLogModel>(Builders<MongoDbLogModel>.IndexKeys.Text(p => p.RenderedMessage)));
}

var logs = await _collection
.Find(builder)
.Skip(count * page)
Expand Down Expand Up @@ -89,7 +94,7 @@ private void GenerateWhereClause(
if (!string.IsNullOrWhiteSpace(level))
builder &= Builders<MongoDbLogModel>.Filter.Eq(entry => entry.Level, level);

if (!string.IsNullOrWhiteSpace(searchCriteria))
if (!string.IsNullOrWhiteSpace(searchCriteria))
builder &= Builders<MongoDbLogModel>.Filter.Text(searchCriteria);

if (startDate != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
using Microsoft.Extensions.DependencyInjection;
using Serilog.Ui.Core;
using System;

namespace Serilog.Ui.MsSqlServerProvider
{
/// <summary>
/// SQL Server data provider specific extension methods for <see cref="SerilogUiOptionsBuilder"/>.
/// </summary>
public static class SerilogUiOptionBuilderExtensions
{
/// <summary>
/// Configures the SerilogUi to connect to a SQL Server database.
/// </summary>
/// <param name="optionsBuilder"> The options builder. </param>
/// <param name="connectionString"> The connection string. </param>
/// <param name="tableName"> Name of the table. </param>
/// <param name="schemaName">
/// Name of the table schema. default value is <c> dbo </c>
/// </param>
/// <exception cref="ArgumentNullException"> throw if connectionString is null </exception>
/// <exception cref="ArgumentNullException"> throw is tableName is null </exception>
public static void UseSqlServer(
this SerilogUiOptionsBuilder optionsBuilder,
string connectionString,
string tableName,
string schemaName = "dbo"
)
{
if (string.IsNullOrEmpty(connectionString))
throw new ArgumentNullException(nameof(connectionString));

if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException(nameof(tableName));

var relationProvider = new RelationalDbOptions
{
ConnectionString = connectionString,
TableName = tableName,
Schema = schemaName
};

((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddSingleton(relationProvider);
((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddScoped<IDataProvider, SqlServerDataProvider>();
}
}
using Microsoft.Extensions.DependencyInjection;
using Serilog.Ui.Core;
using System;

namespace Serilog.Ui.MsSqlServerProvider
{
/// <summary>
/// SQL Server data provider specific extension methods for <see cref="SerilogUiOptionsBuilder"/>.
/// </summary>
public static class SerilogUiOptionBuilderExtensions
{
/// <summary>
/// Configures the SerilogUi to connect to a SQL Server database.
/// </summary>
/// <param name="optionsBuilder"> The options builder. </param>
/// <param name="connectionString"> The connection string. </param>
/// <param name="tableName"> Name of the table. </param>
/// <param name="schemaName">
/// Name of the table schema. default value is <c> dbo </c>
/// </param>
/// <exception cref="ArgumentNullException"> throw if connectionString is null </exception>
/// <exception cref="ArgumentNullException"> throw is tableName is null </exception>
public static void UseSqlServer(
this SerilogUiOptionsBuilder optionsBuilder,
string connectionString,
string tableName,
string schemaName = "dbo"
)
{
if (string.IsNullOrWhiteSpace(connectionString))
throw new ArgumentNullException(nameof(connectionString));

if (string.IsNullOrWhiteSpace(tableName))
throw new ArgumentNullException(nameof(tableName));

var relationProvider = new RelationalDbOptions
{
ConnectionString = connectionString,
TableName = tableName,
Schema = !string.IsNullOrWhiteSpace(schemaName) ? schemaName : "dbo"
};

((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddSingleton(relationProvider);
((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddScoped<IDataProvider, SqlServerDataProvider>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.35" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.2" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
using Microsoft.Extensions.DependencyInjection;
using Serilog.Ui.Core;
using System;

namespace Serilog.Ui.MySqlProvider
{
/// <summary>
/// MySQL data provider specific extension methods for <see cref="SerilogUiOptionsBuilder"/>.
/// </summary>
public static class SerilogUiOptionBuilderExtensions
{
/// <summary>
/// Configures the SerilogUi to connect to a MySQL database.
/// </summary>
/// <param name="optionsBuilder"> The options builder. </param>
/// <param name="connectionString"> The connection string. </param>
/// <param name="tableName"> Name of the table. </param>
/// <exception cref="ArgumentNullException"> throw if connectionString is null </exception>
/// <exception cref="ArgumentNullException"> throw is tableName is null </exception>
public static void UseMySqlServer(
this SerilogUiOptionsBuilder optionsBuilder,
string connectionString,
string tableName
)
{
if (string.IsNullOrEmpty(connectionString))
throw new ArgumentNullException(nameof(connectionString));

if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException(nameof(tableName));

var relationProvider = new RelationalDbOptions
{
ConnectionString = connectionString,
TableName = tableName
};

((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddSingleton(relationProvider);
((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddScoped<IDataProvider, MySqlDataProvider>();
}
}
using Microsoft.Extensions.DependencyInjection;
using Serilog.Ui.Core;
using System;

namespace Serilog.Ui.MySqlProvider
{
/// <summary>
/// MySQL data provider specific extension methods for <see cref="SerilogUiOptionsBuilder"/>.
/// </summary>
public static class SerilogUiOptionBuilderExtensions
{
/// <summary>
/// Configures the SerilogUi to connect to a MySQL database.
/// </summary>
/// <param name="optionsBuilder"> The options builder. </param>
/// <param name="connectionString"> The connection string. </param>
/// <param name="tableName"> Name of the table. </param>
/// <exception cref="ArgumentNullException"> throw if connectionString is null </exception>
/// <exception cref="ArgumentNullException"> throw is tableName is null </exception>
public static void UseMySqlServer(
this SerilogUiOptionsBuilder optionsBuilder,
string connectionString,
string tableName
)
{
if (string.IsNullOrWhiteSpace(connectionString))
throw new ArgumentNullException(nameof(connectionString));

if (string.IsNullOrWhiteSpace(tableName))
throw new ArgumentNullException(nameof(tableName));

var relationProvider = new RelationalDbOptions
{
ConnectionString = connectionString,
TableName = tableName
};

((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddSingleton(relationProvider);
((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddScoped<IDataProvider, MySqlDataProvider>();
}
}
}
Loading

0 comments on commit a41fe14

Please sign in to comment.