Mercurial > pub > bltoolkit
comparison UnitTests/CS/Data/InnerTypesTest.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.Data; | |
3 | |
4 using NUnit.Framework; | |
5 | |
6 using BLToolkit.Reflection; | |
7 using BLToolkit.Data; | |
8 using BLToolkit.Mapping; | |
9 | |
10 #pragma warning disable 0252 | |
11 | |
12 namespace Data | |
13 { | |
14 [TestFixture] | |
15 public class InnerTypesTest | |
16 { | |
17 public class TypeMapper: MemberMapper | |
18 { | |
19 public override object GetValue(object o) | |
20 { | |
21 var value = (Type)MemberAccessor.GetValue(o); | |
22 return (null != value && MapMemberInfo.NullValue != value)? value.FullName: null; | |
23 } | |
24 | |
25 public override void SetValue(object o, object value) | |
26 { | |
27 MemberAccessor.SetValue(o, (null != value)? | |
28 Type.GetType((string)value, true, true): MapMemberInfo.NullValue); | |
29 } | |
30 } | |
31 | |
32 public class First | |
33 { | |
34 public string Name; | |
35 } | |
36 | |
37 public abstract class Last | |
38 { | |
39 public string Name; | |
40 public abstract string Suffix { get; set; } | |
41 | |
42 // Inner type of the inner type | |
43 // | |
44 [MapField("FirstName", "Name")] | |
45 public abstract First First { get; set; } | |
46 | |
47 // This reference type field will be ignored | |
48 // | |
49 public Type Type; | |
50 } | |
51 | |
52 [MapField("FirstName", "First.Name")] | |
53 [MapField("LastName", "Last.Name")] | |
54 [MapField("LastSuffix", "Last.Suffix")] | |
55 [MapField("LastFirstName", "Last.First.Name")] | |
56 public abstract class Person | |
57 { | |
58 [MapField("PersonID")] | |
59 public int ID; | |
60 public First First = new First(); | |
61 public abstract Last Last { get; set; } | |
62 public string Name; | |
63 [MemberMapper(typeof(TypeMapper))] | |
64 public Type Type; | |
65 } | |
66 | |
67 public abstract class Person2 | |
68 { | |
69 [MapField("PersonID")] | |
70 public int ID; | |
71 | |
72 [MapField("FirstName", "Name")] | |
73 public First First = new First(); | |
74 | |
75 [MapField(Format="Last{0}")] | |
76 public abstract Last Last { get; set; } | |
77 | |
78 public string Name; | |
79 public string Type; | |
80 } | |
81 | |
82 [Test] | |
83 public void MapFieldTest() | |
84 { | |
85 var p = (Person)TypeAccessor.CreateInstance(typeof(Person)); | |
86 | |
87 p.ID = 12345; | |
88 p.First.Name = "Crazy"; | |
89 p.Last.Name = "Frog"; | |
90 p.Last.Suffix = "Jr"; | |
91 p.Last.Type = typeof(DbManager); | |
92 p.Last.First.Name = "Crazy Frog"; | |
93 p.Name = "Froggy"; | |
94 p.Type = typeof(DbManager); | |
95 | |
96 var p2 = (Person2)Map.ObjectToObject(p, typeof(Person2)); | |
97 | |
98 Assert.AreEqual(p.ID, p2.ID); | |
99 Assert.AreEqual(p.First.Name, p2.First.Name); | |
100 Assert.AreEqual(p.Last.Name, p2.Last.Name); | |
101 Assert.AreEqual(p.Last.Suffix, p2.Last.Suffix); | |
102 Assert.AreEqual(p.Last.First.Name, p2.Last.First.Name); | |
103 Assert.AreEqual(p.Name, p2.Name); | |
104 Assert.AreEqual(p.Type.FullName, p2.Type); | |
105 | |
106 // The 'Last.Type' field should be ignored by mapping process. | |
107 // | |
108 Assert.IsNull(p2.Last.Type); | |
109 } | |
110 | |
111 [Test] | |
112 public void CreateParametersTest() | |
113 { | |
114 IDbDataParameter[] parameters; | |
115 var p = (Person)TypeAccessor.CreateInstance(typeof(Person)); | |
116 p.ID = 12345; | |
117 p.First.Name = "Crazy"; | |
118 p.Last.Name = "Frog"; | |
119 p.Name = "Froggy"; | |
120 p.Type = typeof(DbManager); | |
121 | |
122 using (var db = new DbManager()) | |
123 { | |
124 parameters = db.CreateParameters(p); | |
125 } | |
126 | |
127 Assert.IsNotNull(parameters); | |
128 Assert.AreEqual(7, parameters.Length); | |
129 | |
130 foreach (var parameter in parameters) | |
131 Console.WriteLine("{0}: {1}", parameter.ParameterName, parameter.Value); | |
132 } | |
133 | |
134 public abstract class Template2 | |
135 { | |
136 public abstract int ID { get; set; } | |
137 public abstract string DisplayName { get; set; } | |
138 } | |
139 | |
140 [MapField("TPL_ID", "tpl.ID")] | |
141 [MapField("TPL_DisplayName", "tpl.DisplayName")] | |
142 public abstract class Template1 | |
143 { | |
144 public abstract int ID { get; set; } | |
145 public abstract string DisplayName { get; set; } | |
146 | |
147 public abstract Template2 tpl { get; set; } | |
148 } | |
149 | |
150 [Test] | |
151 public void TemplateTest() | |
152 { | |
153 using (var db = new DbManager()) | |
154 { | |
155 var cmd = @" | |
156 SELECT | |
157 1 as ID, | |
158 '2' as DisplayName, | |
159 3 as TPL_ID, | |
160 '4' as TPL_DisplayName"; | |
161 #if ORACLE || FIREBIRD | |
162 cmd += " FROM dual"; | |
163 #endif | |
164 var list = db | |
165 .SetCommand(cmd) | |
166 .ExecuteList(typeof(Template1)); | |
167 | |
168 Assert.IsNotNull(list); | |
169 } | |
170 } | |
171 } | |
172 } |