comparison UnitTests/Linq/CompileTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading;
5 using NUnit.Framework;
6
7 using BLToolkit.Data.Linq;
8
9 namespace Data.Linq
10 {
11 using Model;
12
13 [TestFixture]
14 public class CompileTest : TestBase
15 {
16 [Test]
17 public void CompiledTest1()
18 {
19 var query = CompiledQuery.Compile((ITestDataContext db, string n1, int n2) =>
20 n1 + n2);
21
22 ForEachProvider(db =>
23 {
24 Assert.AreEqual("11", query(db, "1", 1));
25 Assert.AreEqual("22", query(db, "2", 2));
26 });
27 }
28
29 [Test]
30 public void CompiledTest2([DataContexts] string context)
31 {
32 var query = CompiledQuery.Compile((ITestDataContext db, int n) =>
33 db.Child.Where(c => c.ParentID == n).Take(n));
34
35 using (var db = GetDataContext(context))
36 {
37 Assert.AreEqual(1, query(db, 1).ToList().Count());
38 Assert.AreEqual(2, query(db, 2).ToList().Count());
39 }
40 }
41
42 [Test]
43 public void CompiledTest3()
44 {
45 var query = CompiledQuery.Compile((ITestDataContext db, int n) =>
46 db.GetTable<Child>().Where(c => c.ParentID == n).Take(n));
47
48 ForEachProvider(db =>
49 {
50 Assert.AreEqual(1, query(db, 1).ToList().Count());
51 Assert.AreEqual(2, query(db, 2).ToList().Count());
52 });
53 }
54
55 [Test]
56 public void CompiledTest4()
57 {
58 var query = CompiledQuery.Compile((ITestDataContext db, int[] n) =>
59 db.GetTable<Child>().Where(c => n.Contains(c.ParentID)));
60
61 ForEachProvider(db =>
62 Assert.AreEqual(3, query(db, new[] { 1, 2 }).ToList().Count()));
63 }
64
65 [Test]
66 public void CompiledTest5()
67 {
68 var query = CompiledQuery.Compile((ITestDataContext db, object[] ps) =>
69 db.Parent.Where(p => p.ParentID == (int)ps[0] && p.Value1 == (int?)ps[1]));
70
71 ForEachProvider(db =>
72 {
73 Assert.AreEqual(1, query(db, new object[] { 1, 1 }).ToList().Count());
74 Assert.AreEqual(1, query(db, new object[] { 2, null }).ToList().Count());
75 });
76 }
77
78 [Test]
79 public void CompiledTable1()
80 {
81 var query = CompiledQuery.Compile((ITestDataContext db) =>
82 db.Child);
83
84 ForEachProvider(db => query(db).ToList().Count());
85 }
86
87 [Test]
88 public void CompiledTable2()
89 {
90 var query = CompiledQuery.Compile((ITestDataContext db) =>
91 db.GetTable<Child>());
92
93 ForEachProvider(db => query(db).ToList().Count());
94 }
95
96 [Test]
97 public void ConcurentTest1()
98 {
99 var query = CompiledQuery.Compile((ITestDataContext db, int n) =>
100 db.GetTable<Parent>().Where(p => p.ParentID == n).First().ParentID);
101
102 const int count = 100;
103
104 var threads = new Thread[count];
105 var results = new int [count, 2];
106
107 for (var i = 0; i < count; i++)
108 {
109 var n = i;
110
111 threads[i] = new Thread(() =>
112 {
113 using (var db = new TestDbManager())
114 {
115 var id = (n % 6) + 1;
116 results[n,0] = id;
117 results[n,1] = query(db, id);
118 }
119 });
120 }
121
122 for (var i = 0; i < count; i++)
123 threads[i].Start();
124
125 for (var i = 0; i < count; i++)
126 threads[i].Join();
127
128 for (var i = 0; i < count; i++)
129 Assert.AreEqual(results[i,0], results[i,1]);
130 }
131
132 [Test]
133 public void ConcurentTest2()
134 {
135 var threads = new Thread[100];
136 var results = new int [100,2];
137
138 for (var i = 0; i < 100; i++)
139 {
140 var n = i;
141
142 threads[i] = new Thread(() =>
143 {
144 using (var db = new TestDbManager())
145 {
146 var id = (n % 6) + 1;
147 results[n,0] = id;
148 results[n,1] = db.Parent.Where(p => p.ParentID == id).First().ParentID;
149 }
150 });
151 }
152
153 for (var i = 0; i < 100; i++)
154 threads[i].Start();
155
156 for (var i = 0; i < 100; i++)
157 threads[i].Join();
158
159 for (var i = 0; i < 100; i++)
160 Assert.AreEqual(results[i,0], results[i,1]);
161 }
162
163 [Test]
164 public void ParamTest1()
165 {
166 var query = CompiledQuery.Compile<ITestDataContext,int,IEnumerable<Child>>((db, id) =>
167 from c in db.Child
168 where c.ParentID == id
169 select new Child
170 {
171 ParentID = id,
172 ChildID = c.ChildID
173 });
174
175 ForEachProvider(db => Assert.AreEqual(2, query(db, 2).ToList().Count()));
176 }
177
178 [Test]
179 public void ElementTest1()
180 {
181 var query = CompiledQuery.Compile((ITestDataContext db, int n) =>
182 db.Child.Where(c => c.ParentID == n).First());
183
184 ForEachProvider(db =>
185 {
186 Assert.AreEqual(1, query(db, 1).ParentID);
187 Assert.AreEqual(2, query(db, 2).ParentID);
188 });
189 }
190 }
191 }