File: DataColumnExpressionTest.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (171 lines) | stat: -rw-r--r-- 4,440 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Data;
using NUnit.Framework;

namespace Monotests_Mono.Data.SqlExpressions
{
	[TestFixture]	
	public class DataColumnExprTest
	{
		[Test]
		public void TestDataColumnExpr0SingleColumnValue ()
		{
			DataTable table = new DataTable ();
			table.Columns.Add ("Col_0.Value", Type.GetType ("System.Int32"));
			table.Columns.Add ("Col_1", Type.GetType ("System.Int32"));
			table.Columns.Add ("Result", Type.GetType ("System.Int32"), "IIF(Col_0.Value, Col_1 + 5, 0)");

			DataRow row = table.NewRow ();
			row ["Col_0.Value"] = 0;
			row ["Col_1"] = 10;

			table.Rows.Add (row);
			Assert.AreEqual (0, (int)table.Rows[0][2], "#1");
		}
		
		[Test]
		public void TestDataColumnLikeExpr ()
		{
			DataTable dt = new DataTable ();
			dt.Columns.Add ("c1");
			dt.Rows.Add (new string [] { null });
			dt.Rows.Add (new string [] { "xax" });
			dt.Columns.Add ("c2", typeof (bool), "c1 LIKE '%a%'");
			Assert.IsFalse ((bool) dt.Rows [0] [1]);
			Assert.IsTrue ((bool) dt.Rows [1] [1]);
		}
		
		[Test]
		public void TestDataColumnExpr0Literal ()
		{
			DataTable table = new DataTable ();
			table.Columns.Add ("Col_0.Value", Type.GetType ("System.Int32"));
			table.Columns.Add ("Col_1", Type.GetType ("System.Int32"));
			table.Columns.Add ("Result", Type.GetType ("System.Int32"), "IIF(false, Col_1 + 5, 0)");

			DataRow row = table.NewRow ();
			row ["Col_0.Value"] = 0;
			row ["Col_1"] = 10;

			table.Rows.Add (row);
			Assert.AreEqual (0, (int)table.Rows[0][2], "#1");
		}
		[Test]
		public void TestDataColumnExpr1 ()
		{
			DataTable table = new DataTable ();
			table.Columns.Add ("Col_0.Value", Type.GetType ("System.Int32"));
			table.Columns.Add ("Col_1", Type.GetType ("System.Int32"));
			table.Columns.Add ("Result", Type.GetType ("System.Int32"), "IIF(Col_0.Value > 10, Col_1 + 5, 0)");

			DataRow row = table.NewRow ();
			row ["Col_0.Value"] = 20;
			row ["Col_1"] = 10;

			table.Rows.Add (row);
			Assert.AreEqual ((int)table.Rows[0][1] + 5, table.Rows[0][2], "#1");
		}
		[Test]
		public void TestDataColumnExpr2 ()
		{
			DataTable table = new DataTable ();
			table.Columns.Add ("Col_0.Value", Type.GetType ("System.Int32"));
			table.Columns.Add ("Col_1", Type.GetType ("System.Int32"));
			table.Columns.Add ("Result", Type.GetType ("System.Int32"), "IIF(Col_0.Value > 10, Col_1 + 5, 0)");

			DataRow row = table.NewRow ();
			row ["Col_0.Value"] = 9;
			row ["Col_1"] = 10;

			table.Rows.Add (row);
			Assert.AreEqual (0, (int)table.Rows[0][2], "#1");
		}
		[Test]
		public void TestDataColumnSubstring ()
		{
			DataTable table = new DataTable ();
			table.Columns.Add ("Col_0", Type.GetType ("System.String"));
			table.Columns.Add ("Result", Type.GetType ("System.String"), "SUBSTRING(Col_0+'K?', 2+2, 2)");

			DataRow row = table.NewRow ();
			row ["Col_0"] = "Is O";

			table.Rows.Add (row);
			Assert.AreEqual ("OK", (string)table.Rows[0][1], "#1");
		}
		[Test]
		public void TestConcat ()
		{
			DataTable table = new DataTable ();
			table.Columns.Add ("Result", Type.GetType ("System.Int32"), "'3' + '2'");

			DataRow row = table.NewRow ();

			table.Rows.Add (row);
			Assert.AreEqual (32, table.Rows[0][0], "#1");
		}
		
		[Test]
		public void TestIsNull ()
		{
			DataTable table = new DataTable ();
			table.Columns.Add ("Result", typeof(bool), "('3') IS NULL");

			DataRow row = table.NewRow ();

			table.Rows.Add (row);
			Assert.AreEqual (false, table.Rows[0][0], "#1");
		}
	}
	[TestFixture]
	public class DataColumnCharTest
	{
		private static DataTable _dt = new DataTable();

		[Test]
		public void Test1 ()
		{
			_dt.Columns.Add(new DataColumn("a", typeof(char)));

			AddData('1');
			AddData('2');
			AddData('3');
			AddData('A');

			Assert.AreEqual (true, FindRow("'A'"), "Test1-1 failed");
			Assert.AreEqual (true, FindRow("65"), "Test1-2 failed");
			Assert.AreEqual (true, FindRow("'1'"), "Test1-3 failed");
		}

		[Test]
		[ExpectedException(typeof(FormatException))]
		public void Test2 ()
		{
			FindRow("'65'");
		}
		[Test]
		public void Test3 ()
		{
			Assert.AreEqual (false, FindRow ("1"), "Test3-1 failed");
		}

		private static bool FindRow(string f)
		{
			string filter = string.Format("a = {0}", f);

			DataRow[] rows = _dt.Select(filter);

			if (rows.Length == 0)
				return false;
			else
				return true;
		}

		private static void AddData(char a)
		{
			DataRow row = _dt.NewRow();
			row["a"] = a;
			_dt.Rows.Add(row);
		}
	}
}