Mercurial > pub > bltoolkit
comparison UnitTests/CS/Mapping/MapTest.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; | |
3 using System.Collections.Generic; | |
4 using System.Data; | |
5 using System.Data.SqlTypes; | |
6 using System.Globalization; | |
7 using BLToolkit.Common; | |
8 using BLToolkit.Reflection; | |
9 using NUnit.Framework; | |
10 | |
11 using BLToolkit.Mapping; | |
12 | |
13 namespace Mapping | |
14 { | |
15 [TestFixture, Category("Mapping")] | |
16 public class MapTest : TestFixtureBase | |
17 { | |
18 #region Enum To Int | |
19 | |
20 public class EnumClass | |
21 { | |
22 public enum Enum | |
23 { | |
24 Value1 = 1, | |
25 Value2 = 2 | |
26 } | |
27 | |
28 public Enum Value = Enum.Value1; | |
29 public Enum? NullableValue = Enum.Value2; | |
30 } | |
31 | |
32 public class IntClass | |
33 { | |
34 public int Value; | |
35 public int? NullableValue; | |
36 } | |
37 | |
38 [Test] | |
39 public void EnumToInt() | |
40 { | |
41 var list1 = new ArrayList(); | |
42 | |
43 list1.Add(new EnumClass()); | |
44 | |
45 var list2 = Map.ListToList(list1, typeof(IntClass)); | |
46 | |
47 Assert.AreEqual(1, ((IntClass)list2[0]).Value); | |
48 Assert.AreEqual(2, ((IntClass)list2[0]).NullableValue); | |
49 } | |
50 | |
51 #endregion | |
52 | |
53 #region ToEnum, FromEnum | |
54 | |
55 [DefaultValue(Enum1.Value3)] | |
56 [MapValue(Enum1.Value2, "2")] | |
57 public enum Enum1 | |
58 { | |
59 [MapValue("1")] Value1, | |
60 [NullValue] Value2, | |
61 [MapValue("3")] Value3 | |
62 } | |
63 | |
64 [Test] | |
65 public void ToEnum() | |
66 { | |
67 Assert.AreEqual(Enum1.Value1, Map.ValueToEnum("1", typeof(Enum1))); | |
68 Assert.AreEqual(Enum1.Value2, Map.ValueToEnum(null, typeof(Enum1))); | |
69 Assert.AreEqual(Enum1.Value3, Map.ValueToEnum("2727", typeof(Enum1))); | |
70 } | |
71 | |
72 [Test] | |
73 public void FromEnum() | |
74 { | |
75 Assert.AreEqual("1", Map.EnumToValue(Enum1.Value1)); | |
76 Assert.IsNull ( Map.EnumToValue(Enum1.Value2)); | |
77 Assert.AreEqual("3", Map.EnumToValue(Enum1.Value3)); | |
78 } | |
79 | |
80 public enum Enum2 : short | |
81 { | |
82 Value1 = 1, | |
83 [NullValue] Value2, | |
84 [DefaultValue]Value3 | |
85 } | |
86 | |
87 [Test] | |
88 public void ToShortEnum() | |
89 { | |
90 Assert.AreEqual(Enum2.Value1, Map.ValueToEnum(1, typeof(Enum2))); | |
91 Assert.AreEqual(Enum2.Value2, Map.ValueToEnum(null, typeof(Enum2))); | |
92 Assert.AreEqual(Enum2.Value3, Map.ValueToEnum(3, typeof(Enum2))); | |
93 Assert.AreEqual(Enum2.Value3, Map.ValueToEnum(2727, typeof(Enum2))); | |
94 | |
95 // DateTime.Now can't converted to Enum2. | |
96 // Expected result in this case is default value. | |
97 // | |
98 Assert.AreEqual(Enum2.Value3, Map.ValueToEnum(DateTime.Now, typeof(Enum2))); | |
99 } | |
100 | |
101 public enum Enum3 : long | |
102 { | |
103 Value1 = 1, | |
104 } | |
105 | |
106 [Test, ExpectedException(typeof(InvalidCastException))] | |
107 public void ToEnumWithIncompatibleValue() | |
108 { | |
109 // An object can't be converted to Enum3. | |
110 // Expected result is an exception, since Enum3 does not have | |
111 // a default value defined. | |
112 // | |
113 Map.ValueToEnum(new object(), typeof(Enum3)); | |
114 } | |
115 | |
116 [Test] | |
117 public void ToEnumWithCompatibleValue() | |
118 { | |
119 // 123 is not defined for this enum, but can be converted to. | |
120 // Enum3 does not have a default value defined, so just cast 123 to Enum3 | |
121 // | |
122 Assert.AreEqual((Enum3)123, Map.ValueToEnum(123, typeof(Enum3))); | |
123 | |
124 // DateTime.Now also can be converted to Enum3 (as ticks). | |
125 // | |
126 Map.ValueToEnum(DateTime.Now, typeof(Enum3)); | |
127 } | |
128 #endregion | |
129 | |
130 #region ObjectToObject | |
131 | |
132 public class SourceObject | |
133 { | |
134 public int Field1 = 10; | |
135 public string Field2 = "20"; | |
136 public double Field3 = 30.0; | |
137 } | |
138 | |
139 public class Object1 | |
140 { | |
141 public int Field1; | |
142 public int Field2; | |
143 public int Field3; | |
144 } | |
145 | |
146 [Test] | |
147 public void ObjectToObjectOO() | |
148 { | |
149 SourceObject so = new SourceObject(); | |
150 Object1 o = new Object1(); | |
151 | |
152 Map.ObjectToObject(so, o); | |
153 | |
154 Assert.AreEqual(10, o.Field1); | |
155 Assert.AreEqual(20, o.Field2); | |
156 Assert.AreEqual(30, o.Field3); | |
157 } | |
158 | |
159 [Test] | |
160 public void ObjectToObjectOT() | |
161 { | |
162 SourceObject so = new SourceObject(); | |
163 Object1 o = (Object1)Map.ObjectToObject(so, typeof(Object1)); | |
164 | |
165 Assert.AreEqual(10, o.Field1); | |
166 Assert.AreEqual(20, o.Field2); | |
167 Assert.AreEqual(30, o.Field3); | |
168 } | |
169 | |
170 [Test] | |
171 public void ObjectToObjectTO() | |
172 { | |
173 SourceObject so = new SourceObject(); | |
174 Object1 o = Map.ObjectToObject<Object1>(so); | |
175 | |
176 Assert.AreEqual(10, o.Field1); | |
177 Assert.AreEqual(20, o.Field2); | |
178 Assert.AreEqual(30, o.Field3); | |
179 } | |
180 | |
181 public class DefaultNullType | |
182 { | |
183 [NullValue(-1)] | |
184 public int NullableInt; | |
185 } | |
186 | |
187 #endregion | |
188 | |
189 #region DataRowToObject | |
190 | |
191 [Test] | |
192 public void DataRowToObjectD() | |
193 { | |
194 DataTable table = new DataTable(); | |
195 | |
196 table.Columns.Add("NullableInt", typeof(int)); | |
197 | |
198 table.Rows.Add(new object[] { DBNull.Value }); | |
199 table.Rows.Add(new object[] { 1 }); | |
200 table.AcceptChanges(); | |
201 | |
202 DefaultNullType dn = (DefaultNullType)Map.DataRowToObject(table.Rows[0], typeof(DefaultNullType)); | |
203 | |
204 Assert.AreEqual(-1, dn.NullableInt); | |
205 | |
206 Map.DataRowToObject(table.Rows[1], DataRowVersion.Current, dn); | |
207 | |
208 Assert.AreEqual(1, dn.NullableInt); | |
209 } | |
210 | |
211 #endregion | |
212 | |
213 #region ObjectToDictionary | |
214 | |
215 [Test] | |
216 public void ObjectToDictionary() | |
217 { | |
218 SourceObject so = new SourceObject(); | |
219 Hashtable ht = new Hashtable(); | |
220 | |
221 Map.ObjectToDictionary(so, ht); | |
222 | |
223 Assert.AreEqual(10, ht["Field1"]); | |
224 Assert.AreEqual("20", ht["Field2"]); | |
225 Assert.AreEqual(30, ht["Field3"]); | |
226 } | |
227 | |
228 #endregion | |
229 | |
230 #region DictionaryToObject | |
231 | |
232 [Test] | |
233 public void DictionaryToObject() | |
234 { | |
235 var so = new SourceObject(); | |
236 var ht = Map.ObjectToDictionary(so); | |
237 var o1 = (Object1)Map.DictionaryToObject(ht, typeof(Object1)); | |
238 | |
239 Assert.AreEqual(10, o1.Field1); | |
240 Assert.AreEqual(20, o1.Field2); | |
241 Assert.AreEqual(30, o1.Field3); | |
242 } | |
243 | |
244 #endregion | |
245 | |
246 #region DataRowToDictionary | |
247 | |
248 [Test] | |
249 public void DataRowToDictionary() | |
250 { | |
251 DataTable table = GetDataTable(); | |
252 Hashtable hash = Map.DataRowToDictionary(table.Rows[0]); | |
253 | |
254 Assert.AreEqual(table.Rows[0]["ID"], hash["ID"]); | |
255 Assert.AreEqual(table.Rows[0]["Name"], hash["Name"]); | |
256 Assert.AreEqual(table.Rows[0]["Date"], hash["Date"]); | |
257 } | |
258 | |
259 #endregion | |
260 | |
261 #region DictionaryToDataRow | |
262 | |
263 [Test] | |
264 public void DictionaryToDataRow() | |
265 { | |
266 DataTable table1 = GetDataTable(); | |
267 Hashtable hash = Map.DataRowToDictionary(table1.Rows[0]); | |
268 DataTable table2 = new DataTable(); | |
269 | |
270 Map.DictionaryToDataRow(hash, table2); | |
271 | |
272 Assert.AreEqual(table1.Rows[0]["ID"], table2.Rows[0]["ID"]); | |
273 Assert.AreEqual(table1.Rows[0]["Name"], table2.Rows[0]["Name"]); | |
274 Assert.AreEqual(table1.Rows[0]["Date"], table2.Rows[0]["Date"]); | |
275 } | |
276 | |
277 #endregion | |
278 | |
279 #region SqlTypes | |
280 | |
281 public class SqlTypeTypes | |
282 { | |
283 public class SourceObject | |
284 { | |
285 public string s1 = "123"; | |
286 public SqlString s2 = "1234"; | |
287 public int i1 = 123; | |
288 public SqlInt32 i2 = 1234; | |
289 | |
290 public DateTime d1 = DateTime.Now; | |
291 public SqlDateTime d2 = DateTime.Now; | |
292 } | |
293 | |
294 public class Object1 | |
295 { | |
296 public SqlString s1; | |
297 public string s2; | |
298 public SqlInt32 i1; | |
299 public int i2; | |
300 | |
301 public SqlDateTime d1; | |
302 public DateTime d2; | |
303 } | |
304 } | |
305 | |
306 [Test] | |
307 public void SqlTypes() | |
308 { | |
309 SqlTypeTypes.SourceObject so = new SqlTypeTypes.SourceObject(); | |
310 SqlTypeTypes.Object1 o = (SqlTypeTypes.Object1)Map.ObjectToObject(so, typeof(SqlTypeTypes.Object1)); | |
311 | |
312 Console.WriteLine(o.s1); Assert.AreEqual("123", o.s1.Value); | |
313 Console.WriteLine(o.s2); Assert.AreEqual("1234", o.s2); | |
314 | |
315 Console.WriteLine(o.i1); Assert.IsTrue(o.i1.Value == 123); | |
316 Console.WriteLine(o.i2); Assert.IsTrue(o.i2 == 1234); | |
317 | |
318 Console.WriteLine("{0} - {1}", so.d2, o.d2); Assert.AreEqual(o.d2, so.d2.Value); | |
319 //Console.WriteLine("{0} - {1}", s.d1, d.d1); Assert.IsTrue(d.d1.Value == s.d1); | |
320 } | |
321 | |
322 #endregion | |
323 | |
324 #region Arrays | |
325 | |
326 public class ArrayTypes | |
327 { | |
328 public class SourceObject | |
329 { | |
330 public int[,] DimArray = new int[2, 2] { {1,2}, {3,4} }; | |
331 public string[] StrArray = new string[] {"5","4","3","2","1"}; | |
332 public int[] IntArray = new int[] {1,2,3,4,5}; | |
333 public Enum1[] EnmArray = new Enum1[] {Enum1.Value1,Enum1.Value2,Enum1.Value3}; | |
334 | |
335 public byte[][,][][,] ComplexArray = InitComplexArray(); | |
336 public static byte[][,][][,] InitComplexArray() | |
337 { | |
338 byte[][,][][,] ret = new byte[1][,][][,]; | |
339 ret[0] = new byte[1,1][][,]; | |
340 ret[0][0,0] = new byte[1][,]; | |
341 ret[0][0,0][0] = new byte[,] { {1,2}, {3,4} }; | |
342 | |
343 return ret; | |
344 } | |
345 } | |
346 | |
347 public class DestObject | |
348 { | |
349 public float[,] DimArray; | |
350 public string[] StrArray; | |
351 public string[] IntArray; | |
352 public string[] EnmArray; | |
353 public sbyte[][,][][,] ComplexArray; | |
354 } | |
355 | |
356 public class IncompatibleObject | |
357 { | |
358 public int[][] DimArray; | |
359 } | |
360 } | |
361 | |
362 [Test] | |
363 public void ArrayTypesTest() | |
364 { | |
365 ArrayTypes.SourceObject so = new ArrayTypes.SourceObject(); | |
366 ArrayTypes.DestObject o = (ArrayTypes.DestObject)Map.ObjectToObject(so, typeof(ArrayTypes.DestObject)); | |
367 | |
368 Console.WriteLine(o.DimArray); Assert.AreEqual(so.DimArray[0,0], (int)o.DimArray[0,0]); | |
369 Console.WriteLine(o.StrArray); Assert.AreEqual(so.StrArray, o.StrArray); | |
370 Console.WriteLine(o.IntArray); Assert.AreEqual(so.IntArray[0].ToString(), o.IntArray[0]); | |
371 | |
372 Console.WriteLine(o.ComplexArray); Assert.IsTrue(o.ComplexArray[0][0,0][0][1,1] == 4); | |
373 } | |
374 | |
375 [Test, ExpectedException(typeof(InvalidCastException))] | |
376 public void IncompatibleArrayTypesTest() | |
377 { | |
378 ArrayTypes.SourceObject so = new ArrayTypes.SourceObject(); | |
379 Map.ObjectToObject(so, typeof(ArrayTypes.IncompatibleObject)); | |
380 } | |
381 | |
382 #endregion | |
383 | |
384 #region SourceListToDestList | |
385 | |
386 [Test] | |
387 public void ListToList() | |
388 { | |
389 DataTable table = GetDataTable(); | |
390 ArrayList list1 = Map.DataTableToList(table, typeof(TestObject)); | |
391 ArrayList list2 = new ArrayList(); | |
392 | |
393 Map.ListToList(list1, list2, typeof(TestObject)); | |
394 | |
395 CompareLists(table, list2); | |
396 } | |
397 | |
398 [Test] | |
399 public void TableToList() | |
400 { | |
401 DataTable table = GetDataTable(); | |
402 ArrayList list = Map.DataTableToList(table, typeof(TestObject)); | |
403 | |
404 CompareLists(table, list); | |
405 } | |
406 | |
407 [Test] | |
408 public void ListToTable1() | |
409 { | |
410 DataTable table1 = GetDataTable(); | |
411 ArrayList list = Map.DataTableToList(table1, typeof(TestObject)); | |
412 DataTable table2 = Map.ListToDataTable(list); | |
413 | |
414 table2.AcceptChanges(); | |
415 | |
416 CompareLists(table1, table2); | |
417 } | |
418 | |
419 [Test] | |
420 public void ListToTable2() | |
421 { | |
422 DataTable table1 = GetDataTable(); | |
423 ArrayList list = Map.DataTableToList(table1, typeof(TestObject)); | |
424 DataTable table2 = table1.Clone(); | |
425 | |
426 Map.ListToDataTable(list, table2); | |
427 | |
428 table2.AcceptChanges(); | |
429 | |
430 CompareLists(table1, table2); | |
431 } | |
432 | |
433 [Test] | |
434 public void TableToTable1() | |
435 { | |
436 DataTable table1 = GetDataTable(); | |
437 DataTable table2 = Map.DataTableToDataTable(table1); | |
438 | |
439 table2.AcceptChanges(); | |
440 | |
441 CompareLists(table1, table2); | |
442 } | |
443 | |
444 [Test] | |
445 public void TableToTable2() | |
446 { | |
447 DataTable table1 = GetDataTable(); | |
448 DataTable table2 = new DataTable(); | |
449 | |
450 Map.DataTableToDataTable(table1, table2); | |
451 | |
452 table2.AcceptChanges(); | |
453 | |
454 CompareLists(table1, table2); | |
455 } | |
456 | |
457 [Test] | |
458 public void TableToDictionary() | |
459 { | |
460 DataTable table = GetDataTable(); | |
461 IDictionary dic = Map.DataTableToDictionary(table, new SortedList(), "ID", typeof(TestObject)); | |
462 | |
463 CompareLists(table, Map.ListToList(dic.Values, typeof(TestObject))); | |
464 } | |
465 | |
466 [Test] | |
467 public void ListToDictionary() | |
468 { | |
469 DataTable table = GetDataTable(); | |
470 ArrayList list = Map.DataTableToList (table, typeof(TestObject)); | |
471 IDictionary dic = Map.ListToDictionary(list, new SortedList(), "ID", typeof(TestObject)); | |
472 | |
473 CompareLists(table, Map.ListToList(dic.Values, typeof(TestObject))); | |
474 } | |
475 | |
476 [Test] | |
477 public void DictionaryToDictionary() | |
478 { | |
479 DataTable table = GetDataTable(); | |
480 ArrayList list = Map.DataTableToList (table, typeof(TestObject)); | |
481 IDictionary dic1 = Map.ListToDictionary (list, new SortedList(), "ID", typeof(TestObject)); | |
482 IDictionary dic2 = Map.DictionaryToDictionary(dic1, new SortedList(), "@ID", typeof(TestObject)); | |
483 | |
484 CompareLists(table, Map.ListToList(dic2.Values, typeof(TestObject))); | |
485 } | |
486 | |
487 #endregion | |
488 | |
489 #region ObjectToDataRow | |
490 | |
491 public class DataRowTestType | |
492 { | |
493 public Int32 Int32Column = 12345; | |
494 public String StringColumn = "string"; | |
495 public Byte[] ByteArrayColumn = new Byte[]{1,2,3,4,5}; | |
496 //public Byte[] ByteArrayColumn2 = null; | |
497 } | |
498 | |
499 [Test] | |
500 public void ObjectToDataRowTest() | |
501 { | |
502 DataTable table = new DataTable(); | |
503 DataRowTestType obj = new DataRowTestType(); | |
504 | |
505 Map.ObjectToDataRow(obj, table); | |
506 | |
507 Assert.IsNotEmpty(table.Rows); | |
508 DataRow dr = table.Rows[0]; | |
509 | |
510 Assert.AreEqual(table.Columns["Int32Column"] .DataType, typeof(Int32)); | |
511 Assert.AreEqual(table.Columns["StringColumn"] .DataType, typeof(String)); | |
512 Assert.AreEqual(table.Columns["ByteArrayColumn"] .DataType, typeof(Byte[])); | |
513 //Assert.AreEqual(table.Columns["ByteArrayColumn2"].DataType, typeof(Byte[])); | |
514 | |
515 Assert.AreEqual(obj.Int32Column, dr["Int32Column"]); | |
516 Assert.AreEqual(obj.StringColumn, dr["StringColumn"]); | |
517 Assert.AreEqual(obj.ByteArrayColumn, dr["ByteArrayColumn"]); | |
518 //Assert.AreEqual(obj.ByteArrayColumn2, dr["ByteArrayColumn2"]); | |
519 } | |
520 | |
521 #endregion | |
522 | |
523 class ObjectStr1 | |
524 { | |
525 public string Field; | |
526 } | |
527 | |
528 class ObjectDateTime1 | |
529 { | |
530 public DateTime Field = DateTime.Now; | |
531 } | |
532 | |
533 [Test] | |
534 public void TestConverter() | |
535 { | |
536 var prev = Convert<DateTime,string>.From; | |
537 | |
538 Convert<DateTime,string>.From = str => | |
539 DateTime.ParseExact(str, new[] { "yyyymmdd" }, CultureInfo.InvariantCulture, DateTimeStyles.None); | |
540 | |
541 var map = Map.GetObjectMapper<List<ObjectStr1>,List<ObjectDateTime1>>(); | |
542 var obj = map(new List<ObjectStr1> { new ObjectStr1 { Field = "20110102" } }); | |
543 | |
544 Assert.That(obj.Count, Is.EqualTo(1)); | |
545 Assert.That(obj[0].Field.Year, Is.EqualTo(2011)); | |
546 | |
547 Convert<DateTime,string>.From = prev; | |
548 } | |
549 | |
550 public abstract class Test | |
551 { | |
552 public abstract int Id { get; set; } | |
553 } | |
554 | |
555 [Test] | |
556 public void AccessorTest() | |
557 { | |
558 var test = TypeAccessor<Test>.CreateInstance(); | |
559 var mapper = Map.GetObjectMapper<Test,Test>(); | |
560 var testCopy = mapper(test); | |
561 } | |
562 } | |
563 } |