Skip to content
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

tests(data-providers): integration tests #47

Merged
merged 30 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ffd45c2
fix: setup xunit settings
followynne Sep 11, 2022
4f9fe28
feat: base testcontainer abstract builder
followynne Sep 11, 2022
94d3dd1
feat(provider-test): tests interfaces to implement
followynne Sep 11, 2022
3b2fd80
refactor(mongo): use interfaces, fix IDisposable
followynne Sep 11, 2022
e8b7113
feat(ms-sql): first testcontainer impl!
followynne Sep 11, 2022
4768898
feat: create mysql-provider first test
followynne Sep 11, 2022
41a449b
feat(postgres): first test implementation
followynne Sep 11, 2022
3f4c50b
test: mongodb provider
followynne Sep 18, 2022
a784277
test: mssql - mysql search provider
followynne Sep 18, 2022
5e1268d
test: complete mongo's
followynne Sep 18, 2022
94fc2ff
chore(nuget): bump pkgs
followynne Feb 18, 2023
2ef13ef
chore(ms-sql): bump sqlclient after test coverage
followynne Feb 18, 2023
f689d7d
fix(logs-faker): stabilize timestamps generation as UTC
followynne Feb 18, 2023
cad02b0
test: complete postgres and update mongo
followynne Feb 18, 2023
0ef4ab4
test(my-sql): implement interfaces
followynne Feb 18, 2023
8cccef5
test(postgres): add level per log level converter util
followynne Feb 18, 2023
c2daadc
chore(tests): improve namespace names, add first traits
followynne Feb 19, 2023
fd40bb6
refactor(tests): extract common tests and trait them
followynne Feb 19, 2023
baa62dc
fix(pagination): specify throw test on each derived class
followynne Feb 19, 2023
143f6ff
tests(elastic): create failing classes
followynne Feb 19, 2023
54a7993
refactor(test-base): extract base interface to share on all prj
followynne Feb 27, 2023
b101ae5
refactor(mongodb-tests): implement main interfaces
followynne Feb 27, 2023
793e1e4
refactor(testprovider): change classfixture to collectionfixture
followynne Mar 6, 2023
33ab4c0
tests(elastic): FINALLY completed first round of tests
followynne Mar 6, 2023
7717b79
tests(elastic): complete setup and search tests
followynne Mar 11, 2023
8133af6
chore(tests): bump nuget
followynne Mar 11, 2023
280d554
tests: update testcontainers to 3.0.0
followynne Mar 11, 2023
3d352e4
chore(README): add info on test requirements
followynne Mar 12, 2023
b4fa976
Squashed commit of the following:
followynne Mar 12, 2023
4c9948a
Merge branch 'dev' into perf/testcontainers-impl
followynne Mar 12, 2023
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
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