Skip to content
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

Test ISession.IsDirty() should not trigger cascade saving #1414

Merged
merged 1 commit into from
Nov 5, 2018
Merged
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
111 changes: 111 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1413/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1413
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
public class ByCodeFixtureAsync : TestCaseMappingByCode
{
private int _parentId;

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<EntityParent>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Native));
rc.Property(x => x.Name);
rc.Bag(x => x.Children, m =>
{
m.Cascade(Mapping.ByCode.Cascade.All);
m.Inverse(true);
}, a => a.OneToMany()
);
});

mapper.Class<EntityChild>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Native));
rc.Property(x => x.Name);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var parent = new EntityParent
{
Name = "InitialParent",
};
session.Save(parent);

session.Flush();
transaction.Commit();
_parentId = parent.Id;
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}

[Test]
[KnownBug("#1413")]
public async Task SessionIsDirtyShouldNotTriggerCascadeSavingAsync()
{
Sfi.Statistics.IsStatisticsEnabled = true;
using (var session = OpenSession())
using (session.BeginTransaction())
{
var parent = await (GetParentAsync(session));
var entityChild = new EntityChild
{
Name = "NewListElem"
};

//parent.Children is cascaded
parent.Children.Add(entityChild);

Sfi.Statistics.Clear();
var isDirty = await (session.IsDirtyAsync());

Assert.That(Sfi.Statistics.EntityInsertCount, Is.EqualTo(0), "Dirty has triggered an insert");
Assert.That(
entityChild.Id,
Is.EqualTo(0),
"Transient objects should not be saved by ISession.IsDirty() call (expected 0 as Id)");
Assert.That(isDirty, "ISession.IsDirty() call should return true.");
}
}

private Task<EntityParent> GetParentAsync(ISession session, CancellationToken cancellationToken = default(CancellationToken))
{
return session.GetAsync<EntityParent>(_parentId, cancellationToken);
}
}
}
18 changes: 18 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1413/Entities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH1413
{
public class EntityChild
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}

public class EntityParent
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<EntityChild> Children { get; set; } = new List<EntityChild>();
}
}
99 changes: 99 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1413/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1413
{
[TestFixture]
public class ByCodeFixture : TestCaseMappingByCode
{
private int _parentId;

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<EntityParent>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Native));
rc.Property(x => x.Name);
rc.Bag(x => x.Children, m =>
{
m.Cascade(Mapping.ByCode.Cascade.All);
m.Inverse(true);
}, a => a.OneToMany()
);
});

mapper.Class<EntityChild>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Native));
rc.Property(x => x.Name);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var parent = new EntityParent
{
Name = "InitialParent",
};
session.Save(parent);

session.Flush();
transaction.Commit();
_parentId = parent.Id;
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}

[Test]
[KnownBug("#1413")]
public void SessionIsDirtyShouldNotTriggerCascadeSaving()
{
Sfi.Statistics.IsStatisticsEnabled = true;
using (var session = OpenSession())
using (session.BeginTransaction())
{
var parent = GetParent(session);
var entityChild = new EntityChild
{
Name = "NewListElem"
};

//parent.Children is cascaded
parent.Children.Add(entityChild);

Sfi.Statistics.Clear();
var isDirty = session.IsDirty();

Assert.That(Sfi.Statistics.EntityInsertCount, Is.EqualTo(0), "Dirty has triggered an insert");
Assert.That(
entityChild.Id,
Is.EqualTo(0),
"Transient objects should not be saved by ISession.IsDirty() call (expected 0 as Id)");
Assert.That(isDirty, "ISession.IsDirty() call should return true.");
}
}

private EntityParent GetParent(ISession session)
{
return session.Get<EntityParent>(_parentId);
}
}
}