Skip to content

Failing test using ienumerable for #1874 #1876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions src/NHibernate.Test/Extralazy/ExtraLazyFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.Extralazy
Expand Down Expand Up @@ -28,11 +29,11 @@ public void ExtraLazyWithWhereClause()
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.CreateSQLQuery("insert into Users (Name,Password) values('gavin','secret')")
s.CreateSQLQuery("insert into users (Name,Password) values('gavin','secret')")
.UniqueResult();
s.CreateSQLQuery("insert into Photos (Title,Owner) values('PRVaaa','gavin')")
s.CreateSQLQuery("insert into photos (Title,Owner) values('PRVaaa','gavin')")
.UniqueResult();
s.CreateSQLQuery("insert into Photos (Title,Owner) values('PUBbbb','gavin')")
s.CreateSQLQuery("insert into photos (Title,Owner) values('PUBbbb','gavin')")
.UniqueResult();
t.Commit();
}
Expand All @@ -47,9 +48,9 @@ public void ExtraLazyWithWhereClause()
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.CreateSQLQuery("delete from Photos")
s.CreateSQLQuery("delete from photos")
.UniqueResult();
s.CreateSQLQuery("delete from Users")
s.CreateSQLQuery("delete from users")
.UniqueResult();

t.Commit();
Expand All @@ -72,7 +73,7 @@ public void OrphanDelete()
hia = new Document("HiA", "blah blah blah", gavin);
hia2 = new Document("HiA2", "blah blah blah blah", gavin);
gavin.Documents.Add(hia); // NH: added ; I don't understand how can work in H3.2.5 without add
gavin.Documents.Add(hia2); // NH: added
gavin.Documents.Add(hia2); // NH: added
s.Persist(gavin);
t.Commit();
}
Expand Down Expand Up @@ -106,6 +107,52 @@ public void OrphanDelete()
}
}

[Test]
public void OrphanEnumerableDelete()
{
User gavin = null;
Widget widget = null;
Widget widget2 = null;

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
gavin = new User("gavin", "secret");
gavin.AddWidget("widg1", "blah blah blah");
gavin.AddWidget("widg2", "blah blah blah blah");
s.Persist(gavin);
t.Commit();
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
gavin = s.Get<User>("gavin");
widget = s.Get<Widget>("widg1");
//Uncomment this to initialise the collection and make the test pass:
//Assert.IsTrue(gavin.Widgets.Any(w => w == widget));

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

gavin.RemoveWidget(widget);
Assert.IsFalse(gavin.Widgets.Any(w => w == widget));
Assert.IsTrue(gavin.Widgets.Any(w => w.Title == "widg2"));
Assert.AreEqual(1, gavin.Widgets.Count());
t.Commit();
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
gavin = s.Get<User>("gavin");
Assert.AreEqual(1, gavin.Widgets.Count());
Assert.IsFalse(gavin.Widgets.Any(w => w.Title == "widg1"));
Assert.IsTrue(gavin.Widgets.Any(w => w.Title == "widg2"));
Assert.That(s.Get<Widget>("widg1"), Is.Null);
widget2 = s.Get<Widget>("widg2");
gavin.RemoveWidget(widget2);
s.Delete(gavin);
t.Commit();
}
}

[Test]
public void Get()
{
Expand Down Expand Up @@ -329,4 +376,4 @@ public void AddToUninitializedSetWithLaterLazyLoad()
}
}
}
}
}
18 changes: 17 additions & 1 deletion src/NHibernate.Test/Extralazy/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class User
private IDictionary<string, SessionAttribute> session = new Dictionary<string, SessionAttribute>();
private ISet<Document> documents = new HashSet<Document>();
private ISet<Photo> photos = new HashSet<Photo>();
private ISet<Widget> widgets = new HashSet<Widget>();
protected User() {}
public User(string name, string password)
{
Expand Down Expand Up @@ -46,5 +47,20 @@ public virtual ISet<Photo> Photos
get { return photos; }
set { photos = value; }
}

public virtual IEnumerable<Widget> Widgets
{
get { return widgets; }
}

public virtual void AddWidget(string title, string description)
{
widgets.Add(new Widget(title, description, this));
}

public virtual void RemoveWidget(Widget widget)
{
widgets.Remove(widget);
}
}
}
}
29 changes: 19 additions & 10 deletions src/NHibernate.Test/Extralazy/UserGroup.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<many-to-many class="User" column="personName"/>
</map>
</class>

<class name="User" table="users">
<id name="Name"/>
<property name="Password"/>
Expand All @@ -29,6 +29,10 @@
<key column="owner"/>
<one-to-many class="Photo"/>
</set>
<set name="Widgets" inverse="true" lazy="extra" cascade="all,delete-orphan" access="nosetter.camelcase">
<key column="owner"/>
<one-to-many class="Widget"/>
</set>
</class>
<class name="Photo" table="photos">
<id name="Title"/>
Expand All @@ -39,7 +43,12 @@
<property name="Content" type="string" length="10000"/>
<many-to-one name="Owner" not-null="true"/>
</class>

<class name="Widget" table="widgets">
<id name="Title"/>
<property name="Description" type="string" length="10000"/>
<many-to-one name="Owner" not-null="true"/>
</class>

<class name="SessionAttribute" table="session_attributes">
<id name="id" access="field">
<generator class="native"/>
Expand All @@ -48,22 +57,22 @@
<property name="StringData"/>
<property name="ObjectData" type="Serializable"/>
</class>
<!--
<!--
NH the map of SessionAttribute in H3.2.6 have insert="false" for property "Name" but it can't work with not-null="true"
We remove the inconsistence.
-->

<sql-query name="UserSessionData">
<return alias="u" class="User"/>
<return-join alias="s" property="u.Session"/>
select
lower(u.name) as {u.Name}, lower(u.password) as {u.Password},
lower(s.userName) as {s.key}, lower(s.name) as {s.index}, s.id as {s.element},
select
lower(u.name) as {u.Name}, lower(u.password) as {u.Password},
lower(s.userName) as {s.key}, lower(s.name) as {s.index}, s.id as {s.element},
{s.element.*}
from users u
from users u
join session_attributes s on lower(s.userName) = lower(u.name)
where u.name like :uname
</sql-query>
</sql-query>


</hibernate-mapping>
62 changes: 62 additions & 0 deletions src/NHibernate.Test/Extralazy/Widget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace NHibernate.Test.Extralazy
{
public class Widget
{
private string title;
private string description;
private User owner;

protected Widget() { }
public Widget(string title, string description, User owner)
{
this.title = title;
this.description = description;
this.owner = owner;
}

public virtual string Title
{
get { return title; }
set { title = value; }
}

public virtual string Description
{
get { return description; }
set { description = value; }
}

public virtual User Owner
{
get { return owner; }
set { owner = value; }
}

public override bool Equals(object obj)
{
var other = obj as Widget;

if (other == null)
{
return false;
}

return Title.Equals(other.Title);
}

public static bool operator ==(Widget lhs, Widget rhs)
{
return Equals(lhs, rhs);
}

public static bool operator !=(Widget lhs, Widget rhs)
{
return !Equals(lhs, rhs);
}

public override int GetHashCode()
{
return Title.GetHashCode();
}
}
}