[System.Data] move to corefx (#4893)
[mono-project.git] / mcs / class / System.Data.DataSetExtensions / Test / System.Data / DataTableExtensionsTest.cs
blob31f2d75a4f7debfde2a4542463e9a2c1237890a5
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 //
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.
31 using System;
32 using System.Collections.Generic;
33 using System.Data;
34 using System.Linq;
35 using NUnit.Framework;
37 namespace MonoTests.System.Data
39 [TestFixture]
40 public class DataTableExtensionsTest
42 [Test]
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> ();
52 [Test]
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");
64 [Test]
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);
76 [Test]
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");
92 #if !COREFX //LinqDataView is not supported yet
93 [Test]
94 public void AsDataView ()
96 DataSet ds = new DataSet ();
97 ds.ReadXml ("Test/System.Data/testdataset1.xml");
98 DataTable dt = ds.Tables [0];
99 var dv = dt.AsEnumerable ().Where<DataRow> ((DataRow r) => (int) r ["Score"] > 60).AsDataView<DataRow> ();
100 Assert.AreEqual (1, dv [0] ["ID"], "#1");
101 Assert.AreEqual (4, dv [1] ["ID"], "#2");
103 #endif