Skip to content

Commit 8ef2ccd

Browse files
committed
Test cases GH1413 (ISession.IsDirty() unexpected behavior)
1 parent 0891c13 commit 8ef2ccd

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH1413
5+
{
6+
public class EntityChild
7+
{
8+
public virtual Guid Id { get; set; }
9+
public virtual string Name { get; set; }
10+
}
11+
12+
public class EntityParent
13+
{
14+
public virtual Guid Id { get; set; }
15+
public virtual string Name { get; set; }
16+
public virtual EntityChild Child { get; set; }
17+
public virtual IList<EntityChild> Children { get; set; } = new List<EntityChild>();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Linq;
3+
using NHibernate.Cfg.MappingSchema;
4+
using NHibernate.Mapping.ByCode;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.GH1413
8+
{
9+
[TestFixture]
10+
public class ByCodeFixture : TestCaseMappingByCode
11+
{
12+
private Guid ParentId;
13+
14+
protected override HbmMapping GetMappings()
15+
{
16+
var mapper = new ModelMapper();
17+
mapper.Class<EntityParent>(rc =>
18+
{
19+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
20+
rc.Property(x => x.Name);
21+
rc.ManyToOne(ep => ep.Child);
22+
rc.Bag(x => x.Children, m =>
23+
{
24+
m.Cascade(Mapping.ByCode.Cascade.All);
25+
m.Inverse(true);
26+
}, a => a.OneToMany()
27+
);
28+
});
29+
30+
mapper.Class<EntityChild>(rc =>
31+
{
32+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
33+
rc.Property(x => x.Name);
34+
});
35+
36+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
37+
}
38+
39+
protected override void OnSetUp()
40+
{
41+
using (ISession session = OpenSession())
42+
using (ITransaction transaction = session.BeginTransaction())
43+
{
44+
var child = new EntityChild { Name = "InitialChild" };
45+
var parent = new EntityParent
46+
{
47+
Name = "InitialParent",
48+
Child = child
49+
};
50+
session.Save(child);
51+
session.Save(parent);
52+
53+
session.Flush();
54+
transaction.Commit();
55+
ParentId = parent.Id;
56+
}
57+
}
58+
59+
protected override void OnTearDown()
60+
{
61+
using (ISession session = OpenSession())
62+
using (ITransaction transaction = session.BeginTransaction())
63+
{
64+
session.Delete("from System.Object");
65+
66+
session.Flush();
67+
transaction.Commit();
68+
}
69+
}
70+
71+
[Test]
72+
public void SessionIsDirtyShouldNotFailForNewManyToOneObject()
73+
{
74+
using (ISession session = OpenSession())
75+
using (session.BeginTransaction())
76+
{
77+
EntityParent parent = GetParent(session);
78+
79+
//parent.Child entity is not cascaded, I want to save it explictilty later
80+
parent.Child = new EntityChild { Name = "NewManyToOneChild" };
81+
82+
bool isDirty = false;
83+
Assert.That(() => isDirty = session.IsDirty(), Throws.Nothing, () => "ISession.IsDirty() call should not fail for transient many-to-one object referenced in session.");
84+
Assert.That(isDirty, "ISession.IsDirty() call should return true.");
85+
}
86+
}
87+
88+
[Test]
89+
public void SessionIsDirtyShouldNotTriggerCascadeSaving()
90+
{
91+
using (ISession session = OpenSession())
92+
using (session.BeginTransaction())
93+
{
94+
var parent = GetParent(session);
95+
var entityChild = new EntityChild
96+
{
97+
Name = "NewListElem"
98+
};
99+
100+
//parent.Children is cascaded
101+
parent.Children.Add(entityChild);
102+
103+
var isDirty = session.IsDirty();
104+
Assert.That(entityChild.Id, Is.EqualTo(Guid.Empty), "Transient objects should not be saved by ISession.IsDirty() call (expected empty Guid Id)");
105+
Assert.That(isDirty, "ISession.IsDirty() call should return true.");
106+
}
107+
}
108+
109+
private EntityParent GetParent(ISession session)
110+
{
111+
return session.Get<EntityParent>(ParentId);
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)