/test/SlowTests/Smuggler/SmugglerApiTests.cs

https://github.com/fitzchak/ravendb · C# · 370 lines · 323 code · 45 blank · 2 comment · 2 complexity · d856d6eb9b78527f71dbe7d6edd45783 MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using FastTests;
  6. using FastTests.Server.Documents.Revisions;
  7. using Raven.Client;
  8. using Raven.Client.Documents;
  9. using Raven.Client.Documents.Indexes;
  10. using Raven.Client.Documents.Operations;
  11. using Raven.Client.Documents.Operations.Expiration;
  12. using Raven.Client.Documents.Smuggler;
  13. using Raven.Tests.Core.Utils.Entities;
  14. using Sparrow;
  15. using Xunit;
  16. namespace SlowTests.Smuggler
  17. {
  18. public class SmugglerApiTests : RavenTestBase
  19. {
  20. private class Users_ByName : AbstractIndexCreationTask<User>
  21. {
  22. public Users_ByName()
  23. {
  24. Map = users => from u in users
  25. select new
  26. {
  27. u.Name
  28. };
  29. Stores.Add(x => x.Name, FieldStorage.Yes);
  30. }
  31. }
  32. [Fact]
  33. public async Task CanExportDirectlyToRemote()
  34. {
  35. using (var store1 = GetDocumentStore(new Options
  36. {
  37. ModifyDatabaseName = s => $"{s}_1"
  38. }))
  39. using (var store2 = GetDocumentStore(new Options
  40. {
  41. ModifyDatabaseName = s => $"{s}_2"
  42. }))
  43. {
  44. using (var session = store1.OpenAsyncSession())
  45. {
  46. await session.StoreAsync(new User { Name = "Name1", LastName = "LastName1" });
  47. await session.StoreAsync(new User { Name = "Name2", LastName = "LastName2" });
  48. await session.SaveChangesAsync();
  49. }
  50. await store1.Smuggler.ExportAsync(new DatabaseSmugglerExportOptions(), store2.Smuggler);
  51. using (var commands = store2.Commands())
  52. {
  53. var docs = await commands.GetAsync(0, 10);
  54. Assert.Equal(3, docs.Length);
  55. }
  56. }
  57. }
  58. [Fact]
  59. public async Task CanExportAndImport()
  60. {
  61. var file = Path.GetTempFileName();
  62. try
  63. {
  64. using (var store1 = GetDocumentStore(new Options
  65. {
  66. ModifyDatabaseName = s => $"{s}_1"
  67. }))
  68. using (var store2 = GetDocumentStore(new Options
  69. {
  70. ModifyDatabaseName = s => $"{s}_2"
  71. }))
  72. {
  73. using (var session = store1.OpenSession())
  74. {
  75. // creating auto-indexes
  76. session.Query<User>()
  77. .Where(x => x.Age > 10)
  78. .ToList();
  79. session.Query<User>()
  80. .GroupBy(x => x.Name)
  81. .Select(x => new { Name = x.Key, Count = x.Count() })
  82. .ToList();
  83. }
  84. new Users_ByName().Execute(store1);
  85. using (var session = store1.OpenAsyncSession())
  86. {
  87. await session.StoreAsync(new User { Name = "Name1", LastName = "LastName1" });
  88. await session.StoreAsync(new User { Name = "Name2", LastName = "LastName2" });
  89. await session.SaveChangesAsync();
  90. }
  91. await store1.Smuggler.ExportAsync(new DatabaseSmugglerExportOptions(), file);
  92. await store2.Smuggler.ImportAsync(new DatabaseSmugglerImportOptions(), file);
  93. var stats = await store2.Maintenance.SendAsync(new GetStatisticsOperation());
  94. Assert.Equal(3, stats.CountOfDocuments);
  95. Assert.Equal(3, stats.CountOfIndexes);
  96. }
  97. }
  98. finally
  99. {
  100. File.Delete(file);
  101. }
  102. }
  103. [Fact]
  104. public async Task ShouldReturnCorrectSmugglerResult()
  105. {
  106. var file = Path.GetTempFileName();
  107. try
  108. {
  109. using (var store1 = GetDocumentStore(new Options
  110. {
  111. ModifyDatabaseName = s => $"{s}_1"
  112. }))
  113. using (var store2 = GetDocumentStore(new Options
  114. {
  115. ModifyDatabaseName = s => $"{s}_2"
  116. }))
  117. {
  118. using (var session = store1.OpenSession())
  119. {
  120. // creating auto-indexes
  121. session.Query<User>()
  122. .Where(x => x.Age > 10)
  123. .ToList();
  124. session.Query<User>()
  125. .GroupBy(x => x.Name)
  126. .Select(x => new { Name = x.Key, Count = x.Count() })
  127. .ToList();
  128. }
  129. new Users_ByName().Execute(store1);
  130. using (var session = store1.OpenAsyncSession())
  131. {
  132. await session.StoreAsync(new User { Name = "Name1", LastName = "LastName1" });
  133. await session.StoreAsync(new User { Name = "Name2", LastName = "LastName2" });
  134. await session.SaveChangesAsync();
  135. }
  136. var exportOperation = await store1.Smuggler.ExportAsync(new DatabaseSmugglerExportOptions(), file);
  137. var exportResult = (SmugglerResult)exportOperation.WaitForCompletion();
  138. var stats = await store1.Maintenance.SendAsync(new GetStatisticsOperation());
  139. var progress = (SmugglerResult.SmugglerProgress)exportResult.Progress;
  140. Assert.Equal(stats.CountOfDocuments, progress.Documents.ReadCount);
  141. Assert.Equal(stats.CountOfIndexes, progress.Indexes.ReadCount);
  142. var importOperation = await store2.Smuggler.ImportAsync(new DatabaseSmugglerImportOptions(), file);
  143. var importResult = (SmugglerResult)importOperation.WaitForCompletion();
  144. stats = await store2.Maintenance.SendAsync(new GetStatisticsOperation());
  145. progress = (SmugglerResult.SmugglerProgress)importResult.Progress;
  146. Assert.Equal(stats.CountOfDocuments, progress.Documents.ReadCount);
  147. Assert.Equal(stats.CountOfIndexes, progress.Indexes.ReadCount);
  148. }
  149. }
  150. finally
  151. {
  152. File.Delete(file);
  153. }
  154. }
  155. [Fact]
  156. public async Task SkipExpiredDocumentWhenExport()
  157. {
  158. var file = Path.GetTempFileName();
  159. try
  160. {
  161. using (var exportStore = GetDocumentStore(new Options
  162. {
  163. ModifyDatabaseName = s => $"{s}_exportStore"
  164. }))
  165. {
  166. var database = await GetDocumentDatabaseInstanceFor(exportStore);
  167. using (var session = exportStore.OpenAsyncSession())
  168. {
  169. await SetupExpiration(exportStore);
  170. var person1 = new Person { Name = "Name1" };
  171. await session.StoreAsync(person1).ConfigureAwait(false);
  172. var metadata = session.Advanced.GetMetadataFor(person1);
  173. metadata[Constants.Documents.Metadata.Expires] = database.Time.GetUtcNow().AddSeconds(10).ToString(DefaultFormat.DateTimeOffsetFormatsToWrite);
  174. await session.SaveChangesAsync().ConfigureAwait(false);
  175. }
  176. database.Time.UtcDateTime = () => DateTime.UtcNow.AddSeconds(11);
  177. await exportStore.Smuggler.ExportAsync(new DatabaseSmugglerExportOptions { IncludeExpired = false }, file).ConfigureAwait(false);
  178. }
  179. using (var importStore = GetDocumentStore(new Options
  180. {
  181. ModifyDatabaseName = s => $"{s}_importStore"
  182. }))
  183. {
  184. await importStore.Smuggler.ImportAsync(new DatabaseSmugglerImportOptions(), file);
  185. using (var session = importStore.OpenAsyncSession())
  186. {
  187. var person = await session.LoadAsync<Person>("people/1").ConfigureAwait(false);
  188. Assert.Null(person);
  189. }
  190. }
  191. }
  192. finally
  193. {
  194. File.Delete(file);
  195. }
  196. }
  197. [Fact]
  198. public async Task CanExportAndImportWithRevisionDocuments()
  199. {
  200. var file = Path.GetTempFileName();
  201. try
  202. {
  203. using (var store1 = GetDocumentStore(new Options
  204. {
  205. ModifyDatabaseName = s => $"{s}_store1"
  206. }))
  207. {
  208. using (var session = store1.OpenAsyncSession())
  209. {
  210. await RevisionsHelper.SetupRevisions(Server.ServerStore, store1.Database);
  211. await session.StoreAsync(new Person { Name = "Name1" });
  212. await session.StoreAsync(new Person { Name = "Name2" });
  213. await session.StoreAsync(new Company { Name = "Hibernating Rhinos " });
  214. await session.SaveChangesAsync();
  215. }
  216. for (int i = 0; i < 2; i++)
  217. {
  218. using (var session = store1.OpenAsyncSession())
  219. {
  220. var company = await session.LoadAsync<Company>("companies/1-A");
  221. var person = await session.LoadAsync<Person>("people/1-A");
  222. company.Name += " update " + i;
  223. person.Name += " update " + i;
  224. await session.StoreAsync(company);
  225. await session.StoreAsync(person);
  226. await session.SaveChangesAsync();
  227. }
  228. }
  229. using (var session = store1.OpenAsyncSession())
  230. {
  231. var person = await session.LoadAsync<Person>("people/2-A");
  232. Assert.NotNull(person);
  233. session.Delete(person);
  234. await session.SaveChangesAsync();
  235. }
  236. await store1.Smuggler.ExportAsync(new DatabaseSmugglerExportOptions(), file);
  237. var stats = await store1.Maintenance.SendAsync(new GetStatisticsOperation());
  238. Assert.Equal(4, stats.CountOfDocuments);
  239. Assert.Equal(8, stats.CountOfRevisionDocuments);
  240. }
  241. using (var store2 = GetDocumentStore(new Options
  242. {
  243. ModifyDatabaseName = s => $"{s}_store2"
  244. }))
  245. {
  246. await store2.Smuggler.ImportAsync(new DatabaseSmugglerImportOptions(), file);
  247. var stats = await store2.Maintenance.SendAsync(new GetStatisticsOperation());
  248. Assert.Equal(4, stats.CountOfDocuments);
  249. Assert.Equal(10, stats.CountOfRevisionDocuments);
  250. }
  251. }
  252. finally
  253. {
  254. File.Delete(file);
  255. }
  256. }
  257. [Fact]
  258. public async Task WillNotCreateMoreRevisionsAfterImport()
  259. {
  260. var file = Path.GetTempFileName();
  261. try
  262. {
  263. using (var store1 = GetDocumentStore(new Options
  264. {
  265. ModifyDatabaseName = s => $"{s}_store1"
  266. }))
  267. {
  268. using (var session = store1.OpenAsyncSession())
  269. {
  270. await RevisionsHelper.SetupRevisions(Server.ServerStore, store1.Database);
  271. await session.StoreAsync(new Person { Name = "Name1" });
  272. await session.StoreAsync(new Person { Name = "Name2" });
  273. await session.StoreAsync(new Company { Name = "Hibernating Rhinos " });
  274. await session.SaveChangesAsync();
  275. }
  276. for (int i = 0; i < 2; i++)
  277. {
  278. using (var session = store1.OpenAsyncSession())
  279. {
  280. var company = await session.LoadAsync<Company>("companies/1-A");
  281. var person = await session.LoadAsync<Person>("people/1-A");
  282. company.Name += " update " + i;
  283. person.Name += " update " + i;
  284. await session.StoreAsync(company);
  285. await session.StoreAsync(person);
  286. await session.SaveChangesAsync();
  287. }
  288. }
  289. using (var session = store1.OpenAsyncSession())
  290. {
  291. var person = await session.LoadAsync<Person>("people/2-A");
  292. Assert.NotNull(person);
  293. session.Delete(person);
  294. await session.SaveChangesAsync();
  295. }
  296. await store1.Smuggler.ExportAsync(new DatabaseSmugglerExportOptions(), file);
  297. var stats = await store1.Maintenance.SendAsync(new GetStatisticsOperation());
  298. Assert.Equal(4, stats.CountOfDocuments);
  299. Assert.Equal(8, stats.CountOfRevisionDocuments);
  300. await store1.Smuggler.ImportAsync(new DatabaseSmugglerImportOptions(), file);
  301. stats = await store1.Maintenance.SendAsync(new GetStatisticsOperation());
  302. Assert.Equal(4, stats.CountOfDocuments);
  303. Assert.Equal(8, stats.CountOfRevisionDocuments);
  304. }
  305. }
  306. finally
  307. {
  308. File.Delete(file);
  309. }
  310. }
  311. private static async Task SetupExpiration(DocumentStore store)
  312. {
  313. using (var session = store.OpenAsyncSession())
  314. {
  315. var config = new ExpirationConfiguration
  316. {
  317. Disabled = false,
  318. DeleteFrequencyInSec = 100,
  319. };
  320. await store.Maintenance.SendAsync(new ConfigureExpirationOperation(config));
  321. await session.SaveChangesAsync();
  322. }
  323. }
  324. }
  325. }