comparison UnitTests/CS/Data/ExecuteObjectTest.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 NUnit.Framework;
3 using BLToolkit.Reflection;
4 using BLToolkit.Mapping;
5 using BLToolkit.Data;
6 using System.Collections.Generic;
7
8 namespace UnitTests.CS.Data
9 {
10 [TestFixture]
11 public class ExecuteObjectTest
12 {
13 const string _query = @" SELECT 1 AS PersonId, '1' AS FirstName, '1' AS MiddleName, '1' AS LastName
14 UNION SELECT 2 AS PersonId, '2' AS FirstName, '2' AS MiddleName, '2' AS LastName
15 UNION SELECT 3 AS PersonId, '3' AS FirstName, '3' AS MiddleName, '3' AS LastName
16 UNION SELECT 4 AS PersonId, '4' AS FirstName, '0' AS MiddleName, '4' AS LastName
17 UNION SELECT 5 AS PersonId, '5' AS FirstName, '1' AS MiddleName, '5' AS LastName
18 UNION SELECT 6 AS PersonId, '6' AS FirstName, '2' AS MiddleName, '6' AS LastName
19 UNION SELECT 7 AS PersonId, '7' AS FirstName, '3' AS MiddleName, '7' AS LastName
20 UNION SELECT 8 AS PersonId, '8' AS FirstName, '0' AS MiddleName, '8' AS LastName";
21
22 [ObjectFactory(typeof(Person.Factory))]
23 public class Person
24 {
25 public class Factory : IObjectFactory
26 {
27 public static Type GetType(int id)
28 {
29 int r = id % 4;
30
31 switch (r)
32 {
33 case 0:
34 return typeof(Person);
35 case 1:
36 return typeof(Person1);
37 case 2:
38 return typeof(Person2);
39 default:
40 return typeof(Person3);
41 }
42 }
43
44 #region IObjectFactory Members
45
46 public object CreateInstance(TypeAccessor typeAccessor, InitContext context)
47 {
48 int id = context.DataSource.GetInt32(context.SourceObject,
49 context.DataSource.GetOrdinal("PersonId"));
50
51 context.ObjectMapper = context.MappingSchema.GetObjectMapper(Factory.GetType(id));
52
53 return context.ObjectMapper.TypeAccessor.CreateInstance(context);
54 }
55
56 #endregion
57 }
58
59 public int PersonId;
60 public string FirstName;
61 }
62
63 public class Person1 : Person
64 {
65 public string MiddleName;
66 }
67
68 public class Person2 : Person
69 {
70 public string LastName;
71 }
72
73 public class Person3 : Person
74 {
75 public string MiddleName;
76 public string LastName;
77 }
78
79 [Test]
80 public void PerfomanceTest()
81 {
82 using (DbManager db = new DbManager())
83 {
84
85 List<Person> list = db.SetCommand(_query)
86 .ExecuteList<Person>();
87
88 foreach (Person a in list)
89 {
90 Assert.AreEqual(Person.Factory.GetType(a.PersonId), a.GetType());
91
92 Assert.AreEqual(a.PersonId.ToString(), a.FirstName);
93
94 if (a is Person2)
95 Assert.AreEqual(a.PersonId.ToString(), (a as Person2).LastName);
96
97 if (a is Person1)
98 Assert.AreEqual((a.PersonId % 4).ToString(), (a as Person1).MiddleName);
99
100 if (a is Person3)
101 {
102 Assert.AreEqual(a.PersonId.ToString(), (a as Person3).LastName);
103 Assert.AreEqual((a.PersonId % 4).ToString(), (a as Person3).MiddleName);
104 }
105 }
106 }
107 }
108 }
109 }