PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/UnitTests/Linq/Distinct.cs

https://github.com/ACher/bltoolkit
C# | 118 lines | 100 code | 18 blank | 0 comment | 0 complexity | 999f29707bc1eade24cbfe59d514b24d MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using BLToolkit.Data.DataProvider;
  5. namespace Data.Linq
  6. {
  7. using Model;
  8. [TestFixture]
  9. public class DistinctTest : TestBase
  10. {
  11. [Test]
  12. public void Distinct1()
  13. {
  14. ForEachProvider(db => AreEqual(
  15. (from ch in Child select ch.ParentID).Distinct(),
  16. (from ch in db.Child select ch.ParentID).Distinct()));
  17. }
  18. [Test]
  19. public void Distinct2()
  20. {
  21. ForEachProvider(db => AreEqual(
  22. (from p in Parent select p.Value1 ?? p.ParentID % 2).Distinct(),
  23. (from p in db.Parent select p.Value1 ?? p.ParentID % 2).Distinct()));
  24. }
  25. [Test]
  26. public void Distinct3()
  27. {
  28. ForEachProvider(db => AreEqual(
  29. (from p in Parent select new { Value = p.Value1 ?? p.ParentID % 2, p.Value1 }).Distinct(),
  30. (from p in db.Parent select new { Value = p.Value1 ?? p.ParentID % 2, p.Value1 }).Distinct()));
  31. }
  32. [Test]
  33. public void Distinct4()
  34. {
  35. ForEachProvider(db => AreEqual(
  36. (from p in Parent select new Parent { ParentID = p.Value1 ?? p.ParentID % 2, Value1 = p.Value1 }).Distinct(),
  37. (from p in db.Parent select new Parent { ParentID = p.Value1 ?? p.ParentID % 2, Value1 = p.Value1 }).Distinct()));
  38. }
  39. [Test]
  40. public void Distinct5()
  41. {
  42. var id = 2;
  43. ForEachProvider(db => AreEqual(
  44. (from p in Parent select new Parent { ParentID = p.Value1 ?? p.ParentID % 2, Value1 = id + 1 }).Distinct(),
  45. (from p in db.Parent select new Parent { ParentID = p.Value1 ?? p.ParentID % 2, Value1 = id + 1 }).Distinct()));
  46. }
  47. [Test]
  48. public void Distinct6()
  49. {
  50. var id = 2;
  51. ForEachProvider(new[] { ProviderName.Informix }, db => AreEqual(
  52. (from p in Parent select new Parent { ParentID = p.Value1 ?? p.ParentID + id % 2, Value1 = id + 1 }).Distinct(),
  53. (from p in db.Parent select new Parent { ParentID = p.Value1 ?? p.ParentID + id % 2, Value1 = id + 1 }).Distinct()));
  54. }
  55. [Test]
  56. public void DistinctCount()
  57. {
  58. var expected =
  59. from p in Parent
  60. join c in Child on p.ParentID equals c.ParentID
  61. where c.ChildID > 20
  62. select p;
  63. ForEachProvider(db =>
  64. {
  65. var result =
  66. from p in db.Parent
  67. join c in db.Child on p.ParentID equals c.ParentID
  68. where c.ChildID > 20
  69. select p;
  70. Assert.AreEqual(expected.Distinct().Count(), result.Distinct().Count());
  71. });
  72. }
  73. [Test]
  74. public void DistinctMax()
  75. {
  76. var expected =
  77. from p in Parent
  78. join c in Child on p.ParentID equals c.ParentID
  79. where c.ChildID > 20
  80. select p;
  81. ForEachProvider(db =>
  82. {
  83. var result =
  84. from p in db.Parent
  85. join c in db.Child on p.ParentID equals c.ParentID
  86. where c.ChildID > 20
  87. select p;
  88. Assert.AreEqual(expected.Distinct().Max(p => p.ParentID), result.Distinct().Max(p => p.ParentID));
  89. });
  90. }
  91. [Test]
  92. public void TakeDistinct()
  93. {
  94. ForEachProvider(new[] { ProviderName.Sybase, ProviderName.SQLite },
  95. db => AreEqual(
  96. (from ch in Child orderby ch.ParentID select ch.ParentID).Take(4).Distinct(),
  97. (from ch in db.Child orderby ch.ParentID select ch.ParentID).Take(4).Distinct()));
  98. }
  99. }
  100. }