Skip to content

Test case for ComposedId Entity with Lazy Property is not proxified #1440

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

Merged
merged 8 commits into from
Nov 8, 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
131 changes: 131 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1439/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//------------------------------------------------------------------------------
// <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 System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Intercept;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH1439
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();

mapper.Class<CompositeEntity>(rc =>
{
rc.ComposedId(
c =>
{
c.Property(t => t.Id);
c.Property(t => t.Name);
});

rc.Property(x => x.LazyProperty, x => x.Lazy(true));
});

mapper.Class<EntityWithComponentId>(rc =>
{
rc.ComponentAsId(e => e.Id, map =>
{
map.Property(c => c.Id);
map.Property(c => c.Name);
});

rc.Property(e => e.LazyProperty, map => map.Lazy(true));
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new CompositeEntity { Id = 1, Name = "Bob", LazyProperty = "LazyProperty"};
session.Save(e1);
var e2 = new EntityWithComponentId
{ Id = new ComponentId { Id = 1, Name = "Bob" }, LazyProperty = "LazyProperty" };
session.Save(e2);
transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();
transaction.Commit();
}
}

[Test]
[KnownBug("#1439")]
public async Task LazyPropertyShouldBeUninitializedAndLoadableAsync()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var e1 = await (session.Query<CompositeEntity>().SingleAsync());
Assert.Multiple(
() =>
{
Assert.That(
NHibernateUtil.IsPropertyInitialized(e1, nameof(CompositeEntity.LazyProperty)),
Is.False,
"Lazy property initialization status");
Assert.That(
FieldInterceptionHelper.IsInstrumented(e1),
Is.True,
"Entity IsInstrumented");
Assert.That(
e1,
Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty"));
});
await (tran.CommitAsync());
}
}

[Test]
public async Task LazyPropertyShouldBeUninitializedAndLoadableWithComponentIdAsync()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var e2 = await (session.Query<EntityWithComponentId>().SingleAsync());
Assert.Multiple(
() =>
{
Assert.That(
NHibernateUtil.IsPropertyInitialized(e2, nameof(CompositeEntity.LazyProperty)),
Is.False,
"Lazy property initialization status");
Assert.That(
FieldInterceptionHelper.IsInstrumented(e2),
Is.True,
"Entity IsInstrumented");
Assert.That(
e2,
Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty"));
});
await (tran.CommitAsync());
}
}
}
}
31 changes: 31 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1439/CompositeEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH1439
{
public class CompositeEntity : IEquatable<CompositeEntity>
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string LazyProperty { get; set; }

public virtual bool Equals(CompositeEntity other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Id == other.Id && string.Equals(Name, other.Name);
}

public override bool Equals(object obj)
{
return Equals(obj as CompositeEntity);
}

public override int GetHashCode()
{
unchecked
{
return (Id * 397) ^ (Name != null ? Name.GetHashCode() : 0);
}
}
}
}
36 changes: 36 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1439/EntityWithComponentId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH1439
{
public class EntityWithComponentId
{
public virtual ComponentId Id { get; set; }
public virtual string LazyProperty { get; set; }
}

public class ComponentId : IEquatable<ComponentId>
{
public int Id { get; set; }
public string Name { get; set; }

public bool Equals(ComponentId other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Id == other.Id && string.Equals(Name, other.Name);
}

public override bool Equals(object obj)
{
return Equals(obj as ComponentId);
}

public override int GetHashCode()
{
unchecked
{
return (Id * 397) ^ (Name != null ? Name.GetHashCode() : 0);
}
}
}
}
119 changes: 119 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1439/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Intercept;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1439
{
[TestFixture]
public class Fixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();

mapper.Class<CompositeEntity>(rc =>
{
rc.ComposedId(
c =>
{
c.Property(t => t.Id);
c.Property(t => t.Name);
});

rc.Property(x => x.LazyProperty, x => x.Lazy(true));
});

mapper.Class<EntityWithComponentId>(rc =>
{
rc.ComponentAsId(e => e.Id, map =>
{
map.Property(c => c.Id);
map.Property(c => c.Name);
});

rc.Property(e => e.LazyProperty, map => map.Lazy(true));
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new CompositeEntity { Id = 1, Name = "Bob", LazyProperty = "LazyProperty"};
session.Save(e1);
var e2 = new EntityWithComponentId
{ Id = new ComponentId { Id = 1, Name = "Bob" }, LazyProperty = "LazyProperty" };
session.Save(e2);
transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();
transaction.Commit();
}
}

[Test]
[KnownBug("#1439")]
public void LazyPropertyShouldBeUninitializedAndLoadable()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var e1 = session.Query<CompositeEntity>().Single();
Assert.Multiple(
() =>
{
Assert.That(
NHibernateUtil.IsPropertyInitialized(e1, nameof(CompositeEntity.LazyProperty)),
Is.False,
"Lazy property initialization status");
Assert.That(
FieldInterceptionHelper.IsInstrumented(e1),
Is.True,
"Entity IsInstrumented");
Assert.That(
e1,
Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty"));
});
tran.Commit();
}
}

[Test]
public void LazyPropertyShouldBeUninitializedAndLoadableWithComponentId()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var e2 = session.Query<EntityWithComponentId>().Single();
Assert.Multiple(
() =>
{
Assert.That(
NHibernateUtil.IsPropertyInitialized(e2, nameof(CompositeEntity.LazyProperty)),
Is.False,
"Lazy property initialization status");
Assert.That(
FieldInterceptionHelper.IsInstrumented(e2),
Is.True,
"Entity IsInstrumented");
Assert.That(
e2,
Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty"));
});
tran.Commit();
}
}
}
}