1 // Licensed to the .NET Foundation under one or more agreements. 2 // See the LICENSE file in the project root for more information. 3 // 4 // EnumerableRowCollectionTest.cs 5 // 6 // Author: 7 // Atsushi Enomoto <atsushi@ximian.com> 8 // 9 // Copyright (C) 2008 Novell, Inc. http://www.novell.com 10 // 11 12 // 13 // Permission is hereby granted, free of charge, to any person obtaining 14 // a copy of this software and associated documentation files (the 15 // "Software"), to deal in the Software without restriction, including 16 // without limitation the rights to use, copy, modify, merge, publish, 17 // distribute, sublicense, and/or sell copies of the Software, and to 18 // permit persons to whom the Software is furnished to do so, subject to 19 // the following conditions: 20 // 21 // The above copyright notice and this permission notice shall be 22 // included in all copies or substantial portions of the Software. 23 // 24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 // 32 33 using System; 34 using System.Collections.Generic; 35 using System.Data; 36 using Xunit; 37 38 namespace MonoTests.System.Data 39 { 40 public class EnumerableRowCollectionTest 41 { 42 private string _testDataSet = "testdataset1.xml"; 43 44 [Fact] QueryWhere()45 public void QueryWhere() 46 { 47 var ds = new DataSet(); 48 ds.ReadXml(_testDataSet); 49 var table = ds.Tables[0]; 50 /* schema generated as ... 51 var table = ds.Tables.Add ("ScoreList"); 52 table.Columns.Add ("ID", typeof (int)); 53 table.Columns.Add ("RegisteredDate", typeof (DateTime)); 54 table.Columns.Add ("Name", typeof (string)); 55 table.Columns.Add ("Score", typeof (int)); 56 ds.WriteXml ("Test/System.Data/testdataset1.xsd", XmlWriteMode.WriteSchema); 57 */ 58 var q = from line in table.AsEnumerable() 59 where line.Field<int>("Score") > 80 60 select line; 61 bool iterated = false; 62 foreach (var line in q) 63 { 64 if (iterated) 65 Assert.True(false, "should match only one raw"); 66 Assert.Equal(100, line["Score"]); 67 iterated = true; 68 } 69 } 70 71 [Fact] QueryWhereSelect()72 public void QueryWhereSelect () 73 { 74 var ds = new DataSet (); 75 ds.ReadXml (_testDataSet); 76 var table = ds.Tables [0]; 77 var q = from line in table.AsEnumerable () 78 where line.Field<int> ("Score") > 80 79 select new { 80 StudentID = line.Field<int> ("ID"), 81 StudentName = line.Field<string> ("Name"), 82 StudentScore = line.Field<int> ("Score") }; 83 bool iterated = false; 84 foreach (var ql in q) { 85 if (iterated) 86 Assert.True(false, "should match only one raw"); 87 Assert.Equal(100, ql.StudentScore); 88 iterated = true; 89 } 90 } 91 92 [Fact] QueryWhereSelectOrderBy()93 public void QueryWhereSelectOrderBy () 94 { 95 var ds = new DataSet (); 96 ds.ReadXml (_testDataSet); 97 var table = ds.Tables [0]; 98 var q = from line in table.AsEnumerable () 99 where line.Field<int> ("Score") >= 80 100 orderby line.Field<int> ("ID") 101 select new { 102 StudentID = line.Field<int> ("ID"), 103 StudentName = line.Field<string> ("Name"), 104 StudentScore = line.Field<int> ("Score") }; 105 int prevID = -1; 106 foreach (var ql in q) { 107 switch (prevID) { 108 case -1: 109 Assert.Equal(1, ql.StudentID); 110 break; 111 case 1: 112 Assert.Equal(4, ql.StudentID); 113 break; 114 default: 115 Assert.True(false, "should match only one raw"); 116 break; 117 } 118 prevID = ql.StudentID; 119 } 120 } 121 122 [Fact] QueryWhereSelectOrderByDescending()123 public void QueryWhereSelectOrderByDescending () 124 { 125 var ds = new DataSet (); 126 ds.ReadXml (_testDataSet); 127 var table = ds.Tables [0]; 128 var q = from line in table.AsEnumerable () 129 where line.Field<int> ("Score") >= 80 130 orderby line.Field<int> ("ID") descending 131 select new { 132 StudentID = line.Field<int> ("ID"), 133 StudentName = line.Field<string> ("Name"), 134 StudentScore = line.Field<int> ("Score") }; 135 int prevID = -1; 136 foreach (var ql in q) { 137 switch (prevID) { 138 case -1: 139 Assert.Equal(4, ql.StudentID); 140 break; 141 case 4: 142 Assert.Equal(1, ql.StudentID); 143 break; 144 default: 145 Assert.True(false, "should match only one raw"); 146 break; 147 } 148 prevID = ql.StudentID; 149 } 150 } 151 152 [Fact] ThenBy()153 public void ThenBy () 154 { 155 var ds = new DataSet (); 156 ds.ReadXml (_testDataSet); 157 var table = ds.Tables [0]; 158 var q = from line in table.AsEnumerable () 159 where line.Field<int> ("Score") >= 80 160 orderby line.Field<bool> ("Gender"), line.Field<int> ("ID") 161 select new { 162 StudentID = line.Field<int> ("ID"), 163 StudentName = line.Field<string> ("Name"), 164 StudentScore = line.Field<int> ("Score") }; 165 int prevID = -1; 166 foreach (var ql in q) { 167 switch (prevID) { 168 case -1: 169 Assert.Equal(1, ql.StudentID); 170 break; 171 case 1: 172 Assert.Equal(4, ql.StudentID); 173 break; 174 default: 175 Assert.True(false, "should match only one raw"); 176 break; 177 } 178 prevID = ql.StudentID; 179 } 180 } 181 182 [Fact] ThenByDescending()183 public void ThenByDescending () 184 { 185 var ds = new DataSet (); 186 ds.ReadXml (_testDataSet); 187 var table = ds.Tables [0]; 188 var q = from line in table.AsEnumerable () 189 where line.Field<int> ("Score") >= 80 190 orderby line.Field<bool> ("Gender"), line.Field<int> ("ID") descending 191 select new { 192 StudentID = line.Field<int> ("ID"), 193 StudentName = line.Field<string> ("Name"), 194 StudentScore = line.Field<int> ("Score") }; 195 int prevID = -1; 196 foreach (var ql in q) { 197 switch (prevID) { 198 case -1: 199 Assert.Equal(4, ql.StudentID); 200 break; 201 case 4: 202 Assert.Equal(1, ql.StudentID); 203 break; 204 default: 205 Assert.True(false, "should match only one raw"); 206 break; 207 } 208 prevID = ql.StudentID; 209 } 210 } 211 } 212 }