/
DynamicAndFixture.cs
138 lines (112 loc) · 4.56 KB
/
DynamicAndFixture.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Linq;
using DynamicData.Tests.Domain;
using FluentAssertions;
using Xunit;
namespace DynamicData.Tests.Cache
{
public class DynamicAndFixture: 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 DynamicAndFixture()
{
_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.And().AsAggregator();
}
public void Dispose()
{
_source1.Dispose();
_source2.Dispose();
_source3.Dispose();
_source.Dispose();
_results.Dispose();
}
[Fact]
public void UpdatingOneSourceOnlyProducesNoResults()
{
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
var person = new Person("Adult1", 50);
_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 UpdatingBothProducesResults()
{
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
var person = new Person("Adult1", 50);
_source1.AddOrUpdate(person);
_source2.AddOrUpdate(person);
_results.Messages.Count.Should().Be(1, "Should have no updates");
_results.Data.Count.Should().Be(1, "Cache should have no items");
_results.Data.Items.First().Should().Be(person, "Should be same person");
}
[Fact]
public void RemovingFromOneRemovesFromResult()
{
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
var person = new Person("Adult1", 50);
_source1.AddOrUpdate(person);
_source2.AddOrUpdate(person);
_source2.Remove(person);
_results.Messages.Count.Should().Be(2, "Should be 2 updates");
_results.Data.Count.Should().Be(0, "Cache should have no items");
}
[Fact]
public void UpdatingOneProducesOnlyOneUpdate()
{
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
var person = new Person("Adult1", 50);
_source1.AddOrUpdate(person);
_source2.AddOrUpdate(person);
var personUpdated = new Person("Adult1", 51);
_source2.AddOrUpdate(personUpdated);
_results.Messages.Count.Should().Be(2, "Should be 2 updates");
_results.Data.Count.Should().Be(1, "Cache should have no items");
_results.Data.Items.First().Should().Be(personUpdated, "Should be updated person");
}
[Fact]
public void AddAndRemoveLists()
{
var items = _generator.Take(100).ToArray();
_source1.AddOrUpdate(items.Take(20));
_source2.AddOrUpdate(items.Skip(10).Take(10));
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
_results.Data.Count.Should().Be(10);
_results.Data.Items.Should().BeEquivalentTo(items.Skip(10).Take(10));
_source.Add(_source3.Connect());
_results.Data.Count.Should().Be(0);
_source.RemoveAt(2);
_results.Data.Count.Should().Be(10);
}
[Fact]
public void RemoveAllLists()
{
var items = _generator.Take(100).ToArray();
_source1.AddOrUpdate(items.Take(10));
_source2.AddOrUpdate(items.Skip(20).Take(10));
_source3.AddOrUpdate(items.Skip(30));
_source.Add(_source1.Connect());
_source.Add(_source2.Connect());
_source.Add(_source3.Connect());
_source.RemoveAt(2);
_source.RemoveAt(1);
_source.RemoveAt(0);
//s _source.Clear();
_results.Data.Count.Should().Be(0);
}
}
}