/test/SlowTests/Issues/RavenDB-5256.cs

https://github.com/fitzchak/ravendb · C# · 376 lines · 164 code · 42 blank · 170 comment · 0 complexity · 93e7fea1f28c782b339f842eb861294d MD5 · raw file

  1. // -----------------------------------------------------------------------
  2. // <copyright file="RavenDB-5256.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using FastTests;
  10. using Raven.Client;
  11. using Raven.Client.Documents;
  12. using Raven.Client.Util;
  13. using Xunit;
  14. namespace SlowTests.Issues
  15. {
  16. public class RavenDB_5256 : RavenTestBase
  17. {
  18. [Theory(Skip = "RavenDB-5918, RavenDB-6283")]
  19. [InlineData("ShardA")]
  20. [InlineData("ShardB")]
  21. public void ShouldLoadUserFromSessionTwiceInShardingDocumentStore(string mainShard)
  22. {
  23. //TestWithShardedStore(AssertLoadUsersWithIncludes, mainShard);
  24. }
  25. [Fact(Skip = "RavenDB-6283")]
  26. public void ShouldLoadUserFromSessionTwiceInEmbeddedDocumentStore()
  27. {
  28. using (var store = GetDocumentStore())
  29. {
  30. AssertLoadUsersWithIncludes(store, null);
  31. }
  32. }
  33. [Theory(Skip = "RavenDB-5918, RavenDB-6283")]
  34. [InlineData("ShardA")]
  35. [InlineData("ShardB")]
  36. public void ShouldLoadUserFromAsyncSessionTwiceInShardingDocumentStore(string mainShard)
  37. {
  38. //TestWithShardedStore(AssertLoadUsersWithIncludesAsync, mainShard);
  39. }
  40. [Fact(Skip = "RavenDB-6283")]
  41. public void ShouldLoadUserFromAsyncSessionTwiceInEmbeddedDocumentStore()
  42. {
  43. using (var store = GetDocumentStore())
  44. {
  45. AssertLoadUsersWithIncludesAsync(store, null);
  46. }
  47. }
  48. [Theory(Skip = "RavenDB-5918, RavenDB-6283")]
  49. [InlineData("ShardA")]
  50. [InlineData("ShardB")]
  51. public void ShouldLoadMultipleUsersWithIncludesFromSessionInShardingDocumentStore(string mainShard)
  52. {
  53. //TestWithShardedStore(AssertLoadMultipleUsersWithIncludes, mainShard);
  54. }
  55. [Fact(Skip = "RavenDB-6283")]
  56. public void ShouldLoadMultipleUsersWithIncludesFromSessionInEmbeddedDocumentStore()
  57. {
  58. using (var store = GetDocumentStore())
  59. {
  60. AssertLoadMultipleUsersWithIncludes(store, null);
  61. }
  62. }
  63. [Theory(Skip = "RavenDB-5918, RavenDB-6283")]
  64. [InlineData("ShardA")]
  65. [InlineData("ShardB")]
  66. public void ShouldLoadMultipleUsersWithIncludesFromAsyncSessionInShardingDocumentStore(string mainShard)
  67. {
  68. //TestWithShardedStore(AssertLoadMultipleUsersWithIncludesAsync, mainShard);
  69. }
  70. [Fact(Skip = "RavenDB-6283")]
  71. public void ShouldLoadMultipleUsersWithIncludesFromAsyncSessionInEmbeddedDocumentStore()
  72. {
  73. using (var store = GetDocumentStore())
  74. {
  75. AssertLoadMultipleUsersWithIncludesAsync(store, null);
  76. }
  77. }
  78. private void AssertLoadMultipleUsersWithIncludes(IDocumentStore documentStore, string mainShard)
  79. {
  80. //var userRoles = new[]
  81. //{
  82. // new UserRole {Name = "User Role A", Shard = mainShard},
  83. // new UserRole {Name = "User Role B", Shard = mainShard}
  84. //};
  85. //var roleIds = userRoles.Select(q => q.Id).ToList();
  86. //var users = new[]
  87. //{
  88. // new User
  89. // {
  90. // Name = "User A",
  91. // UserRoles = roleIds,
  92. // Shard = mainShard
  93. // },
  94. // new User
  95. // {
  96. // Name = "User B",
  97. // UserRoles = new List<Guid> {roleIds[0]},
  98. // Shard = mainShard
  99. // }
  100. //};
  101. //var userIds = users.Select(q => q.Id).ToList();
  102. //using (var session = documentStore.OpenSession())
  103. //{
  104. // foreach (var user in users)
  105. // {
  106. // session.Store(user);
  107. // }
  108. // foreach (var userRole in userRoles)
  109. // {
  110. // session.Store(userRole);
  111. // }
  112. // session.SaveChanges();
  113. //}
  114. //var type = typeof(UserRole);
  115. //var keys =
  116. // roleIds.Select(
  117. // q => documentStore.Conventions.FindFullDocumentKeyFromNonStringIdentifier(q, type, false)).ToList();
  118. //using (var session = documentStore.OpenSession())
  119. //{
  120. // var userRolesFromDb = session.Load<UserRole>(roleIds.Cast<ValueType>());
  121. // foreach (var key in keys)
  122. // {
  123. // Assert.True(session.Advanced.IsLoaded(key));
  124. // }
  125. // Assert.NotEmpty(userRolesFromDb);
  126. // foreach (var userRole in userRolesFromDb)
  127. // {
  128. // Assert.NotNull(userRole);
  129. // }
  130. // var usersFromDb =
  131. // session.Include<User, UserRole>(x => x.UserRoles).Load(userIds.Cast<ValueType>());
  132. // Assert.NotEmpty(usersFromDb);
  133. // foreach (var user in usersFromDb)
  134. // {
  135. // Assert.NotNull(user);
  136. // }
  137. // foreach (var key in keys)
  138. // {
  139. // Assert.True(session.Advanced.IsLoaded(key));
  140. // }
  141. // var userRolesFromSession = session.Load<UserRole>(roleIds.Cast<ValueType>());
  142. // Assert.NotEmpty(userRolesFromSession);
  143. // foreach (var userRole in userRolesFromSession)
  144. // {
  145. // Assert.NotNull(userRole);
  146. // }
  147. //}
  148. }
  149. private void AssertLoadMultipleUsersWithIncludesAsync(IDocumentStore documentStore, string mainShard)
  150. {
  151. //var userRoles = new[]
  152. //{
  153. // new UserRole {Name = "User Role A", Shard = mainShard},
  154. // new UserRole {Name = "User Role B", Shard = mainShard}
  155. //};
  156. //var roleIds = userRoles.Select(q => q.Id).ToList();
  157. //var users = new[]
  158. //{
  159. // new User
  160. // {
  161. // Name = "User A",
  162. // UserRoles = roleIds,
  163. // Shard = mainShard
  164. // },
  165. // new User
  166. // {
  167. // Name = "User B",
  168. // UserRoles = new List<Guid> {roleIds[0]},
  169. // Shard = mainShard
  170. // }
  171. //};
  172. //var userIds = users.Select(q => q.Id).ToList();
  173. //using (var session = documentStore.OpenSession())
  174. //{
  175. // foreach (var user in users)
  176. // {
  177. // session.Store(user);
  178. // }
  179. // foreach (var userRole in userRoles)
  180. // {
  181. // session.Store(userRole);
  182. // }
  183. // session.SaveChanges();
  184. //}
  185. //var type = typeof(UserRole);
  186. //var keys =
  187. // roleIds.Select(
  188. // q => documentStore.Conventions.FindFullDocumentKeyFromNonStringIdentifier(q, type, false)).ToList();
  189. //AsyncHelpers.RunSync(async () =>
  190. //{
  191. // using (var session = documentStore.OpenAsyncSession())
  192. // {
  193. // var userRolesFromDb = await session.LoadAsync<UserRole>(roleIds.Cast<ValueType>());
  194. // foreach (var key in keys)
  195. // {
  196. // Assert.True(session.Advanced.IsLoaded(key));
  197. // }
  198. // Assert.NotEmpty(userRolesFromDb);
  199. // foreach (var userRole in userRolesFromDb)
  200. // {
  201. // Assert.NotNull(userRole);
  202. // }
  203. // var usersFromDb =
  204. // await session.Include<User, UserRole>(x => x.UserRoles).LoadAsync(userIds.Cast<ValueType>());
  205. // Assert.NotEmpty(usersFromDb);
  206. // foreach (var user in usersFromDb)
  207. // {
  208. // Assert.NotNull(user);
  209. // }
  210. // foreach (var key in keys)
  211. // {
  212. // Assert.True(session.Advanced.IsLoaded(key));
  213. // }
  214. // var userRolesFromSession = await session.LoadAsync<UserRole>(roleIds.Cast<ValueType>());
  215. // Assert.NotEmpty(userRolesFromSession);
  216. // foreach (var userRole in userRolesFromSession)
  217. // {
  218. // Assert.NotNull(userRole);
  219. // }
  220. // }
  221. //});
  222. }
  223. private void AssertLoadUsersWithIncludes(IDocumentStore documentStore, string mainShard)
  224. {
  225. var userRoles = new[]
  226. {
  227. new UserRole {Name = "User Role A", Shard = mainShard},
  228. new UserRole {Name = "User Role B", Shard = mainShard}
  229. };
  230. var user = new User
  231. {
  232. Name = "User A",
  233. UserRoles = userRoles.Select(q => q.Id).ToList(),
  234. Shard = mainShard
  235. };
  236. var userId = user.Id;
  237. using (var session = documentStore.OpenSession())
  238. {
  239. session.Store(user);
  240. foreach (var userRole in userRoles)
  241. {
  242. session.Store(userRole);
  243. }
  244. session.SaveChanges();
  245. }
  246. using (var session = documentStore.OpenSession())
  247. {
  248. var userFromDb = session.Include<User, UserRole>(x => x.UserRoles).Load(userId);
  249. var userFromSession = session.Load<User>(userId);
  250. Assert.NotNull(userFromDb);
  251. Assert.NotNull(userFromSession);
  252. Assert.NotNull(session.Load<UserRole>(userFromSession.UserRoles.First()));
  253. Assert.NotNull(session.Load<UserRole>(userFromSession.UserRoles.Last()));
  254. }
  255. }
  256. private void AssertLoadUsersWithIncludesAsync(IDocumentStore documentStore, string mainShard)
  257. {
  258. var userRoles = new[]
  259. {
  260. new UserRole {Name = "User Role A", Shard = mainShard},
  261. new UserRole {Name = "User Role B", Shard = mainShard}
  262. };
  263. var user = new User
  264. {
  265. Name = "User A",
  266. UserRoles = userRoles.Select(q => q.Id).ToList(),
  267. Shard = mainShard
  268. };
  269. var userId = user.Id;
  270. using (var session = documentStore.OpenSession())
  271. {
  272. session.Store(user);
  273. foreach (var userRole in userRoles)
  274. {
  275. session.Store(userRole);
  276. }
  277. session.SaveChanges();
  278. }
  279. AsyncHelpers.RunSync(async () =>
  280. {
  281. using (var session = documentStore.OpenAsyncSession())
  282. {
  283. var userFromDb = await session.Include<User, UserRole>(x => x.UserRoles).LoadAsync(userId);
  284. var userFromSession = await session.LoadAsync<User>(userId);
  285. Assert.NotNull(userFromDb);
  286. Assert.NotNull(userFromSession);
  287. Assert.NotNull(await session.LoadAsync<UserRole>(userFromSession.UserRoles.First()));
  288. Assert.NotNull(await session.LoadAsync<UserRole>(userFromSession.UserRoles.Last()));
  289. }
  290. });
  291. }
  292. //private void TestWithShardedStore(Action<ShardedDocumentStore, string> action, string mainShard)
  293. //{
  294. // using (var store1 = GetDocumentStore())
  295. // using (var store2 = GetDocumentStore())
  296. // {
  297. // store1.Identifier = "ShardA";
  298. // store2.Identifier = "ShardB";
  299. // var shards = new List<IDocumentStore>
  300. // {
  301. // store1,
  302. // store2
  303. // }.ToDictionary(x => x.Identifier, x => x);
  304. // using (var documentStore = new ShardedDocumentStore(new ShardStrategy(shards)
  305. // {
  306. // ModifyDocumentId = (convention, shardId, documentId) => documentId
  307. // }.ShardingOn<User>(x => x.Shard).ShardingOn<UserRole>(x => x.Shard)))
  308. // {
  309. // documentStore.Initialize();
  310. // action(documentStore, mainShard);
  311. // }
  312. // }
  313. //}
  314. private class IdObject
  315. {
  316. public IdObject()
  317. {
  318. Id = Guid.NewGuid().ToString();
  319. }
  320. public string Id { get; set; }
  321. }
  322. private class User : IdObject
  323. {
  324. public string Name { get; set; }
  325. public List<string> UserRoles { get; set; }
  326. public string Shard { get; set; }
  327. }
  328. private class UserRole : IdObject
  329. {
  330. public string Name { get; set; }
  331. public string Shard { get; set; }
  332. }
  333. }
  334. }