/test/SlowTests/Issues/RDoc_56.cs

https://github.com/fitzchak/ravendb
C# | 236 lines | 199 code | 32 blank | 5 comment | 0 complexity | 1fccb94f2789c249629bbafc937565e1 MD5 | raw file
  1. // -----------------------------------------------------------------------
  2. // <copyright file="RDoc_56.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using FastTests;
  9. using Raven.Client.Documents;
  10. using Raven.Client.Documents.Indexes;
  11. using Raven.Client.Documents.Queries;
  12. using Xunit;
  13. namespace SlowTests.Issues
  14. {
  15. public class RDoc_56 : RavenTestBase
  16. {
  17. private class Post
  18. {
  19. public Post()
  20. {
  21. Comments = new List<Comment>();
  22. CommentsSet = new HashSet<Comment>();
  23. }
  24. public string Id { get; set; }
  25. public string Name { get; set; }
  26. public IList<Comment> Comments { get; set; }
  27. public ISet<Comment> CommentsSet { get; set; }
  28. }
  29. private class Comment
  30. {
  31. public Comment()
  32. {
  33. Comments = new List<Comment>();
  34. CommentsSet = new HashSet<Comment>();
  35. }
  36. public string Id { get; set; }
  37. public string Author { get; set; }
  38. public string Text { get; set; }
  39. public IList<Comment> Comments { get; set; }
  40. public ISet<Comment> CommentsSet { get; set; }
  41. }
  42. private class RecurseIndexWithIList : AbstractIndexCreationTask<Post>
  43. {
  44. public RecurseIndexWithIList()
  45. {
  46. Map = posts => from post in posts
  47. from comment in Recurse(post, x => x.Comments)
  48. select new
  49. {
  50. comment.Text
  51. };
  52. }
  53. }
  54. private class RecurseIndexWithICollection : AbstractIndexCreationTask<Post>
  55. {
  56. public RecurseIndexWithICollection()
  57. {
  58. Map = posts => from post in posts
  59. from comment in Recurse(post, x => (ICollection<Comment>)x.Comments)
  60. select new
  61. {
  62. comment.Text
  63. };
  64. }
  65. }
  66. private class RecurseIndexWithIEnumerable : AbstractIndexCreationTask<Post>
  67. {
  68. public RecurseIndexWithIEnumerable()
  69. {
  70. Map = posts => from post in posts
  71. from comment in Recurse(post, x => x.Comments.AsEnumerable())
  72. select new
  73. {
  74. comment.Text
  75. };
  76. }
  77. }
  78. private class RecurseIndexWithList : AbstractIndexCreationTask<Post>
  79. {
  80. public RecurseIndexWithList()
  81. {
  82. Map = posts => from post in posts
  83. from comment in Recurse(post, x => x.Comments.ToList())
  84. select new
  85. {
  86. comment.Text
  87. };
  88. }
  89. }
  90. private class RecurseIndexWithArray : AbstractIndexCreationTask<Post>
  91. {
  92. public RecurseIndexWithArray()
  93. {
  94. Map = posts => from post in posts
  95. from comment in Recurse(post, x => x.Comments.ToArray())
  96. select new
  97. {
  98. comment.Text
  99. };
  100. }
  101. }
  102. private class RecurseIndexWithISet : AbstractIndexCreationTask<Post>
  103. {
  104. public RecurseIndexWithISet()
  105. {
  106. Map = posts => from post in posts
  107. from comment in Recurse(post, x => x.CommentsSet)
  108. select new
  109. {
  110. comment.Text
  111. };
  112. }
  113. }
  114. private class RecurseIndexWithHashSet : AbstractIndexCreationTask<Post>
  115. {
  116. public RecurseIndexWithHashSet()
  117. {
  118. Map = posts => from post in posts
  119. from comment in Recurse(post, x => (HashSet<Comment>)x.CommentsSet)
  120. select new
  121. {
  122. comment.Text
  123. };
  124. }
  125. }
  126. private class RecurseIndexWithSortedSet : AbstractIndexCreationTask<Post>
  127. {
  128. public RecurseIndexWithSortedSet()
  129. {
  130. Map = posts => from post in posts
  131. from comment in Recurse(post, x => (SortedSet<Comment>)x.CommentsSet)
  132. select new
  133. {
  134. comment.Text
  135. };
  136. }
  137. }
  138. [Fact]
  139. public void IndexesShouldGetCreated()
  140. {
  141. using (var store = GetDocumentStore())
  142. {
  143. var array = new RecurseIndexWithArray();
  144. var hashSet = new RecurseIndexWithHashSet();
  145. var collection = new RecurseIndexWithICollection();
  146. var enumerable = new RecurseIndexWithIEnumerable();
  147. var iList = new RecurseIndexWithIList();
  148. var iSet = new RecurseIndexWithISet();
  149. var list = new RecurseIndexWithList();
  150. var sortedSet = new RecurseIndexWithSortedSet();
  151. array.Execute(store);
  152. hashSet.Execute(store);
  153. collection.Execute(store);
  154. enumerable.Execute(store);
  155. iList.Execute(store);
  156. iSet.Execute(store);
  157. list.Execute(store);
  158. sortedSet.Execute(store);
  159. var post = new Post { Id = "posts/1", Name = "Post 1" };
  160. var comments1 = new List<Comment>();
  161. var commentsSet1 = new HashSet<Comment>();
  162. var comments2 = new List<Comment>();
  163. var commentsSet2 = new HashSet<Comment>();
  164. comments2.Add(new Comment { Text = "Text 1" });
  165. comments2.Add(new Comment { Text = "Text 2" });
  166. commentsSet2.Add(new Comment { Text = "Text 1" });
  167. commentsSet2.Add(new Comment { Text = "Text 2" });
  168. comments1.Add(new Comment { Text = "Text 3" });
  169. comments1.Add(new Comment { Text = "Text 4", Comments = comments2, CommentsSet = commentsSet2 });
  170. commentsSet1.Add(new Comment { Text = "Text 3" });
  171. commentsSet1.Add(new Comment { Text = "Text 4", Comments = comments2, CommentsSet = commentsSet2 });
  172. post.Comments = comments1;
  173. post.CommentsSet = commentsSet1;
  174. using (var session = store.OpenSession())
  175. {
  176. session.Store(post);
  177. session.SaveChanges();
  178. }
  179. AssertIndexEntries(store, array.IndexName, 4);
  180. AssertIndexEntries(store, hashSet.IndexName, 4);
  181. AssertIndexEntries(store, collection.IndexName, 4);
  182. AssertIndexEntries(store, enumerable.IndexName, 4);
  183. AssertIndexEntries(store, iList.IndexName, 4);
  184. AssertIndexEntries(store, iSet.IndexName, 4);
  185. AssertIndexEntries(store, list.IndexName, 4);
  186. AssertIndexEntries(store, sortedSet.IndexName, 4);
  187. }
  188. }
  189. private static void AssertIndexEntries(DocumentStore store, string indexName, int expectedNumberOfResults)
  190. {
  191. using (var session = store.OpenSession())
  192. {
  193. session.Query<Post>(indexName)
  194. .Customize(x => x.WaitForNonStaleResults())
  195. .ToList();
  196. using (var commands = store.Commands())
  197. {
  198. var arrayResult = commands.Query(new IndexQuery { Query = $"FROM INDEX '{indexName}'" }, metadataOnly: false, indexEntriesOnly: true);
  199. Assert.Equal(expectedNumberOfResults, arrayResult.Results.Length);
  200. }
  201. }
  202. }
  203. }
  204. }