[runtime] Overwrite stacktrace for exception on re-throw. Fixes #1856.
[mono.git] / mcs / class / System.Data / Test / Mono.Data.SqlExpressions / DataColumnExpressionTest.cs
1 using System;
2 using System.Data;
3 using NUnit.Framework;
4
5 namespace Monotests_Mono.Data.SqlExpressions
6 {
7         [TestFixture]   
8         public class DataColumnExprTest
9         {
10                 [Test]
11                 public void TestDataColumnExpr0SingleColumnValue ()
12                 {
13                         DataTable table = new DataTable ();
14                         table.Columns.Add ("Col_0.Value", Type.GetType ("System.Int32"));
15                         table.Columns.Add ("Col_1", Type.GetType ("System.Int32"));
16                         table.Columns.Add ("Result", Type.GetType ("System.Int32"), "IIF(Col_0.Value <> 0, Col_1 + 5, 0)");
17
18                         DataRow row = table.NewRow ();
19                         row ["Col_0.Value"] = 0;
20                         row ["Col_1"] = 10;
21
22                         table.Rows.Add (row);
23                         Assert.AreEqual (0, (int)table.Rows[0][2], "#1");
24                 }
25                 
26                 [Test]
27                 public void TestDataColumnLikeExpr ()
28                 {
29                         DataTable dt = new DataTable ();
30                         dt.Columns.Add ("c1");
31                         dt.Rows.Add (new string [] { null });
32                         dt.Rows.Add (new string [] { "xax" });
33                         dt.Columns.Add ("c2", typeof (bool), "c1 LIKE '%a%'");
34                         //Assert.IsFalse ((bool) dt.Rows [0] [1]); ... cannot cast from DBNull to bool.
35                         Assert.IsTrue ((bool) dt.Rows [1] [1]);
36                 }
37                 
38                 [Test]
39                 public void TestDataColumnExpr0Literal ()
40                 {
41                         DataTable table = new DataTable ();
42                         table.Columns.Add ("Col_0.Value", Type.GetType ("System.Int32"));
43                         table.Columns.Add ("Col_1", Type.GetType ("System.Int32"));
44                         table.Columns.Add ("Result", Type.GetType ("System.Int32"), "IIF(false, Col_1 + 5, 0)");
45
46                         DataRow row = table.NewRow ();
47                         row ["Col_0.Value"] = 0;
48                         row ["Col_1"] = 10;
49
50                         table.Rows.Add (row);
51                         Assert.AreEqual (0, (int)table.Rows[0][2], "#1");
52                 }
53                 [Test]
54                 public void TestDataColumnExpr1 ()
55                 {
56                         DataTable table = new DataTable ();
57                         table.Columns.Add ("Col_0.Value", Type.GetType ("System.Int32"));
58                         table.Columns.Add ("Col_1", Type.GetType ("System.Int32"));
59                         table.Columns.Add ("Result", Type.GetType ("System.Int32"), "IIF(Col_0.Value > 10, Col_1 + 5, 0)");
60
61                         DataRow row = table.NewRow ();
62                         row ["Col_0.Value"] = 20;
63                         row ["Col_1"] = 10;
64
65                         table.Rows.Add (row);
66                         Assert.AreEqual ((int)table.Rows[0][1] + 5, table.Rows[0][2], "#1");
67                 }
68                 [Test]
69                 public void TestDataColumnExpr2 ()
70                 {
71                         DataTable table = new DataTable ();
72                         table.Columns.Add ("Col_0.Value", Type.GetType ("System.Int32"));
73                         table.Columns.Add ("Col_1", Type.GetType ("System.Int32"));
74                         table.Columns.Add ("Result", Type.GetType ("System.Int32"), "IIF(Col_0.Value > 10, Col_1 + 5, 0)");
75
76                         DataRow row = table.NewRow ();
77                         row ["Col_0.Value"] = 9;
78                         row ["Col_1"] = 10;
79
80                         table.Rows.Add (row);
81                         Assert.AreEqual (0, (int)table.Rows[0][2], "#1");
82                 }
83                 [Test]
84                 public void TestDataColumnSubstring ()
85                 {
86                         DataTable table = new DataTable ();
87                         table.Columns.Add ("Col_0", Type.GetType ("System.String"));
88                         table.Columns.Add ("Result", Type.GetType ("System.String"), "SUBSTRING(Col_0+'K?', 2+2, 2)");
89
90                         DataRow row = table.NewRow ();
91                         row ["Col_0"] = "Is O";
92
93                         table.Rows.Add (row);
94                         Assert.AreEqual ("OK", (string)table.Rows[0][1], "#1");
95                 }
96                 [Test]
97                 public void TestConcat ()
98                 {
99                         DataTable table = new DataTable ();
100                         table.Columns.Add ("Result", Type.GetType ("System.Int32"), "'3' + '2'");
101
102                         DataRow row = table.NewRow ();
103
104                         table.Rows.Add (row);
105                         Assert.AreEqual (32, table.Rows[0][0], "#1");
106                 }
107                 
108                 [Test]
109                 public void TestIsNull ()
110                 {
111                         DataTable table = new DataTable ();
112                         table.Columns.Add ("Result", typeof(bool), "('3') IS NULL");
113
114                         DataRow row = table.NewRow ();
115
116                         table.Rows.Add (row);
117                         Assert.AreEqual (false, table.Rows[0][0], "#1");
118                 }
119         }
120         [TestFixture]
121         public class DataColumnCharTest
122         {
123                 private static DataTable _dt = new DataTable();
124
125                 [Test]
126                 public void Test1 ()
127                 {
128                         _dt.Columns.Add(new DataColumn("a", typeof(char)));
129
130                         AddData('1');
131                         AddData('2');
132                         AddData('3');
133                         AddData('A');
134
135                         Assert.AreEqual (true, FindRow("'A'"), "Test1-1 failed");
136                         Assert.AreEqual (true, FindRow("65"), "Test1-2 failed");
137                         Assert.AreEqual (true, FindRow("'1'"), "Test1-3 failed");
138                 }
139
140                 [Test]
141                 [ExpectedException(typeof(FormatException))]
142                 public void Test2 ()
143                 {
144                         FindRow("'65'");
145                 }
146                 [Test]
147                 public void Test3 ()
148                 {
149                         Assert.AreEqual (false, FindRow ("1"), "Test3-1 failed");
150                 }
151
152                 private static bool FindRow(string f)
153                 {
154                         string filter = string.Format("a = {0}", f);
155
156                         DataRow[] rows = _dt.Select(filter);
157
158                         if (rows.Length == 0)
159                                 return false;
160                         else
161                                 return true;
162                 }
163
164                 private static void AddData(char a)
165                 {
166                         DataRow row = _dt.NewRow();
167                         row["a"] = a;
168                         _dt.Rows.Add(row);
169                 }
170         }
171 }