2 // DataTableExtensionsTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell, Inc. http://www.novell.com
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections.Generic;
35 using NUnit.Framework;
37 namespace MonoTests.System.Data
40 public class DataTableExtensionsTest
43 [ExpectedException (typeof (InvalidOperationException))] // for no rows
44 public void CopyToDataTableNoArgNoRows ()
46 DataTable dt = new DataTable ();
47 dt.Columns.Add ("CID", typeof (int));
48 dt.Columns.Add ("CName", typeof (string));
49 dt.AsEnumerable ().CopyToDataTable<DataRow> ();
53 public void CopyToDataTableNoArg ()
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");
65 // no error for empty table this time.
66 [Category ("NotWorking")] // some DataTableReader internal issues
67 public void CopyToDataTableTableArgNoRows ()
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);
77 public void AsEnumerable ()
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");
93 public void AsDataView ()
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");