Skip to content

Commit

Permalink
Fix StartWith tests
Browse files Browse the repository at this point in the history
Skip all performance tests in unit and integration tests
Update performance runner
  • Loading branch information
ninjanye committed Jul 4, 2024
1 parent 5beb478 commit e089112
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Description>Ninjanye.SearchExtensions performance testing</Description>
<TargetFrameworks>net8.0;net5.0;netcoreapp3.1</TargetFrameworks>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion NinjaNye.SearchExtensions.Performance/StringSearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace NinjaNye.SearchExtensions.Performance
[BenchmarkCategory("StringSearch")]
public class StringSearchTests
{
[Params(10000)]
[Params(1000)]
public static int WordsToSearch;
private static List<string> _data;
private static string[] _searchTerms;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public void Containing_CompareAgainstAnotherProperty_ResultDoesNotContainIncorre
}

[Fact]
public async Task Containing_SearchTwoProperties_ReturnsRecordWithMatchedDataInSecondProperty()
public void Containing_SearchTwoProperties_ReturnsRecordWithMatchedDataInSecondProperty()
{
//Arrange
var expected = _context.TestModels.FirstOrDefault(x => x.Id == new Guid("2F75BE28-CEC8-46D8-852E-E6DAE5C8F0A3"));

//Act
var result = await _context.TestModels.Search(x => x.StringOne, x => x.StringTwo)
var result = _context.TestModels.Search(x => x.StringOne, x => x.StringTwo)
.Containing(x => x.StringThree)
.ToListAsync();
.ToList();

//Assert
Assert.Contains(expected, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
namespace NinjaNye.SearchExtensions.Tests.Integration.Fluent.SearchTests
{
[Collection("Database tests")]
public class StartsWithTests
public class StartsWithTests(DatabaseIntegrationTests @base)
{
private readonly TestContext _context;

public StartsWithTests(DatabaseIntegrationTests @base)
{
_context = @base.Context;
}
private readonly TestContext _context = @base.Context;

[Fact]
public void StartsWith_SearchStartsWith_DoesNotThrowAnException()
Expand Down Expand Up @@ -104,8 +99,8 @@ public void StartsWith_SearchPropertyMatchingWholeWord_MatchesWholeWordsOnly()
//Act
var result = _context.TestModels.Search(x => x.StringOne)
.Matching(SearchType.WholeWords)
.StartsWith(x => x.StringThree + " ");

.StartsWith(x => x.StringThree);
//Assert
var guids = result.Select(x => x.Id).ToList();
Assert.DoesNotContain(guids, x => x == new Guid("A8AD8A4F-853B-417A-9C0C-0A2802560B8C"));
Expand Down
1 change: 1 addition & 0 deletions NinjaNye.SearchExtensions.Tests.Integration/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
// optionsBuilder.UseInMemoryDatabase($"SearchExtensions.Tests.Integration{Guid.NewGuid()}");
optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=SearchExtensions.Tests.Integration;Integrated Security=SSPI;TrustServerCertificate=True");
optionsBuilder.UseLoggerFactory(new LoggerFactory(new []{new DebugLoggerProvider()}));
optionsBuilder.LogTo(Console.WriteLine);
base.OnConfiguring(optionsBuilder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ public LevenshteinDistancePerformanceTests(ITestOutputHelper testOutputHelper)
_testOutputHelper = testOutputHelper;
}

#if DEBUG
[Theory(Skip = "Performance tests will likely fail in debug mode. Run in release mode")]
#else
[Theory]
#endif
[InlineData(6)]
[InlineData(7)]
public void ToLevenshteinDistance_CompareOneMillionStringsOfLengthX_ExecutesInLessThanOneSecond(int length)
Expand All @@ -42,11 +38,7 @@ public void ToLevenshteinDistance_CompareOneMillionStringsOfLengthX_ExecutesInLe
Assert.True(stopwatch.Elapsed.TotalMilliseconds < 1000);
}

#if DEBUG
[Fact(Skip = "Performance tests will likely fail in debug mode. Run in release mode")]
#else
[Fact]
#endif
public void PerformLevenshteinDistanceUsingExpressionTreeBuilder()
{
//Setup 1 million comparisons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ public ReverseSoundexPerformanceTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
#if DEBUG

[Fact(Skip = "Performance tests only to be run in Release mode")]
#else
[Fact]
#endif
public void ToReverseSoundex_OneMillionRecords_UnderOneSecond()
{
//Arrange
Expand All @@ -36,11 +33,7 @@ public void ToReverseSoundex_OneMillionRecords_UnderOneSecond()
Assert.True(stopwatch.Elapsed.TotalMilliseconds < 1000);
}

#if DEBUG
[Fact(Skip = "Performance tests only to be run in Release mode")]
#else
[Fact]
#endif
public void ReverseSoundex_ReverseWordSoundexVsToReverseSoundex_ToReverseSoundexIsQuicker()
{
//Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ namespace NinjaNye.SearchExtensions.Tests.SoundexTests
{
public class ToSoundexPerformanceTests : BuildStringTestsBase
{
private readonly ITestOutputHelper _testOutputHelper;
public ToSoundexPerformanceTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
#if DEBUG
private readonly ITestOutputHelper _testOutputHelper;
public ToSoundexPerformanceTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
[Fact(Skip = "Performance tests only to be run in Release mode")]
#else
[Fact]
#endif
public void ToSoundex_OneMillionRecords_UnderOneSecond()
{
//Arrange
Expand All @@ -36,11 +33,7 @@ public void ToSoundex_OneMillionRecords_UnderOneSecond()
Assert.True(stopwatch.Elapsed.TotalMilliseconds < 1000);
}

#if DEBUG
[Fact(Skip = "Performance tests only to be run in Release mode")]
#else
[Fact]
#endif
public void SearchSoundex_OneMillionWordsComparedToOneWord_UnderOneSecond()
{
//Arrange
Expand All @@ -60,11 +53,7 @@ public void SearchSoundex_OneMillionWordsComparedToOneWord_UnderOneSecond()
Assert.True(stopwatch.Elapsed.TotalMilliseconds < 1000);
}

#if DEBUG
[Fact(Skip = "Performance tests only to be run in Release mode")]
#else
[Fact]
#endif
public void SearchSoundex_OneMillionWordsComparedToTwoWords_UnderOneSecond()
{
//Arrange
Expand All @@ -84,11 +73,7 @@ public void SearchSoundex_OneMillionWordsComparedToTwoWords_UnderOneSecond()
Assert.True(stopwatch.Elapsed.TotalMilliseconds < 1000);
}

#if DEBUG
[Fact(Skip = "Performance tests only to be run in Release mode")]
#else
[Fact]
#endif
public void SearchSoundex_OneMillionWordsComparedToTenWords_UnderOneSecond()
{
//Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ private static Expression Build<T>(Expression<Func<T, string>> stringProperty, E
}

var nullSafeStringProperty = BuildNullSafeExpression(stringProperty);
var result = Expression.Call(nullSafeStringProperty.Body, ExpressionMethods.StartsWithMethod, paddedTerm);
var indexOfExpression = Expression.Call(nullSafeStringProperty.Body, ExpressionMethods.IndexOfMethod, paddedTerm);
var result = Expression.Equal(indexOfExpression, ExpressionMethods.ZeroConstantExpression);
if (searchType == SearchType.WholeWords)
{
var isEqualExpression = QueryableEqualsExpressionBuilder.Build(nullSafeStringProperty, propertyToSearchFor);
return ExpressionHelper.JoinOrExpression(result, isEqualExpression);
}

return result;
}

Expand Down

0 comments on commit e089112

Please sign in to comment.