PageRenderTime 70ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NHibernate.Test/NHSpecificTest/NH2583/AbstractMassTestingFixture.cs

https://github.com/RogerKratz/nhibernate-core
C# | 276 lines | 251 code | 20 blank | 5 comment | 27 complexity | b1ce44975604f81f3460861457c37aef MD5 | raw file
  1. using NHibernate.Cfg;
  2. using NUnit.Framework;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using Environment = NHibernate.Cfg.Environment;
  8. namespace NHibernate.Test.NHSpecificTest.NH2583
  9. {
  10. public abstract class AbstractMassTestingFixture : BugTestCase
  11. {
  12. public const int BatchSize = 200;
  13. protected override void Configure(Configuration configuration)
  14. {
  15. base.Configure(configuration);
  16. configuration.DataBaseIntegration(x => x.BatchSize = BatchSize+5);
  17. List<string> cacheSettings = new List<string>(configuration.Properties.Keys.Where(x => x.Contains("cache")));
  18. foreach (var cacheSetting in cacheSettings)
  19. {
  20. configuration.Properties.Remove(cacheSetting);
  21. }
  22. configuration.SetProperty(Environment.UseSecondLevelCache, "false");
  23. }
  24. private class ValueTuple<T1, T2, T3, T4, T5, T6, T7>
  25. {
  26. public T1 Item1;
  27. public T2 Item2;
  28. public T3 Item3;
  29. public T4 Item4;
  30. public T5 Item5;
  31. public T6 Item6;
  32. public T7 Item7;
  33. }
  34. private static IEnumerable<ValueTuple<T1, T2, T3, T4, T5, T6, T7>> GetAllTestCases<T1, T2, T3, T4, T5, T6, T7>()
  35. {
  36. foreach (T1 v1 in Enum.GetValues(typeof(T1)))
  37. {
  38. foreach (T2 v2 in Enum.GetValues(typeof(T2)))
  39. {
  40. foreach (T3 v3 in Enum.GetValues(typeof(T3)))
  41. {
  42. foreach (T4 v4 in Enum.GetValues(typeof(T4)))
  43. {
  44. foreach (T5 v5 in Enum.GetValues(typeof(T5)))
  45. {
  46. foreach (T6 v6 in Enum.GetValues(typeof(T6)))
  47. {
  48. foreach (T7 v7 in Enum.GetValues(typeof(T7)))
  49. {
  50. yield return
  51. new ValueTuple<T1, T2, T3, T4, T5, T6, T7> { Item1 = v1, Item2 = v2, Item3 = v3, Item4 = v4, Item5 = v5, Item6 = v6, Item7 = v7 };
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. public class SetterTuple<T1, T2, T3, T4, T5, T6, T7>
  61. {
  62. private readonly Action<MyBO, ISession, T1> _set1;
  63. private readonly Action<MyBO, ISession, T2> _set2;
  64. private readonly Action<MyBO, ISession, T3> _set3;
  65. private readonly Action<MyBO, ISession, T4> _set4;
  66. private readonly Action<MyBO, ISession, T5> _set5;
  67. private readonly Action<MyBO, ISession, T6> _set6;
  68. private readonly Action<MyBO, ISession, T7> _set7;
  69. public SetterTuple(Action<MyBO, ISession, T1> set1,
  70. Action<MyBO, ISession, T2> set2,
  71. Action<MyBO, ISession, T3> set3,
  72. Action<MyBO, ISession, T4> set4,
  73. Action<MyBO, ISession, T5> set5,
  74. Action<MyBO, ISession, T6> set6,
  75. Action<MyBO, ISession, T7> set7)
  76. {
  77. _set1 = set1;
  78. _set2 = set2;
  79. _set3 = set3;
  80. _set4 = set4;
  81. _set5 = set5;
  82. _set6 = set6;
  83. _set7 = set7;
  84. }
  85. public void Set(MyBO bo, ISession s, T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
  86. {
  87. if (_set1 != null) { _set1(bo, s, item1); }
  88. if (_set2 != null) { _set2(bo, s, item2); }
  89. if (_set3 != null) { _set3(bo, s, item3); }
  90. if (_set4 != null) { _set4(bo, s, item4); }
  91. if (_set5 != null) { _set5(bo, s, item5); }
  92. if (_set6 != null) { _set6(bo, s, item6); }
  93. if (_set7 != null) { _set7(bo, s, item7); }
  94. }
  95. }
  96. protected int RunTest<T1, T2, T3, T4, T5, T6, T7>(Expression<Func<MyBO, bool>> condition, SetterTuple<T1, T2, T3, T4, T5, T6, T7> setters)
  97. {
  98. if (condition == null)
  99. {
  100. throw new ArgumentNullException("condition");
  101. }
  102. if (setters == null)
  103. {
  104. throw new ArgumentNullException("setters");
  105. }
  106. IEnumerable<int> expectedIds;
  107. // Setup
  108. using (var session = OpenSession())
  109. {
  110. expectedIds = CreateObjects(session, setters, condition.Compile());
  111. }
  112. try
  113. {
  114. // Test
  115. using (var session = OpenSession())
  116. {
  117. session.CacheMode = CacheMode.Ignore;
  118. session.DefaultReadOnly = true;
  119. using (session.BeginTransaction())
  120. {
  121. return TestAndAssert(condition, session, expectedIds);
  122. }
  123. }
  124. }
  125. finally
  126. {
  127. // Teardown
  128. using (var session = OpenSession())
  129. {
  130. using (var tx = session.BeginTransaction())
  131. {
  132. DeleteAll<MyBO>(session);
  133. DeleteAll<MyRef1>(session);
  134. DeleteAll<MyRef2>(session);
  135. DeleteAll<MyRef3>(session);
  136. tx.Commit();
  137. }
  138. }
  139. }
  140. }
  141. protected abstract int TestAndAssert(Expression<Func<MyBO, bool>> condition, ISession session, IEnumerable<int> expectedIds);
  142. protected static SetterTuple<T1, T2, T3, T4, T5, T6, T7> Setters<T1, T2, T3, T4, T5, T6, T7>(Action<MyBO, ISession, T1> set1,
  143. Action<MyBO, ISession, T2> set2,
  144. Action<MyBO, ISession, T3> set3,
  145. Action<MyBO, ISession, T4> set4,
  146. Action<MyBO, ISession, T5> set5,
  147. Action<MyBO, ISession, T6> set6,
  148. Action<MyBO, ISession, T7> set7)
  149. {
  150. return new SetterTuple<T1, T2, T3, T4, T5, T6, T7>(set1, set2, set3, set4, set5, set6, set7);
  151. }
  152. protected static SetterTuple<T1, T2, T3, T4, T5, T6, Ignore> Setters<T1, T2, T3, T4, T5, T6>(Action<MyBO, ISession, T1> set1,
  153. Action<MyBO, ISession, T2> set2,
  154. Action<MyBO, ISession, T3> set3,
  155. Action<MyBO, ISession, T4> set4,
  156. Action<MyBO, ISession, T5> set5,
  157. Action<MyBO, ISession, T6> set6)
  158. {
  159. return new SetterTuple<T1, T2, T3, T4, T5, T6, Ignore>(set1, set2, set3, set4, set5, set6, null);
  160. }
  161. protected static SetterTuple<T1, T2, T3, T4, T5, Ignore, Ignore> Setters<T1, T2, T3, T4, T5>(Action<MyBO, ISession, T1> set1,
  162. Action<MyBO, ISession, T2> set2,
  163. Action<MyBO, ISession, T3> set3,
  164. Action<MyBO, ISession, T4> set4,
  165. Action<MyBO, ISession, T5> set5)
  166. {
  167. return new SetterTuple<T1, T2, T3, T4, T5, Ignore, Ignore>(set1, set2, set3, set4, set5, null, null);
  168. }
  169. protected static SetterTuple<T1, T2, T3, T4, Ignore, Ignore, Ignore> Setters<T1, T2, T3, T4>(Action<MyBO, ISession, T1> set1,
  170. Action<MyBO, ISession, T2> set2,
  171. Action<MyBO, ISession, T3> set3,
  172. Action<MyBO, ISession, T4> set4)
  173. {
  174. return new SetterTuple<T1, T2, T3, T4, Ignore, Ignore, Ignore>(set1, set2, set3, set4, null, null, null);
  175. }
  176. protected static SetterTuple<T1, T2, T3, Ignore, Ignore, Ignore, Ignore> Setters<T1, T2, T3>(Action<MyBO, ISession, T1> set1,
  177. Action<MyBO, ISession, T2> set2,
  178. Action<MyBO, ISession, T3> set3)
  179. {
  180. return new SetterTuple<T1, T2, T3, Ignore, Ignore, Ignore, Ignore>(set1, set2, set3,null, null, null, null);
  181. }
  182. protected static SetterTuple<T1, T2, Ignore, Ignore, Ignore, Ignore, Ignore> Setters<T1, T2>(Action<MyBO, ISession, T1> set1,
  183. Action<MyBO, ISession, T2> set2)
  184. {
  185. return new SetterTuple<T1, T2, Ignore, Ignore, Ignore, Ignore, Ignore>(set1, set2, null, null, null, null, null);
  186. }
  187. protected static SetterTuple<T1, Ignore, Ignore, Ignore, Ignore, Ignore, Ignore> Setters<T1>(Action<MyBO, ISession, T1> set1)
  188. {
  189. return new SetterTuple<T1, Ignore, Ignore, Ignore, Ignore, Ignore, Ignore>(set1, null, null, null, null, null, null);
  190. }
  191. private static void DeleteAll<T>(ISession session)
  192. {
  193. session.CreateQuery("delete from " + typeof(T).Name).ExecuteUpdate();
  194. }
  195. private static IEnumerable<int> CreateObjects<T1, T2, T3, T4, T5, T6, T7>(ISession session, SetterTuple<T1, T2, T3, T4, T5, T6, T7> setters, Func<MyBO, bool> condition)
  196. {
  197. var expectedIds = new List<int>();
  198. bool thereAreSomeWithTrue = false;
  199. bool thereAreSomeWithFalse = false;
  200. var allTestCases = GetAllTestCases<T1, T2, T3, T4, T5, T6, T7>().ToList();
  201. var i = 0;
  202. foreach (var q in allTestCases)
  203. {
  204. MyBO bo = new MyBO();
  205. setters.Set(bo, session, q.Item1, q.Item2, q.Item3, q.Item4, q.Item5, q.Item6, q.Item7);
  206. try
  207. {
  208. if (condition(bo))
  209. {
  210. expectedIds.Add(bo.Id);
  211. thereAreSomeWithTrue = true;
  212. }
  213. else
  214. {
  215. thereAreSomeWithFalse = true;
  216. }
  217. if ((i%BatchSize) == 0)
  218. {
  219. if (session.GetCurrentTransaction()?.IsActive == true)
  220. {
  221. session.GetCurrentTransaction().Commit();
  222. session.Clear();
  223. }
  224. session.BeginTransaction();
  225. }
  226. session.Save(bo);
  227. i++;
  228. }
  229. catch (NullReferenceException)
  230. {
  231. // ignore - we only check consistency with Linq2Objects in non-failing cases;
  232. // emulating the outer-join logic for exceptional cases in Lin2Objects is IMO very hard.
  233. }
  234. }
  235. if (session.GetCurrentTransaction()?.IsActive == true)
  236. {
  237. session.GetCurrentTransaction().Commit();
  238. session.Clear();
  239. }
  240. Console.WriteLine("Congratulation!! you have saved "+ i +" entities.");
  241. if (!thereAreSomeWithTrue)
  242. {
  243. throw new ArgumentException("Condition is false for all - not a good test", "condition");
  244. }
  245. if (!thereAreSomeWithFalse)
  246. {
  247. throw new ArgumentException("Condition is true for all - not a good test", "condition");
  248. }
  249. return expectedIds;
  250. }
  251. protected static void AreEqual(IEnumerable<int> expectedIds, IEnumerable<int> actualList)
  252. {
  253. Assert.That(() => actualList.ToList(), Is.EquivalentTo(expectedIds));
  254. }
  255. }
  256. }