Skip to content

Commit

Permalink
Updated code style to use new-ish C# syntax features.
Browse files Browse the repository at this point in the history
  • Loading branch information
impworks committed May 3, 2024
1 parent b5a5274 commit fa452d8
Show file tree
Hide file tree
Showing 289 changed files with 14,557 additions and 14,973 deletions.
11 changes: 5 additions & 6 deletions src/Bonsai.Tests.Search/Fixtures/SearchCollection.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Xunit;

namespace Bonsai.Tests.Search.Fixtures
namespace Bonsai.Tests.Search.Fixtures;

[CollectionDefinition("Search tests")]
public class SearchCollection: ICollectionFixture<SearchEngineFixture>
{
[CollectionDefinition("Search tests")]
public class SearchCollection: ICollectionFixture<SearchEngineFixture>
{
}
}
}
85 changes: 42 additions & 43 deletions src/Bonsai.Tests.Search/Fixtures/SearchEngineFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,50 @@
using Microsoft.EntityFrameworkCore;
using Xunit;

namespace Bonsai.Tests.Search.Fixtures
namespace Bonsai.Tests.Search.Fixtures;

/// <summary>
/// Shared context for all fulltext search tests.
/// </summary>
public class SearchEngineFixture: IAsyncLifetime
{
public SearchEngineFixture()
{
var opts = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase("default")
.Options;

Db = new AppDbContext(opts);
// Search = new ElasticService(new ElasticSearchConfig { Host = "http://localhost:9200", IndexName = "test_pages" });
Search = new LuceneNetService();
}

public readonly AppDbContext Db;
public readonly ISearchEngine Search;

/// <summary>
/// Initializes the search index with test data.
/// </summary>
public async Task InitializeAsync()
{
var rootPath = Path.GetFullPath(Directory.GetCurrentDirectory());
var wwwPath = Path.Combine(rootPath, "wwwroot");
if(Directory.Exists(wwwPath))
Directory.Delete(wwwPath, true);
await Search.ClearDataAsync();

var seedPath = Path.Combine(rootPath, "..", "..", "..", "..", "Bonsai", "Data", "Utils", "Seed");
await SeedData.EnsureSampleDataSeededAsync(Db, seedPath);

await foreach (var page in Db.Pages)
await Search.AddPageAsync(page);
}

/// <summary>
/// Shared context for all fulltext search tests.
/// Releases the in-memory testing database.
/// </summary>
public class SearchEngineFixture: IAsyncLifetime
public async Task DisposeAsync()
{
public SearchEngineFixture()
{
var opts = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase("default")
.Options;

Db = new AppDbContext(opts);
// Search = new ElasticService(new ElasticSearchConfig { Host = "http://localhost:9200", IndexName = "test_pages" });
Search = new LuceneNetService();
}

public readonly AppDbContext Db;
public readonly ISearchEngine Search;

/// <summary>
/// Initializes the search index with test data.
/// </summary>
public async Task InitializeAsync()
{
var rootPath = Path.GetFullPath(Directory.GetCurrentDirectory());
var wwwPath = Path.Combine(rootPath, "wwwroot");
if(Directory.Exists(wwwPath))
Directory.Delete(wwwPath, true);
await Search.ClearDataAsync();

var seedPath = Path.Combine(rootPath, "..", "..", "..", "..", "Bonsai", "Data", "Utils", "Seed");
await SeedData.EnsureSampleDataSeededAsync(Db, seedPath);

await foreach (var page in Db.Pages)
await Search.AddPageAsync(page);
}

/// <summary>
/// Releases the in-memory testing database.
/// </summary>
public async Task DisposeAsync()
{
await Db.DisposeAsync();
}
await Db.DisposeAsync();
}
}
}
97 changes: 48 additions & 49 deletions src/Bonsai.Tests.Search/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,68 @@
using Bonsai.Tests.Search.Fixtures;
using Xunit;

namespace Bonsai.Tests.Search
namespace Bonsai.Tests.Search;

[Collection("Search tests")]
public class SearchTests
{
[Collection("Search tests")]
public class SearchTests
public SearchTests(SearchEngineFixture ctx)
{
public SearchTests(SearchEngineFixture ctx)
{
_ctx = ctx;
}
_ctx = ctx;
}

private readonly SearchEngineFixture _ctx;
private readonly SearchEngineFixture _ctx;

[Fact]
public async Task Search_finds_exact_matches()
{
var query = "Иванов Иван Петрович";
[Fact]
public async Task Search_finds_exact_matches()
{
var query = "Иванов Иван Петрович";

var result = await _ctx.Search.SearchAsync(query);
var result = await _ctx.Search.SearchAsync(query);

Assert.Contains(result, x => x.Key == "Иванов_Иван_Петрович");
}
Assert.Contains(result, x => x.Key == "Иванов_Иван_Петрович");
}

[Fact]
public async Task Search_considers_page_body()
{
var query = "Авиастроения";
[Fact]
public async Task Search_considers_page_body()
{
var query = "Авиастроения";

var result = await _ctx.Search.SearchAsync(query);
var result = await _ctx.Search.SearchAsync(query);

Assert.Contains(result, x => x.Key == "Иванов_Иван_Петрович");
}
Assert.Contains(result, x => x.Key == "Иванов_Иван_Петрович");
}

[Theory]
[InlineData("Иванов", "Иванов.*")]
[InlineData("Иванова", "Иванов.*")]
public async Task Search_highlights_matches_in_title(string query, string regex)
{
var result = await _ctx.Search.SearchAsync(query);
[Theory]
[InlineData("Иванов", "Иванов.*")]
[InlineData("Иванова", "Иванов.*")]
public async Task Search_highlights_matches_in_title(string query, string regex)
{
var result = await _ctx.Search.SearchAsync(query);

Assert.NotEmpty(result);
Assert.All(result, x => Assert.True(Regex.IsMatch(x.HighlightedTitle, "<b>" + regex + "</b>", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)));
}
Assert.NotEmpty(result);
Assert.All(result, x => Assert.True(Regex.IsMatch(x.HighlightedTitle, "<b>" + regex + "</b>", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)));
}

[Theory]
[InlineData("Иванов", "Иванов.*")]
[InlineData("Иванова", "Иванов.*")]
public async Task Search_highlights_matches_in_body(string query, string regex)
{
var result = await _ctx.Search.SearchAsync(query);
[Theory]
[InlineData("Иванов", "Иванов.*")]
[InlineData("Иванова", "Иванов.*")]
public async Task Search_highlights_matches_in_body(string query, string regex)
{
var result = await _ctx.Search.SearchAsync(query);

Assert.NotEmpty(result);
Assert.Contains(result, x => Regex.IsMatch(x.HighlightedDescription, "<b>" + regex + "</b>", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase));
}
Assert.NotEmpty(result);
Assert.Contains(result, x => Regex.IsMatch(x.HighlightedDescription, "<b>" + regex + "</b>", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase));
}

[Fact]
public async Task Search_considers_pagination()
{
var query = "Иванов";
[Fact]
public async Task Search_considers_pagination()
{
var query = "Иванов";

var p1 = await _ctx.Search.SearchAsync(query, 0);
var p2 = await _ctx.Search.SearchAsync(query, 1);
var p1 = await _ctx.Search.SearchAsync(query, 0);
var p2 = await _ctx.Search.SearchAsync(query, 1);

Assert.Empty(p1.Select(x => x.Key).Intersect(p2.Select(x => x.Key)));
}
Assert.Empty(p1.Select(x => x.Key).Intersect(p2.Select(x => x.Key)));
}
}
}
Loading

0 comments on commit fa452d8

Please sign in to comment.