comparison UnitTests/Linq/ConvertTest.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.Linq;
3
4 using NUnit.Framework;
5
6 using BLToolkit.Data.DataProvider;
7 using BLToolkit.Data.Linq;
8
9 namespace Data.Linq
10 {
11 [TestFixture]
12 public class ConvertTest : TestBase
13 {
14 [Test]
15 public void Test1()
16 {
17 ForEachProvider(new[] { ProviderName.SQLite },
18 db => Assert.AreEqual(1, (from t in db.Types where t.MoneyValue * t.ID == 1.11m select t).Single().ID));
19 }
20
21 #region Int
22
23 [Test]
24 public void ToInt1()
25 {
26 ForEachProvider(db => AreEqual(
27 from t in Types select Sql.ConvertTo<int>.From(t.MoneyValue),
28 from t in db.Types select Sql.AsSql(Sql.ConvertTo<int>.From(t.MoneyValue))));
29 }
30
31 [Test]
32 public void ToInt2()
33 {
34 ForEachProvider(db => AreEqual(
35 from t in Types select Sql.Convert<int,decimal>(t.MoneyValue),
36 from t in db.Types select Sql.AsSql(Sql.Convert<int,decimal>(t.MoneyValue))));
37 }
38
39 [Test]
40 public void ToBigInt()
41 {
42 ForEachProvider(new[] { ProviderName.MySql }, db => AreEqual(
43 from t in Types select Sql.Convert(Sql.BigInt, t.MoneyValue),
44 from t in db.Types select Sql.Convert(Sql.BigInt, t.MoneyValue)));
45 }
46
47 [Test]
48 public void ToInt64()
49 {
50 ForEachProvider(new[] { ProviderName.MySql }, db => AreEqual(
51 from p in from t in Types select (Int64)t.MoneyValue where p > 0 select p,
52 from p in from t in db.Types select (Int64)t.MoneyValue where p > 0 select p));
53 }
54
55 [Test]
56 public void ConvertToInt64()
57 {
58 ForEachProvider(new[] { ProviderName.MySql }, db => AreEqual(
59 from p in from t in Types select Convert.ToInt64(t.MoneyValue) where p > 0 select p,
60 from p in from t in db.Types select Convert.ToInt64(t.MoneyValue) where p > 0 select p));
61 }
62
63 [Test]
64 public void ToInt()
65 {
66 ForEachProvider(db => AreEqual(
67 from t in Types select Sql.Convert(Sql.Int, t.MoneyValue),
68 from t in db.Types select Sql.Convert(Sql.Int, t.MoneyValue)));
69 }
70
71 [Test]
72 public void ToInt32()
73 {
74 ForEachProvider(db => AreEqual(
75 from p in from t in Types select (Int32)t.MoneyValue where p > 0 select p,
76 from p in from t in db.Types select (Int32)t.MoneyValue where p > 0 select p));
77 }
78
79 [Test]
80 public void ConvertToInt32()
81 {
82 ForEachProvider(db => AreEqual(
83 from p in from t in Types select Convert.ToInt32(t.MoneyValue) where p > 0 select p,
84 from p in from t in db.Types select Convert.ToInt32(t.MoneyValue) where p > 0 select p));
85 }
86
87 [Test]
88 public void ToSmallInt()
89 {
90 ForEachProvider(db => AreEqual(
91 from t in Types select Sql.Convert(Sql.SmallInt, t.MoneyValue),
92 from t in db.Types select Sql.Convert(Sql.SmallInt, t.MoneyValue)));
93 }
94
95 [Test]
96 public void ToInt16()
97 {
98 ForEachProvider(db => AreEqual(
99 from p in from t in Types select (Int16)t.MoneyValue where p > 0 select p,
100 from p in from t in db.Types select (Int16)t.MoneyValue where p > 0 select p));
101 }
102
103 [Test]
104 public void ConvertToInt16()
105 {
106 ForEachProvider(db => AreEqual(
107 from p in from t in Types select Convert.ToInt16(t.MoneyValue) where p > 0 select p,
108 from p in from t in db.Types select Convert.ToInt16(t.MoneyValue) where p > 0 select p));
109 }
110
111 [Test]
112 public void ToTinyInt()
113 {
114 ForEachProvider(db => AreEqual(
115 from t in Types select Sql.Convert(Sql.TinyInt, t.MoneyValue),
116 from t in db.Types select Sql.Convert(Sql.TinyInt, t.MoneyValue)));
117 }
118
119 [Test]
120 public void ToSByte()
121 {
122 ForEachProvider(db => AreEqual(
123 from p in from t in Types select (sbyte)t.MoneyValue where p > 0 select p,
124 from p in from t in db.Types select (sbyte)t.MoneyValue where p > 0 select p));
125 }
126
127 [Test]
128 public void ConvertToSByte()
129 {
130 ForEachProvider(db => AreEqual(
131 from p in from t in Types select Convert.ToSByte(t.MoneyValue) where p > 0 select p,
132 from p in from t in db.Types select Convert.ToSByte(t.MoneyValue) where p > 0 select p));
133 }
134
135 #endregion
136
137 #region UInts
138
139 [Test]
140 public void ToUInt1()
141 {
142 ForEachProvider(new[] { ProviderName.MySql }, db => AreEqual(
143 from t in Types select Sql.ConvertTo<uint>.From(t.MoneyValue),
144 from t in db.Types select Sql.AsSql(Sql.ConvertTo<uint>.From(t.MoneyValue))));
145 }
146
147 [Test]
148 public void ToUInt2()
149 {
150 ForEachProvider(new[] { ProviderName.MySql }, db => AreEqual(
151 from t in Types select Sql.Convert<uint,decimal>(t.MoneyValue),
152 from t in db.Types select Sql.AsSql(Sql.Convert<uint,decimal>(t.MoneyValue))));
153 }
154
155 [Test]
156 public void ToUInt64()
157 {
158 ForEachProvider(new[] { ProviderName.MySql }, db => AreEqual(
159 from p in from t in Types select (UInt64)t.MoneyValue where p > 0 select p,
160 from p in from t in db.Types select (UInt64)t.MoneyValue where p > 0 select p));
161 }
162
163 [Test]
164 public void ConvertToUInt64()
165 {
166 ForEachProvider(new[] { ProviderName.MySql }, db => AreEqual(
167 from p in from t in Types select Convert.ToUInt64(t.MoneyValue) where p > 0 select p,
168 from p in from t in db.Types select Convert.ToUInt64(t.MoneyValue) where p > 0 select p));
169 }
170
171 [Test]
172 public void ToUInt32()
173 {
174 ForEachProvider(new[] { ProviderName.MySql }, db => AreEqual(
175 from p in from t in Types select (UInt32)t.MoneyValue where p > 0 select p,
176 from p in from t in db.Types select (UInt32)t.MoneyValue where p > 0 select p));
177 }
178
179 [Test]
180 public void ConvertToUInt32()
181 {
182 ForEachProvider(new[] { ProviderName.MySql }, db => AreEqual(
183 from p in from t in Types select Convert.ToUInt32(t.MoneyValue) where p > 0 select p,
184 from p in from t in db.Types select Convert.ToUInt32(t.MoneyValue) where p > 0 select p));
185 }
186
187 [Test]
188 public void ToUInt16()
189 {
190 ForEachProvider(db => AreEqual(
191 from p in from t in Types select (UInt16)t.MoneyValue where p > 0 select p,
192 from p in from t in db.Types select (UInt16)t.MoneyValue where p > 0 select p));
193 }
194
195 [Test]
196 public void ConvertToUInt16()
197 {
198 ForEachProvider(db => AreEqual(
199 from p in from t in Types select Convert.ToUInt16(t.MoneyValue) where p > 0 select p,
200 from p in from t in db.Types select Convert.ToUInt16(t.MoneyValue) where p > 0 select p));
201 }
202
203 [Test]
204 public void ToByte()
205 {
206 ForEachProvider(db => AreEqual(
207 from p in from t in Types select (byte)t.MoneyValue where p > 0 select p,
208 from p in from t in db.Types select (byte)t.MoneyValue where p > 0 select p));
209 }
210
211 [Test]
212 public void ConvertToByte([DataContexts] string context)
213 {
214 using (var db = GetDataContext(context))
215 AreEqual(
216 from p in from t in Types select Convert.ToByte(t.MoneyValue) where p > 0 select p,
217 from p in from t in db.Types select Convert.ToByte(t.MoneyValue) where p > 0 select p);
218 }
219
220 #endregion
221
222 #region Floats
223
224 [Test]
225 public void ToDefaultDecimal()
226 {
227 ForEachProvider(db => AreEqual(
228 from t in Types select Sql.Convert(Sql.DefaultDecimal, t.MoneyValue * 1000),
229 from t in db.Types select Sql.Convert(Sql.DefaultDecimal, t.MoneyValue * 1000)));
230 }
231
232 [Test]
233 public void ToDecimal1()
234 {
235 ForEachProvider(db => AreEqual(
236 from t in Types select Sql.Convert(Sql.Decimal(10), t.MoneyValue * 1000),
237 from t in db.Types select Sql.Convert(Sql.Decimal(10), t.MoneyValue * 1000)));
238 }
239
240 [Test]
241 public void ToDecimal2()
242 {
243 ForEachProvider(db => AreEqual(
244 from t in Types select Sql.Convert(Sql.Decimal(10,4), t.MoneyValue),
245 from t in db.Types select Sql.Convert(Sql.Decimal(10,4), t.MoneyValue)));
246 }
247
248 [Test]
249 public void ToDecimal3()
250 {
251 ForEachProvider(db => AreEqual(
252 from p in from t in Types select (Decimal)t.MoneyValue where p > 0 select p,
253 from p in from t in db.Types select (Decimal)t.MoneyValue where p > 0 select p));
254 }
255
256 [Test]
257 public void ConvertToDecimal()
258 {
259 ForEachProvider(db => AreEqual(
260 from p in from t in Types select Convert.ToDecimal(t.MoneyValue) where p > 0 select p,
261 from p in from t in db.Types select Convert.ToDecimal(t.MoneyValue) where p > 0 select p));
262 }
263
264 [Test]
265 public void ToMoney()
266 {
267 ForEachProvider(db => AreEqual(
268 from t in Types select (int)Sql.Convert(Sql.Money, t.MoneyValue),
269 from t in db.Types select (int)Sql.Convert(Sql.Money, t.MoneyValue)));
270 }
271
272 [Test]
273 public void ToSmallMoney()
274 {
275 ForEachProvider(db => AreEqual(
276 from t in Types select (decimal)Sql.Convert(Sql.SmallMoney, t.MoneyValue),
277 from t in db.Types select (decimal)Sql.Convert(Sql.SmallMoney, t.MoneyValue)));
278 }
279
280 [Test]
281 public void ToSqlFloat()
282 {
283 ForEachProvider(db => AreEqual(
284 from t in Types select (int)Sql.Convert(Sql.Float, t.MoneyValue),
285 from t in db.Types select (int)Sql.Convert(Sql.Float, t.MoneyValue)));
286 }
287
288 [Test]
289 public void ToDouble()
290 {
291 ForEachProvider(db => AreEqual(
292 from p in from t in Types select (int)(Double)t.MoneyValue where p > 0 select p,
293 from p in from t in db.Types select (int)(Double)t.MoneyValue where p > 0 select p));
294 }
295
296 [Test]
297 public void ConvertToDouble()
298 {
299 ForEachProvider(db => AreEqual(
300 from p in from t in Types select Convert.ToDouble(t.MoneyValue) where p > 0 select (int)p,
301 from p in from t in db.Types select Convert.ToDouble(t.MoneyValue) where p > 0 select (int)p));
302 }
303
304 [Test]
305 public void ToSqlReal()
306 {
307 ForEachProvider(db => AreEqual(
308 from t in Types select (int)Sql.Convert(Sql.Real, t.MoneyValue),
309 from t in db.Types select (int)Sql.Convert(Sql.Real, t.MoneyValue)));
310 }
311
312 [Test]
313 public void ToSingle()
314 {
315 ForEachProvider(db => AreEqual(
316 from p in from t in Types select (Single)t.MoneyValue where p > 0 select p,
317 from p in from t in db.Types select (Single)t.MoneyValue where p > 0 select p));
318 }
319
320 [Test]
321 public void ConvertToSingle()
322 {
323 ForEachProvider(db => AreEqual(
324 from p in from t in Types select Convert.ToSingle(t.MoneyValue) where p > 0 select (int)p,
325 from p in from t in db.Types select Convert.ToSingle(t.MoneyValue) where p > 0 select (int)p));
326 }
327
328 #endregion
329
330 #region DateTime
331
332 [Test]
333 public void ToSqlDateTime()
334 {
335 ForEachProvider(db => AreEqual(
336 from t in Types select Sql.Convert(Sql.DateTime, t.DateTimeValue.Year + "-01-01 00:20:00"),
337 from t in db.Types select Sql.Convert(Sql.DateTime, t.DateTimeValue.Year + "-01-01 00:20:00")));
338 }
339
340 [Test]
341 public void ToSqlDateTime2()
342 {
343 ForEachProvider(db => AreEqual(
344 from t in Types select Sql.Convert(Sql.DateTime2, t.DateTimeValue.Year + "-01-01 00:20:00"),
345 from t in db.Types select Sql.Convert(Sql.DateTime2, t.DateTimeValue.Year + "-01-01 00:20:00")));
346 }
347
348 [Test]
349 public void ToSqlSmallDateTime()
350 {
351 ForEachProvider(db => AreEqual(
352 from t in Types select Sql.Convert(Sql.SmallDateTime, t.DateTimeValue.Year + "-01-01 00:20:00"),
353 from t in db.Types select Sql.Convert(Sql.SmallDateTime, t.DateTimeValue.Year + "-01-01 00:20:00")));
354 }
355
356 [Test]
357 public void ToSqlDate()
358 {
359 ForEachProvider(db => AreEqual(
360 from t in Types select Sql.Convert(Sql.Date, t.DateTimeValue.Year + "-01-01"),
361 from t in db.Types select Sql.Convert(Sql.Date, t.DateTimeValue.Year + "-01-01")));
362 }
363
364 [Test]
365 public void ToSqlTime([DataContexts(ProviderName.SQLite)] string context)
366 {
367 using (var db = GetDataContext(context))
368 AreEqual(
369 from t in Types select Sql.Convert(Sql.Time, t.DateTimeValue.Hour + ":01:01"),
370 from t in db.Types select Sql.Convert(Sql.Time, t.DateTimeValue.Hour + ":01:01"));
371 }
372
373 DateTime ToDateTime(DateTimeOffset dto)
374 {
375 return new DateTime(dto.Year, dto.Month, dto.Day, dto.Hour, dto.Minute, dto.Second);
376 }
377
378 [Test]
379 public void ToSqlDateTimeOffset()
380 {
381 ForEachProvider(db => AreEqual(
382 from t in Types select ToDateTime(Sql.Convert(Sql.DateTimeOffset, t.DateTimeValue.Year + "-01-01 00:20:00")),
383 from t in db.Types select ToDateTime(Sql.Convert(Sql.DateTimeOffset, t.DateTimeValue.Year + "-01-01 00:20:00"))));
384 }
385
386 [Test]
387 public void ToDateTime()
388 {
389 ForEachProvider(db => AreEqual(
390 from p in from t in Types select DateTime.Parse(t.DateTimeValue.Year + "-01-01 00:00:00") where p.Day > 0 select p,
391 from p in from t in db.Types select DateTime.Parse(t.DateTimeValue.Year + "-01-01 00:00:00") where p.Day > 0 select p));
392 }
393
394 [Test]
395 public void ConvertToDateTime()
396 {
397 ForEachProvider(db => AreEqual(
398 from p in from t in Types select Convert.ToDateTime(t.DateTimeValue.Year + "-01-01 00:00:00") where p.Day > 0 select p,
399 from p in from t in db.Types select Convert.ToDateTime(t.DateTimeValue.Year + "-01-01 00:00:00") where p.Day > 0 select p));
400 }
401
402 #endregion
403
404 #region String
405
406 [Test]
407 public void ToChar()
408 {
409 ForEachProvider(db => AreEqual(
410 from t in Types select Sql.Convert(Sql.Char(20), t.MoneyValue).Trim(' ', '0', '.'),
411 from t in db.Types select Sql.Convert(Sql.Char(20), t.MoneyValue).Trim(' ', '0', '.')));
412 }
413
414 [Test]
415 public void ToDefaultChar()
416 {
417 ForEachProvider(new[] { "Oracle", "DevartOracle", ProviderName.Firebird, ProviderName.PostgreSQL }, db => AreEqual(
418 from t in Types select Sql.Convert(Sql.DefaultChar, t.MoneyValue).Trim(' ', '0', '.'),
419 from t in db.Types select Sql.Convert(Sql.DefaultChar, t.MoneyValue).Trim(' ', '0', '.')));
420 }
421
422 [Test]
423 public void ToVarChar()
424 {
425 ForEachProvider(db => AreEqual(
426 from t in Types select Sql.Convert(Sql.VarChar(20), t.MoneyValue).Trim(' ', '0', '.'),
427 from t in db.Types select Sql.Convert(Sql.VarChar(20), t.MoneyValue).Trim(' ', '0', '.')));
428 }
429
430 [Test]
431 public void ToDefaultVarChar()
432 {
433 ForEachProvider(new[] { "Oracle", "DevartOracle", ProviderName.Firebird, ProviderName.PostgreSQL }, db => AreEqual(
434 from t in Types select Sql.Convert(Sql.DefaultVarChar, t.MoneyValue).Trim(' ', '0', '.'),
435 from t in db.Types select Sql.Convert(Sql.DefaultVarChar, t.MoneyValue).Trim(' ', '0', '.')));
436 }
437
438 [Test]
439 public void ToNChar()
440 {
441 ForEachProvider(db => AreEqual(
442 from t in Types select Sql.Convert(Sql.NChar(20), t.MoneyValue).Trim(' ', '0', '.'),
443 from t in db.Types select Sql.Convert(Sql.NChar(20), t.MoneyValue).Trim(' ', '0', '.')));
444 }
445
446 [Test]
447 public void ToDefaultNChar()
448 {
449 ForEachProvider(new[] { "Oracle", "DevartOracle", ProviderName.Firebird, ProviderName.PostgreSQL }, db => AreEqual(
450 from t in Types select Sql.Convert(Sql.DefaultNChar, t.MoneyValue).Trim(' ', '0', '.'),
451 from t in db.Types select Sql.Convert(Sql.DefaultNChar, t.MoneyValue).Trim(' ', '0', '.')));
452 }
453
454 [Test]
455 public void ToNVarChar()
456 {
457 ForEachProvider(db => AreEqual(
458 from t in Types select Sql.Convert(Sql.NVarChar(20), t.MoneyValue).Trim(' ', '0', '.'),
459 from t in db.Types select Sql.Convert(Sql.NVarChar(20), t.MoneyValue).Trim(' ', '0', '.')));
460 }
461
462 [Test]
463 public void ToDefaultNVarChar()
464 {
465 ForEachProvider(new[] { "Oracle", "DevartOracle", ProviderName.Firebird, ProviderName.PostgreSQL }, db => AreEqual(
466 from t in Types select Sql.Convert(Sql.DefaultNVarChar, t.MoneyValue).Trim(' ', '0', '.'),
467 from t in db.Types select Sql.Convert(Sql.DefaultNVarChar, t.MoneyValue).Trim(' ', '0', '.')));
468 }
469
470 [Test]
471 public void DecimalToString()
472 {
473 ForEachProvider(db => AreEqual(
474 from p in from t in Types select Convert.ToString(t.MoneyValue) where p.Length > 0 select p.Replace(',', '.').TrimEnd('0', '.'),
475 from p in from t in db.Types select Convert.ToString(t.MoneyValue) where p.Length > 0 select p.Replace(',', '.').TrimEnd('0', '.')));
476 }
477
478 [Test]
479 public void ByteToString()
480 {
481 ForEachProvider(db => AreEqual(
482 from p in from t in Types select ((byte)t.ID).ToString() where p.Length > 0 select p,
483 from p in from t in db.Types select ((byte)t.ID).ToString() where p.Length > 0 select p));
484 }
485
486 [Test]
487 public void GuidToString()
488 {
489 ForEachProvider(db => AreEqual(
490 from t in Types where Sql.ConvertTo<string>.From(t.GuidValue) == "febe3eca-cb5f-40b2-ad39-2979d312afca" select t.GuidValue,
491 from t in db.Types where Sql.ConvertTo<string>.From(t.GuidValue) == "febe3eca-cb5f-40b2-ad39-2979d312afca" select t.GuidValue));
492 }
493
494 #endregion
495
496 #region Boolean
497
498 [Test]
499 public void ToBit1()
500 {
501 ForEachProvider(db => AreEqual(
502 from t in from t in Types where Sql.Convert(Sql.Bit, t.MoneyValue) select t select t,
503 from t in from t in db.Types where Sql.Convert(Sql.Bit, t.MoneyValue) select t select t));
504 }
505
506 [Test]
507 public void ToBit2()
508 {
509 ForEachProvider(db => AreEqual(
510 from t in from t in Types where !Sql.Convert(Sql.Bit, t.MoneyValue - 4.5m) select t select t,
511 from t in from t in db.Types where !Sql.Convert(Sql.Bit, t.MoneyValue - 4.5m) select t select t));
512 }
513
514 [Test]
515 public void ConvertToBoolean1()
516 {
517 ForEachProvider(db => AreEqual(
518 from p in from t in Types select Convert.ToBoolean(t.MoneyValue) where p == true select p,
519 from p in from t in db.Types select Convert.ToBoolean(t.MoneyValue) where p == true select p));
520 }
521
522 [Test]
523 public void ConvertToBoolean2()
524 {
525 ForEachProvider(db => AreEqual(
526 from p in from t in Types select Convert.ToBoolean(t.MoneyValue - 4.5m) where !p select p,
527 from p in from t in db.Types select Convert.ToBoolean(t.MoneyValue - 4.5m) where !p select p));
528 }
529
530 #endregion
531 }
532 }