Files
Orchard/src/Orchard.Tests/FluentDbTests.cs
loudej 72851737cc Starting an implementation of a composite model management system
--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4038935
2009-11-08 05:27:47 +00:00

84 lines
2.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using Orchard.Tests.Records;
namespace Orchard.Tests {
[TestFixture]
public class FluentDbTests {
public class Types : ITypeSource {
private readonly IEnumerable<Type> _types;
public Types(params Type[] types) {
_types = types;
}
#region ITypeSource Members
public IEnumerable<Type> GetTypes() {
return _types;
}
#endregion
}
[Test]
public void CreatingSchemaForStatedClassesInTempFile() {
var types = new Types(typeof (Foo), typeof (Bar));
var sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.UsingFile("temp"))
.Mappings(m => m.AutoMappings.Add(AutoMap.Source(types)))
.ExposeConfiguration(c => new SchemaExport(c).Create(false, true))
.BuildSessionFactory();
var session = sessionFactory.OpenSession();
session.Save(new Foo {Name = "Hello"});
session.Save(new Bar {Height = 3, Width = 4.5m});
session.Close();
session = sessionFactory.OpenSession();
var foos = session.CreateCriteria<Foo>().List();
Assert.That(foos.Count, Is.EqualTo(1));
Assert.That(foos, Has.All.Property("Name").EqualTo("Hello"));
session.Close();
}
[Test]
public void InMemorySQLiteCanBeUsedInSessionFactory() {
var sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory())
.BuildSessionFactory();
var session = sessionFactory.OpenSession();
session.Close();
}
[Test]
public void UsingDataUtilityToBuildSessionFactory() {
var factory = DataUtility.CreateSessionFactory(typeof (Foo), typeof (Bar));
var session = factory.OpenSession();
var foo1 = new Foo {Name = "world"};
session.Save(foo1);
session.Close();
session = factory.OpenSession();
var foo2 = session.CreateCriteria<Foo>()
.Add(Restrictions.Eq("Name", "world"))
.List<Foo>().Single();
session.Close();
Assert.That(foo1, Is.Not.SameAs(foo2));
Assert.That(foo1.Id, Is.EqualTo(foo2.Id));
}
}
}