Skip to content

Commit fa1815f

Browse files
committed
Fixed issues across the Join operators, regarding incomplete or missing support for re-grouping, when foreign key values change.
1 parent 77f9539 commit fa1815f

File tree

10 files changed

+732
-114
lines changed

10 files changed

+732
-114
lines changed

src/DynamicData.Tests/Cache/InnerJoinFixture.cs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,59 @@ public void Dispose()
103103
_result?.Dispose();
104104
}
105105

106+
[Fact]
107+
public void RefreshRightKey()
108+
{
109+
_left.Edit(
110+
innerCache =>
111+
{
112+
innerCache.AddOrUpdate(new Device("Device1"));
113+
innerCache.AddOrUpdate(new Device("Device2"));
114+
innerCache.AddOrUpdate(new Device("Device3"));
115+
});
116+
117+
_right.Edit(
118+
innerCache =>
119+
{
120+
innerCache.AddOrUpdate(new DeviceMetaData(1,"Device1"));
121+
innerCache.AddOrUpdate(new DeviceMetaData(2,"Device2"));
122+
innerCache.AddOrUpdate(new DeviceMetaData(3,"Device3"));
123+
});
124+
125+
var refreshItem = _right.Lookup(2).Value;
126+
127+
128+
// Change pairing
129+
refreshItem.Name = "Device3";
130+
_right.Refresh(refreshItem);
131+
132+
_result.Data.Count.Should().Be(3);
133+
_result.Data.Keys.Should().Contain(("Device3", 2));
134+
135+
136+
// Remove pairing
137+
refreshItem.Name = "Device4";
138+
_right.Refresh(refreshItem);
139+
140+
_result.Data.Count.Should().Be(2);
141+
_result.Data.Keys.Should().NotContain(pair => pair.rightKey == 2);
142+
143+
144+
// Restore pairing
145+
refreshItem.Name = "Device2";
146+
_right.Refresh(refreshItem);
147+
148+
_result.Data.Count.Should().Be(3);
149+
_result.Data.Keys.Should().Contain(("Device2", 2));
150+
151+
152+
// No change
153+
_right.Refresh(refreshItem);
154+
155+
_result.Data.Count.Should().Be(3);
156+
_result.Data.Keys.Should().Contain(("Device2", 2));
157+
}
158+
106159
[Fact]
107160
public void RemoveVarious()
108161
{
@@ -159,6 +212,53 @@ public void UpdateRight()
159212
_result.Data.Count.Should().Be(3);
160213
}
161214

215+
[Fact]
216+
public void UpdateRightKey()
217+
{
218+
_left.Edit(
219+
innerCache =>
220+
{
221+
innerCache.AddOrUpdate(new Device("Device1"));
222+
innerCache.AddOrUpdate(new Device("Device2"));
223+
innerCache.AddOrUpdate(new Device("Device3"));
224+
});
225+
226+
_right.Edit(
227+
innerCache =>
228+
{
229+
innerCache.AddOrUpdate(new DeviceMetaData(1,"Device1"));
230+
innerCache.AddOrUpdate(new DeviceMetaData(2,"Device2"));
231+
innerCache.AddOrUpdate(new DeviceMetaData(3,"Device3"));
232+
});
233+
234+
235+
// Change pairing
236+
_right.AddOrUpdate(new DeviceMetaData(2,"Device3"));
237+
238+
_result.Data.Count.Should().Be(3);
239+
_result.Data.Keys.Should().Contain(("Device3", 2));
240+
241+
242+
// Remove pairing
243+
_right.AddOrUpdate(new DeviceMetaData(2,"Device4"));
244+
245+
_result.Data.Count.Should().Be(2);
246+
_result.Data.Keys.Should().NotContain(pair => pair.rightKey == 2);
247+
248+
249+
// Restore pairing
250+
_right.AddOrUpdate(new DeviceMetaData(2,"Device2"));
251+
252+
_result.Data.Count.Should().Be(3);
253+
_result.Data.Keys.Should().Contain(("Device2", 2));
254+
255+
256+
// No change
257+
_right.AddOrUpdate(new DeviceMetaData(2,"Device2"));
258+
259+
_result.Data.Count.Should().Be(3);
260+
_result.Data.Keys.Should().Contain(("Device2", 2));
261+
}
162262

163263
[Fact]
164264
public void MultipleRight()
@@ -284,7 +384,7 @@ public class DeviceMetaData(int key, string name, bool isAutoConnect = false) :
284384
{
285385
public bool IsAutoConnect { get; } = isAutoConnect;
286386
public int Key { get; } = key;
287-
public string Name { get; } = name;
387+
public string Name { get; set; } = name;
288388

289389
public override string ToString() => $"Key: {Key}. Metadata: {Name}. IsAutoConnect = {IsAutoConnect}";
290390

src/DynamicData.Tests/Cache/InnerJoinManyFixture.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,52 @@ public void Dispose()
7474
_result.Dispose();
7575
}
7676

