|
4 | 4 | using System.Diagnostics; |
5 | 5 | using System.Linq; |
6 | 6 | using System.Reactive.Linq; |
7 | | - |
| 7 | +using DynamicData.Binding; |
8 | 8 | using DynamicData.Tests.Domain; |
9 | 9 |
|
10 | 10 | using FluentAssertions; |
@@ -117,6 +117,57 @@ public void FlattenReadOnlyObservableCollection() |
117 | 117 | aggregator.Data.Lookup("Replacement").HasValue.Should().BeTrue(); |
118 | 118 | } |
119 | 119 |
|
| 120 | + [Fact] |
| 121 | + public void FlattenObservableCache() |
| 122 | + { |
| 123 | + var children = Enumerable.Range(1, 100).Select(i => new Person("Name" + i, i)).ToArray(); |
| 124 | + |
| 125 | + int childIndex = 0; |
| 126 | + var parents = Enumerable.Range(1, 50).Select( |
| 127 | + i => |
| 128 | + { |
| 129 | + var parent = new Parent( |
| 130 | + i, |
| 131 | + new[] |
| 132 | + { |
| 133 | + children[childIndex], |
| 134 | + children[childIndex + 1] |
| 135 | + }); |
| 136 | + |
| 137 | + childIndex += 2; |
| 138 | + return parent; |
| 139 | + }).ToArray(); |
| 140 | + |
| 141 | + using var source = new SourceCache<Parent, int>(x => x.Id); |
| 142 | + using var aggregator = source.Connect().TransformMany(p => p.ChildrenCache, c => c.Name).AsAggregator(); |
| 143 | + source.AddOrUpdate(parents); |
| 144 | + |
| 145 | + aggregator.Data.Count.Should().Be(100); |
| 146 | + |
| 147 | + //add a child to an observable collection and check the new item is added |
| 148 | + parents[0].Children.Add(new Person("NewlyAddded", 100)); |
| 149 | + aggregator.Data.Count.Should().Be(101); |
| 150 | + |
| 151 | + ////remove first parent and check children have gone |
| 152 | + source.RemoveKey(1); |
| 153 | + aggregator.Data.Count.Should().Be(98); |
| 154 | + |
| 155 | + //check items can be cleared and then added back in |
| 156 | + var childrenInZero = parents[1].Children.ToArray(); |
| 157 | + parents[1].Children.Clear(); |
| 158 | + aggregator.Data.Count.Should().Be(96); |
| 159 | + parents[1].Children.AddRange(childrenInZero); |
| 160 | + aggregator.Data.Count.Should().Be(98); |
| 161 | + |
| 162 | + //replace produces an update |
| 163 | + var replacedChild = parents[1].Children[0]; |
| 164 | + parents[1].Children[0] = new Person("Replacement", 100); |
| 165 | + aggregator.Data.Count.Should().Be(98); |
| 166 | + |
| 167 | + aggregator.Data.Lookup(replacedChild.Key).HasValue.Should().BeFalse(); |
| 168 | + aggregator.Data.Lookup("Replacement").HasValue.Should().BeTrue(); |
| 169 | + } |
| 170 | + |
120 | 171 | [Fact] |
121 | 172 | public void ObservableCollectionWithoutInitialData() |
122 | 173 | { |
@@ -183,25 +234,47 @@ public void ReadOnlyObservableCollectionWithoutInitialData() |
183 | 234 | collection.Count.Should().Be(2); |
184 | 235 | } |
185 | 236 |
|
| 237 | + [Fact] |
| 238 | + public void ObservableCacheWithoutInitialData() |
| 239 | + { |
| 240 | + using var parents = new SourceCache<Parent, int>(d => d.Id); |
| 241 | + var collection = parents.Connect().TransformMany(d => d.ChildrenCache, p => p.Name).AsObservableCache(); |
| 242 | + |
| 243 | + var parent = new Parent(1); |
| 244 | + parents.AddOrUpdate(parent); |
| 245 | + |
| 246 | + collection.Count.Should().Be(0); |
| 247 | + |
| 248 | + parent.Children.Add(new Person("child1", 1)); |
| 249 | + collection.Count.Should().Be(1); |
| 250 | + |
| 251 | + parent.Children.Add(new Person("child2", 2)); |
| 252 | + collection.Count.Should().Be(2); |
| 253 | + } |
| 254 | + |
186 | 255 | private class Parent |
187 | 256 | { |
188 | 257 | public Parent(int id, IEnumerable<Person> children) |
189 | 258 | { |
190 | 259 | Id = id; |
191 | 260 | Children = new ObservableCollection<Person>(children); |
192 | 261 | ChildrenReadonly = new ReadOnlyObservableCollection<Person>(Children); |
| 262 | + ChildrenCache = Children.ToObservableChangeSet(x => x.Name).AsObservableCache(); |
193 | 263 | } |
194 | 264 |
|
195 | 265 | public Parent(int id) |
196 | 266 | { |
197 | 267 | Id = id; |
198 | 268 | Children = new ObservableCollection<Person>(); |
199 | 269 | ChildrenReadonly = new ReadOnlyObservableCollection<Person>(Children); |
| 270 | + ChildrenCache = Children.ToObservableChangeSet(x => x.Name).AsObservableCache(); |
200 | 271 | } |
201 | 272 |
|
202 | 273 | public ObservableCollection<Person> Children { get; } |
203 | 274 |
|
204 | | - public ReadOnlyObservableCollection<Person> ChildrenReadonly { get; } |
| 275 | + public ReadOnlyObservableCollection<Person> ChildrenReadonly { get; } |
| 276 | + |
| 277 | + public IObservableCache<Person, string> ChildrenCache { get; } |
205 | 278 |
|
206 | 279 | public int Id { get; } |
207 | 280 | } |
|
0 commit comments