PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Aggregates/InLineCustomAggregatesTests.cs

http://tgh.codeplex.com
C# | 258 lines | 200 code | 58 blank | 0 comment | 7 complexity | 30b2e3e57d45d20e38591428479cc227 MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using AutoMapper;
  4. using ExampleWebApplication.Entities;
  5. using ExampleWebApplication.Models;
  6. using FizzWare.NBuilder;
  7. using FluentAssertions;
  8. using NHibernate;
  9. using NHibernate.Criterion;
  10. using NSubstitute;
  11. using NUnit.Framework;
  12. using Telerik.Web.Mvc;
  13. using TelerikMvcGridCustomBindingHelper;
  14. using TelerikMvcGridCustomBindingHelper.Mapper;
  15. using TelerikMvcGridCustomBindingHelper.NHibernate;
  16. using Tests.Stubs;
  17. namespace Tests.Aggregates
  18. {
  19. public class InLineCustomAggregatesTests
  20. {
  21. [TestFixtureSetUp]
  22. public void TestFixtureSetUp()
  23. {
  24. Mapper.Reset();
  25. GridModelMapper.Reset();
  26. Mapper.CreateMap<Product, ProductModel>();
  27. }
  28. [Test]
  29. public void inline_expression_for_queryover_and_its_queryable_groups()
  30. {
  31. var dataSource = new QueryOverStub<Product, Product>(Substitute.For<ICriteria>());
  32. dataSource.RootCriteria.FutureValue<object>().Returns(new FutureValueStub<object>(385M));
  33. var helper = new NHibernateGridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  34. .AddAggregateFunction(model => model.Units_OnOrder,
  35. queryOver => queryOver.Sum(product => product.UnitPrice * product.Units_On_Order),
  36. groups => groups.Sum(product => product.UnitPrice * product.Units_On_Order));
  37. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  38. dataSource.RootCriteria.Received(1).SetProjection(Arg.Any<IProjection>());
  39. const string sqlString = "sum({UnitPrice} * {Units_On_Order})";
  40. dataSource.RootCriteria.Received(1).SetProjection(Arg.Is<IProjection>(projections => projections.ToString() == sqlString));
  41. dataSource.RootCriteria.Received(2).FutureValue<object>();
  42. aggregates.Should().HaveCount(1);
  43. aggregates.Single().Key.Should().Be("Units_OnOrder");
  44. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  45. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(385M);
  46. }
  47. #region QueryOver with auto convertible expressions
  48. [Test]
  49. public void inline_sum_aggregate_expression_with_queryover()
  50. {
  51. var dataSource = new QueryOverStub<Product, Product>(Substitute.For<ICriteria>());
  52. dataSource.RootCriteria.FutureValue<object>().Returns(new FutureValueStub<object>(385M));
  53. var helper = new NHibernateGridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  54. .AddAggregateFunction(model => model.Units_OnOrder, query => query.Sum(product => product.UnitPrice * product.Units_On_Order));
  55. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  56. dataSource.RootCriteria.Received(1).SetProjection(Arg.Any<IProjection>());
  57. const string sqlString = "sum({UnitPrice} * {Units_On_Order})";
  58. dataSource.RootCriteria.Received(1).SetProjection(Arg.Is<IProjection>(projections => projections.ToString() == sqlString));
  59. dataSource.RootCriteria.Received(2).FutureValue<object>();
  60. aggregates.Should().HaveCount(1);
  61. aggregates.Single().Key.Should().Be("Units_OnOrder");
  62. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  63. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(385M);
  64. }
  65. [Test]
  66. public void inline_count_aggregate_expression_with_queryover()
  67. {
  68. var dataSource = new QueryOverStub<Product, Product>(Substitute.For<ICriteria>());
  69. dataSource.RootCriteria.FutureValue<object>().Returns(new FutureValueStub<object>(385M));
  70. var helper = new NHibernateGridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  71. .AddAggregateFunction(model => model.Units_OnOrder, query => query.Count(product => product.Units_On_Order > 7));
  72. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  73. dataSource.RootCriteria.Received(1).SetProjection(Arg.Any<IProjection>());
  74. const string sqlString = "count(*) as expressionProjectionHelperResult";
  75. dataSource.RootCriteria.Received(1).SetProjection(Arg.Is<IProjection>(projections => projections.ToString() == sqlString));
  76. dataSource.RootCriteria.Received(1).Add(Arg.Any<ICriterion>());
  77. const string criterion = "{Units_On_Order} > 7";
  78. dataSource.RootCriteria.Received(1).Add(Arg.Is<ICriterion>(projections => projections.ToString() == criterion));
  79. dataSource.RootCriteria.Received(2).FutureValue<object>();
  80. aggregates.Should().HaveCount(1);
  81. aggregates.Single().Key.Should().Be("Units_OnOrder");
  82. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  83. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(385M);
  84. }
  85. [Test]
  86. public void inline_average_aggregate_expression_with_queryover()
  87. {
  88. var dataSource = new QueryOverStub<Product, Product>(Substitute.For<ICriteria>());
  89. dataSource.RootCriteria.FutureValue<object>().Returns(new FutureValueStub<object>(38.5));
  90. var helper = new NHibernateGridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  91. .AddAggregateFunction(model => model.Units_OnOrder, query => query.Average(product => product.UnitPrice * product.Units_On_Order));
  92. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  93. dataSource.RootCriteria.Received(1).SetProjection(Arg.Any<IProjection>());
  94. const string sqlString = "average({UnitPrice} * {Units_On_Order})";
  95. dataSource.RootCriteria.Received(1).SetProjection(Arg.Is<IProjection>(projections => projections.ToString() == sqlString));
  96. dataSource.RootCriteria.Received(2).FutureValue<object>();
  97. aggregates.Should().HaveCount(1);
  98. aggregates.Single().Key.Should().Be("Units_OnOrder");
  99. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  100. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(38.5);
  101. }
  102. [Test]
  103. public void inline_min_aggregate_expression_with_queryover()
  104. {
  105. var dataSource = new QueryOverStub<Product, Product>(Substitute.For<ICriteria>());
  106. dataSource.RootCriteria.FutureValue<object>().Returns(new FutureValueStub<object>(38.5));
  107. var helper = new NHibernateGridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  108. .AddAggregateFunction(model => model.Units_OnOrder, query => query.Min(product => product.UnitPrice * product.Units_On_Order));
  109. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  110. dataSource.RootCriteria.Received(1).SetProjection(Arg.Any<IProjection>());
  111. const string sqlString = "min({UnitPrice} * {Units_On_Order})";
  112. dataSource.RootCriteria.Received(1).SetProjection(Arg.Is<IProjection>(projections => projections.ToString() == sqlString));
  113. dataSource.RootCriteria.Received(2).FutureValue<object>();
  114. aggregates.Should().HaveCount(1);
  115. aggregates.Single().Key.Should().Be("Units_OnOrder");
  116. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  117. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(38.5);
  118. }
  119. [Test]
  120. public void inline_max_aggregate_expression_with_queryover()
  121. {
  122. var dataSource = new QueryOverStub<Product, Product>(Substitute.For<ICriteria>());
  123. dataSource.RootCriteria.FutureValue<object>().Returns(new FutureValueStub<object>(38.5));
  124. var helper = new NHibernateGridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  125. .AddAggregateFunction(model => model.Units_OnOrder, query => query.Max(product => product.UnitPrice * product.Units_On_Order));
  126. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  127. dataSource.RootCriteria.Received(1).SetProjection(Arg.Any<IProjection>());
  128. const string sqlString = "max({UnitPrice} * {Units_On_Order})";
  129. dataSource.RootCriteria.Received(1).SetProjection(Arg.Is<IProjection>(projections => projections.ToString() == sqlString));
  130. dataSource.RootCriteria.Received(2).FutureValue<object>();
  131. aggregates.Should().HaveCount(1);
  132. aggregates.Single().Key.Should().Be("Units_OnOrder");
  133. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  134. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(38.5);
  135. }
  136. #endregion
  137. #region Queryable
  138. [Test]
  139. public void inline_sum_aggregate_expression_with_queryable()
  140. {
  141. var dataSource = Builder<Product>.CreateListOfSize(10).Build().AsQueryable();
  142. var helper = new GridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  143. .AddAggregateFunction(model => model.Units_OnOrder, query => query.Sum(product => product.UnitPrice * product.Units_On_Order));
  144. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  145. aggregates.Should().HaveCount(1);
  146. aggregates.Single().Key.Should().Be("Units_OnOrder");
  147. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  148. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(385M);
  149. }
  150. [Test]
  151. public void inline_count_aggregate_expression_with_queryable()
  152. {
  153. var dataSource = Builder<Product>.CreateListOfSize(10).Build().AsQueryable();
  154. var helper = new GridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  155. .AddAggregateFunction(model => model.Units_OnOrder, query => query.Count(product => product.Units_On_Order > 7));
  156. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  157. aggregates.Should().HaveCount(1);
  158. aggregates.Single().Key.Should().Be("Units_OnOrder");
  159. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  160. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(3);
  161. }
  162. [Test]
  163. public void inline_average_aggregate_expression_with_queryable()
  164. {
  165. var dataSource = Builder<Product>.CreateListOfSize(10).Build().AsQueryable();
  166. var helper = new GridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  167. .AddAggregateFunction(model => model.Units_OnOrder, query => query.Average(product => product.Units_On_Order * product.Units_On_Order));
  168. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  169. aggregates.Should().HaveCount(1);
  170. aggregates.Single().Key.Should().Be("Units_OnOrder");
  171. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  172. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(38.5);
  173. }
  174. [Test]
  175. public void inline_min_aggregate_expression_with_queryable()
  176. {
  177. var dataSource = Builder<Product>.CreateListOfSize(10).Build().AsQueryable();
  178. var helper = new GridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  179. .AddAggregateFunction(model => model.Units_OnOrder, query => query.Min(product => product.Units_On_Order * product.Units_On_Order));
  180. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  181. aggregates.Should().HaveCount(1);
  182. aggregates.Single().Key.Should().Be("Units_OnOrder");
  183. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  184. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(1);
  185. }
  186. [Test]
  187. public void inline_max_aggregate_expression_with_queryable()
  188. {
  189. var dataSource = Builder<Product>.CreateListOfSize(10).Build().AsQueryable();
  190. var helper = new GridCustomBindingHelper<Product, ProductModel>(new GridCommand(), dataSource)
  191. .AddAggregateFunction(model => model.Units_OnOrder, query => query.Max(product => product.Units_On_Order * product.Units_On_Order));
  192. var aggregates = helper.BuildGridModel().Aggregates.As<Dictionary<string, object>>();
  193. aggregates.Should().HaveCount(1);
  194. aggregates.Single().Key.Should().Be("Units_OnOrder");
  195. aggregates.Single().Value.As<Dictionary<string, object>>().First().Key.Should().Be("Custom");
  196. aggregates.Single().Value.As<Dictionary<string, object>>().First().Value.Should().Be(100);
  197. }
  198. #endregion
  199. }
  200. }