comparison UnitTests/CS/Data/ExecuteScalarListTest.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.SqlTypes;
5 using System.IO;
6 using System.Xml;
7
8 using NUnit.Framework;
9
10 using BLToolkit.Data;
11
12 namespace Data
13 {
14 [TestFixture]
15 public class ExecuteScalarListTest
16 {
17 //[TestFixtureSetUp]
18 public void SetUp()
19 {
20 using (var db = new DbManager())
21 {
22 var query = "INSERT INTO Person(FirstName, LastName, Gender) SELECT FirstName, LastName, Gender FROM Person";
23
24 db.SetCommand(query).ExecuteNonQuery(); // 4
25 db.SetCommand(query).ExecuteNonQuery(); // 8
26 db.SetCommand(query).ExecuteNonQuery(); // 16
27 db.SetCommand(query).ExecuteNonQuery(); // 32
28 db.SetCommand(query).ExecuteNonQuery(); // 64
29 db.SetCommand(query).ExecuteNonQuery(); // 128
30 db.SetCommand(query).ExecuteNonQuery(); // 256
31 db.SetCommand(query).ExecuteNonQuery(); // 512
32 db.SetCommand(query).ExecuteNonQuery(); // 1024
33 db.SetCommand(query).ExecuteNonQuery(); // 2048
34 db.SetCommand(query).ExecuteNonQuery(); // 4096
35 db.SetCommand(query).ExecuteNonQuery(); // 8192
36 db.SetCommand(query).ExecuteNonQuery(); // 16384
37 db.SetCommand(query).ExecuteNonQuery(); // 32768
38 db.SetCommand(query).ExecuteNonQuery(); // 65536
39 db.SetCommand(query).ExecuteNonQuery(); // 128k
40 db.SetCommand(query).ExecuteNonQuery(); // 256k
41 db.SetCommand(query).ExecuteNonQuery(); // 512k
42 db.SetCommand(query).ExecuteNonQuery(); // 1m
43 }
44 }
45
46 //[TestFixtureTearDown]
47 public void TearDown()
48 {
49 using (var db = new DbManager())
50 {
51 db.SetCommand("DELETE FROM Person WHERE PersonID > 2").ExecuteNonQuery();
52 }
53 }
54
55 [Test]
56 public void ScalarListTest()
57 {
58 using (var db = new DbManager())
59 {
60 var array = db
61 #if SQLITE || SQLCE || SQLCE
62 .SetCommand("SELECT * FROM Person")
63 #else
64 .SetSpCommand("Person_SelectAll")
65 #endif
66 .ExecuteScalarList(typeof(int));
67
68 Assert.IsNotNull(array);
69 Assert.IsTrue(array.Count > 0);
70 }
71 }
72
73 [Test]
74 public void ScalarListTest2()
75 {
76 using (var db = new DbManager())
77 {
78 var array = new ArrayList();
79
80 db.SetCommand("SELECT * FROM Person").ExecuteScalarList(array, typeof(int));
81
82 Assert.IsNotNull(array);
83 Assert.IsTrue(array.Count > 0);
84 }
85 }
86
87 [Test]
88 public void ScalarListTest3()
89 {
90 using (var db = new DbManager())
91 {
92 var array = db.SetCommand("SELECT * FROM Person").ExecuteScalarList(typeof(string),1);
93
94 Assert.IsNotNull(array);
95 Assert.IsTrue(array.Count > 0);
96 }
97 }
98
99 [Test]
100 public void ScalarListTest4()
101 {
102 using (var db = new DbManager())
103 {
104 var array = new ArrayList();
105
106 db.SetCommand("SELECT * FROM Person").ExecuteScalarList(array, typeof(string), "LastName");
107
108 Assert.IsNotNull(array);
109 Assert.IsTrue(array.Count > 0);
110 }
111 }
112
113 [Test]
114 public void GenericsScalarListTest()
115 {
116 var cmd = "SELECT PersonID FROM Person UNION ALL SELECT NULL";
117 #if !MSSQL && !SQLCE
118 cmd += " FROM dual";
119 #endif
120
121 using (var db = new DbManager())
122 {
123 var array = db.SetCommand(cmd).ExecuteScalarList<int?>();
124
125 Assert.IsNotNull(array);
126 Assert.IsTrue(array.Count > 0);
127 Assert.IsNull(array[array.Count - 1]);
128 }
129 }
130
131 [Test]
132 public void GenericsScalarListTest2()
133 {
134 var cmd = "SELECT PersonID FROM Person UNION ALL SELECT NULL";
135 #if !MSSQL && !SQLCE
136 cmd += " FROM dual";
137 #endif
138 using (var db = new DbManager())
139 {
140 var array = new List<int>();
141
142 db.SetCommand(cmd).ExecuteScalarList(array);
143
144 Assert.IsNotNull(array);
145 Assert.IsTrue(array.Count > 0);
146 Console.WriteLine("Records processed: {0}", array.Count);
147 }
148 }
149
150 [Test]
151 public void GenericsScalarListTest3()
152 {
153 using (var db = new DbManager())
154 {
155 var array = db.SetCommand("SELECT * FROM Person").ExecuteScalarList<string>(1);
156
157 Assert.IsNotNull(array);
158 Assert.IsTrue(array.Count > 0);
159 }
160 }
161
162 [Test]
163 public void GenericsScalarListTest4()
164 {
165 using (var db = new DbManager())
166 {
167 var array = new List<string>();
168
169 db.SetCommand("SELECT * FROM Person").ExecuteScalarList(array, "LastName");
170
171 Assert.IsNotNull(array);
172 Assert.IsTrue(array.Count > 0);
173 }
174 }
175
176 [Test]
177 public void GenericsScalarListTest5()
178 {
179 using (var db = new DbManager())
180 {
181 var cmd = "SELECT PersonID FROM Person UNION ALL SELECT NULL";
182 #if !MSSQL && !SQLCE
183 cmd += " FROM dual";
184 #endif
185 var array = db.SetCommand(cmd).ExecuteScalarList<uint?>();
186
187 Assert.IsNotNull(array);
188 Assert.IsTrue(array.Count > 0);
189 Assert.IsNull(array[array.Count - 1]);
190 }
191 }
192
193 [Test]
194 public void GenericsScalarListTest6()
195 {
196 using (var db = new DbManager())
197 {
198 var array = new List<uint>();
199
200 db.SetCommand("SELECT * FROM Person").ExecuteScalarList(array);
201
202 Assert.IsNotNull(array);
203 Assert.IsTrue(array.Count > 0);
204 }
205 }
206
207 private List<T> TestType<T>(DbManager db, string columnName) where T : class
208 {
209 var array = db
210 .SetCommand(string.Format("SELECT {0} FROM DataTypeTest ORDER BY DataTypeID", columnName))
211 .ExecuteScalarList<T>();
212
213 Assert.IsNotNull(array);
214 Assert.IsTrue (array.Count > 1);
215 Assert.IsNotNull(array[1]);
216
217 return array;
218 }
219
220 private List<T?> TestNullableType<T>(DbManager db, string columnName) where T : struct
221 {
222 List<T?> array = db
223 .SetCommand(string.Format("SELECT {0} FROM DataTypeTest ORDER BY DataTypeID", columnName))
224 .ExecuteScalarList<T?>();
225
226 Assert.IsNotNull(array);
227 Assert.IsTrue (array.Count > 1);
228 Assert.IsNull (array[0]);
229 Assert.IsTrue (array[1].HasValue);
230
231 return array;
232 }
233
234 private List<T?> TestINullableType<T>(DbManager db, string columnName) where T : struct, INullable
235 {
236 var array = db
237 .SetCommand(string.Format("SELECT {0} FROM DataTypeTest ORDER BY DataTypeID", columnName))
238 .ExecuteScalarList<T?>();
239
240 Assert.IsNotNull(array);
241 Assert.IsTrue(array.Count > 1);
242 Assert.IsTrue(array[0].HasValue);
243 Assert.IsTrue(array[0].Value.IsNull);
244 Assert.IsTrue(array[1].HasValue);
245
246 return array;
247 }
248
249 [Test]
250 public void GenericsScalarListDataTypesTest()
251 {
252 using (var db = new DbManager())
253 {
254 // Base types
255 //
256 TestNullableType<Boolean> (db, "Boolean_");
257 TestNullableType<Byte> (db, "Byte_");
258 TestNullableType<Char> (db, "Char_");
259 TestNullableType<DateTime> (db, "DateTime_");
260 TestNullableType<Decimal> (db, "Decimal_");
261 TestNullableType<Double> (db, "Double_");
262 TestNullableType<Guid> (db, "Guid_");
263 TestNullableType<Int16> (db, "Int16_");
264 TestNullableType<Int32> (db, "Int32_");
265 TestNullableType<Int64> (db, "Int64_");
266 TestNullableType<SByte> (db, "SByte_");
267 TestNullableType<Single> (db, "Single_");
268 TestType<String> (db, "String_");
269 TestNullableType<UInt16> (db, "UInt16_");
270 TestNullableType<UInt32> (db, "UInt32_");
271 TestNullableType<UInt64> (db, "UInt64_");
272
273 #if !ORACLE
274 // Sql types
275 //
276 TestINullableType<SqlBinary> (db, "Binary_");
277 TestINullableType<SqlBoolean> (db, "Boolean_");
278 TestINullableType<SqlByte> (db, "Byte_");
279 TestType<SqlBytes> (db, "Bytes_");
280 TestType<SqlChars> (db, "String_");
281 TestINullableType<SqlDateTime>(db, "DateTime_");
282 TestINullableType<SqlDecimal> (db, "Decimal_");
283 TestINullableType<SqlDouble> (db, "Double_");
284 #if !ACCESS
285 TestINullableType<SqlGuid> (db, "Bytes_");
286 #endif
287 TestINullableType<SqlGuid> (db, "Guid_");
288 TestINullableType<SqlInt16> (db, "Int16_");
289 TestINullableType<SqlInt32> (db, "Int32_");
290 TestINullableType<SqlInt64> (db, "Int64_");
291 TestINullableType<SqlMoney> (db, "Money_");
292 TestINullableType<SqlString> (db, "String_");
293 TestINullableType<SqlSingle> (db, "Single_");
294 TestType<SqlXml> (db, "Xml_");
295 #endif
296 // BLToolkit extension
297 var arrays = TestType<Byte[]>(db, "Binary_");
298 Console.WriteLine("{0}", arrays[1][0]);
299
300 var streams = TestType<Stream>(db, "Bytes_");
301 Console.WriteLine("{0}", streams[1].ReadByte());
302
303 var symbols = TestType<Char[]>(db, "String_");
304 Assert.AreEqual(symbols[1][0], 's');
305
306 var xmlReaders = TestType<XmlReader>(db, "Xml_");
307 xmlReaders[1].MoveToContent();
308 Assert.IsTrue(xmlReaders[1].ReadToDescendant("element"));
309 Console.WriteLine("{0}", xmlReaders[1].GetAttribute("strattr"));
310
311 var xmlDocs = TestType<XmlDocument>(db, "Xml_");
312 Assert.IsNotNull(xmlDocs[1]);
313 Assert.IsNotNull(xmlDocs[1].DocumentElement);
314 Console.WriteLine("{0}", xmlDocs[1].DocumentElement.GetAttribute("strattr"));
315 }
316 }
317 }
318 }