|
From: <ste...@us...> - 2009-11-25 21:46:15
|
Revision: 4859
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4859&view=rev
Author: steverstrong
Date: 2009-11-25 21:46:06 +0000 (Wed, 25 Nov 2009)
Log Message:
-----------
More Linq test cases added
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate.Test/Linq/Entities/Northwind.cs
trunk/nhibernate/src/NHibernate.Test/Linq/LinqTestCase.cs
trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
Added Paths:
-----------
trunk/nhibernate/src/NHibernate.Test/Linq/Entities/Patient.cs
trunk/nhibernate/src/NHibernate.Test/Linq/Mappings/Patient.hbm.xml
trunk/nhibernate/src/NHibernate.Test/Linq/OrderByTests.cs
trunk/nhibernate/src/NHibernate.Test/Linq/PagingTests.cs
trunk/nhibernate/src/NHibernate.Test/Linq/PatientTests.cs
Modified: trunk/nhibernate/src/NHibernate.Test/Linq/Entities/Northwind.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Linq/Entities/Northwind.cs 2009-11-25 20:16:50 UTC (rev 4858)
+++ trunk/nhibernate/src/NHibernate.Test/Linq/Entities/Northwind.cs 2009-11-25 21:46:06 UTC (rev 4859)
@@ -1,4 +1,6 @@
-using System.Linq;
+using System;
+using System.Collections.Generic;
+using System.Linq;
using NHibernate.Linq;
namespace NHibernate.Test.Linq.Entities
@@ -61,5 +63,25 @@
{
get { return _session.Query<User>(); }
}
+
+ public IQueryable<PatientRecord> PatientRecords
+ {
+ get { return _session.Query<PatientRecord>(); }
+ }
+
+ public IQueryable<State> States
+ {
+ get { return _session.Query<State>(); }
+ }
+
+ public IQueryable<Patient> Patients
+ {
+ get { return _session.Query<Patient>(); }
+ }
+
+ public IQueryable<Physician> Physicians
+ {
+ get { return _session.Query<Physician>(); }
+ }
}
}
\ No newline at end of file
Added: trunk/nhibernate/src/NHibernate.Test/Linq/Entities/Patient.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Linq/Entities/Patient.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/Linq/Entities/Patient.cs 2009-11-25 21:46:06 UTC (rev 4859)
@@ -0,0 +1,88 @@
+using System;
+using System.Collections.Generic;
+
+namespace NHibernate.Test.Linq.Entities
+{
+ public class Patient
+ {
+ private IList<PatientRecord> patientRecords;
+ private bool active;
+ private Physician physician;
+
+ protected Patient() { }
+ public Patient(IEnumerable<PatientRecord> patientRecords, bool active, Physician physician)
+ {
+ this.active = active;
+ this.physician = physician;
+ this.patientRecords = new List<PatientRecord>(patientRecords);
+ foreach (var record in this.patientRecords)
+ {
+ record.Patient = this;
+ }
+ }
+
+ public virtual long Id { get; set; }
+
+ public virtual bool Active
+ {
+ get { return active; }
+ set { active = value; }
+ }
+
+ public virtual IList<PatientRecord> PatientRecords
+ {
+ get { return patientRecords; }
+ set { patientRecords = value; }
+ }
+
+ public virtual Physician Physician
+ {
+ get { return physician; }
+ set { physician = value; }
+ }
+ }
+
+ public class Physician
+ {
+ public virtual long Id { get; set; }
+ public virtual string Name { get; set; }
+ }
+
+ public class PatientRecord
+ {
+ public virtual long Id { get; set; }
+ public virtual PatientName Name { get; set; }
+ public virtual DateTime BirthDate { get; set; }
+ public virtual Gender Gender { get; set; }
+ public virtual PatientAddress Address { get; set; }
+ public virtual Patient Patient { get; set; }
+ }
+
+ public enum Gender
+ {
+ Unknown,
+ Male,
+ Female
+ }
+
+ public class PatientName
+ {
+ public virtual string FirstName { get; set; }
+ public virtual string LastName { get; set; }
+ }
+
+ public class PatientAddress
+ {
+ public virtual string AddressLine1 { get; set; }
+ public virtual string AddressLine2 { get; set; }
+ public virtual string City { get; set; }
+ public virtual State State { get; set; }
+ public virtual string ZipCode { get; set; }
+ }
+ public class State
+ {
+ public virtual long Id { get; set; }
+ public virtual string Abbreviation { get; set; }
+ public virtual string FullName { get; set; }
+ }
+}
Modified: trunk/nhibernate/src/NHibernate.Test/Linq/LinqTestCase.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Linq/LinqTestCase.cs 2009-11-25 20:16:50 UTC (rev 4858)
+++ trunk/nhibernate/src/NHibernate.Test/Linq/LinqTestCase.cs 2009-11-25 21:46:06 UTC (rev 4859)
@@ -49,7 +49,8 @@
"Linq.Mappings.Role.hbm.xml",
"Linq.Mappings.User.hbm.xml",
"Linq.Mappings.TimeSheet.hbm.xml",
- "Linq.Mappings.Animal.hbm.xml"
+ "Linq.Mappings.Animal.hbm.xml",
+ "Linq.Mappings.Patient.hbm.xml"
};
}
@@ -69,7 +70,7 @@
using (ITransaction tx = session.BeginTransaction())
{
CreateMiscTestData(session);
-
+ CreatePatientData(session);
tx.Commit();
}
}
@@ -220,6 +221,93 @@
session.Save(animal);
}
+ private void CreatePatientData(ISession session)
+ {
+ State newYork = new State
+ {
+ Abbreviation = "NY",
+ FullName = "New York"
+ };
+ State florida = new State
+ {
+ Abbreviation = "FL",
+ FullName = "Florida"
+ };
+
+ Physician drDobbs = new Physician
+ {
+ Name = "Dr Dobbs"
+ };
+ Physician drWatson = new Physician
+ {
+ Name = "Dr Watson"
+ };
+
+ PatientRecord bobBarkerRecord = new PatientRecord
+ {
+ Name = new PatientName
+ {
+ FirstName = "Bob",
+ LastName = "Barker"
+ },
+ Address = new PatientAddress
+ {
+ AddressLine1 = "123 Main St",
+ City = "New York",
+ State = newYork,
+ ZipCode = "10001"
+ },
+ BirthDate = new DateTime(1930, 1, 1),
+ Gender = Gender.Male
+ };
+
+ PatientRecord johnDoeRecord1 = new PatientRecord
+ {
+ Name = new PatientName
+ {
+ FirstName = "John",
+ LastName = "Doe"
+ },
+ Address = new PatientAddress
+ {
+ AddressLine1 = "123 Main St",
+ City = "Tampa",
+ State = florida,
+ ZipCode = "33602"
+ },
+ BirthDate = new DateTime(1969, 1, 1),
+ Gender = Gender.Male
+ };
+
+ PatientRecord johnDoeRecord2 = new PatientRecord
+ {
+ Name = new PatientName
+ {
+ FirstName = "John",
+ LastName = "Doe"
+ },
+ Address = new PatientAddress
+ {
+ AddressLine1 = "123 Main St",
+ AddressLine2 = "Apt 2",
+ City = "Tampa",
+ State = florida,
+ ZipCode = "33602"
+ },
+ BirthDate = new DateTime(1969, 1, 1)
+ };
+
+ Patient bobBarker = new Patient(new[] { bobBarkerRecord }, false, drDobbs);
+ Patient johnDoe = new Patient(new[] { johnDoeRecord1, johnDoeRecord2 }, true, drWatson);
+
+ session.Save(newYork);
+ session.Save(florida);
+ session.Save(drDobbs);
+ session.Save(drWatson);
+ session.Save(bobBarker);
+ session.Save(johnDoe);
+ }
+
private void CreateNorthwindData(IStatelessSession session)
{
var shippers = new List<Shipper>();
Added: trunk/nhibernate/src/NHibernate.Test/Linq/Mappings/Patient.hbm.xml
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Linq/Mappings/Patient.hbm.xml (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/Linq/Mappings/Patient.hbm.xml 2009-11-25 21:46:06 UTC (rev 4859)
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate.Test.Linq.Entities" assembly="NHibernate.Test">
+ <class name="Patient" table="Patients">
+ <id name="Id" column="PatientId" type="Int64">
+ <generator class="native" />
+ </id>
+ <property type="System.Boolean" not-null="true" name="Active" column="[Active]" />
+ <many-to-one name="Physician" cascade="none" column="PhysicianId" not-null="true" class="Physician" />
+ <bag name="PatientRecords" inverse="true" lazy="true" cascade="all">
+ <key column="PatientId" />
+ <one-to-many class="PatientRecord" />
+ </bag>
+ </class>
+
+ <class name="Physician" table="Physicians">
+ <id name="Id" column="PhysicianId" type="Int64">
+ <generator class="native" />
+ </id>
+ <property type="System.String" not-null="true" name="Name" column="[Name]" />
+ </class>
+
+ <class name="PatientRecord" table="PatientRecords">
+ <id name="Id" column="PatientRecordId" type="System.Int64">
+ <generator class="native" />
+ </id>
+ <property type="NHibernate.Test.Linq.Entities.Gender, NHibernate.Test" not-null="true" name="Gender" column="[Gender]" />
+ <property type="System.DateTime" not-null="true" name="BirthDate" column="[BirthDate]" />
+
+ <component name="Name" class="PatientName">
+ <property type="System.String" not-null="true" name="FirstName" column="[FirstName]" />
+ <property type="System.String" not-null="true" name="LastName" column="[LastName]" />
+ </component>
+
+ <component name="Address" class="PatientAddress">
+
+ <property type="System.String" name="AddressLine1" column="[AddressLine1]" />
+
+ <property type="System.String" name="AddressLine2" column="[AddressLine2]" />
+
+ <property type="System.String" name="City" column="[City]" />
+
+ <many-to-one name="State" cascade="none" column="StateId" class="State" />
+
+ <property type="System.String" name="ZipCode" column="[ZipCode]" />
+
+ </component>
+
+
+ <many-to-one name="Patient" cascade="none" column="PatientId" not-null="true" class="Patient" />
+ </class>
+
+ <class name="State" table="States">
+ <id name="Id" column="StateId" type="System.Int64">
+ <generator class="native" />
+ </id>
+ <property type="System.String" not-null="true" name="Abbreviation" column="[Abbreviation]" />
+ <property type="System.String" not-null="true" name="FullName" column="[FullName]" />
+ </class>
+</hibernate-mapping>
Added: trunk/nhibernate/src/NHibernate.Test/Linq/OrderByTests.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Linq/OrderByTests.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/Linq/OrderByTests.cs 2009-11-25 21:46:06 UTC (rev 4859)
@@ -0,0 +1,119 @@
+using System.Linq;
+using NUnit.Framework;
+
+namespace NHibernate.Test.Linq
+{
+ [TestFixture]
+ public class OrderByTests : LinqTestCase
+ {
+ [Test]
+ public void AscendingOrderByClause()
+ {
+ var query = from c in db.Customers
+ orderby c.CustomerId
+ select c.CustomerId;
+
+ var ids = query.ToList();
+
+ if (ids.Count > 1)
+ {
+ Assert.Greater(ids[1], ids[0]);
+ }
+ }
+
+ [Test]
+ public void DescendingOrderByClause()
+ {
+ var query = from c in db.Customers
+ orderby c.CustomerId descending
+ select c.CustomerId;
+
+ var ids = query.ToList();
+
+ if (ids.Count > 1)
+ {
+ Assert.Greater(ids[0], ids[1]);
+ }
+ }
+
+ [Test]
+ [Ignore("NHibernate does not currently support subqueries in select clause (no way to specify a projection from a detached criteria).")]
+ public void AggregateAscendingOrderByClause()
+ {
+ var query = from c in db.Customers
+ orderby c.Orders.Count
+ select c;
+
+ var customers = query.ToList();
+
+ if (customers.Count > 1)
+ {
+ Assert.Less(customers[0].Orders.Count, customers[1].Orders.Count);
+ }
+ }
+
+ [Test]
+ [Ignore("NHibernate does not currently support subqueries in select clause (no way to specify a projection from a detached criteria).")]
+ public void AggregateDescendingOrderByClause()
+ {
+ var query = from c in db.Customers
+ orderby c.Orders.Count descending
+ select c;
+
+ var customers = query.ToList();
+
+ if (customers.Count > 1)
+ {
+ Assert.Greater(customers[0].Orders.Count, customers[1].Orders.Count);
+ }
+ }
+
+ [Test]
+ public void ComplexAscendingOrderByClause()
+ {
+ var query = from c in db.Customers
+ where c.Address.Country == "Belgium"
+ orderby c.Address.Country, c.Address.City
+ select c.Address.City;
+
+ var ids = query.ToList();
+
+ if (ids.Count > 1)
+ {
+ Assert.Greater(ids[1], ids[0]);
+ }
+ }
+
+ [Test]
+ public void ComplexDescendingOrderByClause()
+ {
+ var query = from c in db.Customers
+ where c.Address.Country == "Belgium"
+ orderby c.Address.Country descending, c.Address.City descending
+ select c.Address.City;
+
+ var ids = query.ToList();
+
+ if (ids.Count > 1)
+ {
+ Assert.Greater(ids[0], ids[1]);
+ }
+ }
+
+ [Test]
+ public void ComplexAscendingDescendingOrderByClause()
+ {
+ var query = from c in db.Customers
+ where c.Address.Country == "Belgium"
+ orderby c.Address.Country ascending, c.Address.City descending
+ select c.Address.City;
+
+ var ids = query.ToList();
+
+ if (ids.Count > 1)
+ {
+ Assert.Greater(ids[0], ids[1]);
+ }
+ }
+ }
+}
\ No newline at end of file
Added: trunk/nhibernate/src/NHibernate.Test/Linq/PagingTests.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Linq/PagingTests.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/Linq/PagingTests.cs 2009-11-25 21:46:06 UTC (rev 4859)
@@ -0,0 +1,63 @@
+using System.Linq;
+using NUnit.Framework;
+
+namespace NHibernate.Test.Linq
+{
+ [TestFixture]
+ public class PagingTests : LinqTestCase
+ {
+ [Test]
+ public void Customers1to5()
+ {
+ var q = (from c in db.Customers select c.CustomerId).Take(5);
+ var query = q.ToList();
+
+ Assert.AreEqual(5, query.Count);
+ }
+
+ [Test]
+ public void Customers11to20()
+ {
+ var query = (from c in db.Customers
+ orderby c.CustomerId
+ select c.CustomerId).Skip(10).Take(10).ToList();
+ Assert.AreEqual(query[0], "BSBEV");
+ Assert.AreEqual(10, query.Count);
+ }
+
+ [Test]
+ [Ignore("NHibernate does not currently support subqueries in from clause")]
+ public void CustomersChainedTake()
+ {
+ var q = (from c in db.Customers
+ orderby c.CustomerId
+ select c.CustomerId).Take(5).Take(6);
+
+ var query = q.ToList();
+
+ Assert.AreEqual(5, query.Count);
+ Assert.AreEqual("ALFKI", query[0]);
+ Assert.AreEqual("BLAUS", query[4]);
+ }
+
+ [Test]
+ [Ignore("NHibernate does not currently support subqueries in from clause")]
+ public void CustomersChainedSkip()
+ {
+ var q = (from c in db.Customers select c.CustomerId).Skip(10).Skip(5);
+ var query = q.ToList();
+ Assert.AreEqual(query[0], "CONSH");
+ Assert.AreEqual(76, query.Count);
+ }
+
+
+
+ [Test]
+ [Ignore("NHibernate does not currently support subqueries in from clause")]
+ public void CountAfterTakeShouldReportTheCorrectNumber()
+ {
+ var users = db.Customers.Skip(3).Take(10);
+ Assert.AreEqual(10, users.Count());
+ }
+ }
+}
\ No newline at end of file
Added: trunk/nhibernate/src/NHibernate.Test/Linq/PatientTests.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Linq/PatientTests.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/Linq/PatientTests.cs 2009-11-25 21:46:06 UTC (rev 4859)
@@ -0,0 +1,84 @@
+using System.Linq;
+using NHibernate.Test.Linq.Entities;
+using NUnit.Framework;
+
+namespace NHibernate.Test.Linq
+{
+ [TestFixture]
+ public class PatientTests : LinqTestCase
+ {
+ [Test]
+ public void CanQueryOnPropertyOfComponent()
+ {
+ var query = (from pr in db.PatientRecords
+ where pr.Name.LastName == "Doe"
+ select pr).ToList();
+
+ Assert.AreEqual(2, query.Count);
+ }
+
+ [Test]
+ public void CanQueryOnManyToOneOfComponent()
+ {
+ var florida = db.States.FirstOrDefault(x => x.Abbreviation == "FL");
+
+ var query = (from pr in db.PatientRecords
+ where pr.Address.State == florida
+ select pr).ToList();
+
+ Assert.AreEqual(2, query.Count);
+ }
+
+ [Test]
+ public void CanQueryOnPropertyOfManyToOneOfComponent()
+ {
+ var query = (from pr in db.PatientRecords
+ where pr.Address.State.Abbreviation == "FL"
+ select pr).ToList();
+
+ Assert.AreEqual(2, query.Count);
+ }
+
+ [Test]
+ public void CanQueryOnPropertyOfOneToMany()
+ {
+ var query = (from p in db.Patients
+ where p.PatientRecords.Any(x => x.Gender == Gender.Unknown)
+ select p).ToList();
+
+ Assert.AreEqual(1, query.Count);
+ }
+
+ [Test]
+ public void CanQueryOnPropertyOfManyToOne()
+ {
+ var query = (from pr in db.PatientRecords
+ where pr.Patient.Active == true
+ select pr).ToList();
+
+ Assert.AreEqual(2, query.Count);
+ }
+
+ [Test]
+ public void CanQueryOnManyToOneOfManyToOne()
+ {
+ var drWatson = db.Physicians.FirstOrDefault(x => x.Name == "Dr Watson");
+
+ var query = (from pr in db.PatientRecords
+ where pr.Patient.Physician == drWatson
+ select pr).ToList();
+
+ Assert.AreEqual(2, query.Count);
+ }
+
+ [Test]
+ public void CanQueryOnPropertyOfManyToOneOfManyToOne()
+ {
+ var query = (from pr in db.PatientRecords
+ where pr.Patient.Physician.Name == "Dr Watson"
+ select pr).ToList();
+
+ Assert.AreEqual(2, query.Count);
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-11-25 20:16:50 UTC (rev 4858)
+++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-11-25 21:46:06 UTC (rev 4859)
@@ -394,6 +394,7 @@
<Compile Include="Linq\Entities\Northwind.cs" />
<Compile Include="Linq\Entities\Order.cs" />
<Compile Include="Linq\Entities\OrderLine.cs" />
+ <Compile Include="Linq\Entities\Patient.cs" />
<Compile Include="Linq\Entities\Product.cs" />
<Compile Include="Linq\Entities\ProductCategory.cs" />
<Compile Include="Linq\Entities\Region.cs" />
@@ -412,7 +413,10 @@
<Compile Include="Linq\MethodCallTests.cs" />
<Compile Include="Linq\MiscellaneousTextFixture.cs" />
<Compile Include="Linq\ObjectDumper.cs" />
+ <Compile Include="Linq\OrderByTests.cs" />
+ <Compile Include="Linq\PagingTests.cs" />
<Compile Include="Linq\ParameterisedQueries.cs" />
+ <Compile Include="Linq\PatientTests.cs" />
<Compile Include="Linq\ReadonlyTestCase.cs" />
<Compile Include="MappingTest\NonReflectiveBinderFixture.cs" />
<Compile Include="MappingTest\Wicked.cs" />
@@ -2081,6 +2085,7 @@
<EmbeddedResource Include="CfgTest\Loquacious\EntityToCache.hbm.xml" />
<EmbeddedResource Include="DriverTest\SqlServerCeEntity.hbm.xml" />
<Content Include="DynamicEntity\package.html" />
+ <EmbeddedResource Include="Linq\Mappings\Patient.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2011\Mappings.hbm.xml" />
<EmbeddedResource Include="Linq\Mappings\Animal.hbm.xml" />
<EmbeddedResource Include="Linq\Mappings\AnotherEntity.hbm.xml" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|