Standardized Mainsoft DataColumn tests.
[mono.git] / mcs / class / System.Data / Test / System.Data / DataColumnTest2.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 // 
7 // Copyright (c) 2004 Mainsoft Co.
8 // 
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using NUnit.Framework;
30 using System;
31 using System.Data;
32
33 namespace MonoTests.System.Data
34 {
35         [TestFixture] public class DataColumnTest2
36         {
37                 [Test] public void AllowDBNull()
38                 {
39                         DataTable dt = new DataTable();
40                         DataColumn dc;
41                         dc = new DataColumn("ColName",typeof(int));
42                         dc.DefaultValue = DBNull.Value;
43                         dt.Columns.Add(dc);
44                         dc.AutoIncrement=false;
45
46                         // Checking default value (True)
47                         Assert.AreEqual(true ,  dc.AllowDBNull, "DC1");
48
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");
52
53                         // set AllowDBNull=false 
54                         try
55                         {
56                                 dc.AllowDBNull=false; //the exisiting row contains null value
57                                 Assert.Fail("DC3: AllowDbNull Failed to throw DataException");
58                         }
59                         catch (DataException) {}
60                         catch (AssertionException exc) {throw  exc;}
61                         catch (Exception exc)
62                         {
63                                 Assert.Fail("DC4: AllowDbNull. Wrong exception type. Got:" + exc);
64                         }
65
66                         dt.Rows.Clear();
67                         dc.AllowDBNull=false;
68                         // AllowDBNull=false - adding new row with null value
69                         try
70                         {
71                                 dt.Rows.Add(dt.NewRow());
72                                 Assert.Fail("DC5: RowAdd Failed to throw NoNullAllowedException");
73                         }
74                         catch (NoNullAllowedException) {}
75                         catch (AssertionException exc) {throw  exc;}
76                         catch (Exception exc)
77                         {
78                                 Assert.Fail("DC6: RowAdd. Wrong exception type. Got:" + exc);
79                         }
80
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");
86                 }
87
88                 [Test] public void AutoIncrement()
89                 {
90                         DataColumn dc;
91                         dc = new DataColumn("ColName",typeof(string));
92
93                         // Checking default value (False)
94                         Assert.AreEqual(false , dc.AutoIncrement, "DC8");
95
96                         //Cheking Set
97                         dc.AutoIncrement=true;
98                         // Checking Get
99                         Assert.AreEqual(true , dc.AutoIncrement, "DC9");
100                 }
101
102                 [Test] public void AutoIncrementSeed()
103                 {
104                         DataColumn dc;
105                         dc = new DataColumn("ColName",typeof(string));
106
107                         // Checking default value 0
108                         Assert.AreEqual((long)0, dc.AutoIncrementSeed , "DC10");
109
110                         //Cheking Set
111                         dc.AutoIncrementSeed = long.MaxValue;
112                         // Checking Get MaxValue
113                         Assert.AreEqual(long.MaxValue, dc.AutoIncrementSeed , "DC11");
114
115                         //Cheking Set
116                         dc.AutoIncrementSeed = long.MinValue ;
117                         // Checking Get MinValue
118                         Assert.AreEqual(long.MinValue , dc.AutoIncrementSeed, "DC12");
119                 }
120
121                 [Test] public void AutoIncrementStep()
122                 {
123                         DataColumn dc;
124                         dc = new DataColumn("ColName",typeof(string));
125                         // Checking default value 1
126                         Assert.AreEqual((long)1,  dc.AutoIncrementStep , "DC13");
127
128                         //Cheking Set
129                         dc.AutoIncrementStep = long.MaxValue;
130                         // Checking Get MaxValue
131                         Assert.AreEqual(long.MaxValue, dc.AutoIncrementStep , "DC14");
132
133                         //Cheking Set
134                         dc.AutoIncrementStep = long.MinValue ;
135                         // Checking Get MinValue
136                         Assert.AreEqual(long.MinValue ,  dc.AutoIncrementStep , "DC15");
137                 }
138
139                 [Test] public void Caption()
140                 {
141                         DataColumn dc;
142                         string sCaption = "NewCaption";
143                         dc = new DataColumn("ColName",typeof(string));
144
145                         //Checking default value ( ColumnName )
146                         // Checking default value ( ColumnName )
147                         Assert.AreEqual(dc.ColumnName , dc.Caption, "DC16");
148
149                         //Cheking Set
150                         dc.Caption = sCaption;
151                         // Checking Get
152                         Assert.AreEqual(sCaption , dc.Caption , "DC17");
153                 }
154
155                 [Test] public void ColumnName()
156                 {
157                         DataColumn dc;
158                         string sName = "NewName";
159
160                         dc = new DataColumn();
161                         //Checking default value ("")
162                         // ColumnName default value
163                         Assert.AreEqual(string.Empty ,  dc.ColumnName, "DC18");
164
165                         //Cheking Set
166                         dc.ColumnName = sName;
167                         //Checking Get
168                         // ColumnName Get/Set
169                         Assert.AreEqual(sName , dc.ColumnName , "DC19");
170
171                         //Special chars (valid chars)
172                         sName = "~()#\\/=><+-*%&|^'\"[]";
173                         // ColumnName Special chars
174                         dc.ColumnName = sName ; 
175                         Assert.AreEqual(sName , dc.ColumnName , "DC20");
176                 }
177
178                 [Test] public void DataType()
179                 {
180                         DataColumn dc;
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" };
186
187                         //Checking default value (string)
188                         // GetType - Default
189                         Assert.AreEqual(Type.GetType("System.String") ,  dc.DataType, "DC21");
190
191                         foreach (string sType in sTypeArr) 
192                         {
193                                 //Cheking Set
194                                 dc.DataType = Type.GetType(sType);
195                                 // Checking GetType " + sType);
196                                 Assert.AreEqual(Type.GetType(sType) , dc.DataType , "DC22");
197                         }
198                 }
199
200                 [Test] public void Equals()
201                 {
202                         DataColumn dc1,dc2;
203                         dc1 = new DataColumn();
204                         dc2 = new DataColumn();
205                         // #1
206                         // Equals 1
207                         Assert.AreEqual(false , dc1.Equals(dc2) , "DC23");
208
209                         dc1 = dc2;
210                         // #2
211                         // Equals 2
212                         Assert.AreEqual(dc2 , dc1 , "DC24");
213                 }
214
215                 [Test] public void ExtendedProperties()
216                 {
217                         DataColumn dc;
218                         PropertyCollection pc;
219                         dc = new DataColumn();
220
221                         pc = dc.ExtendedProperties ;
222                         // Checking ExtendedProperties default 
223                         Assert.AreEqual(true, pc != null, "DC25");
224
225                         // Checking ExtendedProperties count 
226                         Assert.AreEqual(0, pc.Count , "DC26");
227                 }
228
229                 [Test] public void TestGetHashCode()
230                 {
231                         DataColumn dc1;
232                         int iHashCode1;
233                         dc1 = new DataColumn();
234
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");
240                         }
241                 }
242
243                 [Test] public void TestGetType()
244                 {
245                         DataColumn dc;
246                         Type myType;
247                         dc = new DataColumn();
248                         myType = dc.GetType();
249
250                         // GetType
251                         Assert.AreEqual(typeof(DataColumn), myType, "DC28");
252                 }
253
254                 [Test] public void MaxLength()
255                 {
256                         DataColumn dc;
257                         dc = new DataColumn("ColName",typeof(string));
258
259                         //Checking default value (-1)
260                         // MaxLength default
261                         Assert.AreEqual((int)-1, dc.MaxLength , "DC29");
262
263                         //Cheking Set MaxValue
264                         dc.MaxLength = int.MaxValue ;
265                         //Checking Get MaxValue
266                         // MaxLength MaxValue
267                         Assert.AreEqual(int.MaxValue , dc.MaxLength , "DC30");
268
269                         //Cheking Set MinValue
270                         dc.MaxLength = int.MinValue  ;
271                         //Checking Get MinValue
272                         // MaxLength MinValue
273                         Assert.AreEqual(int.MinValue, dc.MaxLength , "DC31");
274
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"});
279
280                         //MaxLength = 5
281                         try
282                         {
283                                 // MaxLength = 5
284                                 dt.Rows[0][0] = "123456";
285                                 Assert.Fail("DC32: Indexer failed to throw ArgumentException");
286                         }
287                         catch(ArgumentException) {}
288                         catch (AssertionException exc) {throw  exc;}
289                         catch (Exception exc)
290                         {
291                                 Assert.Fail("DC33: Indexer. Wrong exception type. Got:" + exc);
292                         }
293                 }
294
295                 [Test] public void Namespace()
296                 {
297                         DataColumn dc;
298                         string sName = "NewName";
299
300                         dc = new DataColumn();
301                         //Checking default value ("")
302                                 // Namespace default
303                                 Assert.AreEqual(string.Empty , dc.Namespace , "DC34");
304
305                         //Cheking Set
306                         dc.Namespace  = sName;
307                         //Checking Get
308                                 // Namespace Get/Set
309                                 Assert.AreEqual(sName, dc.Namespace , "DC35");
310                 }
311
312                 [Test] public void Prefix()
313                 {
314                         DataColumn dc;
315                         string sPrefix = "Prefix";
316                         dc = new DataColumn("ColName",typeof(string));
317
318                                 // Prefix Checking default value (string.Empty)
319                                 Assert.AreEqual(string.Empty , dc.Prefix , "DC36");
320
321                         //Cheking Set
322                         dc.Prefix = sPrefix;
323                         //Checking Get
324                                 // Prefix Checking Get
325                                 Assert.AreEqual(sPrefix , dc.Prefix , "DC37");
326                 }
327
328                 [Test] public void ReadOnly()
329                 {
330                         DataColumn dc;
331                         dc = new DataColumn();
332
333                         //Checking default value (false)
334                                 // ReadOnly default
335                                 Assert.AreEqual(false , dc.ReadOnly , "DC38");
336
337                         //Cheking Set
338                         dc.ReadOnly=true;
339                         //Checking Get
340                                 // ReadOnly Get/Set
341                                 Assert.AreEqual(true, dc.ReadOnly , "DC39");
342                 }
343
344                 [Test] public void Table()
345                 {
346                         DataColumn dc;
347                         dc = new DataColumn();
348
349                         //Checking First Get
350                                 // Table test1
351                                 Assert.AreEqual(null,  dc.Table, "DC40");
352
353                         DataTable dt = new DataTable();
354                         dt.Columns.Add(dc);
355
356                         //Checking Second Get
357                                 // Table test2
358                                 Assert.AreEqual(dt, dc.Table , "DC41");
359                 }
360
361                 [Test] public void TestToString()
362                 {
363                         DataColumn dc;
364                         string sColName,sExp;
365                         dc = new DataColumn();
366
367                         //ToString = ""
368                         //Console.WriteLine(dc.ToString());
369
370                         //ToString = ColumnName                         
371                         sColName = "Test1";
372                         dc.ColumnName = sColName;
373                                 // ToString - ColumnName
374                                 Assert.AreEqual(sColName , dc.ToString() , "DC42");
375
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");
381                 }
382
383                 [Test] public void Unique()
384                 {
385                         DataColumn dc;
386                         dc = new DataColumn();
387                         //Checking default value (false)
388
389                                 // Unique default
390                                 Assert.AreEqual(false , dc.Unique , "DC44");
391
392                         //Cheking Set
393                         dc.Unique=true;
394
395                         //Checking Get
396                         // Unique Get/Set
397                         Assert.AreEqual(true,  dc.Unique, "DC45");
398                 }
399
400                 [Test] public void ctor()
401                 {
402                         DataColumn dc;
403                         dc = new DataColumn();
404
405                         // ctor
406                         Assert.AreEqual(false, dc == null, "DC46");
407                 }
408
409                 [Test] public void ctor_ByColumnName()
410                 {
411                         DataColumn dc;
412                         string sName = "ColName";
413                         dc = new DataColumn(sName);
414
415                         // ctor - object
416                         Assert.AreEqual(false , dc==null , "DC47");
417
418                         // ctor - ColName
419                         Assert.AreEqual(sName, dc.ColumnName , "DC48");
420                 }
421
422                 [Test] public void ctor_ByColumnNameType()
423                 {
424                         Type typTest;
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" };
430
431                         foreach (string sType in sTypeArr) 
432                         {
433                                 typTest = Type.GetType(sType);
434                                 dc = new DataColumn("ColName",typTest);
435                                 // ctor - object
436                                 Assert.AreEqual(false , dc==null, "DC49");
437
438                                 // ctor - ColName
439                                 Assert.AreEqual(typTest ,  dc.DataType , "DC50");
440                         }
441                 }
442
443                 [Test] public void ctor_ByColumnNameTypeExpression()
444                 {
445                         DataColumn dc;
446                         dc = new DataColumn("ColName",typeof(String),"Price * 1.18");
447
448                         // ctor - object
449                         Assert.AreEqual(false , dc==null, "DC51");
450                 }
451
452                 [Test] public void ctor_ByColumnNameTypeExpressionMappingType()
453                 {
454                         DataColumn dc;
455                         //Cheking constructor for each Enum MappingType
456                         foreach (int i in Enum.GetValues(typeof(MappingType))) 
457                         {
458                                 dc = null;
459                                 dc = new DataColumn("ColName",typeof(string),"Price * 1.18",(MappingType)i );
460                                 // Ctor #" + i.ToString());
461                                 Assert.AreEqual(false , dc==null , "DC52");
462                         }
463                 }
464
465                 [Test] public void ordinal()
466                 {
467                         DataColumn dc;
468                         dc = new DataColumn("ColName",typeof(string));
469
470                         //DEBUG
471                         //Console.WriteLine( "***" + dc.Ordinal.ToString()  + "***
472                         //DEBUG
473
474                         //Checking default value 
475                         // Ordinal default value
476                         Assert.AreEqual((int)-1 ,  dc.Ordinal, "DC53");
477
478                         // needs a DataTable.Columns to test   
479                         DataColumnCollection dcColl ;
480                         DataTable tb = new DataTable();
481                         dcColl = tb.Columns ;
482                         dcColl.Add();   //0
483                         dcColl.Add();   //1
484                         dcColl.Add();   //2
485                         dcColl.Add(dc); //3
486
487                         //Checking Get
488                         // Ordinal Get
489                         Assert.AreEqual((int)3 , dc.Ordinal , "DC54");
490                 }
491         }
492 }