0
|
1 using System.Linq;
|
|
2
|
|
3 using BLToolkit.Data.DataProvider;
|
|
4 using BLToolkit.Data.Linq;
|
|
5 using NUnit.Framework;
|
|
6
|
|
7 namespace Data.Linq
|
|
8 {
|
|
9 using Model;
|
|
10
|
|
11 [TestFixture]
|
|
12 public class ElementOperationTest : TestBase
|
|
13 {
|
|
14 [Test]
|
|
15 public void First()
|
|
16 {
|
|
17 ForEachProvider(db => Assert.AreEqual(
|
|
18 Parent.OrderByDescending(p => p.ParentID).First().ParentID,
|
|
19 db.Parent.OrderByDescending(p => p.ParentID).First().ParentID));
|
|
20 }
|
|
21
|
|
22 [Test]
|
|
23 public void FirstWhere()
|
|
24 {
|
|
25 ForEachProvider(db => Assert.AreEqual(2, db.Parent.First(p => p.ParentID == 2).ParentID));
|
|
26 }
|
|
27
|
|
28 [Test]
|
|
29 public void FirstOrDefault()
|
|
30 {
|
|
31 ForEachProvider(db => Assert.IsNull((from p in db.Parent where p.ParentID == 100 select p).FirstOrDefault()));
|
|
32 }
|
|
33
|
|
34 [Test]
|
|
35 public void FirstOrDefaultWhere()
|
|
36 {
|
|
37 ForEachProvider(db => Assert.AreEqual(2, db.Parent.FirstOrDefault(p => p.ParentID == 2).ParentID));
|
|
38 }
|
|
39
|
|
40 [Test]
|
|
41 public void Single()
|
|
42 {
|
|
43 ForEachProvider(db => Assert.AreEqual(1, db.Parent.Where(p => p.ParentID == 1).Single().ParentID));
|
|
44 }
|
|
45
|
|
46 [Test]
|
|
47 public void SingleWhere()
|
|
48 {
|
|
49 ForEachProvider(db => Assert.AreEqual(2, db.Parent.Single(p => p.ParentID == 2).ParentID));
|
|
50 }
|
|
51
|
|
52 [Test]
|
|
53 public void SingleOrDefault()
|
|
54 {
|
|
55 ForEachProvider(db => Assert.IsNull((from p in db.Parent where p.ParentID == 100 select p).SingleOrDefault()));
|
|
56 }
|
|
57
|
|
58 [Test]
|
|
59 public void SingleOrDefaultWhere()
|
|
60 {
|
|
61 ForEachProvider(db => Assert.AreEqual(2, db.Parent.SingleOrDefault(p => p.ParentID == 2).ParentID));
|
|
62 }
|
|
63
|
|
64 [Test]
|
|
65 public void FirstOrDefaultScalar()
|
|
66 {
|
|
67 ForEachProvider(db => Assert.AreEqual(
|
|
68 Parent.OrderBy(p => p.ParentID).FirstOrDefault().ParentID,
|
|
69 db.Parent.OrderBy(p => p.ParentID).FirstOrDefault().ParentID));
|
|
70 }
|
|
71
|
|
72 [Test]
|
|
73 public void NestedFirstOrDefaultScalar1()
|
|
74 {
|
|
75 ForEachProvider(new[] { ProviderName.Informix, ProviderName.Sybase }, db => AreEqual(
|
|
76 from p in Parent select Child.FirstOrDefault().ChildID,
|
|
77 from p in db.Parent select db.Child.FirstOrDefault().ChildID));
|
|
78 }
|
|
79
|
|
80 [Test]
|
|
81 public void NestedFirstOrDefaultScalar2()
|
|
82 {
|
|
83 ForEachProvider(
|
|
84 new[] { ProviderName.Informix, "Oracle", "DevartOracle", ProviderName.Sybase }, db =>
|
|
85 AreEqual(
|
|
86 from p in Parent
|
|
87 select new
|
|
88 {
|
|
89 p.ParentID,
|
|
90 MaxChild =
|
|
91 Child
|
|
92 .Where(c => c.Parent == p)
|
|
93 .OrderByDescending(c => c.ChildID * c.ParentID)
|
|
94 .FirstOrDefault() == null ?
|
|
95 0 :
|
|
96 Child
|
|
97 .Where(c => c.Parent == p)
|
|
98 .OrderByDescending(c => c.ChildID * c.ParentID)
|
|
99 .FirstOrDefault()
|
|
100 .ChildID
|
|
101 },
|
|
102 from p in db.Parent
|
|
103 select new
|
|
104 {
|
|
105 p.ParentID,
|
|
106 MaxChild = db.Child
|
|
107 .Where(c => c.Parent == p)
|
|
108 .OrderByDescending(c => c.ChildID * c.ParentID)
|
|
109 .FirstOrDefault()
|
|
110 .ChildID
|
|
111 }));
|
|
112 }
|
|
113
|
|
114 [Test]
|
|
115 public void NestedFirstOrDefault1()
|
|
116 {
|
|
117 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
|
|
118
|
|
119 ForEachProvider(
|
|
120 db => AreEqual(
|
|
121 from p in Parent select Child.FirstOrDefault(),
|
|
122 from p in db.Parent select db.Child.FirstOrDefault()));
|
|
123
|
|
124 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
|
|
125 }
|
|
126
|
|
127 [Test]
|
|
128 public void NestedFirstOrDefault2()
|
|
129 {
|
|
130 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
|
|
131
|
|
132 ForEachProvider(
|
|
133 db => AreEqual(
|
|
134 from p in Parent select p.Children.FirstOrDefault(),
|
|
135 from p in db.Parent select p.Children.FirstOrDefault()));
|
|
136
|
|
137 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
|
|
138 }
|
|
139
|
|
140 [Test]
|
|
141 public void NestedFirstOrDefault3()
|
|
142 {
|
|
143 ForEachProvider(
|
|
144 // Can be fixed.
|
|
145 new[] { ProviderName.Informix, ProviderName.Firebird },
|
|
146 db => AreEqual(
|
|
147 from p in Parent select p.Children.Select(c => c.ParentID).Distinct().FirstOrDefault(),
|
|
148 from p in db.Parent select p.Children.Select(c => c.ParentID).Distinct().FirstOrDefault()));
|
|
149 }
|
|
150
|
|
151 [Test]
|
|
152 public void NestedFirstOrDefault4()
|
|
153 {
|
|
154 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
|
|
155
|
|
156 ForEachProvider(
|
|
157 // Can be fixed.
|
|
158 new[] { ProviderName.Informix, ProviderName.Firebird, ProviderName.PostgreSQL },
|
|
159 db => AreEqual(
|
|
160 from p in Parent select p.Children.Where(c => c.ParentID > 0).Distinct().FirstOrDefault(),
|
|
161 from p in db.Parent select p.Children.Where(c => c.ParentID > 0).Distinct().FirstOrDefault()));
|
|
162
|
|
163 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
|
|
164 }
|
|
165
|
|
166 [Test]
|
|
167 public void NestedFirstOrDefault5()
|
|
168 {
|
|
169 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
|
|
170
|
|
171 ForEachProvider(
|
|
172 db => AreEqual(
|
|
173 from p in GrandChild select p.Child.Parent.Children.FirstOrDefault(),
|
|
174 from p in db.GrandChild select p.Child.Parent.Children.FirstOrDefault()));
|
|
175
|
|
176 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
|
|
177 }
|
|
178
|
|
179 [Test]
|
|
180 public void NestedSingleOrDefault1()
|
|
181 {
|
|
182 ForEachProvider(
|
|
183 db => AreEqual(
|
|
184 from p in Parent select p.Children.Select(c => c.ParentID).Distinct().SingleOrDefault(),
|
|
185 from p in db.Parent select p.Children.Select(c => c.ParentID).Distinct().SingleOrDefault()));
|
|
186 }
|
|
187
|
|
188 [Test]
|
|
189 public void FirstOrDefaultEntitySet([IncludeDataContexts("Northwind")] string context)
|
|
190 {
|
|
191 using (var db = new NorthwindDB())
|
|
192 {
|
|
193 AreEqual(
|
|
194 Customer.Select(c => c.Orders.FirstOrDefault()),
|
|
195 db.Customer.Select(c => c.Orders.FirstOrDefault()));
|
|
196 }
|
|
197 }
|
|
198
|
|
199 [Test]
|
|
200 public void NestedSingleOrDefaultTest([IncludeDataContexts("Northwind")] string context)
|
|
201 {
|
|
202 using (var db = new NorthwindDB())
|
|
203 {
|
|
204 AreEqual(
|
|
205 Customer.Select(c => c.Orders.Take(1).SingleOrDefault()),
|
|
206 db.Customer.Select(c => c.Orders.Take(1).SingleOrDefault()));
|
|
207 }
|
|
208 }
|
|
209
|
|
210 [Test]
|
|
211 public void MultipleQuery([IncludeDataContexts("Northwind")] string context)
|
|
212 {
|
|
213 using (var db = new NorthwindDB())
|
|
214 {
|
|
215 var q =
|
|
216 from p in db.Product
|
|
217 select db.Category.Select(zrp => zrp.CategoryName).FirstOrDefault();
|
|
218
|
|
219 q.ToList();
|
|
220 }
|
|
221 }
|
|
222 }
|
|
223 }
|