1 // DataTableTest.cs - NUnit Test Cases for testing the DataTable
4 // Franklin Wise (gracenote@earthlink.net)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
8 // (C) 2003 Martin Willemoes Hansen
11 using NUnit.Framework;
15 namespace MonoTests.System.Data
18 public class DataTableTest : Assertion
23 DataTable dt = new DataTable();
25 AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
26 Assert("Col",dt.Columns != null);
27 //Assert(dt.ChildRelations != null);
28 Assert("Const", dt.Constraints != null);
29 Assert("ds", dt.DataSet == null);
30 Assert("dv", dt.DefaultView != null);
31 Assert("de", dt.DisplayExpression == "");
32 Assert("ep", dt.ExtendedProperties != null);
33 Assert("he", dt.HasErrors == false);
34 Assert("lc", dt.Locale != null);
35 Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
36 Assert("ns", dt.Namespace == "");
37 //Assert(dt.ParentRelations != null);
38 Assert("pf", dt.Prefix == "");
39 Assert("pk", dt.PrimaryKey != null);
40 Assert("rows", dt.Rows != null);
41 Assert("Site", dt.Site == null);
42 Assert("tname", dt.TableName == "");
49 DataSet Set = new DataSet ();
50 DataTable Mom = new DataTable ("Mom");
51 DataTable Child = new DataTable ("Child");
53 Set.Tables.Add (Child);
55 DataColumn Col = new DataColumn ("Name");
56 DataColumn Col2 = new DataColumn ("ChildName");
57 Mom.Columns.Add (Col);
58 Mom.Columns.Add (Col2);
60 DataColumn Col3 = new DataColumn ("Name");
61 DataColumn Col4 = new DataColumn ("Age");
62 Col4.DataType = Type.GetType ("System.Int16");
63 Child.Columns.Add (Col3);
64 Child.Columns.Add (Col4);
66 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
67 Set.Relations.Add (Relation);
69 DataRow Row = Mom.NewRow ();
94 Row = Child.NewRow ();
99 Row = Child.NewRow ();
102 Child.Rows.Add (Row);
104 Row = Child.NewRow ();
107 Child.Rows.Add (Row);
109 Row = Child.NewRow ();
112 Child.Rows.Add (Row);
114 Row = Child.NewRow ();
117 Child.Rows.Add (Row);
119 Row = Child.NewRow ();
122 Child.Rows.Add (Row);
124 DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
125 AssertEquals ("test#01", 2, Rows.Length);
127 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
128 AssertEquals ("test#02", 0, Rows.Length);
130 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
131 AssertEquals ("test#03", 1, Rows.Length);
133 Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
134 AssertEquals ("test#04", "Mack", Rows [0] [1]);
136 Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
137 AssertEquals ("test#05", 5, Rows.Length);
139 Rows = Child.Select ("age = 20 - 1");
140 AssertEquals ("test#06", 1, Rows.Length);
142 Rows = Child.Select ("age <= 20");
143 AssertEquals ("test#07", 3, Rows.Length);
145 Rows = Child.Select ("age >= 20");
146 AssertEquals ("test#08", 3, Rows.Length);
148 Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
149 AssertEquals ("test#09", 2, Rows.Length);
151 Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
152 AssertEquals ("test#10", 1, Rows.Length);
153 AssertEquals ("test#11", "Mack", Rows [0] [0]);
157 public void Select2 ()
159 DataSet Set = new DataSet ();
160 DataTable Child = new DataTable ("Child");
162 Set.Tables.Add (Child);
164 DataColumn Col3 = new DataColumn ("Name");
165 DataColumn Col4 = new DataColumn ("Age");
166 Col4.DataType = Type.GetType ("System.Int16");
167 Child.Columns.Add (Col3);
168 Child.Columns.Add (Col4);
170 DataRow Row = Child.NewRow ();
173 Child.Rows.Add (Row);
175 Row = Child.NewRow ();
178 Child.Rows.Add (Row);
180 Row = Child.NewRow ();
183 Child.Rows.Add (Row);
185 Row = Child.NewRow ();
188 Child.Rows.Add (Row);
190 Row = Child.NewRow ();
193 Child.Rows.Add (Row);
195 Row = Child.NewRow ();
198 Child.Rows.Add (Row);
200 DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
201 AssertEquals ("test#01", 3, Rows.Length);
202 AssertEquals ("test#02", "Mack", Rows [0] [0]);
203 AssertEquals ("test#03", "Mick", Rows [1] [0]);
204 AssertEquals ("test#04", "Dick", Rows [2] [0]);
206 Rows = Child.Select ("age >= 20", "age asc");
207 AssertEquals ("test#05", 3, Rows.Length);
208 AssertEquals ("test#06", "Dick", Rows [0] [0]);
209 AssertEquals ("test#07", "Mick", Rows [1] [0]);
210 AssertEquals ("test#08", "Mack", Rows [2] [0]);
212 Rows = Child.Select ("age >= 20", "name asc");
213 AssertEquals ("test#09", 3, Rows.Length);
214 AssertEquals ("test#10", "Dick", Rows [0] [0]);
215 AssertEquals ("test#11", "Mack", Rows [1] [0]);
216 AssertEquals ("test#12", "Mick", Rows [2] [0]);
218 Rows = Child.Select ("age >= 20", "name desc");
219 AssertEquals ("test#09", 3, Rows.Length);
220 AssertEquals ("test#10", "Mick", Rows [0] [0]);
221 AssertEquals ("test#11", "Mack", Rows [1] [0]);
222 AssertEquals ("test#12", "Dick", Rows [2] [0]);
227 public void SelectParsing ()
229 DataTable T = new DataTable ("test");
230 DataColumn C = new DataColumn ("name");
232 C = new DataColumn ("age");
233 C.DataType = typeof (int);
235 C = new DataColumn ("id");
238 DataSet Set = new DataSet ("TestSet");
242 for (int i = 0; i < 100; i++) {
244 Row [0] = "human" + i;
256 AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
258 AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
261 T.Select ("name = 1human ");
263 } catch (Exception e) {
265 // missing operand after 'human' operand
266 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
270 T.Select ("name = 1");
272 } catch (Exception e) {
274 // Cannot perform '=' operation between string and Int32
275 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
278 AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
283 public void SelectOperators ()
285 DataTable T = new DataTable ("test");
286 DataColumn C = new DataColumn ("name");
288 C = new DataColumn ("age");
289 C.DataType = typeof (int);
291 C = new DataColumn ("id");
294 DataSet Set = new DataSet ("TestSet");
298 for (int i = 0; i < 100; i++) {
300 Row [0] = "human" + i;
312 AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
313 AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);
314 AssertEquals ("test#03", 12, T.Select ("age< =10").Length);
315 AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
316 AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);
317 AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
318 AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
319 AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
320 // FIXME: Somebody explain how this can be possible.
321 // it seems that it is no matter between 10 - 30. The
322 // result is allways 25 :-P
323 AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
328 public void SelectExceptions ()
330 DataTable T = new DataTable ("test");
331 DataColumn C = new DataColumn ("name");
333 C = new DataColumn ("age");
334 C.DataType = typeof (int);
336 C = new DataColumn ("id");
339 for (int i = 0; i < 100; i++) {
340 DataRow Row = T.NewRow ();
341 Row [0] = "human" + i;
348 T.Select ("name = human1");
350 } catch (Exception e) {
352 // column name human not found
353 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
356 AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
357 AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
360 T.Select ("id = 1k3");
362 } catch (Exception e) {
364 // no operands after k3 operator
365 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
370 public void SelectStringOperators ()
372 DataTable T = new DataTable ("test");
373 DataColumn C = new DataColumn ("name");
375 C = new DataColumn ("age");
376 C.DataType = typeof (int);
378 C = new DataColumn ("id");
381 DataSet Set = new DataSet ("TestSet");
385 for (int i = 0; i < 100; i++) {
387 Row [0] = "human" + i;
398 AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
400 AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);
401 AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
402 AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);
403 AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
404 AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
406 AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
408 Set.CaseSensitive = true;
409 AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
411 T.CaseSensitive = false;
412 AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
414 T.CaseSensitive = true;
415 AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
417 Set.CaseSensitive = false;
418 AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
420 T.CaseSensitive = false;
421 AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
423 AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
424 AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
425 AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
428 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
430 } catch (Exception e) {
432 // 'h*an1' is invalid
433 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
437 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
439 } catch (Exception e) {
441 // 'h%an1' is invalid
442 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
445 AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
446 AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
451 public void SelectAggregates ()
453 DataTable T = new DataTable ("test");
454 DataColumn C = new DataColumn ("name");
456 C = new DataColumn ("age");
457 C.DataType = typeof (int);
459 C = new DataColumn ("id");
463 for (int i = 0; i < 1000; i++) {
465 Row [0] = "human" + i;
471 AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
472 AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
473 AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
474 AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
475 AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
476 AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
477 AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
481 public void SelectFunctions ()
483 DataTable T = new DataTable ("test");
484 DataColumn C = new DataColumn ("name");
486 C = new DataColumn ("age");
487 C.DataType = typeof (int);
489 C = new DataColumn ("id");
493 for (int i = 0; i < 1000; i++) {
495 Row [0] = "human" + i;
502 Row [0] = "human" + "test";
503 Row [1] = DBNull.Value;
504 Row [2] = DBNull.Value;
507 //TODO: How to test Convert-function
508 AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);
509 AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
510 AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
511 AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);
512 AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);
513 AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
514 AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
515 AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
516 AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
517 AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
522 public void SelectRelations ()
524 DataSet Set = new DataSet ();
525 DataTable Mom = new DataTable ("Mom");
526 DataTable Child = new DataTable ("Child");
528 Set.Tables.Add (Mom);
529 Set.Tables.Add (Child);
531 DataColumn Col = new DataColumn ("Name");
532 DataColumn Col2 = new DataColumn ("ChildName");
533 Mom.Columns.Add (Col);
534 Mom.Columns.Add (Col2);
536 DataColumn Col3 = new DataColumn ("Name");
537 DataColumn Col4 = new DataColumn ("Age");
538 Col4.DataType = Type.GetType ("System.Int16");
539 Child.Columns.Add (Col3);
540 Child.Columns.Add (Col4);
542 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
543 Set.Relations.Add (Relation);
545 DataRow Row = Mom.NewRow ();
570 Row = Child.NewRow ();
573 Child.Rows.Add (Row);
575 Row = Child.NewRow ();
578 Child.Rows.Add (Row);
580 Row = Child.NewRow ();
583 Child.Rows.Add (Row);
585 Row = Child.NewRow ();
588 Child.Rows.Add (Row);
590 Row = Child.NewRow ();
593 Child.Rows.Add (Row);
595 Row = Child.NewRow ();
598 Child.Rows.Add (Row);
600 DataRow [] Rows = Child.Select ("name = Parent.Childname");
601 AssertEquals ("test#01", 6, Rows.Length);
602 Rows = Child.Select ("Parent.childname = 'Jack'");
603 AssertEquals ("test#02", 1, Rows.Length);
606 // FIXME: LAMESPEC: Why the exception is thrown why... why...
607 Mom.Select ("Child.Name = 'Jack'");
609 } catch (Exception e) {
610 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
611 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
614 Rows = Child.Select ("Parent.name = 'Laura'");
615 AssertEquals ("test#06", 3, Rows.Length);
617 DataTable Parent2 = new DataTable ("Parent2");
618 Col = new DataColumn ("Name");
619 Col2 = new DataColumn ("ChildName");
621 Parent2.Columns.Add (Col);
622 Parent2.Columns.Add (Col2);
623 Set.Tables.Add (Parent2);
625 Row = Parent2.NewRow ();
628 Parent2.Rows.Add (Row);
630 Row = Parent2.NewRow ();
633 Parent2.Rows.Add (Row);
635 Row = Parent2.NewRow ();
638 Parent2.Rows.Add (Row);
640 Row = Parent2.NewRow ();
643 Parent2.Rows.Add (Row);
645 Row = Parent2.NewRow ();
648 Parent2.Rows.Add (Row);
650 Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
651 Set.Relations.Add (Relation);
654 Rows = Child.Select ("Parent.ChildName = 'Jack'");
656 } catch (Exception e) {
657 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
658 AssertEquals ("test#09", "The table [Child] involved in more than one relation. You must explicitly mention a relation name in the expression 'parent.[ChildName]'.", e.Message);
661 Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
662 AssertEquals ("test#10", 1, Rows.Length);
664 Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
665 AssertEquals ("test#10", 1, Rows.Length);
668 Mom.Select ("Parent.name = 'John'");
669 } catch (Exception e) {
670 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
671 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
677 public void ToStringTest()
679 DataTable dt = new DataTable();
680 dt.Columns.Add("Col1",typeof(int));
682 dt.TableName = "Mytable";
683 dt.DisplayExpression = "Col1";
686 string cmpr = dt.TableName + " + " + dt.DisplayExpression;
687 AssertEquals(cmpr,dt.ToString());
691 public void PrimaryKey ()
693 DataTable dt = new DataTable ();
694 DataColumn Col = new DataColumn ();
695 Col.AllowDBNull = false;
696 Col.DataType = typeof (int);
697 dt.Columns.Add (Col);
702 AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
704 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
705 AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
706 AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
708 dt.PrimaryKey = null;
709 AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
711 Col = new DataColumn ("failed");
714 dt.PrimaryKey = new DataColumn [] {Col};
716 } catch (Exception e) {
717 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
718 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
721 DataTable dt2 = new DataTable ();
725 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
727 } catch (Exception e) {
728 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
729 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
733 AssertEquals ("test#11", 0, dt.Constraints.Count);
735 dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
736 AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
737 AssertEquals ("test#13", 1, dt.Constraints.Count);
738 AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
739 AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
740 AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);