@@ -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