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

Stop using obsolete ISession.Transaction property #693

Merged
merged 1 commit into from
May 24, 2024
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using FakeItEasy;
using NHibernate;
using NHibernate.Engine;
using NUnit.Framework;

namespace FluentNHibernate.Testing.Testing;
Expand All @@ -23,44 +24,44 @@ class CatType
[Test]
public void Should_Not_Open_A_New_Transaction_If_One_Is_Passed()
{
var transaction = A.Fake<ITransaction>();
A.CallTo(() => transaction.IsActive).Returns(true);
var sessionImpl = A.Fake<ISessionImplementor>();
A.CallTo(() => sessionImpl.TransactionInProgress).Returns(true);

var session = A.Fake<ISession>();
A.CallTo(() => session.Transaction).Returns(transaction);
A.CallTo(() => session.GetSessionImplementation()).Returns(sessionImpl);

var spec = new PersistenceSpecification<Cat>(session);
spec.VerifyTheMappings();

A.CallTo(() => session.BeginTransaction()).MustNotHaveHappened();
A.CallTo(() => session.Transaction).MustHaveHappened(2, Times.Exactly);
}

[Test]
public void Should_Open_A_New_Transaction_If_None_Is_Passed()
{

var transaction = A.Fake<ITransaction>();
A.CallTo(() => transaction.IsActive).Returns(false);
var sessionImpl = A.Fake<ISessionImplementor>();
A.CallTo(() => sessionImpl.TransactionInProgress).Returns(false);

var session = A.Fake<ISession>();
A.CallTo(() => session.Transaction).Returns(transaction);
A.CallTo(() => session.BeginTransaction()).Returns(transaction);
A.CallTo(() => session.GetSessionImplementation()).Returns(sessionImpl);
A.CallTo(() => session.BeginTransaction()).Returns(A.Dummy<ITransaction>());

var spec = new PersistenceSpecification<Cat>(session);
spec.VerifyTheMappings();

A.CallTo(() => session.Transaction).MustHaveHappened(2, Times.Exactly);
A.CallTo(() => session.BeginTransaction()).MustHaveHappened(1, Times.Exactly);
}

[Test]
public void Passed_Transaction_Should_Apply_For_Reference_Saving()
{
var transaction = A.Fake<ITransaction>();
A.CallTo(() => transaction.IsActive).Returns(true);
var sessionImpl = A.Fake<ISessionImplementor>();
A.CallTo(() => sessionImpl.TransactionInProgress).Returns(true);

var session = A.Fake<ISession>();
A.CallTo(() => session.Transaction).Returns(transaction);
A.CallTo(() => session.GetSessionImplementation()).Returns(sessionImpl);
A.CallTo(() => session.BeginTransaction()).Returns(A.Dummy<ITransaction>());

A.CallTo(() => session.Get<Cat>(null)).WithAnyArguments()
.Returns(
new Cat
Expand All @@ -79,7 +80,6 @@ public void Passed_Transaction_Should_Apply_For_Reference_Saving()

spec.VerifyTheMappings();

A.CallTo(() => session.Transaction).MustHaveHappened(2, Times.Exactly);
A.CallTo(() => session.BeginTransaction()).MustNotHaveHappened();
}

Expand All @@ -90,9 +90,9 @@ bool IEqualityComparer.Equals(object x, object y)
if (x is null || y is null)
return false;

if (x.GetType().GetProperty("Name") is not null && y.GetType().GetProperty("Name") is not null)
if (x.GetType().GetProperty("Name") is { } xProp && y.GetType().GetProperty("Name") is { } yProp)
{
return x.GetType().GetProperty("Name").GetValue(x, null) == y.GetType().GetProperty("Name").GetValue(x, null);
return xProp.GetValue(x, null) == yProp.GetValue(x, null);
}

return x.Equals(y);
Expand Down
4 changes: 1 addition & 3 deletions src/FluentNHibernate/Testing/PersistenceSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ public PersistenceSpecification(ISession session)
public PersistenceSpecification(ISession session, IEqualityComparer entityEqualityComparer)
{
currentSession = session;
#pragma warning disable CS0618 // ISession.Transaction is obsolete
hasExistingTransaction = currentSession.Transaction is not null && currentSession.Transaction.IsActive || System.Transactions.Transaction.Current is not null;
#pragma warning restore CS0618
hasExistingTransaction = currentSession.GetSessionImplementation().TransactionInProgress;
this.entityEqualityComparer = entityEqualityComparer;
}

Expand Down