comparison UnitTests/CS/DataAccess/DataAccessorTest.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.ComponentModel;
5 using System.Data;
6
7 using NUnit.Framework;
8
9 using BLToolkit.Data;
10 using BLToolkit.DataAccess;
11 using BLToolkit.EditableObjects;
12 using BLToolkit.Mapping;
13 using BLToolkit.Reflection;
14 using BLToolkit.TypeBuilder;
15 using BLToolkit.Validation;
16
17 using PersonDataSet = DataAccessTest.PersonDataSet2;
18
19 namespace DataAccess
20 {
21 namespace Other
22 {
23 public abstract class Person : DataAccessorTest.Person
24 {
25 [MaxLength(256), Required]
26 public abstract string Diagnosis { get; set; }
27 }
28 }
29
30 [TestFixture]
31 public class DataAccessorTest
32 {
33 public enum Gender
34 {
35 [MapValue("F")] Female,
36 [MapValue("M")] Male,
37 [MapValue("U")] Unknown,
38 [MapValue("O")] Other
39 }
40
41 public interface IPerson
42 {
43 int ID { get; set; }
44 string LastName { get; set; }
45 string FirstName { get; set; }
46 string MiddleName { get; set; }
47 IList Territories { get; set; }
48 }
49
50 [TableName("Person")]
51 public abstract class Person : EditableObject, IPerson
52 {
53 [PrimaryKey, NonUpdatable]
54 [MapField("PersonID")] public abstract int ID { get; set; }
55 [MaxLength(50), Required] public abstract string FirstName { get; set; }
56 [MaxLength(50), NullValue("")] public abstract string MiddleName { get; set; }
57 [MaxLength(50), Required] public abstract string LastName { get; set; }
58 [Required] public abstract Gender Gender { get; set; }
59
60 public abstract IList Territories { get; set; }
61 }
62
63 public abstract class PersonAccessor : DataAccessor
64 {
65 public abstract int Person_SelectAll();
66 public abstract void Person_SelectAll(DbManager db);
67 public abstract Person SelectByName(string firstName, string lastName);
68
69 [SprocName("Person_SelectByName"), DiscoverParameters]
70 public abstract Person AnySprocName(string anyParameterName, string otherParameterName);
71
72 [ActionName("SelectByName")]
73 public abstract Person AnyActionName(string firstName, string lastName);
74
75 [ActionName("SelectByName")]
76 public abstract Person AnyParamName(
77 [ParamName("FirstName")] string name1,
78 #if ORACLE
79 [ParamName("LastName")] string name2
80 #else
81 [ParamName("@LastName")] string name2
82 #endif
83 );
84
85 [ActionName("SelectByName"), ObjectType(typeof(Person))]
86 public abstract IPerson SelectByNameReturnInterface(string firstName, string lastName);
87
88 [ActionName("SelectByName"), ObjectType(typeof(Person))]
89 public abstract void SelectByNameReturnVoid(string firstName, string lastName, [Destination] object p);
90
91 public abstract IPerson SelectByName(string firstName, string lastName, [Destination] Person p);
92
93 public abstract Person Insert([Destination(NoMap = false)] Person e);
94
95 [SprocName("Person_Insert_OutputParameter")]
96 public abstract void Insert_OutputParameter([Direction.Output("PERSONID")] Person e);
97
98 [SprocName("Scalar_ReturnParameter")]
99 public abstract void Insert_ReturnParameter([Direction.ReturnValue("@PersonID"),
100 Direction.Ignore("PersonID", "FirstName", "LastName", "MiddleName", "Gender")] Person e);
101
102 [SprocName("Scalar_ReturnParameter")]
103 public abstract void Insert_ReturnParameter2([Direction.ReturnValue("ID"),
104 Direction.Ignore("PersonID", "FirstName", "LastName", "MiddleName", "Gender")] Person e);
105
106 [ActionName("SelectAll"), ObjectType(typeof(Person))]
107 public abstract ArrayList SameTypeName();
108
109 [ActionName("SelectAll"), ObjectType(typeof(Person))]
110 public abstract IList SelectAllAsIList();
111
112 [ActionName("SelectByName")]
113 public abstract Person SameTypeName(string firstName, string lastName);
114
115 [ActionName("SelectByKey")]
116 public abstract Person ParamNullID ([ParamNullValue(1)] int id);
117
118 [NoInstance]
119 public abstract Person this[DbManager db, int id]
120 {
121 [ActionName("SelectByKey")]
122 get;
123 [ActionName("Update")]
124 [ObjectType(typeof(Person))]
125 set;
126 }
127
128 [ActionName("SelectByKey")]
129 public abstract Person ParamNullableID([ParamNullValue(1)] int? id);
130
131 [ActionName("SelectByKey")]
132 public abstract Person ParamNullableID2([ParamNullValue("1")] int? id);
133
134 [ActionName("SelectAll")]
135 public abstract IList<Person> SelectAllAsIListT();
136
137 [ActionName("SelectByName")]
138 public abstract Person ParamNullString([ParamNullValue("John")] string firstName, string lastName);
139
140 public abstract Person ParamNullGuid ([ParamNullValue("49F74716-C6DE-4b3e-A753-E40CFE6C6EA0")] Guid guid);
141 public abstract Person ParamNullEnum ([ParamNullValue(Gender.Unknown)] Gender gender);
142
143 #region IDataReader
144
145 [SprocName("Person_SelectAll")]
146 public abstract IDataReader SelectAllIDataReader(DbManager db);
147
148 [SprocName("Person_SelectAll"), CommandBehavior(CommandBehavior.SchemaOnly)]
149 public abstract IDataReader SelectAllIDataReaderSchemaOnly(DbManager db);
150
151 #endregion
152
153 #region DataSet
154
155 [SprocName("Person_SelectAll")]
156 public abstract DataSet SelectAllDataSet();
157
158 [SprocName("Person_SelectAll")]
159 public abstract IListSource SelectAllDataSetWithDestination([Destination] DataSet ds);
160
161 [SprocName("Person_SelectAll")]
162 public abstract void SelectAllDataSetReturnVoid ([Destination] DataSet ds);
163
164 [SprocName("Person_SelectAll")]
165 public abstract PersonDataSet SelectAllTypedDataSet();
166
167 [SprocName("Person_SelectAll")]
168 public abstract object SelectAllTypedDataSetWithDestination([Destination] PersonDataSet ds);
169
170 [SprocName("Person_SelectAll")]
171 public abstract void SelectAllTypedDataSetReturnVoid ([Destination] PersonDataSet ds);
172
173 [ObjectType(typeof(Person)), ActionName("SelectAll"), DataSetTable("First")]
174 public abstract void SelectFirstTable ([Destination] DataSet ds);
175
176 [SprocName("Person_SelectAll"), DataSetTable("Second")]
177 public abstract void SelectSecondTable ([Destination] DataSet ds);
178
179 [ObjectType(typeof(Person)), ActionName("SelectAll"), DataSetTable(0)]
180 public abstract void SelectFirstTable2 ([Destination] PersonDataSet ds);
181
182 [SprocName("Person_SelectAll"), DataSetTable("Second")]
183 public abstract void SelectSecondTable2([Destination] PersonDataSet ds);
184
185 #endregion
186
187 #region DataTable
188
189 [SprocName("Person_SelectAll")]
190 public abstract DataTable SelectAllDataTable();
191
192 [SprocName("Person_SelectAll")]
193 public abstract DataTable SelectAllDataTableWithDestination([Destination] DataTable dt);
194
195 [SprocName("Person_SelectAll")]
196 public abstract void SelectAllDataTableReturnVoid ([Destination] DataTable dt);
197
198 [SprocName("Person_SelectAll"), DataSetTable("Person")]
199 public abstract DataTable SelectAllToTablePerson();
200
201 [SprocName("Person_SelectAll")]
202 public abstract PersonDataSet.PersonDataTable SelectAllTypedDataTable();
203
204 [SprocName("Person_SelectAll")]
205 public abstract void SelectAllTypedDataTableReturnVoid ([Destination] PersonDataSet.PersonDataTable dt);
206
207 #endregion
208
209 #region List
210
211 [ActionName("SelectAll"), ObjectType(typeof(Person))]
212 public abstract ArrayList SelectAllList();
213
214 [ActionName("SelectAll"), ObjectType(typeof(Person))]
215 public abstract IList SelectAllListWithDestination([Destination] IList list);
216
217 [ActionName("SelectAll"), ObjectType(typeof(Person))]
218 public abstract void SelectAllListReturnVoid ([Destination] IList list);
219
220 [ActionName("SelectAll")]
221 public abstract List<Person> SelectAllListT();
222
223 [ActionName("SelectAll"), ObjectType(typeof(Person))]
224 public abstract List<IPerson> SelectAllListTWithObjectType();
225
226 [ActionName("SelectAll"), ObjectType(typeof(Person))]
227 public abstract List<IPerson> SelectAllListTWithDestination([Destination] IList list);
228
229 [ActionName("SelectAll"), ObjectType(typeof(Person))]
230 public abstract void SelectAllListTWithObjectTypeReturnVoid([Destination] IList list);
231
232 [ActionName("SelectAll")]
233 public abstract void SelectAllListTReturnVoid ([Destination] IList<Person> list);
234
235 #endregion
236
237 #region IEnumerable
238
239 [ActionName("SelectAll"), ObjectType(typeof(Person))]
240 public abstract IEnumerable SelectAllEnumerable();
241
242 [ActionName("SelectAll")]
243 public abstract IEnumerable<Person> SelectAllEnumerableT();
244
245 [ActionName("SelectAll"), ObjectType(typeof(Person))]
246 public abstract IEnumerable<IPerson> SelectAllEnumerableTWithObjectType();
247
248 #endregion
249 }
250
251 public abstract class PersonDataAccessor1 : PersonAccessor
252 {
253 public DataSet SelectByName()
254 {
255 using (DbManager db = GetDbManager())
256 {
257 DataSet ds = new DataSet();
258
259 db.SetSpCommand("Person_SelectAll");
260
261 if (ds.Tables.Count > 0)
262 db.ExecuteDataSet(ds, ds.Tables[0].TableName);
263 else
264 db.ExecuteDataSet(ds);
265
266 return ds;
267 }
268 }
269
270 [ActionName("SelectAll"), ObjectType(typeof(Other.Person))]
271 public abstract ArrayList SameTypeName1();
272
273 [ActionName("SelectByName")]
274 public abstract Other.Person SameTypeName1(string firstName, string lastName);
275
276 protected override string GetDefaultSpName(string typeName, string actionName)
277 {
278 return "Patient_" + actionName;
279 }
280 }
281
282 public class PersonList : ArrayList
283 {
284 public new Person this[int idx]
285 {
286 get { return (Person)base[idx]; }
287 set { base[idx] = value; }
288 }
289 }
290
291 private PersonAccessor _da;
292 private SprocQuery _sproc = new SprocQuery();
293 private SqlQuery _sql = new SqlQuery();
294
295 public DataAccessorTest()
296 {
297 object o = TypeAccessor.CreateInstance(typeof(Person));
298 Assert.IsInstanceOf(typeof(Person), o);
299
300 _da = (PersonAccessor)DataAccessor.CreateInstance(typeof(PersonAccessor));
301 Assert.IsInstanceOf(typeof(PersonAccessor), _da);
302 }
303
304 [Test]
305 public void Sql_Select()
306 {
307 Person e = (Person)_sql.SelectByKey(typeof(Person), 1);
308
309 Assert.IsNotNull(e);
310 Assert.AreEqual(1, e.ID);
311 }
312
313 [Test]
314 public void Sql_SelectAll()
315 {
316 ArrayList list = _sql.SelectAll(typeof(Person));
317
318 Console.WriteLine(list.Count);
319 }
320
321 [Test]
322 public void Sql_Insert()
323 {
324 ArrayList list = _sql.SelectAll(typeof(Person));
325 Hashtable tbl = new Hashtable();
326
327 foreach (Person e in list)
328 tbl[e.ID] = e;
329
330 Person em = (Person)Map.CreateInstance(typeof(Person));
331
332 em.FirstName = "1";
333 em.LastName = "2";
334
335 _sql.Insert(em);
336
337 list = _sql.SelectAll(typeof(Person));
338
339 foreach (Person e in list)
340 if (tbl.ContainsKey(e.ID) == false)
341 _sql.Delete(e);
342 }
343
344 [Test]
345 public void Sql_Update()
346 {
347 Person e = (Person)_sql.SelectByKey(typeof(Person), 1);
348
349 int n = _sql.Update(e);
350
351 Assert.AreEqual(1, n);
352 }
353
354 [Test]
355 public void Sql_DeleteByKey()
356 {
357 ArrayList list = _sql.SelectAll(typeof(Person));
358 Hashtable tbl = new Hashtable();
359
360 foreach (Person e in list)
361 tbl[e.ID] = e;
362
363 Person em = (Person)Map.CreateInstance(typeof(Person));
364
365 em.FirstName = "1";
366 em.LastName = "2";
367
368 _sql.Insert(em);
369
370 list = _sql.SelectAll(typeof(Person));
371
372 foreach (Person e in list)
373 if (tbl.ContainsKey(e.ID) == false)
374 _sql.DeleteByKey(typeof(Person), e.ID);
375 }
376
377 [Test]
378 public void Sproc_SelectAll()
379 {
380 ArrayList list = _sproc.SelectAll(typeof(Person));
381 Console.WriteLine(list.Count);
382 }
383
384 [Test]
385 public void Gen_Person_SelectAll()
386 {
387 int n = _da.Person_SelectAll();
388 Console.WriteLine(n);
389 }
390
391 [Test]
392 public void Gen_Person_SelectAll_DbManager()
393 {
394 using (DbManager db = _da.GetDbManager())
395 _da.Person_SelectAll(db);
396 }
397
398 [Test]
399 public void Gen_SelectByName()
400 {
401 Person e = _da.SelectByName("John", "Pupkin");
402 Assert.AreEqual(1, e.ID);
403 }
404
405 [Test]
406 public void Gen_SelectByNameReturnInterface()
407 {
408 IPerson i = _da.SelectByNameReturnInterface("John", "Pupkin");
409
410 Assert.IsNotNull(i);
411 Assert.AreEqual (1, i.ID);
412 }
413
414 [Test]
415 public void Gen_SelectByNameReturnVoid()
416 {
417 Person e = (Person)TypeAccessor.CreateInstance(typeof (Person));
418 Assert.IsNotNull(e);
419
420 _da.SelectByNameReturnVoid("John", "Pupkin", e);
421 Assert.AreEqual (1, e.ID);
422 }
423
424 [Test]
425 public void Gen_SelectByNameWithDestination()
426 {
427 Person e = (Person)TypeAccessor.CreateInstance(typeof (Person));
428 IPerson i = _da.SelectByName("John", "Pupkin", e);
429
430 Assert.AreSame (i, e);
431 Assert.AreEqual(1, i.ID);
432 }
433
434 #if !ACCESS
435 #if !FIREBIRD
436 [Test]
437 public void Gen_InsertGetID()
438 {
439 Person e = (Person)TypeAccessor.CreateInstance(typeof(Person));
440 e.FirstName = "Crazy";
441 e.LastName = "Frog";
442 e.Gender = Gender.Other;
443
444 _da.Insert(e);
445
446 // If you got an assertion here, make sure your Person_Insert sproc looks like
447 //
448 // INSERT INTO Person( LastName, FirstName, MiddleName, Gender)
449 // VALUES (@LastName, @FirstName, @MiddleName, @Gender)
450 //
451 // SELECT Cast(SCOPE_IDENTITY() as int) PersonID
452 // ^==important ^==important
453 //
454 Assert.IsTrue(e.ID > 0);
455 _sproc.Delete(e);
456 }
457 #endif
458 [Test]
459 public void Gen_InsertGetIDReturnParameter()
460 {
461 Person e = (Person)TypeAccessor.CreateInstance(typeof(Person));
462
463 _da.Insert_ReturnParameter(e);
464
465 Assert.AreEqual(12345, e.ID);
466 }
467
468
469 [Test]
470 public void Gen_InsertGetIDReturnParameter2()
471 {
472 Person e = (Person)TypeAccessor.CreateInstance(typeof(Person));
473
474 _da.Insert_ReturnParameter2(e);
475
476 Assert.AreEqual(12345, e.ID);
477 }
478
479 [Test]
480 public void Gen_InsertGetIDOutputParameter()
481 {
482 Person e = (Person)TypeAccessor.CreateInstance(typeof(Person));
483 e.FirstName = "Crazy";
484 e.LastName = "Frog";
485 e.Gender = Gender.Other;
486
487 _da.Insert_OutputParameter(e);
488
489 Assert.IsTrue(e.ID > 0);
490 _sproc.Delete(e);
491 }
492 #endif
493
494 #if !FIREBIRD
495 // Firebird is incomatible with DiscoverParameters due to In/Out parameters emulation.
496 //
497 [Test]
498 #endif
499 public void Gen_SprocName()
500 {
501 Person e = _da.AnySprocName("John", "Pupkin");
502 Assert.AreEqual(1, e.ID);
503 }
504
505 [Test]
506 public void Gen_ActionName()
507 {
508 Person e = _da.AnyActionName("John", "Pupkin");
509 Assert.AreEqual(1, e.ID);
510 }
511
512 [Test]
513 public void Gen_ParamName()
514 {
515 Person e = _da.AnyParamName("John", "Pupkin");
516 Assert.AreEqual(1, e.ID);
517 }
518
519 [Test]
520 public void Gen_SameTypeName()
521 {
522 Person e = _da.SameTypeName("Tester", "Testerson");
523 Assert.IsInstanceOf(typeof(Person), e);
524 Assert.AreEqual(2, e.ID);
525
526 ArrayList list = _da.SameTypeName();
527 Assert.AreNotEqual(0, list.Count);
528 Assert.IsInstanceOf(typeof(Person), list[0]);
529
530 PersonDataAccessor1 da1 = (PersonDataAccessor1)DataAccessor.CreateInstance(typeof(PersonDataAccessor1));
531 Other.Person e1 = da1.SameTypeName1("Tester", "Testerson");
532
533 Assert.IsInstanceOf(typeof(Other.Person), e1);
534 Assert.IsNotEmpty(e1.Diagnosis);
535
536 list = da1.SameTypeName1();
537 Assert.AreNotEqual(0, list.Count);
538 Assert.IsInstanceOf(typeof(Other.Person), list[0]);
539 }
540
541 [Test]
542 public void ParamNullValueIDTest()
543 {
544 // Parameter id == 1 will be replaced with NULL
545 //
546 Person e1 = _da.ParamNullID(1);
547 Assert.IsNull(e1);
548
549 // Parameter id == 2 will be send as is
550 //
551 Person e2 = _da.ParamNullID(2);
552 Assert.IsNotNull(e2);
553 }
554
555 [Test]
556 public void ParamNullValueNullableIDTest()
557 {
558 // Parameter id == 1 will be replaced with NULL
559 //
560 Person e1 = _da.ParamNullableID(1);
561 Assert.IsNull(e1);
562 e1 = _da.ParamNullableID2(1);
563 Assert.IsNull(e1);
564
565 // Parameter id == 2 will be send as is
566 //
567 Person e2 = _da.ParamNullableID(2);
568 Assert.IsNotNull(e2);
569 }
570
571 [Test]
572 public void ParamNullValueStrTest()
573 {
574 // Parameter firstName == 'John' will be replaced with NULL
575 //
576 Person e1 = _da.ParamNullString("John", "Pupkin");
577 Assert.IsNull(e1);
578
579 // Parameter firstName == 'Tester' will be send as is
580 //
581 Person e2 = _da.ParamNullString("Tester", "Testerson");
582 Assert.IsNotNull(e2);
583 }
584
585 [Test]
586 public void AbstractIndexerTest()
587 {
588 using (DbManager db = new DbManager())
589 {
590 Person p = _da[db, 1];
591 Assert.AreEqual("John", p.FirstName);
592
593 p.FirstName = "Frog";
594 _da[db, 1] = p;
595
596 p = _da[db, 1];
597 Assert.AreEqual("Frog", p.FirstName);
598
599 p.FirstName = "John";
600 _da[db, 1] = p;
601 }
602 }
603
604
605 #region IDataReader
606
607 [Test]
608 public void Gen_SelectAllIDataReader()
609 {
610 // Keep connection open for IDataReader
611 //
612 using (DbManager db = _da.GetDbManager())
613 {
614 IDataReader dr = _da.SelectAllIDataReader(db);
615
616 Assert.IsNotNull(dr);
617 Assert.AreNotEqual(0, dr.FieldCount);
618 Assert.IsTrue(dr.Read());
619 #if ORACLE
620 Assert.AreNotEqual(0, dr.GetDecimal(dr.GetOrdinal("PersonID")));
621 #else
622 Assert.AreNotEqual(0, dr.GetInt32(dr.GetOrdinal("PersonID")));
623 #endif
624 }
625 }
626
627 #if !ACCESS
628 [Test]
629 public void Gen_SelectAllIDataReaderSchemaOnly()
630 {
631 // Keep connection open for IDataReader
632 //
633 using (DbManager db = _da.GetDbManager())
634 {
635 IDataReader dr = _da.SelectAllIDataReaderSchemaOnly(db);
636
637 Assert.IsNotNull(dr);
638 Assert.AreNotEqual(0, dr.FieldCount);
639 Assert.IsFalse(dr.Read());
640 }
641 }
642 #endif
643
644 #endregion
645
646 #region DataSet
647
648 [Test]
649 public void Gen_SelectAllDataSet()
650 {
651 DataSet ds = _da.SelectAllDataSet();
652 Assert.AreNotEqual(0, ds.Tables[0].Rows.Count);
653 }
654
655 [Test]
656 public void Gen_SelectAllDataSetWithDestination()
657 {
658 IListSource ls = _da.SelectAllDataSetWithDestination(new DataSet());
659 Assert.AreNotEqual(0, ls.GetList().Count);
660 }
661
662 [Test]
663 public void Gen_SelectAllDataSetReturnVoid()
664 {
665 DataSet ds = new DataSet();
666 _da.SelectAllDataSetReturnVoid(ds);
667 Assert.AreNotEqual(0, ds.Tables[0].Rows.Count);
668 }
669
670 [Test]
671 public void Gen_SelectAllTypedDataSet()
672 {
673 PersonDataSet ds = _da.SelectAllTypedDataSet();
674 Assert.AreNotEqual(0, ds.Person.Rows.Count);
675 }
676
677 [Test]
678 public void Gen_SelectAllTypedDataSetWithObjectType()
679 {
680 object o = _da.SelectAllTypedDataSetWithDestination(new PersonDataSet());
681 Assert.IsInstanceOf(typeof(PersonDataSet), o);
682
683 PersonDataSet ds = (PersonDataSet)o;
684 Assert.AreNotEqual(0, ds.Person.Rows.Count);
685 }
686
687 [Test]
688 public void Gen_SelectAllTypedDataSetReturnVoid()
689 {
690 PersonDataSet ds = new PersonDataSet();
691 _da.SelectAllTypedDataSetReturnVoid(ds);
692
693 Assert.AreNotEqual(0, ds.Person.Rows.Count);
694 }
695
696 [Test]
697 public void DataSetTableAttributeTest()
698 {
699 DataSet ds = new DataSet();
700
701 _da.SelectFirstTable (ds);
702 _da.SelectSecondTable(ds);
703
704 Assert.IsTrue (ds.Tables.Contains("First"), "Table 'First' not found");
705 Assert.IsTrue (ds.Tables.Contains("Second"), "Table 'Second' not found");
706 Assert.IsFalse(ds.Tables.Contains("Table"), "Table 'Table' was found");
707 }
708
709 [Test]
710 public void DataSetTableAttributeTest2()
711 {
712 PersonDataSet ds = new PersonDataSet();
713
714 _da.SelectFirstTable2 (ds);
715 _da.SelectSecondTable2(ds);
716
717 // Table 'Person' is dataset own table.
718 //
719 Assert.IsTrue (ds.Tables.Contains("Person"), "Table 'Person' not found");
720 Assert.IsTrue (ds.Tables.Contains("Second"), "Table 'Second' not found");
721 Assert.IsFalse(ds.Tables.Contains("Table"), "Table 'Table' was found");
722 }
723
724 #endregion
725
726 #region DataTable
727
728 [Test]
729 public void Gen_SelectAllDataTable()
730 {
731 DataTable dt = _da.SelectAllDataTable();
732 Assert.AreNotEqual(0, dt.Rows.Count);
733 }
734
735 [Test]
736 public void Gen_SelectAllDataTableWithDestination()
737 {
738 DataTable dt = _da.SelectAllDataTableWithDestination(new DataTable());
739 Assert.AreNotEqual(0, dt.Rows.Count);
740 }
741
742 [Test]
743 public void Gen_SelectAllDataTableReturnVoid()
744 {
745 DataTable dt = new DataTable();
746 _da.SelectAllDataTableReturnVoid(dt);
747 Assert.AreNotEqual(0, dt.Rows.Count);
748 }
749
750 [Test]
751 public void DataSetTableAttributeTest3()
752 {
753 DataTable dt = _da.SelectAllToTablePerson();
754
755 Assert.AreEqual("Person", dt.TableName);
756 Assert.AreNotEqual(0, dt.Rows.Count);
757 }
758
759 [Test]
760 public void Gen_SelectAllTypedDataTable()
761 {
762 PersonDataSet.PersonDataTable dt = _da.SelectAllTypedDataTable();
763 Assert.AreNotEqual(0, dt.Rows.Count);
764 }
765
766 [Test]
767 public void Gen_SelectAllTypedDataTableReturnVoid()
768 {
769 PersonDataSet.PersonDataTable dt = new PersonDataSet.PersonDataTable();
770
771 _da.SelectAllTypedDataTableReturnVoid(dt);
772 Assert.AreNotEqual(0, dt.Rows.Count);
773 }
774
775 #endregion
776
777 #region List
778
779 [Test]
780 public void Gen_SelectAllList()
781 {
782 ArrayList list = _da.SelectAllList();
783 Assert.AreNotEqual(0, list.Count);
784 }
785
786 [Test]
787 public void Gen_SelectAllListWithDestination()
788 {
789 IList list = _da.SelectAllListWithDestination(new ArrayList());
790 Assert.AreNotEqual(0, list.Count);
791 }
792
793 [Test]
794 public void Gen_SelectAllListReturnVoid()
795 {
796 ArrayList list = new ArrayList();
797 _da.SelectAllListReturnVoid(list);
798 Assert.AreNotEqual(0, list.Count);
799 }
800
801 [Test]
802 public void Gen_SelectAllIList()
803 {
804 IList list = _da.SelectAllAsIList();
805 Assert.IsNotEmpty(list);
806 }
807
808 [Test]
809 public void Gen_SelectAllListT()
810 {
811 List<Person> list = _da.SelectAllListT();
812 Assert.AreNotEqual(0, list.Count);
813 }
814
815 [Test]
816 public void Gen_SelectAllListTWithObjectType()
817 {
818 List<IPerson> list = _da.SelectAllListTWithObjectType();
819 Assert.AreNotEqual(0, list.Count);
820 }
821
822 [Test]
823 public void Gen_SelectAllListTReturnVoid()
824 {
825 List<Person> list = new List<Person>();
826 _da.SelectAllListTReturnVoid(list);
827 Assert.AreNotEqual(0, list.Count);
828 }
829
830 [Test]
831 public void Gen_SelectAllListTWithObjectTypeReturnVoid()
832 {
833 List<IPerson> list = new List<IPerson>();
834 _da.SelectAllListTWithObjectTypeReturnVoid(list);
835 Assert.AreNotEqual(0, list.Count);
836 }
837
838 [Test]
839 public void Gen_SelectAllListTWithDestination()
840 {
841 List<IPerson> list = _da.SelectAllListTWithDestination(new List<IPerson>());
842 Assert.AreNotEqual(0, list.Count);
843 }
844
845 [Test]
846 public void Gen_SelectAllIListT()
847 {
848 IList<Person> list = _da.SelectAllAsIListT();
849 Assert.IsNotEmpty((ICollection)list);
850 }
851
852 #endregion
853
854 #region PrepareParameters
855
856 public abstract class MyAssessorBase : DataAccessor
857 {
858 // This method will inject an additional parameter into each sproc call
859 //
860 protected override IDbDataParameter[] PrepareParameters(
861 DbManager db,
862 object[] parameters)
863 {
864 ArrayList retParams;
865 if (parameters != null && parameters.Length != 0)
866 {
867 parameters = base.PrepareParameters(db, parameters);
868 retParams = new ArrayList(parameters.Length + 1);
869 retParams.AddRange(parameters);
870 }
871 else
872 retParams = new ArrayList(1);
873
874 retParams.Add(db.Parameter((string)db.DataProvider.Convert("ID", db.GetConvertTypeToParameter()), 1));
875
876 return (IDbDataParameter[]) retParams.ToArray(typeof(IDbDataParameter));
877 }
878 }
879
880 public abstract class MyAssessor : MyAssessorBase
881 {
882 public abstract Person SelectByKey();
883 }
884
885 [Test]
886 public void PrepareParametersTest()
887 {
888 IPerson i = ((MyAssessor)TypeAccessor
889 .CreateInstance(typeof (MyAssessor)))
890 .SelectByKey();
891
892 Assert.IsNotNull(i);
893 Assert.AreEqual("John", i.FirstName);
894 Assert.AreEqual("Pupkin", i.LastName);
895 }
896 #endregion
897
898 #region IEnumerable
899
900 [Test]
901 public void Gen_SelectAllEnumerable()
902 {
903 List<Person> list = new List<Person>();
904
905 foreach (Person p in _da.SelectAllEnumerable())
906 list.Add(p);
907
908 Assert.AreNotEqual(0, list.Count);
909 }
910
911 [Test]
912 public void Gen_SelectAllEnumerableT()
913 {
914 List<Person> list = new List<Person>();
915
916 foreach (Person p in _da.SelectAllEnumerableT())
917 list.Add(p);
918
919 Assert.AreNotEqual(0, list.Count);
920 }
921
922 [Test]
923 public void Gen_SelectAllEnumerableTWithObjectType()
924 {
925 List<Person> list = new List<Person>();
926
927 foreach (Person p in _da.SelectAllEnumerableTWithObjectType())
928 list.Add(p);
929
930 Assert.AreNotEqual(0, list.Count);
931 }
932
933 #endregion
934
935 #region Pull Request 121
936
937 public class Whatever
938 {
939 public string Name { get; set; }
940 public int Age { get; set; }
941 }
942
943 public class Whatever2
944 {
945 public bool IsFoo { get; set; }
946 }
947
948 public abstract class SomeDA : DataAccessor<Whatever2>
949 {
950 [SqlQuery("select 'Gogi' as Name, 10 as Age")]
951 public abstract Whatever GetWhatever();
952 }
953
954 [Test]
955 public void TestIssue121()
956 {
957 using (var dbm = new DbManager())
958 {
959 var da = DataAccessor.CreateInstance<SomeDA>(dbm);
960 var foo = da.GetWhatever();
961
962 Assert.That(foo, Is.InstanceOf<Whatever>());
963 }
964 }
965
966 #endregion
967 }
968 }
969