PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/FluentNHibernate.Specs/Visitors/BiDirectionalManyToManyPairingVisitorSpecs.cs

http://github.com/jagregory/fluent-nhibernate
C# | 291 lines | 228 code | 63 blank | 0 comment | 0 complexity | 433ef5cecac9c5145d7d1cf286236225 MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-SA-3.0, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using FluentNHibernate.MappingModel;
  5. using FluentNHibernate.MappingModel.Collections;
  6. using FluentNHibernate.Utils;
  7. using FluentNHibernate.Visitors;
  8. using Machine.Specifications;
  9. using FluentAssertions;
  10. namespace FluentNHibernate.Specs.Visitors
  11. {
  12. public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_two_many_to_many_s_of_the_same_type_in_two_entities : BiDirectionalManyToManyPairingVisitorSpec
  13. {
  14. Establish context = () =>
  15. {
  16. members_in_queue = collection<Queue>(x => x.GetMembers());
  17. supervisors_in_queue = collection<Queue>(x => x.GetSupervisors());
  18. membership_queues_in_user = collection<User>(x => x.GetMembershipQueues());
  19. supervised_queues_in_user = collection<User>(x => x.GetSupervisedQueues());
  20. };
  21. Because of = () =>
  22. Visit(members_in_queue, supervisors_in_queue, membership_queues_in_user, supervised_queues_in_user);
  23. It should_call_the_user_defined_func = () =>
  24. udf_was_called.Should().BeTrue();
  25. It should_set_other_side_for_the_members_collection = () =>
  26. members_in_queue.OtherSide.Should().Be(membership_queues_in_user);
  27. It should_set_other_side_for_the_supervisors_collection = () =>
  28. supervisors_in_queue.OtherSide.Should().Be(supervised_queues_in_user);
  29. It should_set_other_side_for_the_membership_queues_collection = () =>
  30. membership_queues_in_user.OtherSide.Should().Be(members_in_queue);
  31. It should_set_other_side_for_the_supervisored_queues_collection = () =>
  32. supervised_queues_in_user.OtherSide.Should().Be(supervisors_in_queue);
  33. static CollectionMapping members_in_queue;
  34. static CollectionMapping supervisors_in_queue;
  35. static CollectionMapping membership_queues_in_user;
  36. static CollectionMapping supervised_queues_in_user;
  37. private class User
  38. {
  39. public IEnumerable<Queue> GetMembershipQueues() { yield break; }
  40. public IEnumerable<Queue> GetSupervisedQueues() { yield break; }
  41. }
  42. private class Queue
  43. {
  44. public IEnumerable<User> GetMembers() { yield break; }
  45. public IEnumerable<User> GetSupervisors() { yield break; }
  46. }
  47. }
  48. public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_two_many_to_manys_of_the_same_type_in_two_entities_with_names_that_have_no_likeness : BiDirectionalManyToManyPairingVisitorSpec
  49. {
  50. Establish context = () =>
  51. {
  52. fish_in_queue = collection<Queue>(x => x.Fish);
  53. chips_in_queue = collection<Queue>(x => x.Chips);
  54. bacon_in_queue = collection<User>(x => x.Bacon);
  55. eggs_in_queue = collection<User>(x => x.Eggs);
  56. };
  57. Because of = () =>
  58. ex = Catch.Exception(() => Visit(fish_in_queue, chips_in_queue, bacon_in_queue, eggs_in_queue));
  59. It should_not_fail = () =>
  60. ex.Should().BeNull();
  61. It should_call_the_user_defined_func = () =>
  62. udf_was_called.Should().BeTrue();
  63. It shouldnt_set_the_other_side_of_any_of_the_relationships = () =>
  64. {
  65. fish_in_queue.OtherSide.Should().BeNull();
  66. chips_in_queue.OtherSide.Should().BeNull();
  67. bacon_in_queue.OtherSide.Should().BeNull();
  68. eggs_in_queue.OtherSide.Should().BeNull();
  69. };
  70. static CollectionMapping fish_in_queue;
  71. static CollectionMapping chips_in_queue;
  72. static CollectionMapping bacon_in_queue;
  73. static CollectionMapping eggs_in_queue;
  74. static Exception ex;
  75. private class User
  76. {
  77. public IEnumerable<Queue> Bacon { get; set; }
  78. public IEnumerable<Queue> Eggs { get; set; }
  79. }
  80. private class Queue
  81. {
  82. public IEnumerable<User> Fish { get; set; }
  83. public IEnumerable<User> Chips { get; set; }
  84. }
  85. }
  86. public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_two_many_to_manys_of_the_same_type_in_two_entities_with_names_that_have_the_same_likeness : BiDirectionalManyToManyPairingVisitorSpec
  87. {
  88. Establish context = () =>
  89. {
  90. dsers_in_queue = collection<Queue>(x => x.GetDsers());
  91. fsers_in_queue = collection<Queue>(x => x.GetFsers());
  92. wueues_in_user = collection<User>(x => x.GetWueues());
  93. eueues_in_user = collection<User>(x => x.GetEueues());
  94. };
  95. Because of = () =>
  96. ex = Catch.Exception(() => Visit(dsers_in_queue, fsers_in_queue, wueues_in_user, eueues_in_user));
  97. It should_not_fail = () =>
  98. ex.Should().BeNull();
  99. It should_call_the_user_defined_func = () =>
  100. udf_was_called.Should().BeTrue();
  101. It shouldnt_set_the_other_side_of_any_of_the_relationships = () =>
  102. {
  103. dsers_in_queue.OtherSide.Should().BeNull();
  104. fsers_in_queue.OtherSide.Should().BeNull();
  105. wueues_in_user.OtherSide.Should().BeNull();
  106. eueues_in_user.OtherSide.Should().BeNull();
  107. };
  108. static CollectionMapping dsers_in_queue;
  109. static CollectionMapping fsers_in_queue;
  110. static CollectionMapping wueues_in_user;
  111. static CollectionMapping eueues_in_user;
  112. static Exception ex;
  113. private class User
  114. {
  115. public IEnumerable<Queue> GetWueues() { yield break; }
  116. public IEnumerable<Queue> GetEueues() { yield break; }
  117. }
  118. private class Queue
  119. {
  120. public IEnumerable<User> GetDsers() { yield break; }
  121. public IEnumerable<User> GetFsers() { yield break; }
  122. }
  123. }
  124. public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_a_many_to_many_relationship_when_one_side_has_two_possible_collections : BiDirectionalManyToManyPairingVisitorSpec
  125. {
  126. Establish context = () =>
  127. {
  128. users_in_queue = collection<Queue>(x => x.GetUsers());
  129. users2_in_queue = collection<Queue>(x => x.GetUsers2());
  130. queues_in_user = collection<User>(x => x.GetQueues());
  131. };
  132. Because of = () =>
  133. Visit(queues_in_user, users2_in_queue, users_in_queue);
  134. It should_call_the_user_defined_func = () =>
  135. udf_was_called.Should().BeTrue();
  136. It should_link_queues_in_user_to_the_most_similar_member_in_the_other_entity = () =>
  137. queues_in_user.OtherSide.Should().Be(users_in_queue);
  138. It should_link_users_in_queue_to_the_most_similar_member_in_the_other_entity = () =>
  139. users_in_queue.OtherSide.Should().Be(queues_in_user);
  140. It shouldnt_link_the_orphaned_member_with_anything = () =>
  141. users2_in_queue.OtherSide.Should().BeNull();
  142. static CollectionMapping users_in_queue;
  143. static CollectionMapping users2_in_queue;
  144. static CollectionMapping queues_in_user;
  145. private class User
  146. {
  147. public IEnumerable<Queue> GetQueues() { yield break; }
  148. }
  149. private class Queue
  150. {
  151. public IEnumerable<User> GetUsers() { yield break; }
  152. public IEnumerable<User> GetUsers2() { yield break; }
  153. }
  154. }
  155. public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_a_self_referential_many_to_many_relationship_when_has_two_possible_collections : BiDirectionalManyToManyPairingVisitorSpec
  156. {
  157. Establish context = () =>
  158. {
  159. ancestors = collection<TreeNode>(x => x.Ancestors);
  160. descendants = collection<TreeNode>(x => x.Descendants);
  161. };
  162. Because of = () =>
  163. Visit(descendants, ancestors);
  164. It should_call_the_user_defined_func = () =>
  165. udf_was_called.Should().BeTrue();
  166. It should_link_ancestors_to_descendants = () =>
  167. ancestors.OtherSide.Should().Be(descendants);
  168. It should_link_descendants_to_ancestors = () =>
  169. descendants.OtherSide.Should().Be(ancestors);
  170. static CollectionMapping ancestors;
  171. static CollectionMapping descendants;
  172. private class TreeNode
  173. {
  174. public IEnumerable<TreeNode> Ancestors { get; set; }
  175. public IEnumerable<TreeNode> Descendants { get; set; }
  176. }
  177. }
  178. public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_two_collections_that_are_exposed_through_methods : BiDirectionalManyToManyPairingVisitorSpec
  179. {
  180. Establish context = () =>
  181. {
  182. users_in_queue = collection<Queue>(x => x.GetUsers());
  183. queues_in_user = collection<User>(x => x.GetQueues());
  184. };
  185. Because of = () =>
  186. Visit(users_in_queue, queues_in_user);
  187. It should_call_the_user_defined_func = () =>
  188. udf_was_called.Should().BeTrue();
  189. It should_set_other_side_for_the_users_collection = () =>
  190. users_in_queue.OtherSide.Should().Be(queues_in_user);
  191. It should_set_other_side_for_the_queues_collection = () =>
  192. queues_in_user.OtherSide.Should().Be(users_in_queue);
  193. static CollectionMapping users_in_queue;
  194. static CollectionMapping queues_in_user;
  195. private class User
  196. {
  197. public IEnumerable<Queue> GetQueues() { yield break; }
  198. }
  199. private class Queue
  200. {
  201. public IEnumerable<User> GetUsers() { yield break; }
  202. }
  203. }
  204. #region spec base
  205. public abstract class BiDirectionalManyToManyPairingVisitorSpec
  206. {
  207. Establish context = () =>
  208. visitor = new RelationshipPairingVisitor((c, o, w) => udf_was_called = true);
  209. static RelationshipPairingVisitor visitor;
  210. protected static bool udf_was_called;
  211. protected static CollectionMapping collection<T>(Expression<Func<T, object>> expression)
  212. {
  213. var member = expression.ToMember();
  214. var bag = CollectionMapping.Bag();
  215. bag.ContainingEntityType = typeof(T);
  216. bag.Member = member;
  217. bag.Set(x => x.Relationship, Layer.Defaults, new ManyToManyMapping());
  218. bag.Set(x => x.ChildType, Layer.Defaults, member.PropertyType.GetGenericArguments()[0]);
  219. return bag;
  220. }
  221. protected static void Visit(params CollectionMapping[] mappings)
  222. {
  223. mappings.Each(visitor.Visit);
  224. visitor.Visit(new HibernateMapping[0]); // simulate end of visit
  225. }
  226. }
  227. #endregion
  228. }