Skip to content

Commit

Permalink
Add contributed unit test for NH-3142, with cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarb committed May 26, 2012
1 parent 15e90bb commit f31be36
Showing 4 changed files with 205 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3142/ChildrenTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3142
{
[TestFixture]
public class ChildrenTest : BugTestCase
{
protected override void OnSetUp()
{
base.OnSetUp();

using (var session = OpenSession())
{
for (int h = 1; h < 3; h++)
{
for (int i = 1; i < 4; i++)
{
var parent = new DomainParent { Id1 = h, Id2 = i };
session.Save(parent);

var parentId = new DomainParentWithComponentId { Id = { Id1 = h, Id2 = i } };
session.Save(parentId);

for (int j = 1; j < 4; j++)
{
var child = new DomainChild { ParentId1 = h, ParentId2 = i };
session.Save(child);

var childId = new DomainChildWCId { ParentId1 = h, ParentId2 = i };
session.Save(childId);
}
}
}

session.Flush();
}
}

protected override void OnTearDown()
{
base.OnTearDown();
using (var session = OpenSession())
{
session.Delete("from DomainChild");
session.Delete("from DomainChildWCId");
session.Delete("from System.Object");
session.Flush();
}
}


[Test]
public void ChildrenCollectionOfAllParentsShouldContainsThreeElements()
{
using (var session = OpenSession())
{
var entities = session.CreateQuery("from DomainParent").List<DomainParent>();

foreach (var parent in entities)
Assert.AreEqual(3, parent.Children.Count);
}
}

[Test]
public void ChildrenCollectionOfAllParentsWithComponentIdShouldContainsThreeElements()
{
using (var session = OpenSession())
{
var entities = session.CreateQuery("from DomainParentWithComponentId").List<DomainParentWithComponentId>();

foreach (var parent in entities)
Assert.AreEqual(3, parent.Children.Count);
}
}
}
}
75 changes: 75 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3142/DomainClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Collections.Generic;
using System;


namespace NHibernate.Test.NHSpecificTest.NH3142
{
public class DomainParent
{
public virtual int Id1 { get; set; }
public virtual int Id2 { get; set; }

public virtual ICollection<DomainChild> Children { get; set; }

public override bool Equals(object other)
{
var otherParent = other as DomainParent;
if (otherParent == null)
return false;
return Id1 == otherParent.Id1 &&
Id2 == otherParent.Id2;
}

public override int GetHashCode()
{
return Id1.GetHashCode() ^ Id2.GetHashCode();
}
}

public class DomainChild
{
public virtual int Id { get; set; }
public virtual int ParentId1 { get; set; }
public virtual int ParentId2 { get; set; }
}

[Serializable]
public class DomainParentIdentifier
{
public virtual int Id1 { get; set; }
public virtual int Id2 { get; set; }

public override bool Equals(object other)
{
var otherParent = other as DomainParentIdentifier;
if (otherParent == null)
return false;
return Id1 == otherParent.Id1 &&
Id2 == otherParent.Id2;
}

public override int GetHashCode()
{
return Id1.GetHashCode() ^ Id2.GetHashCode();
}
}

public class DomainParentWithComponentId
{
public DomainParentWithComponentId()
{
Id = new DomainParentIdentifier();
}

public virtual DomainParentIdentifier Id { get; protected set; }

public virtual ICollection<DomainChildWCId> Children { get; set; }
}

public class DomainChildWCId
{
public virtual int Id { get; set; }
public virtual int ParentId1 { get; set; }
public virtual int ParentId2 { get; set; }
}
}
48 changes: 48 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3142/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH3142" default-access="property"
default-lazy="true">
<class name="DomainParent">
<composite-id>
<key-property name="Id1"/>
<key-property name="Id2"/>
</composite-id>
<set name="Children" inverse="true" batch-size="4">
<key>
<column name="ParentId1" />
<column name="ParentId2" />
</key>
<one-to-many class="DomainChild" />
</set>
</class>

<class name="DomainChild">
<id name="Id">
<generator class="identity" />
</id>
<property name="ParentId1"/>
<property name="ParentId2"/>
</class>

<class name="DomainParentWithComponentId">
<composite-id name="Id" class="DomainParentIdentifier">
<key-property name="Id1"/>
<key-property name="Id2"/>
</composite-id>
<set name="Children" inverse="true" batch-size="4">
<key>
<column name="ParentId1" />
<column name="ParentId2" />
</key>
<one-to-many class="DomainChildWCId" />
</set>
</class>

<class name="DomainChildWCId">
<id name="Id">
<generator class="identity" />
</id>
<property name="ParentId1"/>
<property name="ParentId2"/>
</class>
</hibernate-mapping>
3 changes: 3 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
@@ -972,6 +972,8 @@
<Compile Include="NHSpecificTest\NH3126\Property.cs" />
<Compile Include="NHSpecificTest\NH3126\PropertyValue.cs" />
<Compile Include="NHSpecificTest\NH3124\FixtureByCode.cs" />
<Compile Include="NHSpecificTest\NH3142\ChildrenTest.cs" />
<Compile Include="NHSpecificTest\NH3142\DomainClass.cs" />
<Compile Include="NHSpecificTest\NH3149\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3149\Model.cs" />
<Compile Include="NHSpecificTest\NH3138\FixtureByCode.cs" />
@@ -2833,6 +2835,7 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="NHSpecificTest\NH2789\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3142\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3153\SchemaInClass.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3153\SchemaInMapping.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3149\Mappings.hbm.xml" />

0 comments on commit f31be36

Please sign in to comment.