Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mcs / class / System.Data.DataSetExtensions / Test / System.Data / DataTableExtensionsTest.cs
1 //
2 // DataTableExtensionsTest.cs
3 //
4 // Author:
5 //   Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc. http://www.novell.com
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections.Generic;
33 using System.Data;
34 using System.Linq;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Data
38 {
39         [TestFixture]
40         public class DataTableExtensionsTest
41         {
42                 [Test]
43                 [ExpectedException (typeof (InvalidOperationException))] // for no rows
44                 public void CopyToDataTableNoArgNoRows ()
45                 {
46                         DataTable dt = new DataTable ();
47                         dt.Columns.Add ("CID", typeof (int));
48                         dt.Columns.Add ("CName", typeof (string));
49                         dt.AsEnumerable ().CopyToDataTable<DataRow> ();
50                 }
51
52                 [Test]
53                 public void CopyToDataTableNoArg ()
54                 {
55                         DataTable dt = new DataTable ();
56                         dt.Columns.Add ("CID", typeof (int));
57                         dt.Columns.Add ("CName", typeof (string));
58                         dt.Rows.Add (new object [] {1, "foo"});
59                         DataTable dst = dt.AsEnumerable ().CopyToDataTable<DataRow> ();
60                         Assert.AreEqual (1, dst.Rows.Count, "#1");
61                         Assert.AreEqual ("foo", dst.Rows [0] ["CName"], "#2");
62                 }
63
64                 [Test]
65                 // no error for empty table this time.
66                 [Category ("NotWorking")] // some DataTableReader internal issues
67                 public void CopyToDataTableTableArgNoRows ()
68                 {
69                         DataTable dt = new DataTable ();
70                         dt.Columns.Add ("CID", typeof (int));
71                         dt.Columns.Add ("CName", typeof (string));
72                         DataTable dst = new DataTable ();
73                         dt.AsEnumerable ().CopyToDataTable<DataRow> (dst, LoadOption.PreserveChanges);
74                 }
75
76                 [Test]
77                 public void AsEnumerable ()
78                 {
79                         DataSet ds = new DataSet ();
80                         ds.ReadXml ("Test/System.Data/testdataset1.xml");
81                         DataTable dt = ds.Tables [0];
82                         Assert.AreEqual ("ScoreList", dt.TableName, "TableName");
83                         var dv = dt.AsEnumerable ();
84                         Assert.AreEqual (4, dv.Count (), "#0");
85                         var i = dv.GetEnumerator ();
86                         Assert.IsTrue (i.MoveNext (), "#1");
87                         Assert.AreEqual (1, i.Current ["ID"], "#2");
88                         Assert.IsTrue (i.MoveNext (), "#3");
89                         Assert.AreEqual (2, i.Current ["ID"], "#4");
90                 }
91
92                 [Test]
93                 public void AsDataView ()
94                 {
95                         DataSet ds = new DataSet ();
96                         ds.ReadXml ("Test/System.Data/testdataset1.xml");
97                         DataTable dt = ds.Tables [0];
98                         var dv = dt.AsEnumerable ().Where<DataRow> ((DataRow r) => (int) r ["Score"] > 60).AsDataView<DataRow> ();
99                         Assert.AreEqual (1, dv [0] ["ID"], "#1");
100                         Assert.AreEqual (4, dv [1] ["ID"], "#2");
101                 }
102         }
103 }