PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Sources/DataTableTests/Tests.cs

https://github.com/tpwalke2/DataTable
C# | 218 lines | 159 code | 47 blank | 12 comment | 1 complexity | 2ee8c6f2b8c563b5ae43c40527609ed9 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Xunit;
  6. using DataAccess;
  7. using System.IO;
  8. namespace DataTableTests
  9. {
  10. public class Scenarios
  11. {
  12. public static MutableDataTable GetTable()
  13. {
  14. TextReader tr = new StringReader(
  15. @"name, age, favorite fruit
  16. Bob, 20, apples
  17. Ed, 65, prunes
  18. Sarah, 40, cherries");
  19. MutableDataTable dt = DataTable.New.Read(tr);
  20. return dt;
  21. }
  22. [Fact]
  23. public void ReadStronglyTypedRows()
  24. {
  25. var dt = GetTable();
  26. var rows = dt.RowsAs<RowType>().ToArray();
  27. Assert.Equal(3, rows.Length);
  28. Assert.Equal("Bob", rows[0].Name);
  29. Assert.Equal(20, rows[0].Age);
  30. Assert.Equal(RowType.Fruit.apples, rows[0].FavoriteFruit);
  31. Assert.Equal("Ed", rows[1].Name);
  32. Assert.Equal(65, rows[1].Age);
  33. Assert.Equal(RowType.Fruit.prunes, rows[1].FavoriteFruit);
  34. Assert.Equal("Sarah", rows[2].Name);
  35. Assert.Equal(40, rows[2].Age);
  36. Assert.Equal(RowType.Fruit.cherries, rows[2].FavoriteFruit);
  37. }
  38. class RowType
  39. {
  40. public string Name { get; set; }
  41. public int Age { get; set; }
  42. public enum Fruit
  43. {
  44. apples,
  45. prunes,
  46. cherries,
  47. }
  48. // $$$: Need to sort out name problem. column name has a space.
  49. public Fruit FavoriteFruit { get; set; }
  50. }
  51. [Fact]
  52. public void Columns()
  53. {
  54. MutableDataTable dt = GetTable();
  55. Assert.Equal(new string[] { "name", "age", "favorite fruit" },
  56. dt.ColumnNames);
  57. Assert.Null(dt.GetColumn("missing")); // missing columns return null
  58. }
  59. [Fact]
  60. public void GetValuesFromRows()
  61. {
  62. MutableDataTable dt = GetTable();
  63. Assert.Equal(3, dt.NumRows);
  64. Row[] rows = dt.Rows.ToArray();
  65. Assert.Equal("Bob", rows[0]["name"]); // index by column name
  66. Assert.Equal("cherries", rows[2]["favorite fruit"]); // index by column name
  67. Assert.Equal(new string[] { "Ed", "65", "prunes" },
  68. rows[1].Values.ToArray());
  69. }
  70. [Fact]
  71. public void RowWriteLine()
  72. {
  73. MutableDataTable dt = GetTable();
  74. Row row = dt.GetRow(1);
  75. StringWriter sw = new StringWriter();
  76. row.WriteCsv(sw);
  77. // Test writing a single row back to a stream.
  78. // Spacing is arbitrary. The write won't emit extra spaces.
  79. Assert.Equal("Ed,65,prunes\r\n", sw.ToString());
  80. }
  81. class Point
  82. {
  83. public int x { get; set; }
  84. public int y { get; set; }
  85. }
  86. [Fact]
  87. public void ArrayToTable()
  88. {
  89. Point[] ps = new Point[] {
  90. new Point { x= 11, y=12},
  91. new Point { x= 21, y=22},
  92. new Point { x=31, y=32}
  93. };
  94. // Tests converting an array of structs into a table
  95. MutableDataTable dt = DataTable.New.FromEnumerable(ps);
  96. StringWriter sw = new StringWriter();
  97. dt.SaveToStream(sw);
  98. AnalyzeTests.AssertEquals(
  99. @"x,y
  100. 11,12
  101. 21,22
  102. 31,32
  103. ", sw.ToString());
  104. }
  105. [Fact]
  106. public void TableFromLinqExpressionWithAnonymousType()
  107. {
  108. var x = from i in Enumerable.Range(1, 5) select new { N = i, NSquared = i*i };
  109. var dt = DataTable.New.FromEnumerable(x);
  110. AnalyzeTests.AssertEquals(
  111. @"N,NSquared
  112. 1,1
  113. 2,4
  114. 3,9
  115. 4,16
  116. 5,25
  117. ", dt);
  118. }
  119. [Fact]
  120. public void ToMutable()
  121. {
  122. var x = from i in Enumerable.Range(1, 5) select new { N = i, NSquared = i * i };
  123. DataTable dt = DataTable.New.FromEnumerable(x);
  124. MutableDataTable dt2 = DataTable.New.GetMutableCopy(dt);
  125. AnalyzeTests.AssertEquals(
  126. @"N,NSquared
  127. 1,1
  128. 2,4
  129. 3,9
  130. 4,16
  131. 5,25
  132. ", dt2);
  133. }
  134. [Fact]
  135. public void QueryWithRowAgainstTable()
  136. {
  137. var x = from i in Enumerable.Range(1, 5) select new { N = i, NSquared = i * i };
  138. var dt = DataTable.New.FromEnumerable(x);
  139. // Test using the row object to lookup
  140. var y = from row in dt.Rows where row["N"] == "3" select row["NSquared"];
  141. Assert.Equal(1, y.Count());
  142. Assert.Equal("9", y.First());
  143. }
  144. // Enumerable where we should only ask for the first and then stop.
  145. static IEnumerable<int> TestEnumerable()
  146. {
  147. yield return 1;
  148. // Reaching here means we didn't lazily read the enumerable.
  149. Assert.True(false, "Should not have reached here");
  150. }
  151. [Fact]
  152. public void LazyEnumerable()
  153. {
  154. //var dt = DataTable.New.FromEnumerableLazy(TestEnumerable());
  155. var dt = DataTable.New.FromEnumerableLazy(TestEnumerable());
  156. // Test using the row object to lookup
  157. var y = from row in dt.Rows
  158. let i = int.Parse(row["value"])
  159. select i;
  160. // Just taking the first element should succeed since we read lazily.
  161. // If we accidentally read the whole enumeration, we'd fail.
  162. Assert.Equal(1, y.First());
  163. // Verify that we can read it a second time.
  164. var sample = Analyze.SampleTopN(dt, 1);
  165. AnalyzeTests.AssertEquals(
  166. @"value
  167. 1
  168. ", sample);
  169. }
  170. }
  171. }