77+
[Fact]
78+
public void RefreshRightKey()
79+
{
80+
_people.Edit(
81+
innerCache =>
82+
{
83+
innerCache.AddOrUpdate(new Person() { Name = "Person #1", ParentName = string.Empty } );
84+
innerCache.AddOrUpdate(new Person() { Name = "Person #2", ParentName = "Person #1" } );
85+
innerCache.AddOrUpdate(new Person() { Name = "Person #3", ParentName = "Person #2" } );
86+
});
87+
88+
var refreshPerson = _people.Lookup("Person #2").Value;
89+
90+
91+
// Change pairing
92+
refreshPerson.ParentName = "Person #3";
93+
_people.Refresh(refreshPerson);
94+
95+
_result.Data.Count.Should().Be(2);
96+
_result.Data.Items.SelectMany(family => family.Children.Select(child => (family.Parent.Name, child.Name))).Should().NotContain(("Person #1", "Person #2"));
97+
_result.Data.Items.SelectMany(family => family.Children.Select(child => (family.Parent.Name, child.Name))).Should().Contain(("Person #3", "Person #2"));
98+
99+
100+
// Remove pairing
101+
refreshPerson.ParentName = "Person #4";
102+
_people.Refresh(refreshPerson);
103+
104+
_result.Data.Count.Should().Be(1);
105+
_result.Data.Items.SelectMany(family => family.Children.Select(child => (parentName: family.Parent.Name, chilldName: child.Name))).Should().NotContain(pair => pair.chilldName == "Person #2");
106+
107+
108+
// Restore pairing
109+
refreshPerson.ParentName = "Person #1";
110+
_people.Refresh(refreshPerson);
111+
112+
_result.Data.Count.Should().Be(2);
113+
_result.Data.Items.SelectMany(family => family.Children.Select(child => (family.Parent.Name, child.Name))).Should().Contain(("Person #1", "Person #2"));
114+
115+
116+
// No change
117+
_people.Refresh(refreshPerson);
118+
119+
_result.Data.Count.Should().Be(2);
120+
_result.Data.Items.SelectMany(family => family.Children.Select(child => (family.Parent.Name, child.Name))).Should().Contain(("Person #1", "Person #2"));
121+
}
122+
77123
[Fact]
78124
public void RemoveChild()
79125
{
@@ -136,6 +182,47 @@ public void UpdateParent()
136182
AssertDataIsCorrectlyFormed(updatedPeople);
137183
}
138184

185+
[Fact]
186+
public void UpdateRightKey()
187+
{
188+
_people.Edit(
189+
innerCache =>
190+
{
191+
innerCache.AddOrUpdate(new Person() { Name = "Person #1", ParentName = string.Empty } );
192+
innerCache.AddOrUpdate(new Person() { Name = "Person #2", ParentName = "Person #1" } );
193+
innerCache.AddOrUpdate(new Person() { Name = "Person #3", ParentName = "Person #2" } );
194+
});
195+
196+
197+
// Change pairing
198+
_people.AddOrUpdate(new Person() { Name = "Person #2", ParentName = "Person #3" });
199+
200+
_result.Data.Count.Should().Be(2);
201+
_result.Data.Items.SelectMany(family => family.Children.Select(child => (family.Parent.Name, child.Name))).Should().NotContain(("Person #1", "Person #2"));
202+
_result.Data.Items.SelectMany(family => family.Children.Select(child => (family.Parent.Name, child.Name))).Should().Contain(("Person #3", "Person #2"));
203+
204+
205+
// Remove pairing
206+
_people.AddOrUpdate(new Person() { Name = "Person #2", ParentName = "Person #4" });
207+
208+
_result.Data.Count.Should().Be(1);
209+
_result.Data.Items.SelectMany(family => family.Children.Select(child => (parentName: family.Parent.Name, chilldName: child.Name))).Should().NotContain(pair => pair.chilldName == "Person #2");
210+
211+
212+
// Restore pairing
213+
_people.AddOrUpdate(new Person() { Name = "Person #2", ParentName = "Person #1" });
214+
215+
_result.Data.Count.Should().Be(2);
216+
_result.Data.Items.SelectMany(family => family.Children.Select(child => (family.Parent.Name, child.Name))).Should().Contain(("Person #1", "Person #2"));
217+
218+
219+
// No change
220+
_people.AddOrUpdate(new Person() { Name = "Person #2", ParentName = "Person #1" });
221+
222+
_result.Data.Count.Should().Be(2);
223+
_result.Data.Items.SelectMany(family => family.Children.Select(child => (family.Parent.Name, child.Name))).Should().Contain(("Person #1", "Person #2"));
224+
}
225+
139226
private void AssertDataIsCorrectlyFormed(Person[] allPeople, params string[] missingParents)
140227
{
141228
var grouped = allPeople.GroupBy(p => p.ParentName).Where(p => p.Any() && !missingParents.Contains(p.Key)).AsArray();

0 commit comments

Comments
 (0)