comparison UnitTests/Linq/Mapping.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.Linq;
3
4 using BLToolkit.Data.Linq;
5 using BLToolkit.DataAccess;
6 using BLToolkit.EditableObjects;
7 using BLToolkit.Mapping;
8 using BLToolkit.Reflection;
9 using NUnit.Framework;
10
11 using Convert = System.Convert;
12
13 #pragma warning disable 0649
14
15 namespace Data.Linq
16 {
17 using Model;
18
19 [TestFixture]
20 public class Mapping : TestBase
21 {
22 [Test]
23 public void Enum1()
24 {
25 var expected = from p in Person where new[] { Gender.Male }.Contains(p.Gender) select p;
26 ForEachProvider(db => AreEqual(expected, from p in db.Person where new[] { Gender.Male }.Contains(p.Gender) select p));
27 }
28
29 [Test]
30 public void Enum2()
31 {
32 ForEachProvider(db => AreEqual(
33 from p in Person where p.Gender == Gender.Male select p,
34 from p in db.Person where p.Gender == Gender.Male select p));
35 }
36
37 [Test]
38 public void Enum21()
39 {
40 var gender = Gender.Male;
41
42 ForEachProvider(db => AreEqual(
43 from p in Person where p.Gender == gender select p,
44 from p in db.Person where p.Gender == gender select p));
45 }
46
47 [Test]
48 public void Enum3()
49 {
50 var fm = Gender.Female;
51
52 var expected = from p in Person where p.Gender != fm select p;
53 ForEachProvider(db => AreEqual(expected, from p in db.Person where p.Gender != fm select p));
54 }
55
56 [Test]
57 public void Enum4()
58 {
59 ForEachProvider(db => AreEqual(
60 from p in Parent4 where p.Value1 == TypeValue.Value1 select p,
61 from p in db.Parent4 where p.Value1 == TypeValue.Value1 select p));
62 }
63
64 [Test]
65 public void Enum5()
66 {
67 ForEachProvider(db => AreEqual(
68 from p in Parent4 where p.Value1 == TypeValue.Value3 select p,
69 from p in db.Parent4 where p.Value1 == TypeValue.Value3 select p));
70 }
71
72 [Test]
73 public void Enum6()
74 {
75 ForEachProvider(db => AreEqual(
76 from p in Parent4
77 join c in Child on p.ParentID equals c.ParentID
78 where p.Value1 == TypeValue.Value1 select p,
79 from p in db.Parent4
80 join c in db.Child on p.ParentID equals c.ParentID
81 where p.Value1 == TypeValue.Value1 select p));
82 }
83
84 [Test]
85 public void Enum7()
86 {
87 var v1 = TypeValue.Value1;
88
89 ForEachProvider(db => db.Parent4.Update(p => p.Value1 == v1, p => new Parent4 { Value1 = v1 }));
90 }
91
92 enum TestValue
93 {
94 Value1 = 1,
95 }
96
97 [TableName("Parent")]
98 class TestParent
99 {
100 public int ParentID;
101 public TestValue Value1;
102 }
103
104 [Test]
105 public void Enum81()
106 {
107 ForEachProvider(db => db.GetTable<TestParent>().Where(p => p.Value1 == TestValue.Value1).ToList());
108 }
109
110 [Test]
111 public void Enum82()
112 {
113 var testValue = TestValue.Value1;
114 ForEachProvider(db => db.GetTable<TestParent>().Where(p => p.Value1 == testValue).ToList());
115 }
116
117 public enum Gender9
118 {
119 [MapValue('M')] Male,
120 [MapValue('F')] Female,
121 [MapValue('U')] Unknown,
122 [MapValue('O')] Other,
123 }
124
125 [TableName("Person")]
126 public class Person9
127 {
128 public int PersonID;
129 public string FirstName;
130 public string LastName;
131 public string MiddleName;
132 public Gender9 Gender;
133 }
134
135 [Test]
136 public void Enum9()
137 {
138 ForEachProvider(db =>
139 db.GetTable<Person9>().Where(p => p.PersonID == 1 && p.Gender == Gender9.Male).ToList());
140 }
141
142 [Test]
143 public void EditableObject()
144 {
145 ForEachProvider(db =>
146 {
147 var e = (from p in db.GetTable<EditableParent>() where p.ParentID == 1 select p).First();
148 Assert.AreEqual(1, e.ParentID);
149 Assert.AreEqual(1, e.Value1);
150 });
151 }
152
153 [TableName("Parent")]
154 [MapField("Value1", "Value.Value1")]
155 public class ParentObject
156 {
157 public int ParentID;
158 public Inner Value = new Inner();
159
160 public class Inner
161 {
162 public int? Value1;
163 }
164 }
165
166 [Test]
167 public void Inner1()
168 {
169 ForEachProvider(db =>
170 {
171 var e = db.GetTable<ParentObject>().First(p => p.ParentID == 1);
172 Assert.AreEqual(1, e.ParentID);
173 Assert.AreEqual(1, e.Value.Value1);
174 });
175 }
176
177 [Test]
178 public void Inner2()
179 {
180 ForEachProvider(db =>
181 {
182 var e = db.GetTable<ParentObject>().First(p => p.ParentID == 1 && p.Value.Value1 == 1);
183 Assert.AreEqual(1, e.ParentID);
184 Assert.AreEqual(1, e.Value.Value1);
185 });
186 }
187
188 [TableName("Child")]
189 public class ChildObject
190 {
191 public int ParentID;
192 public int ChildID;
193
194 [Association(ThisKey="ParentID", OtherKey="ParentID")]
195 public ParentObject Parent;
196 }
197
198 [Test]
199 public void Inner3()
200 {
201 ForEachProvider(db =>
202 {
203 var e = db.GetTable<ChildObject>().First(c => c.Parent.Value.Value1 == 1);
204 Assert.AreEqual(1, e.ParentID);
205 });
206 }
207
208 [TableName("Parent")]
209 public class ParentObject2
210 {
211 class IntToDateMemberMapper : MemberMapper
212 {
213 public override void SetValue(object o, object value)
214 {
215 ((ParentObject2)o).Value1 = new DateTime(2010, 1, Convert.ToInt32(value));
216 }
217 }
218
219 public int ParentID;
220 [MemberMapper(typeof(IntToDateMemberMapper))]
221 public DateTime Value1;
222 }
223
224 [Test]
225 public void MemberMapperTest1()
226 {
227 ForEachProvider(db =>
228 {
229 var q =
230 from p in db.GetTable<ParentObject2>()
231 where p.ParentID == 1
232 select p;
233
234 Assert.AreEqual(new DateTime(2010, 1, 1), q.First().Value1);
235 });
236 }
237
238 //[Test]
239 public void MemberMapperTest2()
240 {
241 ForEachProvider(db =>
242 {
243 var q =
244 from p in db.GetTable<ParentObject2>()
245 where p.ParentID == 1
246 select p.Value1;
247
248 Assert.AreEqual(new DateTime(2010, 1, 1), q.First());
249 });
250 }
251
252 struct MyInt
253 {
254 public int MyValue;
255 }
256
257 [TableName("Parent")]
258 class MyParent
259 {
260 public MyInt ParentID;
261 public int? Value1;
262 }
263
264 class MyMappingSchema : MappingSchema
265 {
266 public override object ConvertChangeType(object value, Type conversionType, bool isNullable)
267 {
268 if (conversionType == typeof(MyInt))
269 return new MyInt { MyValue = Convert.ToInt32(value) };
270
271 if (value is MyInt)
272 value = ((MyInt)value).MyValue;
273
274 return base.ConvertChangeType(value, conversionType, isNullable);
275 }
276
277 public override object ConvertParameterValue(object value, Type systemType)
278 {
279 return value is MyInt ? ((MyInt)value).MyValue : value;
280 }
281 }
282
283 static readonly MyMappingSchema _myMappingSchema = new MyMappingSchema();
284
285 [Test]
286 public void MyType1()
287 {
288 using (var db = new TestDbManager { MappingSchema = _myMappingSchema })
289 {
290 var list = db.GetTable<MyParent>().ToList();
291 }
292 }
293
294 [Test]
295 public void MyType2()
296 {
297 using (var db = new TestDbManager { MappingSchema = _myMappingSchema })
298 {
299 var list = db.GetTable<MyParent>()
300 .Select(t => new MyParent { ParentID = t.ParentID, Value1 = t.Value1 })
301 .ToList();
302 }
303 }
304
305 [Test]
306 public void MyType3()
307 {
308 using (var db = new TestDbManager { MappingSchema = _myMappingSchema })
309 {
310 db.BeginTransaction();
311 db.Insert(new MyParent { ParentID = new MyInt { MyValue = 1001 }, Value1 = 1001 });
312 db.Parent.Delete(p => p.ParentID >= 1000);
313 }
314 }
315
316 [TableName("Parent")]
317 class MyParent1
318 {
319 public int ParentID;
320 public int? Value1;
321
322 [MapIgnore]
323 public string Value2 { get { return "1"; } }
324
325 public int GetValue() { return 2;}
326 }
327
328 [Test]
329 public void MapIgnore1()
330 {
331 ForEachProvider(db => AreEqual(
332 Parent .Select(p => new { p.ParentID, Value2 = "1" }),
333 db.GetTable<MyParent1>().Select(p => new { p.ParentID, p.Value2 })));
334 }
335
336 [Test]
337 public void MapIgnore2()
338 {
339 ForEachProvider(db => AreEqual(
340 Parent .Select(p => new { p.ParentID, Length = 1 }),
341 db.GetTable<MyParent1>().Select(p => new { p.ParentID, p.Value2.Length })));
342 }
343
344 [Test]
345 public void MapIgnore3()
346 {
347 ForEachProvider(db => AreEqual(
348 Parent .Select(p => new { p.ParentID, Value = 2 }),
349 db.GetTable<MyParent1>().Select(p => new { p.ParentID, Value = p.GetValue() })));
350 }
351
352 [TableName("Parent")]
353 public abstract class AbsParent : EditableObject
354 {
355 public abstract int ParentID { get; set; }
356 public abstract int? Value1 { get; set; }
357 }
358
359 [TableName("Child")]
360 public abstract class AbsChild : EditableObject
361 {
362 public abstract int ParentID { get; set; }
363 public abstract int ChildID { get; set; }
364
365 [Association(ThisKey = "ParentID", OtherKey = "ParentID", CanBeNull = false)]
366 public AbsParent Parent;
367 }
368
369 [Test]
370 public void MapAbstract()
371 {
372 using (var db = new TestDbManager())
373 {
374 var q = from a in db.GetTable<AbsChild>()
375 select new { a.ChildID, a.Parent.Value1 };
376
377 var ql = q.ToList();
378 }
379 }
380
381 public class Entity { public int Id { get; set; } }
382 public interface IDocument { int Id { get; set; } }
383 public class Document : Entity, IDocument { }
384
385 [Test]
386 public void TestMethod()
387 {
388 using (var db = new TestDbManager())
389 {
390 IQueryable<IDocument> query = db.GetTable<Document>();
391 var idsQuery = query.Select(s => s.Id);
392 var str = idsQuery.ToString(); // Exception
393 Assert.IsNotNull(str);
394 }
395 }
396
397 [TableName("Parent")]
398 public abstract class ParentX
399 {
400 [MapField("ParentID")]
401 public abstract int ParentID { get; set; }
402 [MapField("Value1")]
403 public abstract int? Value1 { get; set; }
404 }
405
406 [TableName("Child")]
407 [MapField("ParentID", "Parent.ParentID")]
408 public abstract class ChildX
409 {
410 [MapField("ChildID")]
411 public abstract int ChildID { get; set; }
412 public abstract ParentX Parent { get; set; }
413 }
414
415 [Test]
416 public void Test4([DataContexts] string contexts)
417 {
418 using (var db = GetDataContext(contexts))
419 {
420 db.Child. Delete(p => p.ParentID == 1001);
421 db.Parent.Delete(p => p.ParentID == 1001);
422
423 try
424 {
425 var child = TypeAccessor.CreateInstance<ChildX>();
426 var parent = TypeAccessor.CreateInstance<ParentX>();
427
428 parent.ParentID = 1001;
429 parent.Value1 = 1;
430
431 db.Insert(parent);
432
433 child.ChildID = 1001;
434 child.Parent = parent;
435
436 db.Insert(child);
437 }
438 finally
439 {
440 db.Child. Delete(p => p.ParentID == 1001);
441 db.Parent.Delete(p => p.ParentID == 1001);
442 }
443 }
444 }
445 }
446 }