From: Michael D. <mik...@us...> - 2004-11-12 22:08:36
|
Update of /cvsroot/nhibernate/NHibernateContrib/src/Nullables.Tests/NHibernate In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21720/src/Nullables.Tests/NHibernate Added Files: NullablesClass.cs NullablesClass.hbm.xml NullablesFixture.cs TestCase.cs Log Message: NH-15: created a NHibernateContrib folder in cvs --- NEW FILE: NullablesFixture.cs --- using System; using System.Collections; using NHibernate; using Nullables; using NUnit.Framework; namespace Nullables.Tests.NHibernate { /// <summary> /// Summary description for NullablesFixture. /// </summary> [TestFixture] public class NullablesFixture : TestCase { [SetUp] public void SetUp() { ExportSchema(new string[] { "NullablesClass.hbm.xml"}); } [Test] public void CRUD() { NullablesClass nullNC = InitAllNull( 1 ); NullablesClass notnullNC = InitAllValues( 2 ); ISession s = sessions.OpenSession(); s.Save( nullNC ); s.Save( notnullNC ); s.Flush(); s.Close(); s = sessions.OpenSession(); Assert.AreEqual( 2, s.Find( "from NullablesClass" ).Count, "should be 2 in the db" ); IQuery q = s.CreateQuery( "from NullablesClass as nc where nc.Int32Prop is null" ); IList results = q.List(); Assert.AreEqual( 1, results.Count, "only one null int32 in the db" ); nullNC = (NullablesClass)results[0]; // verify NH did store this fields as null and retrieved them as // the nullable version type. Assert.AreEqual( NullableBoolean.Default, nullNC.BooleanProp ); Assert.AreEqual( NullableByte.Default, nullNC.ByteProp ); Assert.AreEqual( NullableDateTime.Default, nullNC.DateTimeProp ); Assert.AreEqual( NullableDecimal.Default, nullNC.DecimalProp ); Assert.AreEqual( NullableDouble.Default, nullNC.DoubleProp ); Assert.AreEqual( NullableGuid.Default, nullNC.GuidProp ); Assert.AreEqual( NullableInt16.Default, nullNC.Int16Prop); Assert.AreEqual( NullableInt32.Default, nullNC.Int32Prop ); Assert.AreEqual( NullableInt64.Default, nullNC.Int64Prop ); Assert.AreEqual( NullableSingle.Default, nullNC.SingleProp ); Assert.AreEqual( 0, nullNC.Version ); // don't change anything but flush it - should not increment // the version because there were no changes s.Flush(); s.Close(); s = sessions.OpenSession(); nullNC = (NullablesClass)s.Find( "from NullablesClass" )[0]; Assert.AreEqual( 0, nullNC.Version, "no changes to write at last flush - version should not have changed" ); q = s.CreateQuery( "from NullablesClass as nc where nc.Int32Prop = :int32Prop" ); q.SetParameter( "int32Prop", new NullableInt32( Int32.MaxValue) , Nullables.NHibernate.NullablesTypes.NullableInt32 ); results = q.List(); Assert.AreEqual( 1, results.Count ); notnullNC = (NullablesClass)results[0]; // change the Int32 properties nullNC.Int32Prop = 5; notnullNC.Int32Prop = null; s.Flush(); s.Close(); s = sessions.OpenSession(); nullNC = (NullablesClass)s.Load( typeof(NullablesClass), 1 ); notnullNC = (NullablesClass)s.Load( typeof(NullablesClass), 2 ); Assert.IsTrue( 5==nullNC.Int32Prop, "should have actual value" ); Assert.AreEqual( NullableInt32.Default, notnullNC.Int32Prop, "should have 'null' value" ); // clear the table s.Delete( "from NullablesClass" ); s.Flush(); s.Close(); } public NullablesClass InitAllNull(int id) { NullablesClass nc = new NullablesClass(); nc.Id = id; nc.BooleanProp = NullableBoolean.Default; nc.ByteProp = NullableByte.Default; nc.DateTimeProp = NullableDateTime.Default; nc.DecimalProp = NullableDecimal.Default; nc.DoubleProp = NullableDouble.Default; nc.GuidProp = NullableGuid.Default; nc.Int16Prop = NullableInt16.Default; nc.Int32Prop = NullableInt32.Default; nc.Int64Prop = NullableInt64.Default; nc.SingleProp = NullableSingle.Default; return nc; } public NullablesClass InitAllValues(int id) { NullablesClass nc = new NullablesClass(); nc.Id = id; nc.BooleanProp = true; nc.ByteProp = (byte)5; nc.DateTimeProp = DateTime.Parse( "2004-01-01" ); nc.DecimalProp = 2.45M; nc.DoubleProp = 1.7E+3; nc.GuidProp = Guid.NewGuid(); nc.Int16Prop = Int16.MaxValue; nc.Int32Prop = Int32.MaxValue; nc.Int64Prop = Int64.MaxValue; nc.SingleProp = 4.3f; return nc; } } } --- NEW FILE: NullablesClass.hbm.xml --- (This appears to be a binary file; contents omitted.) --- NEW FILE: TestCase.cs --- using System; using System.Reflection; using NHibernate; using NHibernate.Cfg; using NHibernate.Tool.hbm2ddl; using NUnit.Framework; namespace Nullables.Tests.NHibernate { /// <summary> /// Summary description for TestCase. /// </summary> public class TestCase { protected Configuration cfg; protected ISessionFactory sessions; /// <summary> /// Removes the tables used in this TestCase. /// </summary> /// <remarks> /// If the tables are not cleaned up sometimes SchemaExport runs into /// Sql errors because it can't drop tables because of the FKs. This /// will occur if the TestCase does not have the same hbm.xml files /// included as a previous one. /// </remarks> [TearDown] public virtual void TearDown() { DropSchema(); } public void ExportSchema(string[] files) { ExportSchema(files, true); } public void ExportSchema(string[] files, bool exportSchema) { cfg = new Configuration(); for (int i=0; i<files.Length; i++) { cfg.AddResource("Nullables.Tests.NHibernate." + files[i], Assembly.Load("Nullables.Tests")); } if(exportSchema) new SchemaExport(cfg).Create(true, true); sessions = cfg.BuildSessionFactory( ); } /// <summary> /// Drops the schema that was built with the TestCase's Configuration. /// </summary> public void DropSchema() { new SchemaExport(cfg).Drop(true, true); } } } --- NEW FILE: NullablesClass.cs --- using System; namespace Nullables.Tests.NHibernate { /// <summary> /// Summary description for NullablesClass. /// </summary> public class NullablesClass { private int _id; private int _version; private NullableBoolean _booleanProp; private NullableByte _byteProp; private NullableDateTime _dateTimeProp; private NullableDecimal _decimalProp; private NullableDouble _doubleProp; private NullableGuid _guidProp; private NullableInt16 _int16Prop; private NullableInt32 _int32Prop; private NullableInt64 _int64Prop; private NullableSingle _singleProp; public NullablesClass() { } public int Id { get { return _id; } set { _id = value; } } public int Version { get { return _version; } set { _version = value; } } public NullableBoolean BooleanProp { get { return _booleanProp; } set { _booleanProp = value; } } public NullableByte ByteProp { get { return _byteProp; } set { _byteProp = value; } } public NullableDateTime DateTimeProp { get { return _dateTimeProp; } set { _dateTimeProp = value; } } public NullableDecimal DecimalProp { get { return _decimalProp; } set { _decimalProp = value; } } public NullableDouble DoubleProp { get { return _doubleProp; } set { _doubleProp = value; } } public NullableGuid GuidProp { get { return _guidProp; } set { _guidProp = value; } } public NullableInt16 Int16Prop { get { return _int16Prop; } set { _int16Prop = value; } } public NullableInt32 Int32Prop { get { return _int32Prop; } set { _int32Prop = value; } } public NullableInt64 Int64Prop { get { return _int64Prop; } set { _int64Prop = value; } } public NullableSingle SingleProp { get { return _singleProp; } set { _singleProp = value; } } } } |