/src/GiveCRM.DataAccess.Test/SearchTest.cs

https://github.com/kuangmarkeleven/GiveCRM · C# · 246 lines · 217 code · 29 blank · 0 comment · 0 complexity · 769cb0ee6067ed5b14e17f0fcbd51cff MD5 · raw file

  1. using System;
  2. using System.Linq;
  3. using Simple.Data;
  4. using NUnit.Framework;
  5. using GiveCRM.Models.Search;
  6. using GiveCRM.Web.Models.Search;
  7. namespace GiveCRM.DataAccess.Test
  8. {
  9. [TestFixture]
  10. public class SearchTest
  11. {
  12. [Test]
  13. public void LocationOnCity()
  14. {
  15. var criteria = new[]
  16. {
  17. new LocationSearchCriteria
  18. {
  19. InternalName = LocationSearchCriteria.City,
  20. SearchOperator = SearchOperator.EqualTo,
  21. Type = SearchFieldType.String,
  22. Value = "London"
  23. }
  24. };
  25. var expr = new SearchQueryService().CompileLocationCriteria(criteria, null);
  26. var reference = expr.LeftOperand as ObjectReference;
  27. Assert.IsNotNull(reference);
  28. Assert.AreEqual("city", reference.GetName());
  29. Assert.AreEqual(SimpleExpressionType.Equal, expr.Type);
  30. Assert.AreEqual("London", expr.RightOperand);
  31. }
  32. [Test]
  33. public void LocationOnRegion()
  34. {
  35. var criteria = new[]
  36. {
  37. new LocationSearchCriteria
  38. {
  39. InternalName = LocationSearchCriteria.Region,
  40. SearchOperator = SearchOperator.NotEqualTo,
  41. Type = SearchFieldType.String,
  42. Value = "Yorkshire"
  43. }
  44. };
  45. var expr = new SearchQueryService().CompileLocationCriteria(criteria, null);
  46. var reference = expr.LeftOperand as ObjectReference;
  47. Assert.IsNotNull(reference);
  48. Assert.AreEqual("region", reference.GetName());
  49. Assert.AreEqual(SimpleExpressionType.NotEqual, expr.Type);
  50. Assert.AreEqual("Yorkshire", expr.RightOperand);
  51. }
  52. [Test]
  53. public void LocationOnPartialPostalCode()
  54. {
  55. var criteria = new[]
  56. {
  57. new LocationSearchCriteria
  58. {
  59. InternalName = LocationSearchCriteria.PostalCode,
  60. SearchOperator = SearchOperator.StartsWith,
  61. Type = SearchFieldType.String,
  62. Value = "N1"
  63. }
  64. };
  65. var expr = new SearchQueryService().CompileLocationCriteria(criteria, null);
  66. var reference = expr.LeftOperand as ObjectReference;
  67. Assert.IsNotNull(reference);
  68. Assert.AreEqual("postalcode", reference.GetName());
  69. Assert.AreEqual(SimpleExpressionType.Function, expr.Type);
  70. var function = expr.RightOperand as SimpleFunction;
  71. Assert.IsNotNull(function);
  72. Assert.AreEqual("like", function.Name.ToLowerInvariant());
  73. Assert.AreEqual(1, function.Args.Count);
  74. Assert.AreEqual("N1%", function.Args.First());
  75. }
  76. [Test]
  77. public void DonationOnIndividualDonation()
  78. {
  79. var criteria = new[]
  80. {
  81. new DonationSearchCriteria
  82. {
  83. InternalName = DonationSearchCriteria.IndividualDonation,
  84. SearchOperator = SearchOperator.EqualTo,
  85. Type = SearchFieldType.Double,
  86. Value = 100m.ToString()
  87. }
  88. };
  89. SimpleExpression expr = null;
  90. SimpleExpression having = null;
  91. new SearchQueryService().CompileDonationCriteria(criteria, ref expr, ref having);
  92. Assert.IsNotNull(expr);
  93. Assert.IsNull(having);
  94. var reference = expr.LeftOperand as ObjectReference;
  95. Assert.IsNotNull(reference);
  96. Assert.AreEqual("Amount", reference.GetName());
  97. Assert.AreEqual(SimpleExpressionType.Equal, expr.Type);
  98. Assert.AreEqual(100m, expr.RightOperand);
  99. }
  100. [Test]
  101. public void DonationOnTotalDonation()
  102. {
  103. var criteria = new[]
  104. {
  105. new DonationSearchCriteria
  106. {
  107. InternalName = DonationSearchCriteria.TotalDonations,
  108. SearchOperator = SearchOperator.GreaterThan,
  109. Type = SearchFieldType.Double,
  110. Value = 100m.ToString()
  111. }
  112. };
  113. SimpleExpression expr = null;
  114. SimpleExpression having = null;
  115. new SearchQueryService().CompileDonationCriteria(criteria, ref expr, ref having);
  116. Assert.IsNull(expr);
  117. Assert.IsNotNull(having);
  118. var function = having.LeftOperand as FunctionReference;
  119. Assert.IsNotNull(function);
  120. var reference = function.Argument as ObjectReference;
  121. Assert.IsNotNull(reference);
  122. Assert.AreEqual("Amount", reference.GetName());
  123. Assert.AreEqual(SimpleExpressionType.GreaterThan, having.Type);
  124. Assert.AreEqual(100m, having.RightOperand);
  125. }
  126. [Test]
  127. public void DonationOnLastDonationDate()
  128. {
  129. var criteria = new[]
  130. {
  131. new DonationSearchCriteria
  132. {
  133. InternalName = DonationSearchCriteria.LastDonationDate,
  134. SearchOperator = SearchOperator.LessThan,
  135. Type = SearchFieldType.Date,
  136. Value = "2011-01-01"
  137. }
  138. };
  139. SimpleExpression expr = null;
  140. SimpleExpression having = null;
  141. new SearchQueryService().CompileDonationCriteria(criteria, ref expr, ref having);
  142. Assert.IsNull(expr);
  143. Assert.IsNotNull(having);
  144. var function = having.LeftOperand as FunctionReference;
  145. Assert.IsNotNull(function);
  146. Assert.AreEqual("Max", function.Name);
  147. var reference = function.Argument as ObjectReference;
  148. Assert.IsNotNull(reference);
  149. Assert.AreEqual("Date", reference.GetName());
  150. Assert.AreEqual(SimpleExpressionType.LessThan, having.Type);
  151. Assert.AreEqual(new DateTime(2011,1,1), having.RightOperand);
  152. }
  153. [Test]
  154. public void Campaign()
  155. {
  156. var criteria = new[]
  157. {
  158. new CampaignSearchCriteria
  159. {
  160. InternalName = CampaignSearchCriteria.DonatedToCampaign,
  161. SearchOperator = SearchOperator.StartsWith,
  162. Type = SearchFieldType.String,
  163. Value = "Christmas"
  164. }
  165. };
  166. var expr = new SearchQueryService().CompileCampaignCriteria(criteria, null);
  167. var reference = expr.LeftOperand as ObjectReference;
  168. Assert.IsNotNull(reference);
  169. Assert.AreEqual("Name", reference.GetName());
  170. Assert.AreEqual(SimpleExpressionType.Function, expr.Type);
  171. var function = expr.RightOperand as SimpleFunction;
  172. Assert.IsNotNull(function);
  173. Assert.AreEqual("like", function.Name.ToLowerInvariant());
  174. Assert.AreEqual(1, function.Args.Count);
  175. Assert.AreEqual("Christmas%", function.Args.First());
  176. }
  177. [Test]
  178. public void Facet()
  179. {
  180. var search = new SearchQueryService();
  181. var criteria = new[]
  182. {
  183. new FacetSearchCriteria
  184. {
  185. InternalName = "freeTextFacet_1",
  186. DisplayName = "Test",
  187. FacetId = 1,
  188. SearchOperator = SearchOperator.StartsWith,
  189. Type = SearchFieldType.String,
  190. Value = "GiveCamp"
  191. }
  192. };
  193. var expr = search.CompileFacetCriteria(criteria, null);
  194. Assert.AreEqual(SimpleExpressionType.And, expr.Type);
  195. var first = expr.LeftOperand as SimpleExpression;
  196. Assert.IsNotNull(first);
  197. var reference = first.LeftOperand as ObjectReference;
  198. Assert.IsNotNull(reference);
  199. Assert.AreEqual("FacetId", reference.GetName());
  200. Assert.AreEqual("freeTextFacet_1", reference.GetOwner().GetAlias());
  201. Assert.AreEqual(SimpleExpressionType.Equal, first.Type);
  202. Assert.AreEqual(1, first.RightOperand);
  203. var second = expr.RightOperand as SimpleExpression;
  204. Assert.IsNotNull(second);
  205. reference = second.LeftOperand as ObjectReference;
  206. Assert.IsNotNull(reference);
  207. Assert.AreEqual("FreeTextValue", reference.GetName());
  208. Assert.AreEqual("freeTextFacet_1", reference.GetOwner().GetAlias());
  209. var function = second.RightOperand as SimpleFunction;
  210. Assert.IsNotNull(function);
  211. Assert.AreEqual("like", function.Name.ToLowerInvariant());
  212. Assert.AreEqual(1, function.Args.Count);
  213. Assert.AreEqual("GiveCamp%", function.Args.First());
  214. Assert.AreEqual(SimpleExpressionType.Function, second.Type);
  215. }
  216. }
  217. }