INCLUDE_DATA
A few weeks ago, I was faced with the challenge of testing how an application interacted with a database without mangling the data within it. What my company of employment had been doing prior was doing all database testing within a transaction and rolling back at the end. The issue with this approach was that behaviors like cascading wouldn’t occur, thus making the tests less than useful.
Enter SQLite. SQLite is a basic, lightweight sql database wrapped into a single library. A benefit of its use is that SQLite can create an in-memory database that cleans up after all connections are closed. Its excellent for testing. Starting with a clean slate each test run can provide for consistent, reproducible results.
NHibernate comes with support for SQLite from the NHibernate.Driver.SQLite20Driver class. However, you must provide two other files for it to work. First, sqlite3.dll, an unmanaged library of SQLite; include it in your test project and ensure “copy to output directory” is marked. Second, System.Data.SQLite, a managed library that allows ADO.NET to interact with SQLite; include this as a reference in your test project.
The following is a basic example of how to set up a base class for in memory database testing. Any tests that require database interaction should inherit from this class and will have the Session object available. Sessions last as long as each test fixture. Also, typeof(Plan) refers to the type of any of your domain classes.
public class InMemoryDatabaseTest { private static Configuration _configuration; private readonly object _baton = new object(); private readonly ISessionFactory _sessionFactory; protected readonly ISession Session; public InMemoryDatabaseTest() { if (_configuration == null) lock (_baton) if (_configuration == null) { typeof(VersionDate2).GetField("_sqlType",BindingFlags.Static | BindingFlags.NonPublic).SetValue(null,new SqlType(DbType.DateTime)); _configuration = new Configuration() .SetProperty(Environment.ReleaseConnections,"on_close") .SetProperty(Environment.ProxyFactoryFactoryClass, "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle") .SetProperty(Environment.TransactionStrategy, "NHibernate.Transaction.AdoNetTransactionFactory") .SetProperty(Environment.CacheProvider, "NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache") .SetProperty(Environment.ConnectionDriver, typeof (SQLite20Driver).AssemblyQualifiedName) .SetProperty(Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider") .SetProperty(Environment.Isolation, "ReadCommitted") .SetProperty(Environment.ConnectionString, "Data Source=:memory:;Version=3;New=True;") .SetProperty(Environment.ShowSql,"True") .SetProperty(Environment.Dialect, typeof (SQLiteDialect).AssemblyQualifiedName) .AddAssembly(Assembly.GetAssembly(typeof (Plan))); _sessionFactory = _configuration.BuildSessionFactory(); Session = _sessionFactory.OpenSession(); var schemaExport = new SchemaExport(_configuration); schemaExport.Execute(false, true, false, Session.Connection, null); } public void Dispose() { Session.Dispose(); } }
SQLite doesn’t provide native support for schemas. We use SQL Server, and this led to a problem when any of our mappings pointed to tables in anything other than the dbo schema. A simple fix to this is to replace all periods with underscores prior to creating your session factory. This gives the desired behavior without requiring any modification to your mapping files.
if (_configuration == null) { _configuration = new Configuration() //Configuation... foreach (PersistentClass classMapping in _configuration.ClassMappings) { if (classMapping.Table.Name.Contains(".")) classMapping.Table.Name = classMapping.Table.Name.Replace(".", "_"); } }
If you’re a user of Moq-Contrib’s AutoMockContainer (it must be patched to work with the latest version of Moq), you can register the Session object into the container to have it provided instead of a mocked session. It’s as simple as adding the following after creating your session object.
MockContainer.Register(Session);
If you use NHibernate Profiler (if you don’t, you should be), your SQLite database interaction can be monitored using that as well. Add an AssemblyInitialize (or your testing framework’s variant) to your InMemoryDatabaseTest class and all interactions will be profiled.
[AssemblyInitialize] public static void AssemblyInit(TestContext testContext) { NHibernateProfiler.Initialize(); }
As a new but frequent user of the Moq mocking framework for .NET, I was disappointed to find Moq-Contrib to be out of date as I wanted the AutoMockContainer. Performing dependency injection by hand and updating my tests for every code change was a pain. I dug into some source, found a simple fix and it is the patch that follows.
Index: Source/Mvc/HttpContextMock.cs =================================================================== --- Source/Mvc/HttpContextMock.cs (revision 23) +++ Source/Mvc/HttpContextMock.cs (working copy) @@ -58,11 +58,11 @@ this.HttpServerUtility = new HttpServerUtilityMock(); this.HttpSessionState = new HttpSessionStateMock(); - this.ExpectGet(c => c.Application).Returns(this.HttpApplicationState.Object); - this.ExpectGet(c => c.Request).Returns(this.HttpRequest.Object); - this.ExpectGet(c => c.Response).Returns(this.HttpResponse.Object); - this.ExpectGet(c => c.Server).Returns(this.HttpServerUtility.Object); - this.ExpectGet(c => c.Session).Returns(this.HttpSessionState.Object); + this.SetupGet(c => c.Application).Returns(this.HttpApplicationState.Object); + this.SetupGet(c => c.Request).Returns(this.HttpRequest.Object); + this.SetupGet(c => c.Response).Returns(this.HttpResponse.Object); + this.SetupGet(c => c.Server).Returns(this.HttpServerUtility.Object); + this.SetupGet(c => c.Session).Returns(this.HttpSessionState.Object); } /// <summary> Index: Source/Mvc/HttpRequestMock.cs =================================================================== --- Source/Mvc/HttpRequestMock.cs (revision 23) +++ Source/Mvc/HttpRequestMock.cs (working copy) @@ -56,8 +56,8 @@ /// </summary> public HttpRequestMock() { - this.ExpectGet(f => f.Form).Returns(form); - this.ExpectGet(f => f.Headers).Returns(headers); + this.SetupGet(f => f.Form).Returns(form); + this.SetupGet(f => f.Headers).Returns(headers); } } Index: Source/Mvc/HttpResponseMock.cs =================================================================== --- Source/Mvc/HttpResponseMock.cs (revision 23) +++ Source/Mvc/HttpResponseMock.cs (working copy) @@ -57,9 +57,9 @@ this.OutputStream = new Mock<Stream>(); this.Cache = new HttpCachePolicyBaseMock(); - ExpectGet(m => m.Output).Returns(this.Output.Object); - ExpectGet(m => m.OutputStream).Returns(this.OutputStream.Object); - ExpectGet(m => m.Cache).Returns(this.Cache.Object); + SetupGet(m => m.Output).Returns(this.Output.Object); + SetupGet(m => m.OutputStream).Returns(this.OutputStream.Object); + SetupGet(m => m.Cache).Returns(this.Cache.Object); } // TODO: mock other properties.
Ensure that the Moq 4.0 dll is in the Lib/Moq folder before building.
yield is an overlooked, yet powerful feature.
yield places values into an IEnumerable object. To further understand what yield does, here’s an example with trace statements between each yield statement:
//:SimpleYield.cs using System; using System.Collections.Generic; class SimpleYield { static IEnumerable GetValues(){ Console.WriteLine("Returning 1"); yield return 1; Console.WriteLine("Returning 2"); yield return 2; Console.WriteLine("Returning 3"); yield return 3; } static void Main(){ //Iterate over YieldMethod's IEnumerable foreach(int i in GetValues()) Console.WriteLine(i); } }/*Output: Returning 1 1 Returning 2 2 Returning 3 3 *///:~
GetValues( )’s yield statements return hard-coded integer values. Main( ) iterates these values, printing each. Notice how GetValue( )’s trace statements interlace with Main( )’s. A yield statement comes in two different forms, yield return and yield break. yield return places the evaluated expression as the current value of the IEnumerable. yield break marks the end of an iterator:
//:YieldBreak.cs using System; using System.Collections.Generic; class YieldBreak{ static IEnumerable GetValues(){ Console.WriteLine("Returning 1"); yield return 1; Console.WriteLine("Returning 2"); yield return 2; Console.WriteLine("Breaking"); yield break; //Break Here Console.WriteLine("Returning 3"); yield return 3; } static void Main(){ foreach(int i in GetValues()) Console.WriteLine(i); } }/*Output: Returning 1 1 Returning 2 2 Breaking *///:~
yield break prevents remaining code from executing. It reports back to the foreach that there are no values remaining in the IEnumerable. C#’s compiler reports a warning about any unreachable code following yield break. Exercise 1: Use Visual Studio’s debugger to step through (F11) SimpleYield.cs. Follow the code path as the foreach executes. Exercise 2: Create a GetValue( ) method that returns a random number of integer values. Use yield return and yield break.
///: Exercise2.cs using System; using System.Collections.Generic; class Exercise2{ public static IEnumerable GetValues(){ Random rand = new Random(); int current = 0; while (true){ current = rand.Next(20); if (current == 17) yield break; yield return current; } } public static void Main(){ foreach (int i in GetValues()){ Console.WriteLine(i); } } } ///:~
One strong feature of yield is its ability to defer processing. It delays any calculation until absolutely necessary. Deferred processing makes more responsive programs. yield spreads wait time amongst all iterations by creating values only when necessary:
///: DeferredProcessingTime.cs using System; using System.Collections.Generic; using System.Threading; class DeferredProcessingTime{ public static IEnumerable CalculateAtOnce(){ int[] intArray = new int[10]; for (int i = 0; i &amp;lt; 10; i++){ Thread.Sleep(1000); intArray[i] = i; } return intArray; } public static IEnumerable DeferredCalculate(){ for (int i = 0; i &amp;lt; 10; i++){ Thread.Sleep(1000); yield return i; } } public static void Main(){ Console.WriteLine("Calculate at once"); foreach (int i in CalculateAtOnce()) //Ten seconds before printing Console.Write(i); Console.WriteLine("\nUsing Yield"); foreach (int z in DeferredCalculate()) Console.Write(z); //One second between each write } }/*Output: Calculate at once 0123456789 Using Yield 0123456789 *///:~
The above example emulates an algorithm that takes a long time to process by using Thread.Sleep( ) before adding each value. Both foreachs over CalculateAtOnce( ) and DefferedCalculate( ) print the same results to the console. However the execution behavior is noticeably different. CalculateAtOnce( )’s foreach waits for 10 seconds and prints 0 through 9. DeferredCalculate( )’s foreach prints each value per second. Another benefit of yield’s deferred processing is that it does not require a collection stored in memory. yield can iterate over a large set of data without consuming large amounts of memory. Take the following example. You iterate over a set of data with 10 million integer values. Without yield, the entire collection of values must be returned. With yield, each integer value is returned when requested.
///:DeferredProcessingMemory.cs using System; using System.Collections.Generic; using System.Threading; class DeferredProcessingMemory{ public static IEnumerable CalculateAtOnce(){ int[] intArray = new int[10000000]; for (int i = 0; i &amp;lt; 10000000; i++) intArray[i] = i; return intArray; } public static IEnumerable DeferredCalculate(){ for (int i = 0; i &amp;lt; 10000000; i++) yield return i; } public static void Main(){ Console.WriteLine("Using Yield"); IEnumerable deferred = DeferredCalculate(); Console.ReadLine(); //Memory usage: ~2MB Console.WriteLine("Calculating at once"); IEnumerable atOnce = CalculateAtOnce(); Console.ReadLine(); //Memory usage: ~40MB } }/*Output: Using Yield Calculating at once *///:~
Start the Windows Task Manager (Ctrl+Shift+Esc) before running this example. Find your process, and notice the amount of memory usage from using DeferredCalculate( ). Press enter and see the drastic increase that CalculateAtOnce( ) makes; intArray’s 10 million in-memory values are the cause. yield gives potential to handle data sets of any size without concern for memory limitations. Exercise 3: Use yield to iterate over an infinite data set of random integers.
///:Exercise3.cs using System; using System.Collections.Generic; class Exercise3{ public static Random rand = new Random(); public static IEnumerable GetValues(){ while(true) yield return rand.Next(); } public static void Main(){ foreach(int i in GetValues()) Console.WriteLine(i); } } ///:~
Many C# keywords involve the generation of MSIL code, methods, or even classes. yield is no exception. Red Gate’s .NET Reflector shows the generated C# from using the yield statement. The disassembled assembly of SimpleYield.cs contains an extra class implementing IEnumerable.
public class GeneratedClass : IEnumerable, IEnumerable, IEnumerator, IEnumerator, IDisposable{ //...CUT... public bool MoveNext(){ switch (this.state){ case 0: this.state = -1; Console.WriteLine("Returning 1"); this.current = 1; this.state = 1; return true; case 1: this.state = -1; Console.WriteLine("Returning 2"); this.current = 2; this.state = 2; return true; case 2: this.state = -1; Console.WriteLine("Returning 3"); this.current = 3; this.state = 3; return true; case 3: this.state = -1; break; } return false; } //...CUT... }
The compiler generates a switch statement to handle executing the code written in your original yielding method. Only one case statement is used for each time MoveNext( ) is called. This is how the deferred processing actually occurs. GetValues( ) is also changed to use the GeneratedClass( ) in place of the original code.
private static IEnumerable GetValues(){ return new GeneratedClass(-2); }
The -2 in the new GeneratedClass( ) call is used to set the initial state. The state’s value is changed when MoveNext( ) is called. Beginning each case, state is set to -1 to prevent further execution if an exception occurs. Just before a case returns, state is assigned the value of the next case. Exercise 4: Write an implementation of yield’s generated code and execute a foreach over it.
//:Exercise4.cs using System; using System.Collections.Generic; using System.Collections; class Exercise4 : IEnumerable,IEnumerable, IEnumerator,IEnumerator, IDisposable{ string current = ""; int state; public IEnumerator GetEnumerator(){ return this; } IEnumerator IEnumerable.GetEnumerator(){ return GetEnumerator(); } public string Current{ get { return current; } } public void Dispose(){} object IEnumerator.Current{ get { return Current; } } public bool MoveNext(){ switch(state){ case 0: state = -1; current = "One"; state = 1; return true; case 1: state = -1; current = "Two"; state = 2; return true; case 2: state = -3; current = "Three"; state = 3; return true; } return false; } public void Reset(){ throw new NotImplementedException(); } public static void Main(){ foreach(string s in new Exercise4()) Console.WriteLine(s); } }/*Output: One Two Three *///:~
Face it, C# is not as regex-friendly as it could be. To do things like find a single regex match involves several lines or some nasty inline code. Using Extension methods, this can be simplified. In another addition to StatenUtil focused on regex, C# becomes a more coder-friendly language.
using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace StatenUtil.RegexExtensions { public static class StringRegex { public static string RegexReplace(this string source, string regex, string replacement) { Regex r = new Regex(regex); return r.Replace(source, replacement); } public static string Match(this string source, string regex) { Regex r = new Regex(regex); Match m = r.Match(source); return m.Value; } public static string[] Matches(this string source, string regex) { Regex r = new Regex(regex); MatchCollection mc = r.Matches(source); List<string> matches = new List<string>(); foreach(object o in mc) matches.Add(((Match)o).Value); return matches.ToArray(); } public static string RegexRemove(this string source, string regex) { Regex r = new Regex(regex); return r.Replace(source, ""); } } }
This class offers four extension methods to the string class: Replace, Remove, Match first, and Match all. Aside from RegexReplace, the parameters often call for a single regular expression string.
Another addition that C# could use is a string scanner. A string scanner moves through a string one regex at a time matching the beginning of a string. This is useful for parsing languages in the way similar to JSON Parsing in Ruby.
using System; namespace StatenUtil.RegexExtensions { public class StringScanner { public string Remaining { get; set; } public string Match { get; set; } public StringScanner(string input) { Remaining = input; } public string Scan(string regex) { Match = Remaining.Match("^"+regex); if (Match != null) Remaining = Remaining.Substring(Match.Length); return Match; } } }
The StringScanner class constructor takes an input string that it will scan. Having only a Scan method, the class has the single use of moving through the input string as regex matches are made. As Scan finds a match, it returns the matching string. Also, the class has two properties: Remaining and Match. Remaining provides what is left in the string, useful for checking if the end of the input string has been met. Match provides the most recent match of the Scan method.
For anyone who’s new to the blog, StatenUtil is a personal C# utility library. Consisting mainly of extension methods, its main objective is to simplify frequently used operations. This edition includes adding some Ruby-like looping methods into C#. I understand that Func and Action objects aren’t a real replacement for blocks. However, it’s nice to have some features I miss when not using Ruby.
using System; namespace StatenUtil { public static class Int32Extensions { public static void Times(this int source, Action<int> action) { for (int i = 0; i < source; i++) { action(i); } } public static void UpTo(this int source, int max, Action<int> action) { for (int i = source; i <= max; i++) { action(i); } } public static void DownTo(this int source, int min, Action<int> action) { for (int i = source; i >= min; i--) { action(i); } } public static int[] Range(this int source, int max) { int difference = max - source; if (difference < 0) return new int[0]; int[] ret = new int[difference + 1]; for (int i = 0; i <= difference; i++) ret[i] = source+i; return ret; } } }
This class file revolves around adding functions to the int class. The Times method allows for a specific integer capturing lambda expression to be executed source times. For example, 6.Times(x => Console.WriteLine(x)) would print lines from zero to 6.
UpTo and DownTo are similar to the Times method. However, they loop over a range of integers rather than from zero. Compare these to Ruby’s upto and downto methods.
Range is simply a way to create an array covering a range of integers. The source is the minimum value and max is the largest of the array.
using System; using System.Collections.Generic; namespace StatenUtil { public static class IEnumerableExtensions { public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { IEnumerator<T> enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) action(enumerator.Current); } } }
Currently, this class file add inline ForEach capabilities to any generic IEnumerable object, similar to Ruby’s each. Although List has this capability, other IEnumerables like arrays do not.
That’s all for now. However, StatenUtil is a work in progress that’s always being amended. In each significant addition, the details will be shared once again.
Working through endless “Wrestles” in CS290, I’ve come to find some common needs for many of my assignments. Because of this, I’ve started to create my own utility DLL (dynamic link library) of functions. I’ll be honest that not all of the ideas were my own, especially Jamie King’s P() method.
For those who have not heard of or used extension methods, here’s a brief overview. Extension methods are simply static methods that are syntactic sugar. To create an extension method, create a static class and within it create a static method having this before your first argument.
public static class MyStaticClass { public static void MyExtensionMethod(this Object source) { } }
After creating this, you can then use it by calling it on any instance that inherits from Object (anything) as instanceName.MyExtensionMethod(). It’s the same as calling, MyStaticClass.MyExtensionMethod(instanceName), just shorter to type and implicitly inserting the parameter, making it feel like a method of the object, rather than a static extension.
Assertion is an effective way of debugging your code. When “wrestling” with new concepts, its a great way to prove things without spewing out a bunch of console.writeline(). These extension methods give you simple Assert methods to call on any object.
using System; using System.Diagnostics; namespace StatenUtil { public static class Asserter { public static void AssertReferenceEquals(this Object source, Object target) { Debug.Assert(Debug.ReferenceEquals(source, target), "Source and target references not equal"); } public static void AssertEquals(this Object source, Object target) { if (source == null) { Debug.Assert(target == null, "Source is null, target is not"); } else { Debug.Assert(source.Equals(target), "Objects are not equal"); } } public static void AssertNull(this Object source) { Debug.Assert(source == null, "Object is not null"); } } }
Another frequet debugging technique is to print out to the console. Adding the P() extension method can simplify the need to write Console.Writeline(). Also, added is the iteration of enumerable objects to eliminate the need to foreach over its members.
using System; using System.Collections; namespace StatenUtil { public static class PrintExtensions { public static void P(this Object source) { P(source, "{0}"); } public static void P(this Object source, string formatString) { if (!(source is String) && source is IEnumerable) { Console.WriteLine(formatString, source); foreach (object o in (IEnumerable)source) o.P(" " + formatString); return; } Console.WriteLine(formatString, source); } } }
In the future, I’ll continue to add additional features to this utility set and share them as the changes become significant. Check back often, as StatenUtil has plenty of life ahead of it. If you have comments or suggestions, please let me know.