PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Raven.Tests.Issues/RDoc_391.cs

http://github.com/ravendb/ravendb
C# | 331 lines | 273 code | 53 blank | 5 comment | 8 complexity | c3dc7830bf58f3e3c9b2d7b956512c5b MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. // -----------------------------------------------------------------------
  2. // <copyright file="RDoc_391.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.ComponentModel.Composition.Hosting;
  8. using System.Linq;
  9. using FluentAssertions;
  10. using Raven.Abstractions.Data;
  11. using Raven.Abstractions.Util;
  12. using Raven.Client;
  13. using Raven.Client.Indexes;
  14. using Raven.Client.Shard;
  15. using Raven.Database.Config;
  16. using Raven.Json.Linq;
  17. using Raven.Tests.Common;
  18. using Raven.Tests.Common.Dto;
  19. using Xunit;
  20. namespace Raven.Tests.Issues
  21. {
  22. public class RDoc_391 : RavenTest
  23. {
  24. protected override void ModifyConfiguration(InMemoryRavenConfiguration configuration)
  25. {
  26. configuration.Settings["Raven/ActiveBundles"] = "ScriptedIndexResults";
  27. }
  28. private class People_By_Name_Different : AbstractIndexCreationTask<Person>
  29. {
  30. public override string IndexName
  31. {
  32. get
  33. {
  34. return "People/By/Name";
  35. }
  36. }
  37. public People_By_Name_Different()
  38. {
  39. Map = persons => from person in persons select new { person.Name, Count = 1 };
  40. }
  41. }
  42. private class People_By_Name : AbstractIndexCreationTask<Person>
  43. {
  44. public People_By_Name()
  45. {
  46. Map = persons => from person in persons select new { person.Name };
  47. }
  48. }
  49. private class People_By_Name_With_Scripts : AbstractScriptedIndexCreationTask<Person>
  50. {
  51. public override string IndexName
  52. {
  53. get
  54. {
  55. return "People/By/Name";
  56. }
  57. }
  58. public People_By_Name_With_Scripts()
  59. {
  60. Map = persons => from person in persons select new { person.Name };
  61. IndexScript = @"index";
  62. DeleteScript = @"delete";
  63. RetryOnConcurrencyExceptions = false;
  64. }
  65. }
  66. [Fact]
  67. public void GetIndexStatistics_should_not_advance_last_indexed_etag()
  68. {
  69. using (var store = NewRemoteDocumentStore())
  70. {
  71. var index = new People_By_Name_With_Scripts();
  72. index.Execute(store);
  73. WaitForIndexing(store);
  74. var statsBefore = store.DatabaseCommands.GetStatistics();
  75. var indexStats = statsBefore.Indexes.First(x => x.Name == index.IndexName);
  76. var lastIndexedEtag = indexStats.LastIndexedEtag;
  77. var statsAfter = store.DatabaseCommands.GetStatistics();
  78. indexStats = statsAfter.Indexes.First(x => x.Name == index.IndexName);
  79. indexStats.LastIndexedEtag.Should().Be(lastIndexedEtag);
  80. }
  81. }
  82. [Fact]
  83. public void AbstractScriptedIndexCreationTaskWillCreateIndexAndDocument1()
  84. {
  85. using (var store = NewDocumentStore())
  86. {
  87. IndexCreation.CreateIndexes(new CompositionContainer(new TypeCatalog(typeof(People_By_Name_With_Scripts))), store);
  88. var index = new People_By_Name_With_Scripts();
  89. var indexDefinition = store.DatabaseCommands.GetIndex(index.IndexName);
  90. Assert.NotNull(indexDefinition);
  91. using (var session = store.OpenSession())
  92. {
  93. var indexDocument = session.Load<ScriptedIndexResults>(ScriptedIndexResults.IdPrefix + index.IndexName);
  94. Assert.NotNull(indexDocument);
  95. Assert.Equal(index.DeleteScript, indexDocument.DeleteScript);
  96. Assert.Equal(index.IndexScript, indexDocument.IndexScript);
  97. Assert.Equal(index.RetryOnConcurrencyExceptions, indexDocument.RetryOnConcurrencyExceptions);
  98. Assert.Equal(ScriptedIndexResults.IdPrefix + index.IndexName, indexDocument.Id);
  99. }
  100. }
  101. }
  102. [Fact]
  103. public void AbstractScriptedIndexCreationTaskWillCreateIndexAndDocument2()
  104. {
  105. using (var store = NewDocumentStore())
  106. {
  107. var index = new People_By_Name_With_Scripts();
  108. index.Execute(store);
  109. var indexDefinition = store.DatabaseCommands.GetIndex(index.IndexName);
  110. Assert.NotNull(indexDefinition);
  111. using (var session = store.OpenSession())
  112. {
  113. var indexDocument = session.Load<ScriptedIndexResults>(ScriptedIndexResults.IdPrefix + index.IndexName);
  114. Assert.NotNull(indexDocument);
  115. Assert.Equal(index.DeleteScript, indexDocument.DeleteScript);
  116. Assert.Equal(index.IndexScript, indexDocument.IndexScript);
  117. Assert.Equal(index.RetryOnConcurrencyExceptions, indexDocument.RetryOnConcurrencyExceptions);
  118. Assert.Equal(ScriptedIndexResults.IdPrefix + index.IndexName, indexDocument.Id);
  119. }
  120. }
  121. }
  122. [Fact]
  123. public void AbstractScriptedIndexCreationTaskWillCreateIndexAndDocumentOnShardedStore1()
  124. {
  125. using (var store1 = NewDocumentStore())
  126. using (var store2 = NewDocumentStore())
  127. using (var store = new ShardedDocumentStore(new ShardStrategy(new Dictionary<string, IDocumentStore>
  128. {
  129. { "Shard1", store1 },
  130. { "Shard2", store2 },
  131. })))
  132. {
  133. IndexCreation.CreateIndexes(new CompositionContainer(new TypeCatalog(typeof(People_By_Name_With_Scripts))), store);
  134. var index = new People_By_Name_With_Scripts();
  135. var indexDefinition = store1.DatabaseCommands.GetIndex(index.IndexName);
  136. Assert.NotNull(indexDefinition);
  137. indexDefinition = store2.DatabaseCommands.GetIndex(index.IndexName);
  138. Assert.NotNull(indexDefinition);
  139. using (var session = store1.OpenSession())
  140. {
  141. var indexDocument = session.Load<ScriptedIndexResults>(ScriptedIndexResults.IdPrefix + index.IndexName);
  142. Assert.NotNull(indexDocument);
  143. Assert.Equal(index.DeleteScript, indexDocument.DeleteScript);
  144. Assert.Equal(index.IndexScript, indexDocument.IndexScript);
  145. Assert.Equal(index.RetryOnConcurrencyExceptions, indexDocument.RetryOnConcurrencyExceptions);
  146. Assert.Equal(ScriptedIndexResults.IdPrefix + index.IndexName, indexDocument.Id);
  147. }
  148. using (var session = store2.OpenSession())
  149. {
  150. var indexDocument = session.Load<ScriptedIndexResults>(ScriptedIndexResults.IdPrefix + index.IndexName);
  151. Assert.NotNull(indexDocument);
  152. Assert.Equal(index.DeleteScript, indexDocument.DeleteScript);
  153. Assert.Equal(index.IndexScript, indexDocument.IndexScript);
  154. Assert.Equal(index.RetryOnConcurrencyExceptions, indexDocument.RetryOnConcurrencyExceptions);
  155. Assert.Equal(ScriptedIndexResults.IdPrefix + index.IndexName, indexDocument.Id);
  156. }
  157. }
  158. }
  159. [Fact]
  160. public void AbstractScriptedIndexCreationTaskWillCreateIndexAndDocumentOnShardedStore2()
  161. {
  162. using (var store1 = NewDocumentStore())
  163. using (var store2 = NewDocumentStore())
  164. using (var store = new ShardedDocumentStore(new ShardStrategy(new Dictionary<string, IDocumentStore>
  165. {
  166. { "Shard1", store1 },
  167. { "Shard2", store2 },
  168. })))
  169. {
  170. var index = new People_By_Name_With_Scripts();
  171. index.Execute(store);
  172. var indexDefinition = store1.DatabaseCommands.GetIndex(index.IndexName);
  173. Assert.NotNull(indexDefinition);
  174. indexDefinition = store2.DatabaseCommands.GetIndex(index.IndexName);
  175. Assert.NotNull(indexDefinition);
  176. using (var session = store1.OpenSession())
  177. {
  178. var indexDocument = session.Load<ScriptedIndexResults>(ScriptedIndexResults.IdPrefix + index.IndexName);
  179. Assert.NotNull(indexDocument);
  180. Assert.Equal(index.DeleteScript, indexDocument.DeleteScript);
  181. Assert.Equal(index.IndexScript, indexDocument.IndexScript);
  182. Assert.Equal(index.RetryOnConcurrencyExceptions, indexDocument.RetryOnConcurrencyExceptions);
  183. Assert.Equal(ScriptedIndexResults.IdPrefix + index.IndexName, indexDocument.Id);
  184. }
  185. using (var session = store2.OpenSession())
  186. {
  187. var indexDocument = session.Load<ScriptedIndexResults>(ScriptedIndexResults.IdPrefix + index.IndexName);
  188. Assert.NotNull(indexDocument);
  189. Assert.Equal(index.DeleteScript, indexDocument.DeleteScript);
  190. Assert.Equal(index.IndexScript, indexDocument.IndexScript);
  191. Assert.Equal(index.RetryOnConcurrencyExceptions, indexDocument.RetryOnConcurrencyExceptions);
  192. Assert.Equal(ScriptedIndexResults.IdPrefix + index.IndexName, indexDocument.Id);
  193. }
  194. }
  195. }
  196. [Fact]
  197. public void AbstractScriptedIndexCreationTaskWillResetIndexIfDocumentIsMissing()
  198. {
  199. using (var store = NewDocumentStore())
  200. {
  201. using (var session = store.OpenSession())
  202. {
  203. session.Store(new Person
  204. {
  205. Name = "Name1"
  206. });
  207. session.SaveChanges();
  208. }
  209. new People_By_Name().Execute(store);
  210. var index = new People_By_Name_With_Scripts();
  211. WaitForIndexing(store);
  212. var stats = store.DatabaseCommands.GetStatistics();
  213. var indexStats = stats.Indexes.First(x => x.Name == index.IndexName);
  214. Assert.True(EtagUtil.IsGreaterThan(indexStats.LastIndexedEtag, Etag.Empty));
  215. store.DatabaseCommands.Admin.StopIndexing();
  216. index.Execute(store);
  217. stats = store.DatabaseCommands.GetStatistics();
  218. indexStats = stats.Indexes.First(x => x.Name == index.IndexName);
  219. Assert.True(indexStats.LastIndexedEtag.Equals(Etag.Empty));
  220. }
  221. }
  222. [Fact]
  223. public void AbstractScriptedIndexCreationTaskWillResetIndexIfDocumentHasChanged()
  224. {
  225. using (var store = NewDocumentStore())
  226. {
  227. using (var session = store.OpenSession())
  228. {
  229. session.Store(new Person
  230. {
  231. Name = "Name1"
  232. });
  233. session.SaveChanges();
  234. }
  235. new People_By_Name().Execute(store);
  236. var index = new People_By_Name_With_Scripts();
  237. store.DatabaseCommands.Put(ScriptedIndexResults.IdPrefix + index.IndexName, null, new RavenJObject(), new RavenJObject());
  238. WaitForIndexing(store);
  239. var stats = store.DatabaseCommands.GetStatistics();
  240. var indexStats = stats.Indexes.First(x => x.Name == index.IndexName);
  241. Assert.True(EtagUtil.IsGreaterThan(indexStats.LastIndexedEtag, Etag.Empty));
  242. store.DatabaseCommands.Admin.StopIndexing();
  243. index.Execute(store);
  244. stats = store.DatabaseCommands.GetStatistics();
  245. indexStats = stats.Indexes.First(x => x.Name == index.IndexName);
  246. Assert.True(indexStats.LastIndexedEtag.Equals(Etag.Empty));
  247. }
  248. }
  249. [Fact]
  250. public void AbstractScriptedIndexCreationTaskWillNotResetIndexIfNothingHasChanged()
  251. {
  252. using (var store = NewDocumentStore())
  253. {
  254. using (var session = store.OpenSession())
  255. {
  256. session.Store(new Person
  257. {
  258. Name = "Name1"
  259. });
  260. session.SaveChanges();
  261. }
  262. var index = new People_By_Name_With_Scripts();
  263. index.Execute(store);
  264. WaitForIndexing(store);
  265. var stats = store.DatabaseCommands.GetStatistics();
  266. var indexStats = stats.Indexes.First(x => x.Name == index.IndexName);
  267. var lastIndexedEtag = indexStats.LastIndexedEtag;
  268. Assert.True(EtagUtil.IsGreaterThan(lastIndexedEtag, Etag.Empty));
  269. store.DatabaseCommands.Admin.StopIndexing();
  270. index.Execute(store);
  271. stats = store.DatabaseCommands.GetStatistics();
  272. indexStats = stats.Indexes.First(x => x.Name == index.IndexName);
  273. Assert.True(indexStats.LastIndexedEtag.Equals(lastIndexedEtag) ||
  274. EtagUtil.IsGreaterThan(indexStats.LastIndexedEtag, lastIndexedEtag));
  275. }
  276. }
  277. }
  278. }