comparison UnitTests/CS/Data/ExecuteForEach.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.Data;
4
5 using NUnit.Framework;
6
7 using BLToolkit.Data;
8
9 namespace Data
10 {
11 [TestFixture]
12 public class ExecuteForEach
13 {
14 public class TypeWrapper<T>
15 {
16 public TypeWrapper(T value)
17 {
18 _value = value;
19 }
20
21 private T _value;
22 public T Value
23 {
24 get { return _value; }
25 set { _value = value; }
26 }
27 }
28
29 [Test]
30 public void TestFixedTypes()
31 {
32 RunTest(new int[] { 1, 2, 3, 4 });
33 RunTest(new bool[] { true, false });
34 RunTest(new DateTime[] { DateTime.Now, DateTime.Today });
35 RunTest(new double[] { 1, 2, 3, 4 });
36 }
37
38 [Test]
39 public void TestVarTypes()
40 {
41 RunTest(new byte[][] { new byte[] { 1, 2 }, new byte[] { 3, 4 } });
42 RunTest(new char[][] { new char[] { '1', '2' }, new char[] { '3', '4' } });
43 }
44
45 [Test]
46 public void TestDecimal()
47 {
48 RunTest(new decimal[] { 1, 2, 3, 4 });
49 }
50
51 [Test]
52 public void TestString()
53 {
54 RunTest(new string[] { "1", "2", "3", "4" });
55 }
56
57 public class Item
58 {
59 public int Length;
60 public string Name;
61
62 public Item(string s)
63 {
64 Length = s.Length;
65 Name = s;
66 }
67
68 public Item()
69 {
70 Name = string.Empty;
71 }
72 }
73
74 [Test]
75 public void TestStringWithDiffLength()
76 {
77 List<Item> test = new List<Item>();
78 test.Add(new Item("aaaa"));
79 test.Add(new Item("bb"));
80 test.Add(new Item("ccccccc"));
81
82 using (DbManager db = new DbManager())
83 {
84 db
85 .BeginTransaction()
86 .SetCommand(
87 @"if exists (select 1 from sysobjects where id = object_id('_tmp'))
88 drop table _tmp
89 create table _tmp ( Length int, name varchar(50) )")
90 .ExecuteNonQuery();
91
92
93 db
94 .SetCommand(CommandType.Text,
95 "insert into _tmp ( [Length], [name] ) VALUES ( @Length, @name )")
96 .ExecuteForEach<Item>(test);
97
98 List<Item> actial = db
99 .SetCommand("select [Length], [name] from _tmp order by [Name]")
100 .ExecuteList<Item>();
101
102 Assert.AreEqual(test.Count, actial.Count);
103
104 for (int i = 0; i < test.Count; ++i)
105 {
106 Assert.AreEqual(test[i].Length, actial[i].Length);
107 Assert.AreEqual(test[i].Name, actial[i].Name);
108 }
109 }
110 }
111
112 private static void RunTest<T>(T[] args)
113 {
114 List<TypeWrapper<T>> col = new List<TypeWrapper<T>>();
115
116 col.AddRange(Array.ConvertAll<T, TypeWrapper<T>>(args, delegate(T val) { return new TypeWrapper<T>(val); }));
117
118 using (DbManager db = new DbManager())
119 {
120 db
121 .SetCommand(@"SELECT @Value as 'value'")
122 .ExecuteForEach<TypeWrapper<T>>(col);
123 }
124 }
125 }
126 }