Skip to content

Commit a8bb39c

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

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.GHTODO
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.GHTODO
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+
83+
bool isDirty = false;
84+
Assert.DoesNotThrow(() => isDirty = session.IsDirty(), "ISession.IsDirty() call should not fail for transient many-to-one object referenced in session.");
85+
Assert.IsTrue(isDirty, "ISession.IsDirty() call should return true.");
86+
}
87+
}
88+
89+
[Test]
90+
public void SessionIsDirtyShouldNotTriggerCascadeSaving()
91+
{
92+
using (ISession session = OpenSession())
93+
using (session.BeginTransaction())
94+
{
95+
var parent = GetParent(session);
96+
var entityChild = new EntityChild
97+
{
98+
Name = "NewListElem"
99+
};
100+
101+
//parent.Children is cascaded
102+
parent.Children.Add(entityChild);
103+
104+
var isDirty = session.IsDirty();
105+
Assert.AreEqual(Guid.Empty, entityChild.Id, "Transient objects should not be saved by ISession.IsDirty() call (expected empty Guid Id)");
106+
}
107+
}
108+
109+
private EntityParent GetParent(ISession session)
110+
{
111+
return session.Get<EntityParent>(ParentId);
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)