Skip to content

Commit f990393

Browse files
committed
Fixed flaky tests due to duplicate random data being generated
1 parent f518e51 commit f990393

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

tests/KnowledgeBaseServer.Tests/Data/Topic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static Faker<Topic> Faker() =>
1818
new Faker<Topic>()
1919
.RuleFor(x => x.Id, Guid.CreateVersion7)
2020
.RuleFor(x => x.Created, f => f.Date.PastOffset())
21-
.RuleFor(x => x.Name, f => f.Lorem.Sentence(3));
21+
.RuleFor(x => x.Name, f => f.Lorem.Sentence());
2222
}
2323

2424
public static partial class DbConnectionExtensions
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Linq;
5+
using Bogus;
6+
7+
namespace KnowledgeBaseServer.Tests;
8+
9+
public static class FakerExtensions
10+
{
11+
public static IEnumerable<T> MakeUnique<T>(this Faker faker, Func<Faker, T> generator)
12+
{
13+
return CreateItems().Distinct();
14+
15+
[SuppressMessage(
16+
"Blocker Bug",
17+
"S2190:Loops and recursions should not be infinite",
18+
Justification = "The method is intended to generate a potentially infinite sequence of items..."
19+
)]
20+
IEnumerable<T> CreateItems()
21+
{
22+
while (true)
23+
{
24+
yield return generator(faker);
25+
}
26+
// ReSharper disable once IteratorNeverReturns
27+
}
28+
}
29+
30+
public static IEnumerable<TItem> MakeUnique<TItem, TProperty>(
31+
this Faker faker,
32+
Func<Faker, TItem> generator,
33+
Func<TItem, TProperty> uniquePropertySelector
34+
)
35+
where TProperty : notnull
36+
{
37+
return CreateItems().DistinctBy(uniquePropertySelector);
38+
39+
[SuppressMessage(
40+
"Blocker Bug",
41+
"S2190:Loops and recursions should not be infinite",
42+
Justification = "The method is intended to generate a potentially infinite sequence of items."
43+
)]
44+
IEnumerable<TItem> CreateItems()
45+
{
46+
while (true)
47+
{
48+
yield return generator(faker);
49+
}
50+
// ReSharper disable once IteratorNeverReturns
51+
}
52+
}
53+
}

tests/KnowledgeBaseServer.Tests/Tools/SearchMemoryToolTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void ShouldReturnMemoryMatchingPhrases()
8383
public void ShouldFilterMatchingMemoriesByTopic_WhenTopicsAreProvided()
8484
{
8585
// arrange
86-
var topics = _faker.Lorem.Words();
86+
var topics = _faker.MakeUnique(f => f.Lorem.Word()).Take(3).ToArray();
8787
var searchPhrase = _faker.Lorem.Word();
8888

8989
foreach (var topic in topics)
@@ -266,7 +266,7 @@ public void ShouldReturnOnlyNonOutdatedMemories_WhenExcludingOutdated()
266266
public void ShouldFilterResults_WhenFilteringByTopicAndExcludingOutdated()
267267
{
268268
// arrange
269-
var topics = _faker.Lorem.Words();
269+
var topics = _faker.MakeUnique(f => f.Lorem.Word()).Take(3).ToArray();
270270
var searchPhrase = _faker.Lorem.Word();
271271
var memoryIdsByTopic = new Dictionary<string, List<Guid>>();
272272

0 commit comments

Comments
 (0)