99
1010
1111using System . Collections ;
12+ using System . Linq ;
1213using NHibernate . Dialect ;
1314using NUnit . Framework ;
15+ using NHibernate . Linq ;
1416
1517namespace NHibernate . Test . TypedManyToOne
1618{
1719 using System . Threading . Tasks ;
20+ using System . Threading ;
1821 [ TestFixture ]
1922 public class TypedManyToOneTestAsync : TestCase
2023 {
@@ -35,38 +38,27 @@ protected override bool AppliesTo(Dialect.Dialect dialect)
3538 }
3639
3740 [ Test ]
38- public async Task TestCreateQueryAsync ( )
41+ public async Task TestLinqEntityNameQueryAsync ( )
3942 {
40- var cust = new Customer ( ) ;
41- cust . CustomerId = "abc123" ;
42- cust . Name = "Matt" ;
43-
44- var ship = new Address ( ) ;
45- ship . Street = "peachtree rd" ;
46- ship . State = "GA" ;
47- ship . City = "ATL" ;
48- ship . Zip = "30326" ;
49- ship . AddressId = new AddressId ( "SHIPPING" , "xyz123" ) ;
50- ship . Customer = cust ;
51-
52- var bill = new Address ( ) ;
53- bill . Street = "peachtree rd" ;
54- bill . State = "GA" ;
55- bill . City = "ATL" ;
56- bill . Zip = "30326" ;
57- bill . AddressId = new AddressId ( "BILLING" , "xyz123" ) ;
58- bill . Customer = cust ;
59-
60- cust . BillingAddress = bill ;
61- cust . ShippingAddress = ship ;
62-
63- using ( ISession s = Sfi . OpenSession ( ) )
64- using ( ITransaction t = s . BeginTransaction ( ) )
43+ var cust = await ( CreateCustomerAsync ( ) ) ;
44+ using ( var s = Sfi . OpenSession ( ) )
45+ using ( var t = s . BeginTransaction ( ) )
6546 {
66- await ( s . PersistAsync ( cust ) ) ;
47+ var billingNotes = await ( s . Query < Customer > ( ) . Select ( o => o . BillingAddress . BillingNotes ) . FirstAsync ( ) ) ;
48+ Assert . That ( billingNotes , Is . EqualTo ( "BillingNotes" ) ) ;
49+ var shippingNotes = await ( s . Query < Customer > ( ) . Select ( o => o . ShippingAddress . ShippingNotes ) . FirstAsync ( ) ) ;
50+ Assert . That ( shippingNotes , Is . EqualTo ( "ShippingNotes" ) ) ;
51+
6752 await ( t . CommitAsync ( ) ) ;
6853 }
6954
55+ await ( DeleteCustomerAsync ( cust ) ) ;
56+ }
57+
58+ [ Test ]
59+ public async Task TestCreateQueryAsync ( )
60+ {
61+ var cust = await ( CreateCustomerAsync ( ) ) ;
7062 using ( ISession s = Sfi . OpenSession ( ) )
7163 using ( ITransaction t = s . BeginTransaction ( ) )
7264 {
@@ -82,20 +74,7 @@ public async Task TestCreateQueryAsync()
8274 await ( t . CommitAsync ( ) ) ;
8375 }
8476
85- using ( ISession s = Sfi . OpenSession ( ) )
86- using ( ITransaction t = s . BeginTransaction ( ) )
87- {
88- await ( s . SaveOrUpdateAsync ( cust ) ) ;
89- ship = cust . ShippingAddress ;
90- cust . ShippingAddress = null ;
91- await ( s . DeleteAsync ( "ShippingAddress" , ship ) ) ;
92- await ( s . FlushAsync ( ) ) ;
93-
94- Assert . That ( await ( s . GetAsync ( "ShippingAddress" , ship . AddressId ) ) , Is . Null ) ;
95- await ( s . DeleteAsync ( cust ) ) ;
96-
97- await ( t . CommitAsync ( ) ) ;
98- }
77+ await ( DeleteCustomerAsync ( cust ) ) ;
9978 }
10079
10180 [ Test ]
@@ -124,5 +103,60 @@ public async Task TestCreateQueryNullAsync()
124103 await ( t . CommitAsync ( ) ) ;
125104 }
126105 }
106+
107+ private async Task < Customer > CreateCustomerAsync ( CancellationToken cancellationToken = default ( CancellationToken ) )
108+ {
109+ var cust = new Customer ( ) ;
110+ cust . CustomerId = "abc123" ;
111+ cust . Name = "Matt" ;
112+
113+ var ship = new Address ( ) ;
114+ ship . Street = "peachtree rd" ;
115+ ship . State = "GA" ;
116+ ship . City = "ATL" ;
117+ ship . Zip = "30326" ;
118+ ship . AddressId = new AddressId ( "SHIPPING" , "xyz123" ) ;
119+ ship . Customer = cust ;
120+ ship . ShippingNotes = "ShippingNotes" ;
121+
122+ var bill = new Address ( ) ;
123+ bill . Street = "peachtree rd" ;
124+ bill . State = "GA" ;
125+ bill . City = "ATL" ;
126+ bill . Zip = "30326" ;
127+ bill . AddressId = new AddressId ( "BILLING" , "xyz123" ) ;
128+ bill . Customer = cust ;
129+ bill . BillingNotes = "BillingNotes" ;
130+
131+ cust . BillingAddress = bill ;
132+ cust . ShippingAddress = ship ;
133+
134+ using ( ISession s = Sfi . OpenSession ( ) )
135+ using ( ITransaction t = s . BeginTransaction ( ) )
136+ {
137+ await ( s . PersistAsync ( cust , cancellationToken ) ) ;
138+ await ( t . CommitAsync ( cancellationToken ) ) ;
139+ }
140+
141+ return cust ;
142+ }
143+
144+ private async Task DeleteCustomerAsync ( Customer cust , CancellationToken cancellationToken = default ( CancellationToken ) )
145+ {
146+ using ( var s = Sfi . OpenSession ( ) )
147+ using ( var t = s . BeginTransaction ( ) )
148+ {
149+ await ( s . SaveOrUpdateAsync ( cust , cancellationToken ) ) ;
150+ var ship = cust . ShippingAddress ;
151+ cust . ShippingAddress = null ;
152+ await ( s . DeleteAsync ( "ShippingAddress" , ship , cancellationToken ) ) ;
153+ await ( s . FlushAsync ( cancellationToken ) ) ;
154+
155+ Assert . That ( await ( s . GetAsync ( "ShippingAddress" , ship . AddressId , cancellationToken ) ) , Is . Null ) ;
156+ await ( s . DeleteAsync ( cust , cancellationToken ) ) ;
157+
158+ await ( t . CommitAsync ( cancellationToken ) ) ;
159+ }
160+ }
127161 }
128162}
0 commit comments