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