1: /// <summary>
2: /// Provides a base class that database-dependent unit tests can utilize. It
3: /// handles managing the ActiveRecord data layer setup/init, clean up, etc.
4: /// </summary>
5: public abstract class DatabaseDependentTestFixture
6: {
7: #region Protected Methods
8:
9: /// <summary>
10: /// Deletes all data in the database.
11: /// </summary>
12: protected void DeleteAllData()
13: {
14: Type[] types = ActiveRecordHelper.ModelTypes;
15:
16: foreach (Type t in types)
17: {
18: if (typeof(ActiveRecordBase).IsAssignableFrom(t) &&
19: t.GetMethod("DeleteAll", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy, null, new Type[0], null) != null)
20: {
21: t.InvokeMember("DeleteAll", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.InvokeMethod, null, null, null);
22: }
23: }
24: }
25:
26: /// <summary>
27: /// Initializes ActiveRecord to work with an in-memory temporary database
28: /// for the RBCMS objects.
29: /// </summary>
30: protected void SetupActiveRecord()
31: {
32: Dictionary<string, string> settings = new Dictionary<string, string>();
33: settings.Add("connection.driver_class", "NHibernate.Driver.SQLite20Driver");
34: settings.Add("dialect", "NHibernate.Dialect.SQLiteDialect");
35: settings.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
36: settings.Add("connection.connection_string", "Data Source=ActiveRecord.dat;Version=3;");
37:
38: InPlaceConfigurationSource config = new InPlaceConfigurationSource();
39: config.PluralizeTableNames = true;
40: config.Add(typeof(ActiveRecordBase), settings);
41:
42: if (ActiveRecordStarter.IsInitialized)
43: {
44: ActiveRecordStarter.ResetInitializationFlag();
45: }
46:
47: ActiveRecordStarter.Initialize(config, ActiveRecordHelper.ModelTypes);
48: }
49:
50: /// <summary>
51: /// Drops and recreates the RBCMS database schema.
52: /// </summary>
53: protected void RecreateSchema()
54: {
55: ActiveRecordStarter.CreateSchema();
56: }
57:
58: #endregion
59:
60: #region Public Methods
61:
62: /// <summary>
63: /// Initializes the data layer for first use.
64: /// </summary>
65: [TestFixtureSetUp]
66: public virtual void TestFixtureSetup()
67: {
68: SetupActiveRecord();
69: RecreateSchema();
70: }
71:
72: /// <summary>
73: /// Wipes out all existing data in the database, allowing
74: /// unit tests to start with a blank slate each time (which
75: /// is what they should be doing anyway).
76: /// </summary>
77: [SetUp]
78: public virtual void TestSetup()
79: {
80: DeleteAllData();
81: }
82:
83: #endregion
84: }