Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b3092b9

Browse files
committedNov 3, 2017
Test case for GH1413 (ISession.IsDirty() unexpected behavior)
1 parent 0891c13 commit b3092b9

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 IList<EntityChild> Children { get; set; } = new List<EntityChild>();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.Bag(x => x.Children, m =>
22+
{
23+
m.Cascade(Mapping.ByCode.Cascade.All);
24+
m.Inverse(true);
25+
}, a => a.OneToMany()
26+
);
27+
});
28+
29+
mapper.Class<EntityChild>(rc =>
30+
{
31+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
32+
rc.Property(x => x.Name);
33+
});
34+
35+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
36+
}
37+
38+
protected override void OnSetUp()
39+
{
40+
using (ISession session = OpenSession())
41+
using (ITransaction transaction = session.BeginTransaction())
42+
{
43+
var parent = new EntityParent
44+
{
45+
Name = "InitialParent",
46+
};
47+
session.Save(parent);
48+
49+
session.Flush();
50+
transaction.Commit();
51+
ParentId = parent.Id;
52+
}
53+
}
54+
55+
protected override void OnTearDown()
56+
{
57+
using (ISession session = OpenSession())
58+
using (ITransaction transaction = session.BeginTransaction())
59+
{
60+
session.Delete("from System.Object");
61+
62+
session.Flush();
63+
transaction.Commit();
64+
}
65+
}
66+
67+
[Test]
68+
public void SessionIsDirtyShouldNotTriggerCascadeSaving()
69+
{
70+
using (ISession session = OpenSession())
71+
using (session.BeginTransaction())
72+
{
73+
var parent = GetParent(session);
74+
var entityChild = new EntityChild
75+
{
76+
Name = "NewListElem"
77+
};
78+
79+
//parent.Children is cascaded
80+
parent.Children.Add(entityChild);
81+
82+
var isDirty = session.IsDirty();
83+
Assert.That(entityChild.Id, Is.EqualTo(Guid.Empty), "Transient objects should not be saved by ISession.IsDirty() call (expected empty Guid Id)");
84+
Assert.That(isDirty, "ISession.IsDirty() call should return true.");
85+
}
86+
}
87+
88+
private EntityParent GetParent(ISession session)
89+
{
90+
return session.Get<EntityParent>(ParentId);
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)
Please sign in to comment.