comparison UnitTests/CS/DataAccess/DictionaryTest.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
5 using NUnit.Framework;
6
7 using BLToolkit.Common;
8 using BLToolkit.DataAccess;
9 using BLToolkit.TypeBuilder;
10 using BLToolkit.Mapping;
11
12 namespace DataAccess
13 {
14 [TestFixture]
15 public class DictionaryTest
16 {
17 public class Person
18 {
19 [MapField("PersonID"), PrimaryKey]
20 public int ID;
21 public string LastName;
22 public string FirstName;
23 public string MiddleName;
24 }
25
26 public class Derived : Person
27 {
28 [MapIgnore]
29 public string Ignore;
30 public DateTime Date;
31 }
32
33 // Same as Person, but does not have any primary keys.
34 public class PersonNoPK
35 {
36 [MapField("PersonID")]
37 public int ID;
38 public string LastName;
39 public string FirstName;
40 public string MiddleName;
41 }
42
43 // Same as Person, but has a number of primary keys.
44 public class PersonMultiPK
45 {
46 [MapField("PersonID"), PrimaryKey]
47 public int ID;
48 [PrimaryKey(22)]
49 public string LastName;
50 public string FirstName;
51 public string MiddleName;
52 }
53
54 [ObjectType(typeof(Person))]
55 public abstract class TestAccessor : DataAccessor
56 {
57 private const string TEST_QUERY = "SELECT * FROM Person WHERE PersonID < 3";
58
59 [SqlQuery("SELECT * FROM Person")]
60 [Index("ID")]
61 public abstract Hashtable SelectAll1();
62
63 [NoInstance]
64 public abstract Hashtable Persons
65 {
66 [SqlQuery("SELECT * FROM Person")]
67 [Index("ID")]
68 get;
69 }
70
71 [SqlQuery(TEST_QUERY)]
72 [Index("@PersonID", "LastName")]
73 public abstract Hashtable SelectAll2();
74
75 [SqlQuery(TEST_QUERY)]
76 [Index("@PersonID")]
77 [ScalarFieldName("FirstName")]
78 [ObjectType(typeof(string))]
79 public abstract Hashtable SelectAll3();
80
81 [SqlQuery(TEST_QUERY)]
82 [Index("PersonID", "LastName")]
83 [ScalarFieldName("FirstName")]
84 [ObjectType(typeof(string))]
85 public abstract Hashtable SelectAll4();
86
87 [SqlQuery(TEST_QUERY)]
88 [Index("PersonID", "LastName")]
89 [ScalarFieldName(1)]
90 [ObjectType(typeof(string))]
91 public abstract Hashtable SelectAll5();
92
93 [SqlQuery(TEST_QUERY)]
94 [Index("PersonID", "LastName")]
95 [ScalarFieldName(1)]
96 [ObjectType(typeof(string))]
97 public abstract IDictionary SelectAllAsIDictionary();
98
99 // Primary Key(s) => scalar filed. This will fail, since
100 // we can not figure out both key type and scalar object type.
101 // Note that version with generics works just fine.
102 //
103 [SqlQuery(TEST_QUERY)]
104 [ScalarFieldName("FirstName"), ObjectType(typeof(string))]
105 public abstract Hashtable ScalarDictionaryByPK();
106
107 [SqlQuery(TEST_QUERY)]
108 [ObjectType(typeof(PersonNoPK))]
109 public abstract Hashtable Keyless();
110
111 [SqlQuery(TEST_QUERY)]
112 [ObjectType(typeof(PersonMultiPK))]
113 public abstract Hashtable MultiPK();
114
115 [SqlQuery(TEST_QUERY)]
116 [ObjectType(typeof(PersonMultiPK))]
117 public abstract void MultiPKReturnVoid([Destination] Hashtable dictionary);
118
119 [SqlQuery(TEST_QUERY)]
120 public abstract Hashtable DictionaryByPK();
121
122 [SqlQuery(TEST_QUERY)]
123 [Index("@PersonID")]
124 public abstract Hashtable DictionaryByIndex();
125
126 [SqlQuery(TEST_QUERY)]
127 [Index(0, 1)]
128 public abstract Hashtable DictionaryByMapIndex();
129
130 [SqlQuery(TEST_QUERY)]
131 [Index(0, 1)]
132 public abstract IDictionary DictionaryByMapIndexWithDestination([Destination] Hashtable dictionary);
133
134 [SqlQuery(TEST_QUERY)]
135 [Index(0), ScalarFieldName(1)]
136 public abstract void GenericsScalarDictionaryByIndexReturnVoid1([Destination] IDictionary<int, string> dictionary);
137
138 [SqlQuery("SELECT * FROM Person")]
139 [Index(new string[] { "ID" })]
140 public abstract Dictionary<int, Person> SelectAllT1();
141
142 [SqlQuery("SELECT * FROM Person")]
143 [Index("@PersonID", "LastName")]
144 public abstract Dictionary<CompoundValue, Person> SelectAllT2();
145
146 [SqlQuery("SELECT * FROM Person")]
147 [Index("@PersonID")]
148 [ScalarFieldName("FirstName")]
149 public abstract Dictionary<int, string> SelectAllT3();
150
151 [SqlQuery("SELECT * FROM Person")]
152 [Index("PersonID", "LastName")]
153 [ScalarFieldName("FirstName")]
154 public abstract Dictionary<CompoundValue, string> SelectAllT4();
155
156 [SqlQuery("SELECT * FROM Person")]
157 [Index("PersonID", "LastName")]
158 [ScalarFieldName(1)]
159 public abstract Dictionary<CompoundValue, string> SelectAllT5();
160
161 [SqlQuery("SELECT * FROM Person")]
162 [Index("PersonID", "LastName")]
163 [ScalarFieldName(1)]
164 public abstract IDictionary<CompoundValue, string> SelectAllAsIDictionaryT();
165
166 [SqlQuery(TEST_QUERY)]
167 [ScalarFieldName("FirstName")]
168 public abstract Dictionary<object, string> GenericsScalarDictionaryByPK();
169
170 [SqlQuery(TEST_QUERY)]
171 [ScalarFieldName(1)] // Index from Db table Person, not type Person!
172 public abstract Dictionary<int, string> GenericsScalarDictionaryByPK2();
173
174 [SqlQuery(TEST_QUERY)]
175 [Index(0), ScalarFieldName(1)]
176 public abstract Dictionary<int, string> GenericsScalarDictionaryByIndex();
177
178 [SqlQuery(TEST_QUERY)]
179 [Index(0), ScalarFieldName(1)]
180 public abstract void GenericsScalarDictionaryByIndexReturnVoid([Destination] IDictionary<int, string> dictionary);
181
182 [SqlQuery(TEST_QUERY)]
183 [Index(0, 1), ScalarFieldName(1)]
184 public abstract Dictionary<CompoundValue, string> GenericsScalarDictionaryByMapIndex();
185
186 [SqlQuery(TEST_QUERY)]
187 [Index(0)]
188 public abstract Dictionary<int, PersonNoPK> GenericsScalarDictionaryByPKWithCustomType();
189
190 [SqlQuery(TEST_QUERY)]
191 [ObjectType(typeof(Person))]
192 public abstract Dictionary<int, object> GenericsScalarDictionaryByPKWithObjectType();
193
194 #if SQLITE || SQLCE
195 [SqlQuery("SELECT * FROM Person")]
196 #else
197 [ActionName("SelectAll")]
198 #endif
199 [Index("ID")]
200 public abstract Dictionary<uint, Person> SelectAllT7();
201
202 #if SQLITE || SQLCE
203 [SqlQuery("SELECT * FROM Person")]
204 #else
205 [ActionName("SelectAll")]
206 #endif
207 public abstract Dictionary<long, Person> SelectAllT8();
208
209 #if SQLITE || SQLCE
210 [SqlQuery("SELECT * FROM Person")]
211 #else
212 [SprocName("Person_SelectAll")]
213 #endif
214 public abstract Dictionary<long, Derived> SelectAllDerived();
215
216 #if SQLITE || SQLCE
217 [SqlQuery("SELECT * FROM Person")]
218 #else
219 [SprocName("Person_SelectAll")]
220 #endif
221 public abstract Dictionary<CompoundValue, PersonMultiPK> SelectAllT9();
222 }
223
224 private TestAccessor _da;
225
226 #if ORACLE
227 private const decimal _id = 1m;
228 #elif SQLITE
229 private const long _id = 1;
230 #else
231 private const int _id = 1;
232 #endif
233
234 public DictionaryTest()
235 {
236 _da = (TestAccessor)DataAccessor.CreateInstance(typeof(TestAccessor));
237 }
238
239 [Test]
240 public void Test()
241 {
242 Hashtable dic1 = _da.SelectAll1();
243 Assert.AreEqual("John", ((Person)dic1[1]).FirstName);
244
245 Hashtable dic2 = _da.SelectAll2();
246 Assert.AreEqual("John", ((Person)dic2[new CompoundValue(_id, "Pupkin")]).FirstName);
247
248 Hashtable dic3 = _da.SelectAll3();
249 Assert.AreEqual("John", dic3[_id]);
250
251 Hashtable dic4 = _da.SelectAll4();
252 Assert.AreEqual("John", dic4[new CompoundValue(_id, "Pupkin")]);
253
254 Hashtable dic5 = _da.SelectAll5();
255 Assert.AreEqual("John", dic5[new CompoundValue(_id, "Pupkin")]);
256
257 IDictionary dic6 = _da.SelectAllAsIDictionary();
258 Assert.AreEqual("John", dic6[new CompoundValue(_id, "Pupkin")]);
259
260 Dictionary<int, Person> dict1 = _da.SelectAllT1();
261 Assert.AreEqual("John", dict1[1].FirstName);
262
263 Dictionary<CompoundValue, Person> dict2 = _da.SelectAllT2();
264 Assert.AreEqual("John", dict2[new CompoundValue(_id, "Pupkin")].FirstName);
265
266 Dictionary<int, string> dict3 = _da.SelectAllT3();
267 Assert.AreEqual("John", dict3[1]);
268
269 Dictionary<CompoundValue, string> dict4 = _da.SelectAllT4();
270 Assert.AreEqual("John", dict4[new CompoundValue(_id, "Pupkin")]);
271
272 Dictionary<CompoundValue, string> dict5 = _da.SelectAllT5();
273 Assert.AreEqual("John", dict5[new CompoundValue(_id, "Pupkin")]);
274
275 IDictionary<CompoundValue, string> dict6 = _da.SelectAllAsIDictionaryT();
276 Assert.AreEqual("John", dict6[new CompoundValue(_id, "Pupkin")]);
277 }
278
279 [Test]
280 public void AbstractGetterTest()
281 {
282 Hashtable dic = _da.Persons;
283 Assert.AreEqual("John", ((Person)dic[1]).FirstName);
284 }
285
286 [Test, ExpectedException(typeof(DataAccessException))]
287 public void KeylessTest()
288 {
289 TestAccessor da = (TestAccessor)DataAccessor.CreateInstance(typeof(TestAccessor));
290
291 // Exception here:
292 // Index is not defined for the method 'TestAccessor.Keyless'
293 da.Keyless();
294 }
295
296 [Test]
297 public void DictionaryByPKTest()
298 {
299 TestAccessor da = (TestAccessor)DataAccessor.CreateInstance(typeof(TestAccessor));
300
301 Hashtable persons = da.DictionaryByPK();
302
303 Assert.IsNotNull(persons);
304 Assert.IsTrue(persons.Count > 0);
305 Assert.IsNull(persons[-1]);
306
307 Person actualValue = (Person)persons[1];
308 Assert.IsNotNull(actualValue);
309 Assert.AreEqual("John", actualValue.FirstName);
310 }
311
312 [Test]
313 public void DictionaryByIndexTest()
314 {
315 TestAccessor da = (TestAccessor)DataAccessor.CreateInstance(typeof(TestAccessor));
316
317 Hashtable persons = da.DictionaryByIndex();
318
319 Assert.IsNotNull(persons);
320 Assert.IsTrue(persons.Count > 0);
321 Assert.IsNull(persons[-1]);
322
323 Person actualValue = (Person)persons[_id];
324 Assert.IsNotNull(actualValue);
325 Assert.AreEqual("John", actualValue.FirstName);
326 }
327
328 [Test]
329 public void DictionaryByMapIndexTest()
330 {
331 TestAccessor da = (TestAccessor)DataAccessor.CreateInstance(typeof(TestAccessor));
332
333 Hashtable persons = da.DictionaryByMapIndex();
334
335 Assert.IsNotNull(persons);
336 Assert.IsTrue(persons.Count > 0);
337 Assert.IsNull(persons[-1]);
338
339 Person actualValue = (Person)persons[new CompoundValue(1, "Pupkin")];
340 Assert.IsNotNull(actualValue);
341 Assert.AreEqual("John", actualValue.FirstName);
342 }
343
344 [Test]
345 public void DictionaryByMapIndexTestWithDestination()
346 {
347 TestAccessor da = (TestAccessor)DataAccessor.CreateInstance(typeof(TestAccessor));
348
349 IDictionary persons = da.DictionaryByMapIndexWithDestination(new Hashtable());
350
351 Assert.IsNotNull(persons);
352 Assert.IsTrue(persons.Count > 0);
353 Assert.IsNull(persons[-1]);
354
355 Person actualValue = (Person)persons[new CompoundValue(1, "Pupkin")];
356 Assert.IsNotNull(actualValue);
357 Assert.AreEqual("John", actualValue.FirstName);
358 }
359
360 [Test, ExpectedException(typeof(DataAccessException))]
361 public void ScalarDictionaryByPKTest()
362 {
363 TestAccessor da = (TestAccessor)DataAccessor.CreateInstance(typeof(TestAccessor));
364
365 // Exception here:
366 // Index is not defined for the method 'TestAccessor.ScalarDictionaryByPK'
367 da.ScalarDictionaryByPK();
368 }
369
370 [Test]
371 public void MultiPKTest()
372 {
373 TestAccessor da = (TestAccessor)DataAccessor.CreateInstance(typeof(TestAccessor));
374
375 Hashtable persons = da.MultiPK();
376
377 Assert.IsNotNull(persons);
378 Assert.IsTrue(persons.Count > 0);
379 Assert.IsNull(persons[new CompoundValue(-1, "NoSuchPerson")]);
380
381 PersonMultiPK actualValue = (PersonMultiPK)persons[new CompoundValue(1, "Pupkin")];
382 Assert.IsNotNull(actualValue);
383 Assert.AreEqual("John", actualValue.FirstName);
384 }
385
386 [Test]
387 public void MultiPKTestReturnVoid()
388 {
389 TestAccessor da = (TestAccessor)DataAccessor.CreateInstance(typeof(TestAccessor));
390
391 Hashtable persons = new Hashtable();
392 da.MultiPKReturnVoid(persons);
393
394 Assert.IsNotNull(persons);
395 Assert.IsTrue(persons.Count > 0);
396 Assert.IsNull(persons[new CompoundValue(-1, "NoSuchPerson")]);
397
398 PersonMultiPK actualValue = (PersonMultiPK)persons[new CompoundValue(1, "Pupkin")];
399 Assert.IsNotNull(actualValue);
400 Assert.AreEqual("John", actualValue.FirstName);
401 }
402
403 [Test]
404 public void GenericsScalarDictionaryByPKTest()
405 {
406 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
407
408 Dictionary<object, string> persons = da.GenericsScalarDictionaryByPK();
409
410 Assert.IsNotNull(persons);
411 Assert.IsTrue(persons.Count > 0);
412
413 string actualValue = persons[_id];
414 Assert.IsNotNull(actualValue);
415 Assert.AreEqual("John", actualValue);
416 }
417
418 [Test]
419 public void GenericsScalarDictionaryByPKTest2()
420 {
421 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
422
423 Dictionary<int, string> persons = da.GenericsScalarDictionaryByPK2();
424
425 Assert.IsNotNull(persons);
426 Assert.IsTrue(persons.Count > 0);
427
428 string actualValue = persons[1];
429 Assert.IsNotNull(actualValue);
430 Assert.AreEqual("John", actualValue);
431 }
432
433 [Test]
434 public void GenericsScalarDictionaryByIndexTest()
435 {
436 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
437
438 Dictionary<int, string> persons = da.GenericsScalarDictionaryByIndex();
439
440 Assert.IsNotNull(persons);
441 Assert.IsTrue(persons.Count > 0);
442
443 string actualValue = persons[1];
444 Assert.IsNotNull(actualValue);
445 Assert.AreEqual("John", actualValue);
446 }
447
448 [Test]
449 public void GenericsScalarDictionaryByIndexReturnVoidTest()
450 {
451 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
452
453 IDictionary<int, string> persons = new Dictionary<int, string>();
454 da.GenericsScalarDictionaryByIndexReturnVoid(persons);
455
456 Assert.IsNotNull(persons);
457 Assert.IsTrue(persons.Count > 0);
458
459 string actualValue = persons[1];
460 Assert.IsNotNull(actualValue);
461 Assert.AreEqual("John", actualValue);
462 }
463
464 [Test]
465 public void GenericsScalarDictionaryByMapIndexTest()
466 {
467 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
468 Dictionary<CompoundValue, string> persons = da.GenericsScalarDictionaryByMapIndex();
469
470 Assert.IsNotNull(persons);
471 Assert.IsTrue(persons.Count > 0);
472
473 string actualValue = persons[new CompoundValue(_id, "John")];
474 Assert.IsNotNull(actualValue);
475 Assert.AreEqual("John", actualValue);
476 }
477
478 [Test]
479 public void GenericsScalarDictionaryByPKWithCustomTypeTest()
480 {
481 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
482 Dictionary<int, PersonNoPK> persons = da.GenericsScalarDictionaryByPKWithCustomType();
483
484 Assert.IsNotNull(persons);
485 Assert.IsTrue(persons.Count > 0);
486
487 PersonNoPK actualValue = persons[1];
488 Assert.IsNotNull(actualValue);
489 Assert.AreEqual("John", actualValue.FirstName);
490 }
491
492 [Test]
493 public void GenericsScalarDictionaryByPKWithObjectTypeTest()
494 {
495 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
496 Dictionary<int, object> persons = da.GenericsScalarDictionaryByPKWithObjectType();
497
498 Assert.IsNotNull(persons);
499 Assert.IsTrue(persons.Count > 0);
500
501 Person actualValue = (Person) persons[1];
502 Assert.IsNotNull(actualValue);
503 Assert.AreEqual("John", actualValue.FirstName);
504 }
505
506 [Test]
507 public void GenericsDictionaryMismatchKeyTypeTest()
508 {
509 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
510 Dictionary<uint, Person> persons = da.SelectAllT7();
511
512 Assert.IsNotNull(persons);
513 Assert.IsTrue(persons.Count > 0);
514
515 Person actualValue = persons[1];
516 Assert.IsNotNull(actualValue);
517 Assert.AreEqual("John", actualValue.FirstName);
518 }
519
520 [Test]
521 public void GenericsDictionaryMismatchKeyTypeTest2()
522 {
523 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
524 Dictionary<long, Person> persons = da.SelectAllT8();
525
526 Assert.IsNotNull(persons);
527 Assert.IsTrue(persons.Count > 0);
528
529 Person actualValue = persons[1];
530 Assert.IsNotNull(actualValue);
531 Assert.AreEqual("John", actualValue.FirstName);
532 }
533
534 [Test]
535 public void GenericsDictionaryMismatchKeyTypeWithHierarchyTest()
536 {
537 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
538 Dictionary<long, Derived> persons = da.SelectAllDerived();
539
540 Assert.IsNotNull(persons);
541 Assert.IsTrue(persons.Count > 0);
542
543 Person actualValue = persons[1];
544 Assert.IsNotNull(actualValue);
545 Assert.AreEqual("John", actualValue.FirstName);
546 }
547
548 [Test]
549 public void GenericsDictionaryMismatchKeyTypeCompoundValueTest()
550 {
551 TestAccessor da = DataAccessor.CreateInstance<TestAccessor>();
552 Dictionary<CompoundValue, PersonMultiPK> persons = da.SelectAllT9();
553
554 Assert.IsNotNull(persons);
555 Assert.IsTrue(persons.Count > 0);
556
557 PersonMultiPK actualValue = persons[new CompoundValue(1, "Pupkin")];
558 Assert.IsNotNull(actualValue);
559 Assert.AreEqual("John", actualValue.FirstName);
560 }
561 }
562 }