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

Add test for entity with lazy property #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -0,0 +1,217 @@
using NHibernate.Envers.Query;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;

namespace NHibernate.Envers.Tests.NetSpecific.Integration.Lazy
{
public class LazyPropTest : TestBase
{
public LazyPropTest(AuditStrategyForTest strategyType) : base(strategyType)
{
}

protected override void Initialize()
{
}

[Test]
public void UpdateLazyPropInSameSession()
{
string notLazyStart = "notLazyNotUpd",
lazyStart = "lazyBefore",
notLazyEnd = "notLazyNotUpd",
lazyEnd = "lazyAfrer";

var factory = Cfg.BuildSessionFactory();

using (var s = factory.OpenSession())
{
using (var tx = s.BeginTransaction())
{
s.SaveOrUpdate(new EntityWithLazyProp
{
Id = 1,
NotLazyProp = notLazyStart,
LazyProp = lazyStart
});
tx.Commit();
}

using (var tx = s.BeginTransaction())
{
var ent = s.Get<EntityWithLazyProp>(1);

ent.LazyProp = lazyEnd;

s.SaveOrUpdate(ent);
tx.Commit();
}
}

var current = factory.OpenSession().Get<EntityWithLazyProp>(1);

Assert.AreEqual(notLazyEnd, current.NotLazyProp);
Assert.AreEqual(lazyEnd, current.LazyProp);

var history = AuditReader()
.CreateQuery()
.ForRevisionsOfEntity(typeof(EntityWithLazyProp), false, true)
.Add(AuditEntity.Id().Eq(1))
.GetResultList();

Assert.AreEqual(2, history.Count);
}

[Test]
public void UpdateNotLazyPropInSameSession()
{
string notLazyStart = "notLazyBefore",
lazyStart = "lazyNotUpd",
notLazyEnd = "notLazyAfrer",
lazyEnd = "lazyNotUpd";

var factory = Cfg.BuildSessionFactory();

using (var s = factory.OpenSession())
{
using (var tx = s.BeginTransaction())
{
s.SaveOrUpdate(new EntityWithLazyProp
{
Id = 1,
NotLazyProp = notLazyStart,
LazyProp = lazyStart
});
tx.Commit();
}

using (var tx = s.BeginTransaction())
{
var ent = s.Get<EntityWithLazyProp>(1);

ent.NotLazyProp = notLazyEnd;

s.SaveOrUpdate(ent);
tx.Commit();
}
}

var current = factory.OpenSession().Get<EntityWithLazyProp>(1);

Assert.AreEqual(notLazyEnd, current.NotLazyProp);
Assert.AreEqual(lazyEnd, current.LazyProp);

var history = AuditReader()
.CreateQuery()
.ForRevisionsOfEntity(typeof(EntityWithLazyProp), false, true)
.Add(AuditEntity.Id().Eq(1))
.GetResultList();

Assert.AreEqual(2, history.Count);
}

[Test]
public void UpdateLazyPropInOtherSession()
{
string notLazyStart = "notLazyNotUpd",
lazyStart = "lazyBefore",
notLazyEnd = "notLazyNotUpd",
lazyEnd = "lazyAfrer";

var factory = Cfg.BuildSessionFactory();

using (var s = factory.OpenSession())
{
using (var tx = s.BeginTransaction())
{
s.SaveOrUpdate(new EntityWithLazyProp
{
Id = 1,
NotLazyProp = notLazyStart,
LazyProp = lazyStart
});
tx.Commit();
}
}

using (var s = factory.OpenSession())
{
using (var tx = s.BeginTransaction())
{
EntityWithLazyProp ent = s.Get<EntityWithLazyProp>(1);

ent.LazyProp = lazyEnd;

s.SaveOrUpdate(ent);
tx.Commit();
}
}

var current = Session.Get<EntityWithLazyProp>(1);

Assert.AreEqual(notLazyEnd, current.NotLazyProp);
Assert.AreEqual(lazyEnd, current.LazyProp);

var history = AuditReader()
.CreateQuery()
.ForRevisionsOfEntity(typeof(EntityWithLazyProp), false, true)
.Add(AuditEntity.Id().Eq(1))
.GetResultList();

Assert.AreEqual(2, history.Count);
}

[Test]
public void UpdateNotLazyPropInOtherSession()
{
string notLazyStart = "notLazyBefore",
lazyStart = "lazyNotUpd",
notLazyEnd = "notLazyAfrer",
lazyEnd = "lazyNotUpd";

var factory = Cfg.BuildSessionFactory();

using (var s = factory.OpenSession())
{
using (var tx = s.BeginTransaction())
{
s.SaveOrUpdate(new EntityWithLazyProp
{
Id = 1,
NotLazyProp = notLazyStart,
LazyProp = lazyStart
});
tx.Commit();
}
}

using (var s = factory.OpenSession())
{
using (var tx = s.BeginTransaction())
{
var ent = s.Get<EntityWithLazyProp>(1);

ent.NotLazyProp = notLazyEnd;

s.SaveOrUpdate(ent);
tx.Commit();
}
}

var current = Session.Get<EntityWithLazyProp>(1);

Assert.AreEqual(notLazyEnd, current.NotLazyProp);
Assert.AreEqual(lazyEnd, current.LazyProp);

var history = AuditReader()
.CreateQuery()
.ForRevisionsOfEntity(typeof(EntityWithLazyProp), false, true)
.Add(AuditEntity.Id().Eq(1))
.GetResultList();

Assert.AreEqual(2, history.Count);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using NHibernate.Envers.Configuration.Attributes;
using System;
using System.Collections.Generic;
using System.Text;

namespace NHibernate.Envers.Tests.NetSpecific.Integration.Lazy
{
[Audited]
public class EntityWithLazyProp
{
public virtual int Id { get; set; }
public virtual string NotLazyProp { get; set; }
public virtual string LazyProp { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping
namespace="NHibernate.Envers.Tests.NetSpecific.Integration.Lazy"
assembly="NHibernate.Envers.Tests"
xmlns="urn:nhibernate-mapping-2.2">

<class name="EntityWithLazyProp" table="LazyPrp" dynamic-update="true">
<id name="Id" type="Int32" />
<property name="NotLazyProp" length="50" />
<property name="LazyProp" lazy="true" length="50"/>
</class>

</hibernate-mapping>