@@ -77,6 +77,79 @@ protected override void OnTearDown()
77
77
Assert . AreEqual ( 0 , statistics . SecondLevelCacheMissCount , "Second level cache miss count" ) ;
78
78
}
79
79
80
+ private async Task OneToOneUpdateTestAsync < TPerson , TDetails > ( CancellationToken cancellationToken = default ( CancellationToken ) ) where TPerson : Person , new ( ) where TDetails : Details , new ( )
81
+ {
82
+ List < object > ids = await ( this . CreatePersonAndDetailsAsync < TPerson , TDetails > ( cancellationToken ) ) ;
83
+
84
+ IStatistics statistics = Sfi . Statistics ;
85
+
86
+ // Clear the second level cache and the statistics
87
+ await ( Sfi . EvictEntityAsync ( typeof ( TPerson ) . FullName , cancellationToken ) ) ;
88
+ await ( Sfi . EvictEntityAsync ( typeof ( TDetails ) . FullName , cancellationToken ) ) ;
89
+ await ( Sfi . EvictQueriesAsync ( cancellationToken ) ) ;
90
+
91
+ statistics . Clear ( ) ;
92
+
93
+ // Fill the empty caches with data.
94
+ await ( this . FetchPeopleByIdAsync < TPerson > ( ids , cancellationToken ) ) ;
95
+
96
+ // Verify that no data was retrieved from the cache.
97
+ Assert . AreEqual ( 0 , statistics . SecondLevelCacheHitCount , "Second level cache hit count" ) ;
98
+ statistics . Clear ( ) ;
99
+
100
+ int personId = await ( DeleteDetailsFromFirstPersonAsync < TPerson > ( cancellationToken ) ) ;
101
+
102
+ // Verify that the cache was updated
103
+ Assert . AreEqual ( 1 , statistics . SecondLevelCachePutCount , "Second level cache put count" ) ;
104
+ statistics . Clear ( ) ;
105
+
106
+ // Verify that the Person was updated in the cache
107
+ using ( ISession s = Sfi . OpenSession ( ) )
108
+ using ( ITransaction tx = s . BeginTransaction ( ) )
109
+ {
110
+ TPerson person = await ( s . GetAsync < TPerson > ( personId , cancellationToken ) ) ;
111
+
112
+ Assert . IsNull ( person . Details ) ;
113
+ }
114
+
115
+ Assert . AreEqual ( 0 , statistics . SecondLevelCacheMissCount , "Second level cache miss count" ) ;
116
+ statistics . Clear ( ) ;
117
+
118
+ // Verify that the Details was removed from the cache and deleted.
119
+ using ( ISession s = Sfi . OpenSession ( ) )
120
+ using ( ITransaction tx = s . BeginTransaction ( ) )
121
+ {
122
+ TDetails details = await ( s . GetAsync < TDetails > ( personId , cancellationToken ) ) ;
123
+
124
+ Assert . Null ( details ) ;
125
+ }
126
+
127
+ Assert . AreEqual ( 0 , statistics . SecondLevelCacheHitCount , "Second level cache hit count" ) ;
128
+ }
129
+
130
+ private async Task < int > DeleteDetailsFromFirstPersonAsync < TPerson > ( CancellationToken cancellationToken = default ( CancellationToken ) ) where TPerson : Person
131
+ {
132
+ using ( ISession s = Sfi . OpenSession ( ) )
133
+ using ( ITransaction tx = s . BeginTransaction ( ) )
134
+ {
135
+ // Get the first person with details.
136
+ Person person = await ( s . QueryOver < TPerson > ( )
137
+ . Where ( p => p . Details != null )
138
+ . Take ( 1 )
139
+ . SingleOrDefaultAsync ( cancellationToken ) ) ;
140
+
141
+ Assert . NotNull ( person ) ;
142
+ Assert . NotNull ( person . Details ) ;
143
+
144
+ await ( s . SaveOrUpdateAsync ( person , cancellationToken ) ) ;
145
+ person . Details = null ;
146
+
147
+ await ( tx . CommitAsync ( cancellationToken ) ) ;
148
+
149
+ return person . Id ;
150
+ }
151
+ }
152
+
80
153
private async Task < List < object > > CreatePersonAndDetailsAsync < TPerson , TDetails > ( CancellationToken cancellationToken = default ( CancellationToken ) ) where TPerson : Person , new ( ) where TDetails : Details , new ( )
81
154
{
82
155
List < object > ids = new List < object > ( ) ;
@@ -137,5 +210,17 @@ public async Task OneToOneCacheFetchByRefAsync()
137
210
{
138
211
await ( OneToOneFetchTestAsync < PersonByRef , DetailsByRef > ( ) ) ;
139
212
}
213
+
214
+ [ Test ]
215
+ public async Task OneToOneCacheUpdateByForeignKeyAsync ( )
216
+ {
217
+ await ( OneToOneUpdateTestAsync < PersonByFK , DetailsByFK > ( ) ) ;
218
+ }
219
+
220
+ [ Test ]
221
+ public async Task OneToOneCacheUpdateByRefAsync ( )
222
+ {
223
+ await ( OneToOneUpdateTestAsync < PersonByRef , DetailsByRef > ( ) ) ;
224
+ }
140
225
}
141
226
}
0 commit comments