@@ -24,8 +24,9 @@ public class ManyToManyNotFoundIgnoreFixtureAsync : BugTestCase
24
24
private int id1 ;
25
25
private int id2 ;
26
26
private int _drive2Id ;
27
+ private int _withTemplateId ;
27
28
private readonly int _drivesCount ;
28
- private int DrivesCountWithOneIgnored => _drivesCount == 0 ? 0 : _drivesCount - 1 ;
29
+ private int ValidDrivesCount => _drivesCount == 0 ? 0 : _drivesCount - 1 ;
29
30
30
31
public ManyToManyNotFoundIgnoreFixtureAsync ( int drivesCount )
31
32
{
@@ -39,25 +40,26 @@ protected override void OnSetUp()
39
40
Drive dr3 = new Drive ( "Drive 3" ) ;
40
41
Device dv1 = new Device ( "Device 1" ) ;
41
42
Device dv2 = new Device ( "Device 2" ) ;
42
- using ( var s = Sfi . OpenSession ( ) )
43
- using ( var t = s . BeginTransaction ( ) )
44
- {
45
- s . Save ( dr1 ) ;
46
- _drive2Id = ( int ) s . Save ( dr2 ) ;
47
- s . Save ( dr3 ) ;
48
- AddDrive ( dv1 , dr2 ) ;
49
- AddDrive ( dv1 , dr1 ) ;
50
- AddDrive ( dv2 , dr3 ) ;
51
- AddDrive ( dv2 , dr1 ) ;
52
-
53
- id1 = ( int ) s . Save ( dv1 ) ;
54
- id2 = ( int ) s . Save ( dv2 ) ;
55
- s . Flush ( ) ;
56
-
57
- s . Clear ( ) ;
58
- s . Delete ( dr3 ) ;
59
- t . Commit ( ) ;
60
- }
43
+ var withTemplate = new Device ( "Device With Device 2 template" ) { Template = dv2 } ;
44
+
45
+ using var s = Sfi . OpenSession ( ) ;
46
+ using var t = s . BeginTransaction ( ) ;
47
+ s . Save ( dr1 ) ;
48
+ _drive2Id = ( int ) s . Save ( dr2 ) ;
49
+ s . Save ( dr3 ) ;
50
+ AddDrive ( dv1 , dr2 ) ;
51
+ AddDrive ( dv1 , dr1 ) ;
52
+ AddDrive ( dv2 , dr3 ) ;
53
+ AddDrive ( dv2 , dr1 ) ;
54
+
55
+ id1 = ( int ) s . Save ( dv1 ) ;
56
+ id2 = ( int ) s . Save ( dv2 ) ;
57
+ _withTemplateId = ( int ) s . Save ( withTemplate ) ;
58
+ s . Flush ( ) ;
59
+
60
+ s . Clear ( ) ;
61
+ s . Delete ( dr3 ) ;
62
+ t . Commit ( ) ;
61
63
}
62
64
63
65
private void AddDrive ( Device dv , Drive drive )
@@ -87,11 +89,13 @@ public async Task DeviceOfDriveAsync()
87
89
{
88
90
dv1 = ( Device ) await ( s . LoadAsync ( typeof ( Device ) , id1 ) ) ;
89
91
dv2 = ( Device ) await ( s . LoadAsync ( typeof ( Device ) , id2 ) ) ;
92
+ await ( NHibernateUtil . InitializeAsync ( dv1 . Drives ) ) ;
93
+ await ( NHibernateUtil . InitializeAsync ( dv2 . Drives ) ) ;
90
94
}
91
95
92
96
Assert . That ( dv1 . Drives , Has . Count . EqualTo ( _drivesCount ) . And . None . Null ) ;
93
97
// Verify one is missing
94
- Assert . That ( dv2 . Drives , Has . Count . EqualTo ( DrivesCountWithOneIgnored ) . And . None . Null ) ;
98
+ Assert . That ( dv2 . Drives , Has . Count . EqualTo ( ValidDrivesCount ) . And . None . Null ) ;
95
99
96
100
//Make sure that flush didn't touch not-found="ignore" records for not modified collection
97
101
using ( var s = Sfi . OpenSession ( ) )
@@ -102,7 +106,7 @@ public async Task DeviceOfDriveAsync()
102
106
await ( t . CommitAsync ( ) ) ;
103
107
}
104
108
105
- await ( VerifyResultAsync ( expectedInCollection : DrivesCountWithOneIgnored , expectedInDb : _drivesCount , msg : "not modified collection" ) ) ;
109
+ await ( VerifyResultAsync ( expectedInCollection : ValidDrivesCount , expectedInDb : _drivesCount , msg : "not modified collection" ) ) ;
106
110
107
111
// Many-to-many clears collection and recreates it so not-found ignore records are lost
108
112
// Note: It's not the case when no valid records are present, so loaded Drives collection is empty
@@ -139,17 +143,34 @@ async Task VerifyResultAsync(int expectedInCollection, int expectedInDb, string
139
143
[ Test ]
140
144
public async Task QueryOverFetchAsync ( )
141
145
{
142
- using ( var s = OpenSession ( ) )
143
- {
144
- var dv2 = await ( s . QueryOver < Device > ( )
145
- . Fetch ( SelectMode . Fetch , x => x . Drives )
146
- . Where ( Restrictions . IdEq ( id2 ) )
147
- . TransformUsing ( Transformers . DistinctRootEntity )
148
- . SingleOrDefaultAsync ( ) ) ;
149
-
150
- Assert . That ( NHibernateUtil . IsInitialized ( dv2 . Drives ) , Is . True ) ;
151
- Assert . That ( dv2 . Drives , Has . Count . EqualTo ( DrivesCountWithOneIgnored ) . And . None . Null ) ;
152
- }
146
+ using var log = new SqlLogSpy ( ) ;
147
+ using var s = OpenSession ( ) ;
148
+ var dv2 = await ( s . QueryOver < Device > ( )
149
+ . Fetch ( SelectMode . Fetch , x => x . Drives )
150
+ . Where ( Restrictions . IdEq ( id2 ) )
151
+ . TransformUsing ( Transformers . DistinctRootEntity )
152
+ . SingleOrDefaultAsync ( ) ) ;
153
+
154
+ Assert . That ( NHibernateUtil . IsInitialized ( dv2 . Drives ) , Is . True ) ;
155
+ Assert . That ( dv2 . Drives , Has . Count . EqualTo ( ValidDrivesCount ) . And . None . Null ) ;
156
+ Assert . That ( log . Appender . GetEvents ( ) . Length , Is . EqualTo ( 1 ) ) ;
157
+ }
158
+
159
+ [ Test ]
160
+ public async Task QueryOverFetch2Async ( )
161
+ {
162
+ using var log = new SqlLogSpy ( ) ;
163
+ using var s = OpenSession ( ) ;
164
+ var withTemplate = await ( s . QueryOver < Device > ( )
165
+ . Fetch ( SelectMode . Fetch , x => x . Template , x => x . Template . Drives )
166
+ . Where ( Restrictions . IdEq ( _withTemplateId ) )
167
+ . TransformUsing ( Transformers . DistinctRootEntity )
168
+ . SingleOrDefaultAsync ( ) ) ;
169
+
170
+ Assert . That ( NHibernateUtil . IsInitialized ( withTemplate . Template ) , Is . True ) ;
171
+ Assert . That ( NHibernateUtil . IsInitialized ( withTemplate . Template . Drives ) , Is . True ) ;
172
+ Assert . That ( withTemplate . Template . Drives , Has . Count . EqualTo ( ValidDrivesCount ) . And . None . Null ) ;
173
+ Assert . That ( log . Appender . GetEvents ( ) . Length , Is . EqualTo ( 1 ) ) ;
153
174
}
154
175
155
176
[ Test ]
@@ -163,21 +184,36 @@ public async Task HqlFetchAsync()
163
184
. UniqueResultAsync < Device > ( ) ) ;
164
185
165
186
Assert . That ( NHibernateUtil . IsInitialized ( dv2 . Drives ) , Is . True ) ;
166
- Assert . That ( dv2 . Drives , Has . Count . EqualTo ( DrivesCountWithOneIgnored ) . And . None . Null ) ;
187
+ Assert . That ( dv2 . Drives , Has . Count . EqualTo ( ValidDrivesCount ) . And . None . Null ) ;
188
+ Assert . That ( log . Appender . GetEvents ( ) . Length , Is . EqualTo ( 1 ) ) ;
189
+ }
190
+
191
+ [ Test ]
192
+ public async Task HqlFetch2Async ( )
193
+ {
194
+ using var log = new SqlLogSpy ( ) ;
195
+ using var s = OpenSession ( ) ;
196
+ var withTemplate = await ( s . CreateQuery ( "from Device t left join fetch t.Template d left join fetch d.Drives where d.id = :id" )
197
+ . SetResultTransformer ( Transformers . DistinctRootEntity )
198
+ . SetParameter ( "id" , id2 )
199
+ . UniqueResultAsync < Device > ( ) ) ;
200
+
201
+ Assert . That ( NHibernateUtil . IsInitialized ( withTemplate . Template ) , Is . True ) ;
202
+ Assert . That ( NHibernateUtil . IsInitialized ( withTemplate . Template . Drives ) , Is . True ) ;
203
+ Assert . That ( withTemplate . Template . Drives , Has . Count . EqualTo ( ValidDrivesCount ) . And . None . Null ) ;
167
204
Assert . That ( log . Appender . GetEvents ( ) . Length , Is . EqualTo ( 1 ) ) ;
168
205
}
169
206
170
207
[ Test ]
171
208
public async Task LazyLoadAsync ( )
172
209
{
173
- using ( var s = OpenSession ( ) )
174
- {
175
- var dv2 = await ( s . GetAsync < Device > ( id2 ) ) ;
176
- await ( NHibernateUtil . InitializeAsync ( dv2 . Drives ) ) ;
210
+ using var s = OpenSession ( ) ;
177
211
178
- Assert . That ( NHibernateUtil . IsInitialized ( dv2 . Drives ) , Is . True ) ;
179
- Assert . That ( dv2 . Drives , Has . Count . EqualTo ( DrivesCountWithOneIgnored ) . And . None . Null ) ;
180
- }
212
+ var dv2 = await ( s . GetAsync < Device > ( id2 ) ) ;
213
+ using var log = new SqlLogSpy ( ) ;
214
+
215
+ Assert . That ( dv2 . Drives , Has . Count . EqualTo ( ValidDrivesCount ) . And . None . Null ) ;
216
+ Assert . That ( log . Appender . GetEvents ( ) . Length , Is . EqualTo ( 1 ) ) ;
181
217
}
182
218
}
183
219
}
0 commit comments