PageRenderTime 85ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Raven.Tests/Issues/RavenDB_644.cs

https://github.com/jalchr/ravendb
C# | 227 lines | 192 code | 27 blank | 8 comment | 4 complexity | 6e80bf69a953f0fca3e301a0c03caaa3 MD5 | raw file
  1. // -----------------------------------------------------------------------
  2. // <copyright file="RavenDB_644.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using Raven.Abstractions.Exceptions;
  7. namespace Raven.Tests.Issues
  8. {
  9. using System;
  10. using System.ComponentModel.Composition.Hosting;
  11. using System.Linq;
  12. using Raven.Abstractions.Indexing;
  13. using Raven.Client.Indexes;
  14. using Xunit;
  15. /// <remarks>
  16. /// Similar to RavenDB_783
  17. /// </remarks>
  18. public class RavenDB_644 : RavenTest
  19. {
  20. public class Item
  21. {
  22. public int Year { get; set; }
  23. public int Number { get; set; }
  24. }
  25. public class Record
  26. {
  27. public int Year { get; set; }
  28. public int Number { get; set; }
  29. public int Count { get; set; }
  30. }
  31. public class Record2
  32. {
  33. public int Year { get; set; }
  34. public int Number { get; set; }
  35. public Subrecord Count { get; set; }
  36. }
  37. public class Subrecord
  38. {
  39. public int Number { get; set; }
  40. public int Count { get; set; }
  41. }
  42. public class Index : AbstractIndexCreationTask<Item, Record>
  43. {
  44. public Index()
  45. {
  46. Map = items => from i in items
  47. select new
  48. {
  49. Year = i.Year,
  50. Number = i.Number,
  51. Count = 0
  52. };
  53. Reduce = records => from r in records
  54. group r by new { r.Year, r.Number }
  55. into yearAndNumber
  56. select new
  57. {
  58. Year = yearAndNumber.Key.Year,
  59. Number = yearAndNumber.Key.Number,
  60. Count = yearAndNumber.Count()
  61. };
  62. }
  63. }
  64. public class FancyIndex : AbstractIndexCreationTask<Item, Record>
  65. {
  66. public FancyIndex()
  67. {
  68. Map = items => from i in items
  69. select new
  70. {
  71. Year = i.Year,
  72. Number = i.Number,
  73. Count = 0
  74. };
  75. Reduce = records => from r in records
  76. where r.Number == 10 && r.Year == 2010
  77. group r by new { r.Year, r.Number }
  78. into yearAndNumber
  79. select new
  80. {
  81. Year = yearAndNumber.Key.Year,
  82. Number = yearAndNumber.Key.Number,
  83. Count = yearAndNumber.Where(x => x.Number == 0).Select(x => yearAndNumber.Count())
  84. };
  85. }
  86. }
  87. public class ValidFancyIndex : AbstractIndexCreationTask<Item, Record2>
  88. {
  89. public ValidFancyIndex()
  90. {
  91. Map = items => from i in items
  92. select new
  93. {
  94. Year = i.Year,
  95. Number = i.Number,
  96. Count = new { i.Number, Count = 1 }
  97. };
  98. Reduce = records => from r in records
  99. group r by new { r.Year, r.Number }
  100. into yearAndNumber
  101. select new
  102. {
  103. Year = yearAndNumber.Key.Year,
  104. Number = yearAndNumber.Key.Number,
  105. Count = yearAndNumber.GroupBy(x => x.Number)
  106. .Select(g => new
  107. {
  108. Number = g.Key,
  109. Count = g.Count()
  110. })
  111. };
  112. }
  113. }
  114. [Fact]
  115. public void IndexDefinitionBuilderShouldThrow()
  116. {
  117. var exception = Assert.Throws<IndexCompilationException>(
  118. () =>
  119. {
  120. using (var store = NewDocumentStore())
  121. {
  122. new Index().Execute(store);
  123. }
  124. });
  125. Assert.Equal("Reduce cannot contain Count() methods in grouping.", exception.Message);
  126. }
  127. [Fact]
  128. public void ServerShouldThrow()
  129. {
  130. var exception = Assert.Throws<IndexCompilationException>(
  131. () =>
  132. {
  133. using (var store = NewDocumentStore())
  134. {
  135. var container = new CompositionContainer(new TypeCatalog(typeof(Index), typeof(Record)));
  136. IndexCreation.CreateIndexes(container, store);
  137. }
  138. });
  139. Assert.Equal("Reduce cannot contain Count() methods in grouping.", exception.Message);
  140. exception = Assert.Throws<IndexCompilationException>(
  141. () =>
  142. {
  143. using (var store = NewDocumentStore())
  144. {
  145. var container = new CompositionContainer(new TypeCatalog(typeof(FancyIndex), typeof(Record)));
  146. IndexCreation.CreateIndexes(container, store);
  147. }
  148. });
  149. Assert.Equal("Reduce cannot contain Count() methods in grouping.", exception.Message);
  150. }
  151. [Fact]
  152. public void ServerShouldThrow2()
  153. {
  154. var exception = Assert.Throws<IndexCompilationException>(
  155. () =>
  156. {
  157. using (var store = this.NewRemoteDocumentStore())
  158. {
  159. store.DatabaseCommands.PutIndex(
  160. "Index1",
  161. new IndexDefinition
  162. {
  163. Map = "from i in items select new { Year = i.Year, Number = i.Number, Count = 0 }",
  164. Reduce =
  165. "from r in records group r by new { r.Year, r.Number } into yearAndNumber select new { Year = yearAndNumber.Key.Year, Number = yearAndNumber.Key.Number, Count = yearAndNumber.Count() }"
  166. });
  167. }
  168. });
  169. Assert.Contains("Reduce cannot contain Count() methods in grouping.", exception.Message);
  170. exception = Assert.Throws<IndexCompilationException>(
  171. () =>
  172. {
  173. using (var store = this.NewRemoteDocumentStore())
  174. {
  175. store.DatabaseCommands.PutIndex(
  176. "Index1",
  177. new IndexDefinition
  178. {
  179. Map = "from i in items select new { Year = i.Year, Number = i.Number, Count = 0 }",
  180. Reduce =
  181. "from r in records group r by new { r.Year, r.Number } into yearAndNumber select new { Year = yearAndNumber.Key.Year, Number = yearAndNumber.Key.Number, Count = yearAndNumber.Where(x => x.Number == 0).Select(x => yearAndNumber.Count()) }"
  182. });
  183. }
  184. });
  185. Assert.Contains("Reduce cannot contain Count() methods in grouping.", exception.Message);
  186. }
  187. [Fact]
  188. public void ServerShouldNotThrow()
  189. {
  190. Assert.DoesNotThrow(
  191. () =>
  192. {
  193. using (var store = NewDocumentStore())
  194. {
  195. new ValidFancyIndex().Execute(store);
  196. }
  197. });
  198. }
  199. }
  200. }