Skip to content

Commit e99ff26

Browse files
dwcullopJakenVeina
authored andcommitted
Make filtering regression assertions reject stale entries
Backport of #1186 (issue #1184) to release-wip/9.5. The previous Assert.Equivalent calls passed even when an item that stopped matching was retained in the bound collection, so the tests could not detect a stale entry. Replace them with strict FluentAssertions checks, drop the .RemoveKey() operator that was not under test, and move the list-bound case into the List fixture where the list Filter overload it exercises lives. Test-only. No production or public API change.
1 parent b312ffd commit e99ff26

2 files changed

Lines changed: 96 additions & 35 deletions

File tree

‎src/DynamicData.Tests/Cache/FilterFixture.Static.cs‎

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@
44
using System.Reactive.Linq;
55
using System.Reactive.Subjects;
66

7+
using Randomizer = Bogus.Randomizer;
78
using FluentAssertions;
89
using Xunit;
910

1011
using DynamicData.Tests.Domain;
1112
using DynamicData.Tests.Utilities;
1213

14+
using Person = DynamicData.Tests.Domain.Person;
15+
1316
namespace DynamicData.Tests.Cache;
1417

1518
public static partial class FilterFixture
1619
{
1720
public sealed class Static
1821
: Base
1922
{
23+
private const int AutoRefreshSeed = 0x1165;
24+
2025
[Fact]
2126
public void FilterIsNull_ThrowsException()
2227
=> FluentActions.Invoking(static () => ObservableCacheEx.Filter(
@@ -89,54 +94,54 @@ protected override IObservable<IChangeSet<Item, int>> BuildUut(
8994
=> source.Filter(
9095
filter: predicate,
9196
suppressEmptyChangeSets: suppressEmptyChangeSets);
97+
98+
/// <summary>
99+
/// Cache filtering must bind exactly the matching items, so an item that stops matching
100+
/// is removed rather than retained as a stale extra entry.
101+
/// </summary>
92102
[Fact]
93-
public void AutoRefreshRemoveKeyFilterUpdate_CollectionUpdated()
103+
public void AutoRefreshFilterUpdate_CollectionUpdated()
94104
{
95-
RandomPersonGenerator generator = new();
96-
using var source = new SourceCache<Person, string>(p => p.Key);
97-
var people = generator.Take(100).ToArray();
98-
var average = people.Average(x => x.Age);
99-
ReadOnlyObservableCollection<Person> collection;
105+
// Setup: every generated item starts below a derived threshold, so all of them match initially.
106+
var randomizer = new Randomizer(AutoRefreshSeed);
107+
using var source = new TestSourceCache<Person, string>(static person => person.Key);
108+
var people = Fakers.Person.Clone()
109+
.UseSeed(randomizer.Int())
110+
.Generate(randomizer.Int(3, 8))
111+
.ToArray();
112+
var exclusiveAge = people.Max(static person => person.Age) + 1;
113+
114+
115+
// UUT Initialization
100116
using var subscription = source.Connect()
101-
.AutoRefresh(x => x.Age)
102-
.RemoveKey()
103-
.Filter(x => x.Age < average)
104-
.Bind(out collection)
117+
.AutoRefresh(static person => person.Age)
118+
.Filter(person => person.Age < exclusiveAge)
119+
.ValidateSynchronization()
120+
.ValidateChangeSets(static person => person.Key)
121+
.Bind(out ReadOnlyObservableCollection<Person> collection)
105122
.Subscribe();
123+
124+
125+
// UUT Action
106126
source.AddOrUpdate(people);
107127

108-
Assert.Equivalent(people.Where(x => x.Age < average), collection);
128+
collection.Should().BeEquivalentTo(people, "every item matches the predicate");
109129

110-
foreach (var person in people)
111-
{
112-
person.Age = person.Age + 1;
113-
}
114-
Assert.Equivalent(people.Where(x => x.Age < average), collection);
115-
}
116130

117-
[Fact]
118-
public void AutoRefreshFilterRemoveKeyUpdate_CollectionUpdated()
119-
{
120-
RandomPersonGenerator generator = new();
121-
using var source = new SourceCache<Person, string>(p => p.Key);
122-
var people = generator.Take(100).ToArray();
123-
var average = people.Average(x => x.Age);
124-
ReadOnlyObservableCollection<Person> collection;
125-
using var subscription = source.Connect()
126-
.AutoRefresh(x => x.Age)
127-
.Filter(x => x.Age < average)
128-
.RemoveKey()
129-
.Bind(out collection)
130-
.Subscribe();
131-
source.AddOrUpdate(people);
131+
// UUT Action
132+
people[0].Age = exclusiveAge;
133+
134+
collection.Should().BeEquivalentTo(people.Where(person => person.Age < exclusiveAge),
135+
"an item that stops matching must be removed, not retained as a stale extra entry");
132136

133-
Assert.Equivalent(people.Where(x => x.Age < average), collection);
134137

138+
// UUT Action
135139
foreach (var person in people)
136140
{
137-
person.Age = person.Age + 1;
141+
person.Age = exclusiveAge;
138142
}
139-
Assert.Equivalent(people.Where(x => x.Age < average), collection);
143+
144+
collection.Should().BeEmpty("no item matches the predicate any longer");
140145
}
141146
}
142147

‎src/DynamicData.Tests/List/FilterFixture.Static.cs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
using System;
2+
using System.Collections.ObjectModel;
23
using System.Linq;
34
using System.Reactive.Linq;
45
using System.Reactive.Subjects;
56

7+
using Randomizer = Bogus.Randomizer;
68
using FluentAssertions;
79
using Xunit;
810

11+
using DynamicData.Tests.Domain;
912
using DynamicData.Tests.Utilities;
1013

14+
using Person = DynamicData.Tests.Domain.Person;
15+
1116
namespace DynamicData.Tests.List;
1217

1318
public static partial class FilterFixture
1419
{
1520
public class Static
1621
{
22+
private const int AutoRefreshSeed = 0x1165;
23+
1724
[Fact]
1825
public void DuplicateItemsAreAdded_ItemsAreTrackedSeparately()
1926
{
@@ -634,5 +641,54 @@ public void SubscriptionIsDisposed_SubscriptionDisposalPropagates()
634641

635642
source.HasObservers.Should().BeFalse("subscription disposal should propagate upstream");
636643
}
644+
645+
/// <summary>
646+
/// List filtering must bind exactly the matching items, so an item that stops matching
647+
/// is removed rather than retained as a stale extra entry.
648+
/// </summary>
649+
[Fact]
650+
public void AutoRefreshFilterUpdate_CollectionUpdated()
651+
{
652+
// Setup: every generated item starts below a derived threshold, so all of them match initially.
653+
var randomizer = new Randomizer(AutoRefreshSeed);
654+
using var source = new TestSourceList<Person>();
655+
var people = Fakers.Person.Clone()
656+
.UseSeed(randomizer.Int())
657+
.Generate(randomizer.Int(3, 8))
658+
.ToArray();
659+
var exclusiveAge = people.Max(static person => person.Age) + 1;
660+
661+
662+
// UUT Initialization
663+
using var subscription = source.Connect()
664+
.AutoRefresh(static person => person.Age)
665+
.Filter(person => person.Age < exclusiveAge)
666+
.ValidateSynchronization()
667+
.ValidateChangeSets()
668+
.Bind(out ReadOnlyObservableCollection<Person> collection)
669+
.Subscribe();
670+
671+
672+
// UUT Action
673+
source.AddRange(people);
674+
675+
collection.Should().BeEquivalentTo(people, "every item matches the predicate");
676+
677+
678+
// UUT Action
679+
people[0].Age = exclusiveAge;
680+
681+
collection.Should().BeEquivalentTo(people.Where(person => person.Age < exclusiveAge),
682+
"an item that stops matching must be removed, not retained as a stale extra entry");
683+
684+
685+
// UUT Action
686+
foreach (var person in people)
687+
{
688+
person.Age = exclusiveAge;
689+
}
690+
691+
collection.Should().BeEmpty("no item matches the predicate any longer");
692+
}
637693
}
638694
}

0 commit comments

Comments
 (0)
⌂ 换网址