|
| 1 | +#region License and Terms |
| 2 | +// MoreLINQ - Extensions to LINQ to Objects |
| 3 | +// Copyright (c) 2024 Atif Aziz. All rights reserved. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +#endregion |
| 17 | + |
| 18 | +namespace MoreLinq.Test |
| 19 | +{ |
| 20 | + using System; |
| 21 | + using System.Collections; |
| 22 | + using System.Collections.Generic; |
| 23 | + using System.Data; |
| 24 | + using System.Diagnostics.CodeAnalysis; |
| 25 | + using System.Linq; |
| 26 | + using System.Linq.Expressions; |
| 27 | + |
| 28 | + [TestClass] |
| 29 | + public class ToDataTableTest |
| 30 | + { |
| 31 | + sealed class TestObject(int key) |
| 32 | + { |
| 33 | + public int KeyField = key; |
| 34 | + public Guid? ANullableGuidField = Guid.NewGuid(); |
| 35 | + |
| 36 | + public string AString { get; } = "ABCDEFGHIKKLMNOPQRSTUVWXYSZ"; |
| 37 | + public decimal? ANullableDecimal { get; } = key / 3; |
| 38 | + public object Unreadable { set => throw new NotImplementedException(); } |
| 39 | + |
| 40 | + public object this[int index] { get => new(); set { } } |
| 41 | + |
| 42 | + public override string ToString() => nameof(TestObject); |
| 43 | + } |
| 44 | + |
| 45 | + readonly IReadOnlyCollection<TestObject> testObjects; |
| 46 | + |
| 47 | + public ToDataTableTest() => |
| 48 | + this.testObjects = Enumerable.Range(0, 3) |
| 49 | + .Select(i => new TestObject(i)) |
| 50 | + .ToArray(); |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + public void ToDataTableNullMemberExpressionMethod() |
| 54 | + { |
| 55 | + Expression<Func<TestObject, object?>>? expression = null; |
| 56 | + |
| 57 | + [UnconditionalSuppressMessage("Aot", "IL2026")] |
| 58 | + void Act() => _ = this.testObjects.ToDataTable(expression!); |
| 59 | + |
| 60 | + var e = Assert.ThrowsException<ArgumentException>(Act); |
| 61 | + Assert.AreEqual("expressions", e.ParamName); |
| 62 | + } |
| 63 | + |
| 64 | + [TestMethod] |
| 65 | + public void ToDataTableTableWithWrongColumnNames() |
| 66 | + { |
| 67 | + using var dt = new DataTable(); |
| 68 | + _ = dt.Columns.Add("Test"); |
| 69 | + |
| 70 | + [UnconditionalSuppressMessage("Aot", "IL2026")] |
| 71 | + void Act() => _ = this.testObjects.ToDataTable(dt); |
| 72 | + |
| 73 | + var e = Assert.ThrowsException<ArgumentException>(Act); |
| 74 | + Assert.AreEqual("table", e.ParamName); |
| 75 | + } |
| 76 | + |
| 77 | + [TestMethod] |
| 78 | + public void ToDataTableTableWithWrongColumnDataType() |
| 79 | + { |
| 80 | + using var dt = new DataTable(); |
| 81 | + _ = dt.Columns.Add("AString", typeof(int)); |
| 82 | + |
| 83 | + [UnconditionalSuppressMessage("Aot", "IL2026")] |
| 84 | + void Act() => _ = this.testObjects.ToDataTable(dt, t => t.AString); |
| 85 | + |
| 86 | + var e = Assert.ThrowsException<ArgumentException>(Act); |
| 87 | + Assert.AreEqual("table", e.ParamName); |
| 88 | + } |
| 89 | + |
| 90 | + void TestDataTableMemberExpression(Expression<Func<TestObject, object?>> expression) |
| 91 | + { |
| 92 | + [UnconditionalSuppressMessage("Aot", "IL2026")] |
| 93 | + void Act() => _ = this.testObjects.ToDataTable(expression); |
| 94 | + |
| 95 | + var e = Assert.ThrowsException<ArgumentException>(Act); |
| 96 | + Assert.AreEqual("expressions", e.ParamName); |
| 97 | + var innerException = e.InnerException; |
| 98 | + Assert.IsNotNull(innerException); |
| 99 | + Assert.IsInstanceOfType<ArgumentException>(innerException); |
| 100 | + Assert.AreEqual("lambda", ((ArgumentException)innerException).ParamName); |
| 101 | + } |
| 102 | + |
| 103 | + [TestMethod] |
| 104 | + public void ToDataTableMemberExpressionMethod() |
| 105 | + { |
| 106 | + TestDataTableMemberExpression(t => t.ToString()); |
| 107 | + } |
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public void ToDataTableMemberExpressionNonMember() |
| 111 | + { |
| 112 | + TestDataTableMemberExpression(t => t.ToString().Length); |
| 113 | + } |
| 114 | + |
| 115 | + [TestMethod] |
| 116 | + public void ToDataTableMemberExpressionIndexer() |
| 117 | + { |
| 118 | + TestDataTableMemberExpression(t => t[0]); |
| 119 | + } |
| 120 | + |
| 121 | + [TestMethod] |
| 122 | + public void ToDataTableMemberExpressionStatic() |
| 123 | + { |
| 124 | + TestDataTableMemberExpression(_ => DateTime.Now); |
| 125 | + } |
| 126 | + |
| 127 | + [TestMethod] |
| 128 | + public void ToDataTableSchemaInDeclarationOrder() |
| 129 | + { |
| 130 | + [UnconditionalSuppressMessage("Aot", "IL2026")] |
| 131 | + DataTable Act() => this.testObjects.ToDataTable(); |
| 132 | + |
| 133 | + var dt = Act(); |
| 134 | + |
| 135 | + // Assert properties first, then fields, then in declaration order |
| 136 | + |
| 137 | + Assert.AreEqual("KeyField", dt.Columns[2].Caption); |
| 138 | + Assert.AreEqual(typeof(int), dt.Columns[2].DataType); |
| 139 | + |
| 140 | + Assert.AreEqual("ANullableGuidField", dt.Columns[3].Caption); |
| 141 | + Assert.AreEqual(typeof(Guid), dt.Columns[3].DataType); |
| 142 | + Assert.IsTrue(dt.Columns[3].AllowDBNull); |
| 143 | + |
| 144 | + Assert.AreEqual("AString", dt.Columns[0].Caption); |
| 145 | + Assert.AreEqual(typeof(string), dt.Columns[0].DataType); |
| 146 | + |
| 147 | + Assert.AreEqual("ANullableDecimal", dt.Columns[1].Caption); |
| 148 | + Assert.AreEqual(typeof(decimal), dt.Columns[1].DataType); |
| 149 | + |
| 150 | + Assert.AreEqual(4, dt.Columns.Count); |
| 151 | + } |
| 152 | + |
| 153 | + [TestMethod] |
| 154 | + public void ToDataTableContainsAllElements() |
| 155 | + { |
| 156 | + [UnconditionalSuppressMessage("Aot", "IL2026")] |
| 157 | + DataTable Act() => this.testObjects.ToDataTable(); |
| 158 | + |
| 159 | + var dt = Act(); |
| 160 | + |
| 161 | + Assert.AreEqual(this.testObjects.Count, dt.Rows.Count); |
| 162 | + } |
| 163 | + |
| 164 | + [TestMethod] |
| 165 | + public void ToDataTableWithExpression() |
| 166 | + { |
| 167 | + [UnconditionalSuppressMessage("Aot", "IL2026")] |
| 168 | + DataTable Act() => this.testObjects.ToDataTable(t => t.AString); |
| 169 | + |
| 170 | + var dt = Act(); |
| 171 | + |
| 172 | + Assert.AreEqual("AString", dt.Columns[0].Caption); |
| 173 | + Assert.AreEqual(typeof(string), dt.Columns[0].DataType); |
| 174 | + |
| 175 | + Assert.AreEqual(1, dt.Columns.Count); |
| 176 | + } |
| 177 | + |
| 178 | + [TestMethod] |
| 179 | + public void ToDataTableWithSchema() |
| 180 | + { |
| 181 | + using var dt = new DataTable(); |
| 182 | + var columns = dt.Columns; |
| 183 | + _ = columns.Add("Column1", typeof(int)); |
| 184 | + _ = columns.Add("Value", typeof(string)); |
| 185 | + _ = columns.Add("Column3", typeof(int)); |
| 186 | + _ = columns.Add("Name", typeof(string)); |
| 187 | + |
| 188 | + var vars = Environment.GetEnvironmentVariables() |
| 189 | + .Cast<DictionaryEntry>() |
| 190 | + .ToArray(); |
| 191 | + |
| 192 | + [UnconditionalSuppressMessage("Aot", "IL2026")] |
| 193 | + void Act() => _ = vars.Select(e => new { Name = e.Key.ToString(), Value = e.Value!.ToString() }) |
| 194 | + .ToDataTable(dt, e => e.Name, e => e.Value); |
| 195 | + |
| 196 | + Act(); |
| 197 | + |
| 198 | + var rows = dt.Rows.Cast<DataRow>().ToArray(); |
| 199 | + Assert.AreEqual(vars.Length, rows.Length); |
| 200 | + CollectionAssert.AreEqual(vars.Select(e => e.Key).ToArray(), rows.Select(r => r["Name"]).ToArray()); |
| 201 | + CollectionAssert.AreEqual(vars.Select(e => e.Value).ToArray(), rows.Select(r => r["Value"]).ToArray()); |
| 202 | + } |
| 203 | + |
| 204 | + readonly struct Point(int x, int y) |
| 205 | + { |
| 206 | +#pragma warning disable CA1805 // Do not initialize unnecessarily (avoids CS0649) |
| 207 | + public static Point Empty = new(); |
| 208 | +#pragma warning restore CA1805 // Do not initialize unnecessarily |
| 209 | + public bool IsEmpty => X == 0 && Y == 0; |
| 210 | + public int X { get; } = x; |
| 211 | + public int Y { get; } = y; |
| 212 | + } |
| 213 | + |
| 214 | + [TestMethod] |
| 215 | + public void ToDataTableIgnoresStaticMembers() |
| 216 | + { |
| 217 | + [UnconditionalSuppressMessage("Aot", "IL2026")] |
| 218 | + static DataTable Act() => new[] { new Point(12, 34) }.ToDataTable(); |
| 219 | + |
| 220 | + var points = Act(); |
| 221 | + |
| 222 | + Assert.AreEqual(3, points.Columns.Count); |
| 223 | + var x = points.Columns["X"]; |
| 224 | + var y = points.Columns["Y"]; |
| 225 | + var empty = points.Columns["IsEmpty"]; |
| 226 | + Assert.IsNotNull(x); |
| 227 | + Assert.IsNotNull(y); |
| 228 | + Assert.IsNotNull(empty); |
| 229 | + var row = points.Rows.Cast<DataRow>().Single(); |
| 230 | + Assert.AreEqual(12, row[x]); |
| 231 | + Assert.AreEqual(34, row[y]); |
| 232 | + Assert.AreEqual(row[empty], false); |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments