Posts Tagged ‘Unit testing’

NHibernate Mapping tester

August 29, 2011 2 comments

When using NHibernate, you often need to check if your mappings matches the model and database. Using frameworks like FluentNHibernate usually helps form the compilation perspective, because your mappings won’t compile if there’s any misspelled property or class. But for convensions and database schemas, it’s possible to have a mismatch.
The following code is a test that will check every property on every mapped class and then every collection. Basically, it queries each type and each collection so NHibernate needs to create the sql statement and execute it. If any error is thrown, you will notice.

public void AllMappingsAreOk()
	var session = GetCurrentNHibernateSession();
	var errors = new List<string>();

	foreach (var entry in session.SessionFactory.GetAllClassMetadata())
		var type = entry.Key;
		var data = entry.Value;

		catch (Exception e)
			errors.Add(string.Format("Error getting type {0}: {1}", type, e.Message));

		foreach (var collection in data.PropertyNames.Where(x => data.GetPropertyType(x).IsCollectionType))
			catch (Exception e)
				errors.Add(string.Format("Error getting collection {0}.{1}: {2}",
	Assert.True(errors.Count == 0, string.Join(Environment.NewLine, errors));

This piece of code was really helpful in the past and is following me in every project involving NHibernate. It seemed useful to share 🙂


MemoDb 0.1.2 beta released!

July 25, 2011 Leave a comment

Glad to announce the beta of MemoDb is out. It includes

  • Circular references support
  • Support for .NET 3.5 and Mono 2.0

Thanks to Yallie for the work on supporting more targets than 4.0!

Here is the release:

Categories: MemoDb Tags: , , ,

MemoDb 0.1.0 alpha released!

February 11, 2011 Leave a comment

The first release of MemoDb is out!
It supports:

  • Inserts
  • Updates
  • Delete
  • Insert references and collections by cascade
  • Delete collection children by cascade (aka cascade-all-delete-orphans)
  • EntityRef references
  • Id auto generation (With defaults for ints, long and guid)

You can download it from here.

To see other posts of MemoDb:

MemoDb: Cascades and Collections support

February 10, 2011 Leave a comment

I’ve been implementing some new features and I think it’s ready for an alpha release. I’ll be uploading the files in codeplex.

To follow the MemoDb posts saga:

Now, MemoDb supports:

  • Delete
  • Collections
  • EntityRef (for LinqToSql support)
  • Cascade inserts
  • Cascade delete on collections children (aka all-delete-orphans)

Pretty simple, it let you delete objects:

using (var s = memo.CreateSession())
	var city= s.GetById<City>(1);

Cascade Inserts
You can configure MemoDb to insert new references by default:

var memo = new Memo(insertByCascadeByDefault: true)

Or you can specify the behaviour just for one relationship:

 var memo = new Memo()
           .Map<City>(c => c.CasacadeInsert(x => x.Country))

Cascade Delete Orphans
You can configure MemoDb to delete collections children by cascade when the parent object is deleted by default:

var memo = new Memo(deleteOrphansByCascadeByDefault: true)

Or you can specify the behaviour just for one collection:

 var memo = new Memo()
           .Map<Country>(c => c.CasacadeDeleteOrphans(x => x.Cities));

MemoDb: Auto assign id support

February 4, 2011 Leave a comment

MemoDb is a in-memory-linq-database useful for mocking linq repositories in unit testing. You can track the previous posts:

Today I will talk about the generation of ids for new entities. As MemoDb stands for mocking real linq orms and, generally, the underlying databases auto assign the ids.

var memo = new Memo()
     .Map<MyEntity>(x=>x.Id(x => x.Id, autoAssign: true));

By now, MemoDb supports:

  • Int32: increments the last id.
  • Int64: increments the last id.
  • Guid: Generates a new Guid.

If you need another strategy or to map another id type, you can specify the strategy as a delegate:

var memo = new Memo()
     .Map<MyEntity>(x=>x.Id(x => x.Id, last => last + 10));

In the example above, the generation is made adding 10 to the last id assigned.
By default, MemoDb will assign the ids when the object is inserted. You can override this behavior by configuration:

var memo = new Memo(assignIdMode: AssignIdMode.OnFlush)

MemoDb: Collections support

February 4, 2011 Leave a comment

MemoDb is a in-memory-linq-database useful for mocking linq repositories in unit testing.
Now it understand how to store collections. The strategy is pretty simple:

  • For class collections (Arraylist, List, Hashtable, etc.) it will construct one of those and add each item. It only works with classes that have a parameterless constructor.
  • For interfaces collections (IList, IList, IDictionary, etc.) it will construct an ArrayList, List, Hashtable or Dictionary according the interface.

You can download the last version from:

Yoda style unit testing

August 20, 2010 Leave a comment

Few days ago we were doing a new algorithm for a existing (and slow) feature. Basically, the algorithm returns a list of strings, and the way we choose to test it is to trust the first one and run both algorithms looking for the same result in both. The first line of the assertion is natural:


But the second one seems to be coded by Yoda


Ok, it’s not actually a Yoda style sentence, but I found it quite funny.

Categories: About nothing, C# Tags: