2 // Rafael Mizrahi <rafim@mainsoft.com>
3 // Erez Lotan <erezl@mainsoft.com>
4 // Oren Gurfinkel <oreng@mainsoft.com>
7 // Copyright (c) 2004 Mainsoft Co.
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using NUnit.Framework;
33 namespace MonoTests.System.Data
35 [TestFixture] public class DataColumnTest2
37 [Test] public void AllowDBNull()
39 DataTable dt = new DataTable();
41 dc = new DataColumn("ColName",typeof(int));
42 dc.DefaultValue = DBNull.Value;
44 dc.AutoIncrement=false;
46 // Checking default value (True)
47 Assert.AreEqual(true , dc.AllowDBNull, "DC1");
49 // AllowDBNull=true - adding new row with null value
50 dt.Rows.Add(dt.NewRow());
51 Assert.AreEqual(DBNull.Value , dt.Rows[0][0], "DC2");
53 // set AllowDBNull=false
56 dc.AllowDBNull=false; //the exisiting row contains null value
57 Assert.Fail("DC3: AllowDbNull Failed to throw DataException");
59 catch (DataException) {}
60 catch (AssertionException exc) {throw exc;}
63 Assert.Fail("DC4: AllowDbNull. Wrong exception type. Got:" + exc);
68 // AllowDBNull=false - adding new row with null value
71 dt.Rows.Add(dt.NewRow());
72 Assert.Fail("DC5: RowAdd Failed to throw NoNullAllowedException");
74 catch (NoNullAllowedException) {}
75 catch (AssertionException exc) {throw exc;}
78 Assert.Fail("DC6: RowAdd. Wrong exception type. Got:" + exc);
81 dc.AutoIncrement=true;
82 int iRowCount = dt.Rows.Count;
83 // AllowDBNull=false,AutoIncrement=true - adding new row with null value
84 dt.Rows.Add(dt.NewRow());
85 Assert.AreEqual(dt.Rows.Count, iRowCount+1, "DC7");
88 [Test] public void AutoIncrement()
91 dc = new DataColumn("ColName",typeof(string));
93 // Checking default value (False)
94 Assert.AreEqual(false , dc.AutoIncrement, "DC8");
97 dc.AutoIncrement=true;
99 Assert.AreEqual(true , dc.AutoIncrement, "DC9");
102 [Test] public void AutoIncrementSeed()
105 dc = new DataColumn("ColName",typeof(string));
107 // Checking default value 0
108 Assert.AreEqual((long)0, dc.AutoIncrementSeed , "DC10");
111 dc.AutoIncrementSeed = long.MaxValue;
112 // Checking Get MaxValue
113 Assert.AreEqual(long.MaxValue, dc.AutoIncrementSeed , "DC11");
116 dc.AutoIncrementSeed = long.MinValue ;
117 // Checking Get MinValue
118 Assert.AreEqual(long.MinValue , dc.AutoIncrementSeed, "DC12");
121 [Test] public void AutoIncrementStep()
124 dc = new DataColumn("ColName",typeof(string));
125 // Checking default value 1
126 Assert.AreEqual((long)1, dc.AutoIncrementStep , "DC13");
129 dc.AutoIncrementStep = long.MaxValue;
130 // Checking Get MaxValue
131 Assert.AreEqual(long.MaxValue, dc.AutoIncrementStep , "DC14");
134 dc.AutoIncrementStep = long.MinValue ;
135 // Checking Get MinValue
136 Assert.AreEqual(long.MinValue , dc.AutoIncrementStep , "DC15");
139 [Test] public void Caption()
142 string sCaption = "NewCaption";
143 dc = new DataColumn("ColName",typeof(string));
145 //Checking default value ( ColumnName )
146 // Checking default value ( ColumnName )
147 Assert.AreEqual(dc.ColumnName , dc.Caption, "DC16");
150 dc.Caption = sCaption;
152 Assert.AreEqual(sCaption , dc.Caption , "DC17");
155 [Test] public void ColumnName()
158 string sName = "NewName";
160 dc = new DataColumn();
161 //Checking default value ("")
162 // ColumnName default value
163 Assert.AreEqual(string.Empty , dc.ColumnName, "DC18");
166 dc.ColumnName = sName;
168 // ColumnName Get/Set
169 Assert.AreEqual(sName , dc.ColumnName , "DC19");
171 //Special chars (valid chars)
172 sName = "~()#\\/=><+-*%&|^'\"[]";
173 // ColumnName Special chars
174 dc.ColumnName = sName ;
175 Assert.AreEqual(sName , dc.ColumnName , "DC20");
178 [Test] public void DataType()
181 dc = new DataColumn();
182 string[] sTypeArr = { "System.Boolean", "System.Byte", "System.Char", "System.DateTime",
183 "System.Decimal", "System.Double", "System.Int16", "System.Int32",
184 "System.Int64", "System.SByte", "System.Single", "System.String",
185 "System.TimeSpan", "System.UInt16", "System.UInt32", "System.UInt64" };
187 //Checking default value (string)
189 Assert.AreEqual(Type.GetType("System.String") , dc.DataType, "DC21");
191 foreach (string sType in sTypeArr)
194 dc.DataType = Type.GetType(sType);
195 // Checking GetType " + sType);
196 Assert.AreEqual(Type.GetType(sType) , dc.DataType , "DC22");
200 [Test] public void Equals()
203 dc1 = new DataColumn();
204 dc2 = new DataColumn();
207 Assert.AreEqual(false , dc1.Equals(dc2) , "DC23");
212 Assert.AreEqual(dc2 , dc1 , "DC24");
215 [Test] public void ExtendedProperties()
218 PropertyCollection pc;
219 dc = new DataColumn();
221 pc = dc.ExtendedProperties ;
222 // Checking ExtendedProperties default
223 Assert.AreEqual(true, pc != null, "DC25");
225 // Checking ExtendedProperties count
226 Assert.AreEqual(0, pc.Count , "DC26");
229 [Test] public void TestGetHashCode()
233 dc1 = new DataColumn();
235 iHashCode1 = dc1.GetHashCode();
236 for (int i=0; i<10; i++)
237 { // must return the same value each time
238 // GetHashCode #" + i.ToString());
239 Assert.AreEqual(dc1.GetHashCode(), iHashCode1 , "DC27");
243 [Test] public void TestGetType()
247 dc = new DataColumn();
248 myType = dc.GetType();
251 Assert.AreEqual(typeof(DataColumn), myType, "DC28");
254 [Test] public void MaxLength()
257 dc = new DataColumn("ColName",typeof(string));
259 //Checking default value (-1)
261 Assert.AreEqual((int)-1, dc.MaxLength , "DC29");
263 //Cheking Set MaxValue
264 dc.MaxLength = int.MaxValue ;
265 //Checking Get MaxValue
266 // MaxLength MaxValue
267 Assert.AreEqual(int.MaxValue , dc.MaxLength , "DC30");
269 //Cheking Set MinValue
270 dc.MaxLength = int.MinValue ;
271 //Checking Get MinValue
272 // MaxLength MinValue
273 Assert.AreEqual(int.MinValue, dc.MaxLength , "DC31");
275 DataTable dt = new DataTable();
276 dt.Columns.Add(new DataColumn("col",typeof(string)));
277 dt.Columns[0].MaxLength = 5;
278 dt.Rows.Add(new object[] {"a"});
284 dt.Rows[0][0] = "123456";
285 Assert.Fail("DC32: Indexer failed to throw ArgumentException");
287 catch(ArgumentException) {}
288 catch (AssertionException exc) {throw exc;}
289 catch (Exception exc)
291 Assert.Fail("DC33: Indexer. Wrong exception type. Got:" + exc);
295 [Test] public void Namespace()
298 string sName = "NewName";
300 dc = new DataColumn();
301 //Checking default value ("")
303 Assert.AreEqual(string.Empty , dc.Namespace , "DC34");
306 dc.Namespace = sName;
309 Assert.AreEqual(sName, dc.Namespace , "DC35");
312 [Test] public void Prefix()
315 string sPrefix = "Prefix";
316 dc = new DataColumn("ColName",typeof(string));
318 // Prefix Checking default value (string.Empty)
319 Assert.AreEqual(string.Empty , dc.Prefix , "DC36");
324 // Prefix Checking Get
325 Assert.AreEqual(sPrefix , dc.Prefix , "DC37");
328 [Test] public void ReadOnly()
331 dc = new DataColumn();
333 //Checking default value (false)
335 Assert.AreEqual(false , dc.ReadOnly , "DC38");
341 Assert.AreEqual(true, dc.ReadOnly , "DC39");
344 [Test] public void Table()
347 dc = new DataColumn();
351 Assert.AreEqual(null, dc.Table, "DC40");
353 DataTable dt = new DataTable();
356 //Checking Second Get
358 Assert.AreEqual(dt, dc.Table , "DC41");
361 [Test] public void TestToString()
364 string sColName,sExp;
365 dc = new DataColumn();
368 //Console.WriteLine(dc.ToString());
370 //ToString = ColumnName
372 dc.ColumnName = sColName;
373 // ToString - ColumnName
374 Assert.AreEqual(sColName , dc.ToString() , "DC42");
376 //TosTring = ColumnName + " + " + Expression
377 sExp = "Tax * 1.234";
378 dc.Expression = sExp;
379 // TosTring=ColumnName + Expression
380 Assert.AreEqual(sColName + " + " + sExp , dc.ToString() , "DC43");
383 [Test] public void Unique()
386 dc = new DataColumn();
387 //Checking default value (false)
390 Assert.AreEqual(false , dc.Unique , "DC44");
397 Assert.AreEqual(true, dc.Unique, "DC45");
400 [Test] public void ctor()
403 dc = new DataColumn();
406 Assert.AreEqual(false, dc == null, "DC46");
409 [Test] public void ctor_ByColumnName()
412 string sName = "ColName";
413 dc = new DataColumn(sName);
416 Assert.AreEqual(false , dc==null , "DC47");
419 Assert.AreEqual(sName, dc.ColumnName , "DC48");
422 [Test] public void ctor_ByColumnNameType()
425 DataColumn dc = null;
426 string[] sTypeArr = { "System.Boolean", "System.Byte", "System.Char", "System.DateTime",
427 "System.Decimal", "System.Double", "System.Int16", "System.Int32",
428 "System.Int64", "System.SByte", "System.Single", "System.String",
429 "System.TimeSpan", "System.UInt16", "System.UInt32", "System.UInt64" };
431 foreach (string sType in sTypeArr)
433 typTest = Type.GetType(sType);
434 dc = new DataColumn("ColName",typTest);
436 Assert.AreEqual(false , dc==null, "DC49");
439 Assert.AreEqual(typTest , dc.DataType , "DC50");
443 [Test] public void ctor_ByColumnNameTypeExpression()
446 dc = new DataColumn("ColName",typeof(String),"Price * 1.18");
449 Assert.AreEqual(false , dc==null, "DC51");
452 [Test] public void ctor_ByColumnNameTypeExpressionMappingType()
455 //Cheking constructor for each Enum MappingType
456 foreach (int i in Enum.GetValues(typeof(MappingType)))
459 dc = new DataColumn("ColName",typeof(string),"Price * 1.18",(MappingType)i );
460 // Ctor #" + i.ToString());
461 Assert.AreEqual(false , dc==null , "DC52");
465 [Test] public void ordinal()
468 dc = new DataColumn("ColName",typeof(string));
471 //Console.WriteLine( "***" + dc.Ordinal.ToString() + "***
474 //Checking default value
475 // Ordinal default value
476 Assert.AreEqual((int)-1 , dc.Ordinal, "DC53");
478 // needs a DataTable.Columns to test
479 DataColumnCollection dcColl ;
480 DataTable tb = new DataTable();
481 dcColl = tb.Columns ;
489 Assert.AreEqual((int)3 , dc.Ordinal , "DC54");