9
9
10
10
11
11
using System . Collections ;
12
+ using System . Linq ;
12
13
using NHibernate . Dialect ;
13
14
using NUnit . Framework ;
15
+ using NHibernate . Linq ;
14
16
15
17
namespace NHibernate . Test . TypedManyToOne
16
18
{
17
19
using System . Threading . Tasks ;
20
+ using System . Threading ;
18
21
[ TestFixture ]
19
22
public class TypedManyToOneTestAsync : TestCase
20
23
{
@@ -35,38 +38,27 @@ protected override bool AppliesTo(Dialect.Dialect dialect)
35
38
}
36
39
37
40
[ Test ]
38
- public async Task TestCreateQueryAsync ( )
41
+ public async Task TestLinqEntityNameQueryAsync ( )
39
42
{
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 ( ) )
65
46
{
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
+
67
52
await ( t . CommitAsync ( ) ) ;
68
53
}
69
54
55
+ await ( DeleteCustomerAsync ( cust ) ) ;
56
+ }
57
+
58
+ [ Test ]
59
+ public async Task TestCreateQueryAsync ( )
60
+ {
61
+ var cust = await ( CreateCustomerAsync ( ) ) ;
70
62
using ( ISession s = Sfi . OpenSession ( ) )
71
63
using ( ITransaction t = s . BeginTransaction ( ) )
72
64
{
@@ -82,20 +74,7 @@ public async Task TestCreateQueryAsync()
82
74
await ( t . CommitAsync ( ) ) ;
83
75
}
84
76
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 ) ) ;
99
78
}
100
79
101
80
[ Test ]
@@ -124,5 +103,60 @@ public async Task TestCreateQueryNullAsync()
124
103
await ( t . CommitAsync ( ) ) ;
125
104
}
126
105
}
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
+ }
127
161
}
128
162
}
0 commit comments