/
DynamicExceptFixture.cs
123 lines (98 loc) · 3.97 KB
/
DynamicExceptFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Linq;
using DynamicData.Tests.Domain;
using FluentAssertions;
using Xunit;
namespace DynamicData.Tests.Cache
{
public class DynamicExceptFixture: IDisposable
{
private readonly RandomPersonGenerator _generator = new RandomPersonGenerator();
private readonly ISourceCache<Person, string> _source1;
private readonly ISourceCache<Person, string> _source2;
private readonly ISourceCache<Person, string> _source3;
private readonly ISourceList<IObservable<IChangeSet<Person, string>>> _source;
private readonly ChangeSetAggregator<Person, string> _results;
public DynamicExceptFixture()
{
_source1 = new SourceCache<Person, string>(p => p.Name);
_source2 = new SourceCache<Person, string>(p => p.Name);
_source3 = new SourceCache<Person, string>(p => p.Name);
_source = new SourceList<IObservable<IChangeSet<Person, string>>>();
_results = _source.Except().AsAggregator();
}
public void Dispose()
{
_source1.Dispose();
_source2.Dispose();
_source3.Dispose();
_source.Dispose();
_results.Dispose();
}
[Fact]
public void UpdatingOneSourceOnlyProducesResult()
{
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
var person = new Person("Adult1", 50);
_source1.AddOrUpdate(person);
_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Data.Count.Should().Be(1, "Should be 1 item in the cache");
}
[Fact]
public void DoNotIncludeExceptListItems()
{
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
var person = new Person("Adult1", 50);
_source2.AddOrUpdate(person);
_source1.AddOrUpdate(person);
_results.Messages.Count.Should().Be(0, "Should have no updates");
_results.Data.Count.Should().Be(0, "Cache should have no items");
}
[Fact]
public void RemovedAnItemFromExceptThenIncludesTheItem()
{
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
var person = new Person("Adult1", 50);
_source2.AddOrUpdate(person);
_source1.AddOrUpdate(person);
_source2.Remove(person);
_results.Messages.Count.Should().Be(1, "Should be 2 updates");
_results.Data.Count.Should().Be(1, "Cache should have no items");
}
[Fact]
public void AddAndRemoveLists()
{
var items = _generator.Take(100).OrderBy(p => p.Name).ToArray();
_source1.AddOrUpdate(items);
_source2.AddOrUpdate(items.Take(10));
_source3.AddOrUpdate(items.Skip(90).Take(10));
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
_source.Add(_source3.Connect());
_results.Data.Count.Should().Be(80);
_results.Data.Items.Should().BeEquivalentTo(items.Skip(10).Take(80));
_source.RemoveAt(2);
_results.Data.Count.Should().Be(90);
_results.Data.Items.Should().BeEquivalentTo(items.Skip(10));
_source.RemoveAt(0);
_results.Data.Count.Should().Be(10);
_results.Data.Items.Should().BeEquivalentTo(items.Take(10));
}
[Fact]
public void RemoveAllLists()
{
var items = _generator.Take(100).ToArray();
_source1.AddOrUpdate(items.Take(10));
_source2.AddOrUpdate(items.Skip(10).Take(10));
_source3.AddOrUpdate(items.Skip(20));
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
_source.Add(_source3.Connect());
_source.Clear();
_results.Data.Count.Should().Be(0);
}
}
}