Guest User

Untitled

a guest
Jun 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Xunit;
  6. using Raven.Database;
  7. using Raven.Database.Indexing;
  8. using Raven.Client.Indexes;
  9. using Newtonsoft.Json.Linq;
  10. using Raven.Client.Client;
  11. using Raven.Client.Linq;
  12. using System.Threading;
  13.  
  14. namespace Raven.Tests.Bugs
  15. {
  16. public class QueryResultCountsWithProjections : IDisposable
  17. {
  18. EmbeddableDocumentStore store = null;
  19.  
  20. public QueryResultCountsWithProjections()
  21. {
  22. store = new EmbeddableDocumentStore()
  23. {
  24. RunInMemory = true
  25. };
  26. store.Initialize();
  27. PopulateDatastore();
  28. }
  29.  
  30. public void Dispose()
  31. {
  32. store.Dispose();
  33. }
  34.  
  35. [Fact]
  36. public void WhenNoProjectionIsIssuedDuplicateDocumentsAreSkipped()
  37. {
  38. using (var session = store.OpenSession())
  39. {
  40. var results = session.Query<Blog>("SelectManyIndexWithNoTransformer").ToArray();
  41. Assert.Equal(1, results.Length);
  42. }
  43. }
  44.  
  45. // FAILS AND RETURNS TWO BECAUSE OF SELECT MANY
  46. [Fact]
  47. public void WhenProjectionIsIssuedAgainstEntityDuplicateDocumentsAreSkipped()
  48. {
  49. using (var session = store.OpenSession())
  50. {
  51. var results = session.Query<Blog>("SelectManyIndexWithNoTransformer")
  52. .Select(x=> new { x.Title, x.Tags })
  53. .ToArray();
  54. Assert.Equal(1, results.Length);
  55. }
  56. }
  57.  
  58. [Fact]
  59. // FAILS AND RETURNS TWO BECAUSE OF SELECT MANY
  60. public void WhenDynamicQueryInvokedAgainstTagsDuplicateDocumentsAreSkipped()
  61. {
  62. using (var session = store.OpenSession())
  63. {
  64. var results = session.Query<Blog>()
  65. .Customize(x=>x.WaitForNonStaleResults())
  66. .Where(x => x.Tags.Any(y => y.Name.StartsWith("t")))
  67. .Select(x=> new {
  68. x.Title,
  69. x.Tags
  70. })
  71. .ToArray();
  72.  
  73. Assert.Equal(1, results.Length);
  74. }
  75. }
  76.  
  77. [Fact]
  78. public void WhenProjectionIsIssuedAgainstEntityWithTransformResultsDuplicateDocumentsAreSkipped()
  79. {
  80. using (var session = store.OpenSession())
  81. {
  82. var results = session.Query<Blog>("SelectManyIndexWithTransformer")
  83. .As<BlogProjection>()
  84. .ToArray();
  85. Assert.Equal(1, results.Length);
  86. }
  87. }
  88.  
  89. [Fact]
  90. public void WhenProjectionIsIssuedAgainstIndexAndNotEntityDuplicateDocumentsAreNotSkipped()
  91. {
  92. using (var session = store.OpenSession())
  93. {
  94. var results = session.Query<BlogProjection>("SelectManyIndexWithNoTransformer")
  95. .Select(x=> new
  96. {
  97. x.Title,
  98. x.Tags
  99. })
  100. .ToArray();
  101. Assert.Equal(2, results.Length);
  102. }
  103. }
  104.  
  105. private void PopulateDatastore()
  106. {
  107. store.DatabaseCommands.PutIndex("SelectManyIndexWithNoTransformer",
  108. new IndexDefinition<Blog>()
  109. {
  110. Map = docs => from doc in docs
  111. from tag in doc.Tags
  112. select new
  113. {
  114. doc.Title,
  115. tag.Name
  116. }
  117. }.ToIndexDefinition(store.Conventions));
  118.  
  119. store.DatabaseCommands.PutIndex("SelectManyIndexWithTransformer",
  120. new IndexDefinition<Blog, Blog>()
  121. {
  122. Map = docs => from doc in docs
  123. from tag in doc.Tags
  124. select new
  125. {
  126. doc.Title,
  127. tag.Name
  128. },
  129. TransformResults = (database, results) => from result in results
  130. select new
  131. {
  132. result.Title,
  133. result.Tags
  134. }
  135. }.ToIndexDefinition(store.Conventions));
  136.  
  137. using (var session = store.OpenSession())
  138. {
  139. session.Store(new Blog()
  140. {
  141. Title = "1",
  142. Tags = new BlogTag[]{
  143. new BlogTag() { Name = "tagOne" },
  144. new BlogTag() { Name = "tagTwo" }
  145. }
  146. });
  147. session.SaveChanges();
  148. }
  149.  
  150. while (store.DocumentDatabase.Statistics.StaleIndexes.Length > 0)
  151. {
  152. Thread.Sleep(10);
  153. }
  154. }
  155.  
  156. public class BlogProjection
  157. {
  158. public string Title { get; set; }
  159. public BlogTag[] Tags { get; set; }
  160. }
  161.  
  162.  
  163. public class Blog
  164. {
  165. public string Id { get; set; }
  166. public string Title { get; set; }
  167. public BlogTag[] Tags { get; set; }
  168. }
  169.  
  170. public class BlogTag
  171. {
  172. public string Name { get; set; }
  173. }
  174. }
  175. }
Add Comment
Please, Sign In to add comment