1: /// <summary>
2: /// A simple widget!
3: /// </summary>
4: [ActiveRecord]
5: public class Widget : ActiveRecordBase<Widget>
6: {
7: /// <summary>
8: /// The ID.
9: /// </summary>
10: [PrimaryKey]
11: public int Id { get; set; }
12:
13: /// <summary>
14: /// Its name.
15: /// </summary>
16: [Castle.ActiveRecord.Property]
17: public string Name { get; set; }
18:
19: /// <summary>
20: /// Its description.
21: /// </summary>
22: [Castle.ActiveRecord.Property]
23: public string Description { get; set; }
24: }
25:
26: /// <summary>
27: /// Test fixture for <see cref="ActiveRecordLinqContext"/>.
28: /// </summary>
29: /// <author>MBH</author>
30: /// <dateAuthored>7/22/08</dateAuthored>
31: [TestFixture]
32: public class SimpleLinqTests
33: {
34: #region Private Helpers
35:
36: /// <summary>
37: /// Initializes ActiveRecord to work with an in-memory temporary database
38: /// via SQLite.
39: /// </summary>
40: private static void SetupActiveRecord()
41: {
42: Dictionary<string, string> settings = new Dictionary<string, string>();
43: settings.Add("connection.driver_class", "NHibernate.Driver.SQLite20Driver");
44: settings.Add("dialect", "NHibernate.Dialect.SQLiteDialect");
45: settings.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
46: settings.Add("connection.connection_string", "Data Source=LinqTest.dat;Version=3;");
47:
48: InPlaceConfigurationSource config = new InPlaceConfigurationSource();
49: config.PluralizeTableNames = true;
50: config.Add(typeof(ActiveRecordBase), settings);
51:
52: if (ActiveRecordStarter.IsInitialized)
53: {
54: ActiveRecordStarter.ResetInitializationFlag();
55: }
56:
57: ActiveRecordStarter.Initialize(config, typeof (Widget));
58: }
59:
60: /// <summary>
61: /// Drops and recreates the database schema.
62: /// </summary>
63: private static void RecreateSchema()
64: {
65: ActiveRecordStarter.CreateSchema();
66: }
67:
68: #endregion
69:
70: /// <summary>
71: /// Initializes the data layer for first use.
72: /// </summary>
73: [TestFixtureSetUp]
74: public virtual void TestFixtureSetup()
75: {
76: SetupActiveRecord();
77: RecreateSchema();
78: }
79:
80: /// <summary>
81: /// Verifies that LINQ works as expected.
82: /// </summary>
83: [Test]
84: public void LinqTest1()
85: {
86: //First, insert a few test widgets models.
87: for (int i = 0; i < 10; i++)
88: {
89: Widget widget = new Widget { Name = "Test" + i, Description = "Test Description " + i };
90: widget.SaveAndFlush();
91: }
92:
93: using (new SessionScope())
94: {
95: ActiveRecordLinqContext context = new ActiveRecordLinqContext();
96:
97: //The real beauty of this is not testable: it is actually only
98: //querying the database for the names, not the full Widget
99: //objects! You can turn on log4net to verify this.
100: string[] names = (from w in context.Session.Linq<Widget>()
101: select w.Name).ToArray();
102:
103: Assert.AreEqual(10, names.Length);
104:
105: //This is all translated into SQL and executes in the DB, not in
106: //memory!
107: names = (from w in context.Session.Linq<Widget>()
108: where w.Id > 5
109: select w.Name).ToArray();
110:
111: Assert.AreEqual(5, names.Length);
112: }
113: }
114: }